001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.digester;
018:
019: import java.beans.BeanInfo;
020: import java.beans.IntrospectionException;
021: import java.beans.Introspector;
022: import java.beans.PropertyDescriptor;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import junit.textui.TestRunner;
028:
029: import org.apache.commons.betwixt.BeanProperty;
030: import org.apache.commons.betwixt.CustomerBean;
031: import org.apache.commons.betwixt.NodeDescriptor;
032: import org.apache.commons.betwixt.XMLIntrospector;
033: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
034:
035: /** Test harness for the XMLIntrospectorHelper
036: *
037: * @author <a href="mailto:cyu77@yahoo.com">Calvin Yu</a>
038: * @version $Revision: 438373 $
039: */
040: public class TestXMLIntrospectorHelper extends TestCase {
041:
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: public static Test suite() {
047: return new TestSuite(TestXMLIntrospectorHelper.class);
048: }
049:
050: public TestXMLIntrospectorHelper(String testName) {
051: super (testName);
052: }
053:
054: /**
055: * Test the helper's <code>createDescriptor</code> method when a hyphenated name
056: * mapper is set.
057: */
058: public void testCreateDescriptorWithHyphenatedElementNameMapper()
059: throws Exception {
060: XMLIntrospector introspector = new XMLIntrospector();
061: introspector.getConfiguration().setAttributesForPrimitives(
062: false);
063: introspector.getConfiguration().setElementNameMapper(
064: new HyphenatedNameMapper());
065: BeanInfo beanInfo = Introspector
066: .getBeanInfo(CustomerBean.class);
067:
068: NodeDescriptor nickNameProperty = createDescriptor("nickName",
069: beanInfo, introspector);
070: assertNotNull("nickName property not found", nickNameProperty);
071: assertEquals("nick name property", "nick-name",
072: nickNameProperty.getLocalName());
073:
074: NodeDescriptor projectNamesProperty = createDescriptor(
075: "projectNames", beanInfo, introspector);
076: assertNotNull("projectNames property not found",
077: projectNamesProperty);
078: assertEquals("project names property", "project-names",
079: projectNamesProperty.getLocalName());
080: }
081:
082: public void testNullParameters() throws Exception {
083: new XMLIntrospector().isLoopType(null);
084: }
085:
086: /**
087: * Find the specified property and convert it into a descriptor.
088: */
089: private NodeDescriptor createDescriptor(String propertyName,
090: BeanInfo beanInfo, XMLIntrospector introspector)
091: throws IntrospectionException {
092: PropertyDescriptor[] properties = beanInfo
093: .getPropertyDescriptors();
094: for (int i = 0; i < properties.length; i++) {
095: if (propertyName.equals(properties[i].getName())) {
096: NodeDescriptor desc = (NodeDescriptor) introspector
097: .createXMLDescriptor(new BeanProperty(
098: properties[i]));
099: return desc;
100: }
101: }
102: return null;
103: }
104:
105: }
|