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