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