1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.dotbetwixt;
19
20
21 import java.io.StringReader;
22 import java.io.StringWriter;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.betwixt.AttributeDescriptor;
27 import org.apache.commons.betwixt.ElementDescriptor;
28 import org.apache.commons.betwixt.XMLBeanInfo;
29 import org.apache.commons.betwixt.XMLIntrospector;
30 import org.apache.commons.betwixt.io.BeanReader;
31 import org.apache.commons.betwixt.io.BeanWriter;
32
33 /***
34 * @author Brian Pugh
35 */
36 public class TestLoopType extends TestCase {
37 public void testSimpleList() throws Exception {
38 Father father = new Father();
39 father.setSpouse("Julie");
40 father.addKid("John");
41 father.addKid("Jane");
42
43 StringWriter outputWriter = new StringWriter();
44
45 outputWriter.write("<?xml version='1.0' ?>\n");
46 BeanWriter beanWriter = new BeanWriter(outputWriter);
47 beanWriter.enablePrettyPrint();
48 beanWriter.getBindingConfiguration().setMapIDs(true);
49 beanWriter.write(father);
50
51 BeanReader beanReader = new BeanReader();
52
53
54 beanReader.registerBeanClass(Father.class);
55 StringReader xmlReader = new StringReader(outputWriter.toString());
56
57
58 Father result = (Father) beanReader.parse(xmlReader);
59
60 assertNotNull("Unexpected null list of children!", result.getKids());
61 assertEquals(
62 "got wrong number of children",
63 father.getKids().size(),
64 result.getKids().size());
65 assertNull(
66 "Spouse should not get set because it is not in the .betwixt file",
67 result.getSpouse());
68 }
69
70 public void testIgnoredProperty() throws Exception {
71 XMLIntrospector introspector = new XMLIntrospector();
72 XMLBeanInfo beanInfo = introspector.introspect(IgnoreBean.class);
73 ElementDescriptor ignoreDescriptor = beanInfo.getElementDescriptor();
74
75 assertEquals("element name matches", "ignore", ignoreDescriptor.getLocalName());
76 ElementDescriptor[] childDescriptors = ignoreDescriptor.getElementDescriptors();
77 assertEquals("number of child elements", 1, childDescriptors.length);
78
79 }
80
81
82 public void testIgnoredAdders() throws Exception {
83 XMLIntrospector introspector = new XMLIntrospector();
84 XMLBeanInfo beanInfo = introspector.introspect(IgnoreAddersBean.class);
85 ElementDescriptor ignoreDescriptor = beanInfo.getElementDescriptor();
86
87 assertEquals("element name matches", "ignore", ignoreDescriptor.getLocalName());
88 ElementDescriptor[] childDescriptors = ignoreDescriptor.getElementDescriptors();
89 assertEquals("number of child elements", 1, childDescriptors.length);
90
91 }
92
93
94 public void _testAddDefaults() throws Exception {
95 XMLIntrospector introspector = new XMLIntrospector();
96 XMLBeanInfo beanInfo = introspector.introspect(LibraryBean.class);
97 ElementDescriptor libraryDescriptor = beanInfo.getElementDescriptor();
98
99 AttributeDescriptor[] libraryAttributeDescriptors = libraryDescriptor.getAttributeDescriptors();
100 assertEquals("Only one attribute", 1, libraryAttributeDescriptors.length);
101
102 ElementDescriptor[] libraryElementDescriptors = libraryDescriptor.getElementDescriptors();
103 assertEquals("Only one element", 1, libraryElementDescriptors.length);
104
105
106 }
107 }
108