1   
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  /** Test harness for the XMLIntrospectorHelper
37    *
38    * @author <a href="mailto:cyu77@yahoo.com">Calvin Yu</a>
39    * @version $Revision: 438373 $
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       * Test the helper's <code>createDescriptor</code> method when a hyphenated name
57       * mapper is set.
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       * Find the specified property and convert it into a descriptor.
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  }