1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.io.read;
18
19 import java.io.StringReader;
20 import java.util.List;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.betwixt.AbstractTestCase;
26 import org.apache.commons.betwixt.BindingConfiguration;
27 import org.apache.commons.betwixt.ElementDescriptor;
28 import org.apache.commons.betwixt.XMLIntrospector;
29 import org.apache.commons.betwixt.io.BeanReader;
30
31 /***
32 * Test harness for Mapping Actions.
33 *
34 * @author Robert Burrell Donkin
35 * @version $Id: TestMappingActions.java,v 1.2 2004/06/13 21:32:48 rdonkin Exp $
36 */
37 public class TestMappingActions extends AbstractTestCase {
38
39
40 public TestMappingActions(String name) {
41 super(name);
42 }
43
44 public static Test suite() {
45 return new TestSuite(TestMappingActions.class);
46 }
47
48 public void testSimpleRead() throws Exception {
49
50 String xml="<?xml version='1.0'?><AddressBean><street>1 Main Street</street><city>New Town</city>"
51 + "<code>NT1 1AA</code><country>UK</country></AddressBean>";
52
53
54
55
56 BeanReader reader = new BeanReader();
57 reader.registerBeanClass(AddressBean.class);
58 AddressBean address = (AddressBean) reader.parse(new StringReader(xml));
59
60 assertFalse("Address is mapped", address == null);
61 assertEquals("Street", "1 Main Street", address.getStreet());
62 assertEquals("City", "New Town", address.getCity());
63 assertEquals("Code", "NT1 1AA", address.getCode());
64 assertEquals("Country", "UK", address.getCountry());
65 }
66
67 public void testPrimitiveCollective() throws Exception{
68
69 String xml="<?xml version='1.0'?><SimpleStringCollective><strings>"
70 + "<string>one</string><string>two</string><string>three</string>"
71 + "</strings></SimpleStringCollective>";
72
73
74
75
76 BeanReader reader = new BeanReader();
77 reader.registerBeanClass(SimpleStringCollective.class);
78 SimpleStringCollective collective = (SimpleStringCollective) reader.parse(new StringReader(xml));
79
80 assertFalse("SimpleStringCollective mapped", collective == null);
81 List strings = collective.getStrings();
82 assertEquals("String count", 3, strings.size());
83 assertEquals("First string", "one", strings.get(0));
84 assertEquals("Second string", "two", strings.get(1));
85 assertEquals("Third string", "three", strings.get(2));
86 }
87
88
89
90 public void testBodyUpdateActionNoMatch() throws Exception {
91 AddressBean bean = new AddressBean();
92 bean.setStreet("DEFAULT");
93 bean.setCode("DEFAULT");
94 bean.setCountry("DEFAULT");
95
96 XMLIntrospector introspector = new XMLIntrospector();
97 ElementDescriptor elementDescriptor = introspector.introspect(AddressBean.class).getElementDescriptor();
98
99 ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
100 context.setBean(bean);
101 context.markClassMap(AddressBean.class);
102 context.pushElement("NoMatch");
103 context.setXMLIntrospector(introspector);
104 SimpleTypeBindAction action = new SimpleTypeBindAction();
105 action.body("Street value", context);
106 assertEquals("Street is unset", "DEFAULT", bean.getStreet());
107 assertEquals("Country is unset", "DEFAULT", bean.getCountry());
108 assertEquals("Code is unset", "DEFAULT", bean.getCode());
109 }
110
111
112 public void testBodyUpdateActionMatch() throws Exception {
113 AddressBean bean = new AddressBean();
114 bean.setStreet("DEFAULT");
115 bean.setCode("DEFAULT");
116 bean.setCountry("DEFAULT");
117
118 XMLIntrospector introspector = new XMLIntrospector();
119 ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
120 context.pushBean(bean);
121 context.markClassMap(AddressBean.class);
122 context.pushElement("street");
123 context.setXMLIntrospector(introspector);
124 SimpleTypeBindAction action = new SimpleTypeBindAction();
125 action.body("Street value", context);
126 assertEquals("Street is set", "Street value", bean.getStreet());
127 assertEquals("Country is unset", "DEFAULT", bean.getCountry());
128 assertEquals("Code is unset", "DEFAULT", bean.getCode());
129 }
130
131 public void testCollection() throws Exception {
132 String xml = "<?xml version='1.0'?>"
133 + "<elements><element><value>alpha</value></element></elements>";
134 StringReader in = new StringReader(xml);
135 BeanReader reader = new BeanReader();
136 reader.registerBeanClass(Elements.class);
137 Elements result = (Elements) reader.parse(in);
138 assertNotNull("Element alpha exists", result.getElement("alpha"));
139 }
140 }