1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.introspection;
18
19 import java.beans.BeanInfo;
20 import java.beans.Introspector;
21 import java.beans.PropertyDescriptor;
22 import java.io.StringWriter;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 import org.apache.commons.betwixt.AbstractTestCase;
29 import org.apache.commons.betwixt.AttributeDescriptor;
30 import org.apache.commons.betwixt.ElementDescriptor;
31 import org.apache.commons.betwixt.XMLBeanInfo;
32 import org.apache.commons.betwixt.XMLIntrospector;
33 import org.apache.commons.betwixt.io.BeanWriter;
34 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
35 import org.apache.commons.betwixt.registry.NoCacheRegistry;
36 import org.apache.commons.betwixt.strategy.ClassNormalizer;
37 import org.apache.commons.betwixt.strategy.ListedClassNormalizer;
38
39
40 /*** Test harness for the XMLIntrospector
41 *
42 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
43 * @version $Revision: 1.11 $
44 */
45 public class TestXMLIntrospector extends AbstractTestCase {
46
47 public static void main( String[] args ) {
48 TestRunner.run( suite() );
49 }
50
51 public static Test suite() {
52 return new TestSuite(TestXMLIntrospector.class);
53 }
54
55 public TestXMLIntrospector(String testName) {
56 super(testName);
57 }
58
59 public void testIntrospector() throws Exception {
60
61
62 XMLIntrospector introspector = new XMLIntrospector();
63
64
65 introspector.setAttributesForPrimitives(true);
66
67 Object bean = createBean();
68
69 XMLBeanInfo info = introspector.introspect( bean );
70
71 assertTrue( "Found XMLBeanInfo", info != null );
72
73 ElementDescriptor descriptor = info.getElementDescriptor();
74
75 assertTrue( "Found root element descriptor", descriptor != null );
76
77 AttributeDescriptor[] attributes = descriptor.getAttributeDescriptors();
78
79 assertTrue( "Found attributes", attributes != null && attributes.length > 0 );
80
81
82 info = introspector.introspect( bean );
83
84 assertTrue( "Found XMLBeanInfo", info != null );
85
86 descriptor = info.getElementDescriptor();
87
88 assertTrue( "Found root element descriptor", descriptor != null );
89
90 attributes = descriptor.getAttributeDescriptors();
91
92 assertTrue( "Found attributes", attributes != null && attributes.length > 0 );
93
94
95
96
97 introspector.setRegistry(new NoCacheRegistry());
98 info = introspector.introspect( bean );
99
100 assertTrue( "Found XMLBeanInfo", info != null );
101
102 descriptor = info.getElementDescriptor();
103
104 assertTrue( "Found root element descriptor", descriptor != null );
105
106 attributes = descriptor.getAttributeDescriptors();
107
108 assertTrue( "Found attributes", attributes != null && attributes.length > 0 );
109
110
111
112
113 introspector.setRegistry(new DefaultXMLBeanInfoRegistry());
114
115 info = introspector.introspect( bean );
116
117 assertTrue( "Found XMLBeanInfo", info != null );
118
119 descriptor = info.getElementDescriptor();
120
121 assertTrue( "Found root element descriptor", descriptor != null );
122
123 attributes = descriptor.getAttributeDescriptors();
124
125 assertTrue( "Found attributes", attributes != null && attributes.length > 0 );
126
127 }
128
129 public void testBeanWithBeanInfo() throws Exception {
130
131
132 BeanInfo bwbiBeanInfo = Introspector.getBeanInfo(BeanWithBeanInfoBean.class);
133
134 PropertyDescriptor[] propertyDescriptors = bwbiBeanInfo.getPropertyDescriptors();
135
136 assertEquals("Wrong number of properties", 2 , propertyDescriptors.length);
137
138
139 if ("alpha".equals(propertyDescriptors[0].getName())) {
140
141 assertEquals("Second property name", "gamma" , propertyDescriptors[1].getName());
142
143 } else {
144
145 assertEquals("First property name", "gamma" , propertyDescriptors[0].getName());
146 assertEquals("Second property name", "alpha" , propertyDescriptors[1].getName());
147 }
148
149
150 propertyDescriptors = null;
151
152
153
154
155 XMLIntrospector introspector = new XMLIntrospector();
156 introspector.setAttributesForPrimitives(false);
157
158
159 XMLBeanInfo xmlBeanInfo = introspector.introspect(BeanWithBeanInfoBean.class);
160
161 ElementDescriptor[] elementDescriptors = xmlBeanInfo.getElementDescriptor().getElementDescriptors();
162
163
164
165
166
167
168
169
170
171
172 assertEquals("Wrong number of elements", 2 , elementDescriptors.length);
173
174
175 boolean alphaFirst = true;
176 if ("alpha".equals(elementDescriptors[0].getPropertyName())) {
177
178 assertEquals("Second element name", "gamma" , elementDescriptors[1].getPropertyName());
179
180 } else {
181 alphaFirst = false;
182 assertEquals("First element name", "gamma" , elementDescriptors[0].getPropertyName());
183 assertEquals("Second element name", "alpha" , elementDescriptors[1].getPropertyName());
184 }
185
186
187 StringWriter out = new StringWriter();
188 BeanWriter writer = new BeanWriter(out);
189 writer.setWriteIDs(false);
190 BeanWithBeanInfoBean bean = new BeanWithBeanInfoBean("alpha value","beta value","gamma value");
191 writer.write(bean);
192
193 if (alphaFirst) {
194
195 xmlAssertIsomorphicContent(
196 parseFile("src/test/org/apache/commons/betwixt/introspection/test-bwbi-output-a.xml"),
197 parseString(out.toString()));
198
199 } else {
200 xmlAssertIsomorphicContent(
201 parseFile("src/test/org/apache/commons/betwixt/introspection/test-bwbi-output-g.xml"),
202 parseString(out.toString()));
203 }
204 }
205
206 public void testDefaultClassNormalizer() throws Exception {
207 XMLIntrospector introspector = new XMLIntrospector();
208
209 FaceImpl face = new FaceImpl();
210 XMLBeanInfo info = introspector.introspect( face );
211 ElementDescriptor elementDescriptor = info.getElementDescriptor();
212
213 AttributeDescriptor[] attributeDescriptor = elementDescriptor.getAttributeDescriptors();
214 ElementDescriptor[] children = elementDescriptor.getElementDescriptors();
215
216 assertEquals("Expected no attributes", 0, attributeDescriptor.length);
217 assertEquals("Expected two elements", 2, children.length);
218 }
219
220 public void testClassNormalizer() throws Exception {
221 XMLIntrospector introspector = new XMLIntrospector();
222 introspector.setClassNormalizer( new ClassNormalizer() {
223
224 public Class normalize(Class clazz) {
225 if (IFace.class.isAssignableFrom( clazz )) {
226 return IFace.class;
227 }
228 return super.normalize( clazz );
229 }
230 });
231
232 FaceImpl face = new FaceImpl();
233 XMLBeanInfo info = introspector.introspect( face );
234 ElementDescriptor elementDescriptor = info.getElementDescriptor();
235 assertEquals("Expected only itself", 1, elementDescriptor.getElementDescriptors().length);
236
237 AttributeDescriptor[] attributeDescriptor = elementDescriptor.getAttributeDescriptors();
238 ElementDescriptor[] children = elementDescriptor.getElementDescriptors();
239
240 assertEquals("Expected no attributes", 0, attributeDescriptor.length);
241 assertEquals("Expected one elements", 1, children.length);
242 assertEquals("Expected element", "name", children[0].getLocalName());
243 }
244
245 public void testListedClassNormalizer() throws Exception {
246 ListedClassNormalizer classNormalizer = new ListedClassNormalizer();
247 classNormalizer.addSubstitution( IFace.class );
248 XMLIntrospector introspector = new XMLIntrospector();
249 introspector.setClassNormalizer(classNormalizer);
250
251 FaceImpl face = new FaceImpl();
252
253 XMLBeanInfo info = introspector.introspect( face );
254 ElementDescriptor elementDescriptor = info.getElementDescriptor();
255 AttributeDescriptor[] attributeDescriptor = elementDescriptor.getAttributeDescriptors();
256 ElementDescriptor[] children = elementDescriptor.getElementDescriptors();
257
258 assertEquals("Expected no attributes", 0, attributeDescriptor.length);
259 assertEquals("Expected one elements", 1, children.length);
260 assertEquals("Expected element", "name", children[0].getLocalName());
261 }
262
263 public void testListedClassNormalizerWrite() throws Exception {
264 ListedClassNormalizer classNormalizer = new ListedClassNormalizer();
265 classNormalizer.addSubstitution( IFace.class );
266
267 StringWriter out = new StringWriter();
268 out.write("<?xml version='1.0'?>");
269 BeanWriter writer = new BeanWriter( out );
270 writer.getXMLIntrospector().setClassNormalizer( classNormalizer );
271 FaceImpl bean = new FaceImpl();
272 bean.setName("Old Tom Cobbly");
273 writer.write(bean);
274
275 String xml="<?xml version='1.0'?><IFace><name>Old Tom Cobbly</name></IFace>";
276 xmlAssertIsomorphicContent(
277 parseString(out.getBuffer().toString()),
278 parseString(xml),
279 true);
280 }
281 }
282