1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt;
18
19 import java.io.StringWriter;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23 import junit.textui.TestRunner;
24
25 import org.apache.commons.beanutils.BasicDynaClass;
26 import org.apache.commons.beanutils.DynaBean;
27 import org.apache.commons.beanutils.DynaClass;
28 import org.apache.commons.beanutils.DynaProperty;
29 import org.apache.commons.betwixt.io.BeanWriter;
30 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
31
32
33
34
35
36
37 public class TestDynaBeanSupport extends AbstractTestCase {
38
39 public static void main( String[] args ) {
40 TestRunner.run( suite() );
41 }
42
43 public static Test suite() {
44 return new TestSuite(TestDynaBeanSupport.class);
45 }
46
47 public TestDynaBeanSupport(String testName) {
48 super(testName);
49 }
50
51 public void testIntrospectDynaBean() throws Exception
52 {
53 XMLIntrospector introspector = new XMLIntrospector();
54 introspector.getConfiguration().setAttributesForPrimitives(false);
55 XMLBeanInfo beanInfo = introspector.introspect(createDynasaurClass());
56 ElementDescriptor baseElement = beanInfo.getElementDescriptor();
57
58 assertEquals("Correct number of attributes", 0, baseElement.getAttributeDescriptors().length);
59 ElementDescriptor[] descriptors = baseElement.getElementDescriptors();
60 assertEquals("Correct number of elements", 3, descriptors.length);
61
62 boolean matchedSpecies = false;
63 boolean matchedIsRaptor = false;
64 boolean matchedPeriod = false;
65
66 for (int i=0, size = descriptors.length; i< size; i++) {
67 if ("Species".equals(descriptors[i].getPropertyName())) {
68 matchedSpecies = true;
69 }
70
71 if ("isRaptor".equals(descriptors[i].getPropertyName())) {
72 matchedIsRaptor = true;
73 }
74
75 if ("Period".equals(descriptors[i].getPropertyName())) {
76 matchedPeriod = true;
77 }
78 }
79
80 assertTrue("Species descriptor not found", matchedSpecies);
81 assertTrue("isRaptor descriptor not found", matchedIsRaptor);
82 assertTrue("Period descriptor not found", matchedPeriod);
83 }
84
85 public void testWriteDynaBean() throws Exception {
86 DynaBean dynasaur = createDynasaurClass().newInstance();
87 dynasaur.set("Species", "Allosaurus");
88 dynasaur.set("isRaptor", Boolean.TRUE);
89 dynasaur.set("Period", "Jurassic");
90
91 StringWriter out = new StringWriter();
92 out.write("<?xml version='1.0'?>");
93 BeanWriter writer = new BeanWriter(out);
94 writer.getBindingConfiguration().setMapIDs(false);
95 writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
96 writer.write(dynasaur);
97
98 String xml = "<?xml version='1.0'?><dynasaur><species>Allosaurus</species>"
99 + "<isRaptor>true</isRaptor><period>Jurassic</period></dynasaur>";
100
101 xmlAssertIsomorphicContent(
102 "Test write dyna beans",
103 parseString(xml),
104 parseString(out.getBuffer().toString()),
105 true);
106 }
107
108 public void testOverrideWithDotBetwixt() throws Exception {
109 DynaWithDotBetwixt bean = new DynaWithDotBetwixt("Tweedledum","Tweedledee");
110 StringWriter out = new StringWriter();
111 out.write("<?xml version='1.0'?>");
112 BeanWriter writer = new BeanWriter(out);
113 writer.getBindingConfiguration().setMapIDs(false);
114 writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
115 writer.write("bean", bean);
116
117 String xml = "<?xml version='1.0'?><bean><ndp>Tweedledum</ndp></bean>";
118 xmlAssertIsomorphicContent(
119 "Test write dyna beans with dt betwixt",
120 parseString(xml),
121 parseString(out.getBuffer().toString()),
122 true);
123
124 }
125
126 private DynaClass createDynasaurClass() throws Exception {
127 DynaClass dynaClass = new BasicDynaClass
128 ("Dynasaur", null,
129 new DynaProperty[]{
130 new DynaProperty("Species", String.class),
131 new DynaProperty("isRaptor", Boolean.TYPE),
132 new DynaProperty("Period", String.class),
133 });
134 return (dynaClass);
135 }
136 }
137
138