KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > digester > TestXMLIntrospectorHelper


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 JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
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 /** Test harness for the XMLIntrospectorHelper
36   *
37   * @author <a HREF="mailto:cyu77@yahoo.com">Calvin Yu</a>
38   * @version $Revision: 1.8 $
39   */

40 public class TestXMLIntrospectorHelper extends TestCase {
41
42     public static void main( String JavaDoc[] args ) {
43         TestRunner.run( suite() );
44     }
45
46     public static Test suite() {
47         return new TestSuite(TestXMLIntrospectorHelper.class);
48     }
49
50     public TestXMLIntrospectorHelper(String JavaDoc testName) {
51         super(testName);
52     }
53
54     /**
55      * Test the helper's <code>createDescriptor</code> method when a hyphenated name
56      * mapper is set.
57      */

58     public void testCreateDescriptorWithHyphenatedElementNameMapper() throws Exception JavaDoc {
59         XMLIntrospector introspector = new XMLIntrospector();
60         introspector.getConfiguration().setAttributesForPrimitives(false);
61         introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
62         BeanInfo JavaDoc 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 JavaDoc {
74         XMLIntrospectorHelper.isLoopType(null);
75     }
76
77     /**
78      * Find the specified property and convert it into a descriptor.
79      */

80     private NodeDescriptor createDescriptor(String JavaDoc propertyName, BeanInfo JavaDoc beanInfo, XMLIntrospector introspector)
81         throws IntrospectionException JavaDoc {
82         PropertyDescriptor JavaDoc[] 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