1   
2   /*
3    * Copyright 2001-2004 The Apache Software Foundation.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }