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