1 2 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.BeanProperty; 30 import org.apache.commons.betwixt.CustomerBean; 31 import org.apache.commons.betwixt.NodeDescriptor; 32 import org.apache.commons.betwixt.XMLIntrospector; 33 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper; 34 35 40 public class TestXMLIntrospectorHelper extends TestCase { 41 42 public static void main( String [] args ) { 43 TestRunner.run( suite() ); 44 } 45 46 public static Test suite() { 47 return new TestSuite(TestXMLIntrospectorHelper.class); 48 } 49 50 public TestXMLIntrospectorHelper(String testName) { 51 super(testName); 52 } 53 54 58 public void testCreateDescriptorWithHyphenatedElementNameMapper() throws Exception { 59 XMLIntrospector introspector = new XMLIntrospector(); 60 introspector.getConfiguration().setAttributesForPrimitives(false); 61 introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper()); 62 BeanInfo beanInfo = Introspector.getBeanInfo(CustomerBean.class); 63 64 NodeDescriptor nickNameProperty = createDescriptor("nickName", beanInfo, introspector); 65 assertNotNull("nickName property not found", nickNameProperty); 66 assertEquals("nick name property", "nick-name", nickNameProperty.getLocalName()); 67 68 NodeDescriptor projectNamesProperty = createDescriptor("projectNames", beanInfo, introspector); 69 assertNotNull("projectNames property not found", projectNamesProperty); 70 assertEquals("project names property", "project-names", projectNamesProperty.getLocalName()); 71 } 72 73 public void testNullParameters() throws Exception { 74 XMLIntrospectorHelper.isLoopType(null); 75 } 76 77 80 private NodeDescriptor createDescriptor(String propertyName, BeanInfo beanInfo, XMLIntrospector introspector) 81 throws IntrospectionException { 82 PropertyDescriptor [] properties = beanInfo.getPropertyDescriptors(); 83 for (int i=0; i<properties.length; i++) { 84 if (propertyName.equals(properties[i].getName())) { 85 NodeDescriptor desc = (NodeDescriptor) introspector 86 .createXMLDescriptor(new BeanProperty(properties[i])); 87 return desc; 88 } 89 } 90 return null; 91 } 92 93 } 94 | Popular Tags |