1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.digester;
18
19 import java.beans.BeanInfo;
20 import java.beans.IntrospectionException;
21 import java.beans.Introspector;
22 import java.beans.PropertyDescriptor;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28
29 import org.apache.commons.betwixt.CustomerBean;
30 import org.apache.commons.betwixt.NodeDescriptor;
31 import org.apache.commons.betwixt.XMLIntrospector;
32 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
33
34 /*** Test harness for the XMLIntrospectorHelper
35 *
36 * @author <a href="mailto:cyu77@yahoo.com">Calvin Yu</a>
37 * @version $Revision: 1.7 $
38 */
39 public class TestXMLIntrospectorHelper extends TestCase {
40
41 public static void main( String[] args ) {
42 TestRunner.run( suite() );
43 }
44
45 public static Test suite() {
46 return new TestSuite(TestXMLIntrospectorHelper.class);
47 }
48
49 public TestXMLIntrospectorHelper(String testName) {
50 super(testName);
51 }
52
53 /***
54 * Test the helper's <code>createDescriptor</code> method when a hyphenated name
55 * mapper is set.
56 */
57 public void testCreateDescriptorWithHyphenatedElementNameMapper() throws Exception {
58 XMLIntrospector introspector = new XMLIntrospector();
59 introspector.setAttributesForPrimitives(false);
60 introspector.setElementNameMapper(new HyphenatedNameMapper());
61 BeanInfo beanInfo = Introspector.getBeanInfo(CustomerBean.class);
62
63 NodeDescriptor nickNameProperty = createDescriptor("nickName", beanInfo, introspector);
64 assertNotNull("nickName property not found", nickNameProperty);
65 assertEquals("nick name property", "nick-name", nickNameProperty.getLocalName());
66
67 NodeDescriptor projectNamesProperty = createDescriptor("projectNames", beanInfo, introspector);
68 assertNotNull("projectNames property not found", projectNamesProperty);
69 assertEquals("project names property", "project-names", projectNamesProperty.getLocalName());
70 }
71
72 public void testNullParameters() throws Exception {
73 XMLIntrospectorHelper.isLoopType(null);
74 }
75
76 /***
77 * Find the specified property and convert it into a descriptor.
78 */
79 private NodeDescriptor createDescriptor(String propertyName, BeanInfo beanInfo, XMLIntrospector introspector)
80 throws IntrospectionException {
81 PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
82 for (int i=0; i<properties.length; i++) {
83 if (propertyName.equals(properties[i].getName())) {
84 NodeDescriptor desc = (NodeDescriptor) introspector
85 .createDescriptor(properties[i],
86 introspector.isAttributesForPrimitives());
87 return desc;
88 }
89 }
90 return null;
91 }
92
93 }