1
2 /*
3 * $Header: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/digester/TestXMLIntrospectorHelper.java,v 1.2 2002/12/30 18:16:48 mvdb Exp $
4 * $Revision: 1.2 $
5 * $Date: 2002/12/30 18:16:48 $
6 *
7 * ====================================================================
8 *
9 * The Apache Software License, Version 1.1
10 *
11 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
12 * reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
24 * distribution.
25 *
26 * 3. The end-user documentation included with the redistribution, if
27 * any, must include the following acknowlegement:
28 * "This product includes software developed by the
29 * Apache Software Foundation (http://www.apache.org/)."
30 * Alternately, this acknowlegement may appear in the software itself,
31 * if and wherever such third-party acknowlegements normally appear.
32 *
33 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
34 * Foundation" must not be used to endorse or promote products derived
35 * from this software without prior written permission. For written
36 * permission, please contact apache@apache.org.
37 *
38 * 5. Products derived from this software may not be called "Apache"
39 * nor may "Apache" appear in their names without prior written
40 * permission of the Apache Group.
41 *
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
43 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
46 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 * ====================================================================
55 *
56 * This software consists of voluntary contributions made by many
57 * individuals on behalf of the Apache Software Foundation. For more
58 * information on the Apache Software Foundation, please see
59 * <http://www.apache.org/>.
60 *
61 * $Id: TestXMLIntrospectorHelper.java,v 1.2 2002/12/30 18:16:48 mvdb Exp $
62 */
63 package org.apache.commons.betwixt.digester;
64
65 import java.beans.BeanInfo;
66 import java.beans.IntrospectionException;
67 import java.beans.Introspector;
68 import java.beans.PropertyDescriptor;
69
70 import junit.framework.Test;
71 import junit.framework.TestCase;
72 import junit.framework.TestSuite;
73 import junit.textui.TestRunner;
74
75 import org.apache.commons.betwixt.CustomerBean;
76 import org.apache.commons.betwixt.NodeDescriptor;
77 import org.apache.commons.betwixt.XMLIntrospector;
78 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
79
80 /*** Test harness for the XMLIntrospectorHelper
81 *
82 * @author <a href="mailto:cyu77@yahoo.com">Calvin Yu</a>
83 * @version $Revision: 1.2 $
84 */
85 public class TestXMLIntrospectorHelper extends TestCase {
86
87 public static void main( String[] args ) {
88 TestRunner.run( suite() );
89 }
90
91 public static Test suite() {
92 return new TestSuite(TestXMLIntrospectorHelper.class);
93 }
94
95 public TestXMLIntrospectorHelper(String testName) {
96 super(testName);
97 }
98
99 /***
100 * Test the helper's <code>createDescriptor</code> method when a hyphenated name
101 * mapper is set.
102 */
103 public void testCreateDescriptorWithHyphenatedElementNameMapper() throws Exception {
104 XMLIntrospector introspector = new XMLIntrospector();
105 introspector.setAttributesForPrimitives(false);
106 introspector.setElementNameMapper(new HyphenatedNameMapper());
107 BeanInfo beanInfo = Introspector.getBeanInfo(CustomerBean.class);
108
109 NodeDescriptor nickNameProperty = createDescriptor("nickName", beanInfo, introspector);
110 assertNotNull("nickName property not found", nickNameProperty);
111 assertEquals("nick name property", "nick-name", nickNameProperty.getLocalName());
112
113 NodeDescriptor projectNamesProperty = createDescriptor("projectNames", beanInfo, introspector);
114 assertNotNull("projectNames property not found", projectNamesProperty);
115 assertEquals("project names property", "project-names", projectNamesProperty.getLocalName());
116 }
117
118 /***
119 * Find the specified property and convert it into a descriptor.
120 */
121 private NodeDescriptor createDescriptor(String propertyName, BeanInfo beanInfo, XMLIntrospector introspector)
122 throws IntrospectionException {
123 PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
124 for (int i=0; i<properties.length; i++) {
125 if (propertyName.equals(properties[i].getName())) {
126 NodeDescriptor desc = XMLIntrospectorHelper
127 .createDescriptor(properties[i],
128 introspector.isAttributesForPrimitives(),
129 introspector);
130 return desc;
131 }
132 }
133 return null;
134 }
135
136 }
This page was automatically generated by Maven