01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: package org.apache.commons.betwixt.strategy;
19:
20: import java.beans.BeanDescriptor;
21: import java.util.ArrayList;
22:
23: import junit.framework.Test;
24: import junit.framework.TestCase;
25: import junit.framework.TestSuite;
26:
27: import org.apache.commons.betwixt.XMLIntrospector;
28:
29: /** Test harness for the HyphenatedNameMapper
30: *
31: * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
32: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
34: * @version $Revision: 438373 $
35: */
36: public class TestHyphenatedNameMapper extends TestCase {
37:
38: public static Test suite() {
39: return new TestSuite(TestHyphenatedNameMapper.class);
40: }
41:
42: public TestHyphenatedNameMapper(String testName) {
43: super (testName);
44: }
45:
46: public void testLowerCase() {
47: HyphenatedNameMapper mapper = new HyphenatedNameMapper();
48: String result = mapper.mapTypeToElementName("FooBar");
49: assertEquals("foo-bar", result);
50: }
51:
52: public void testLowerCaseViaBeanDescriptor() {
53: HyphenatedNameMapper mapper = new HyphenatedNameMapper(false,
54: "_");
55: BeanDescriptor bd = new BeanDescriptor(getClass());
56: String result = mapper.mapTypeToElementName(bd.getName());
57: assertEquals("test_hyphenated_name_mapper", result);
58: }
59:
60: public void testUpperCase() {
61: HyphenatedNameMapper mapper = new HyphenatedNameMapper(true,
62: "_");
63: String result = mapper.mapTypeToElementName("FooBar");
64: assertEquals("FOO_BAR", result);
65: }
66:
67: public void testUpperCaseViaProperties() {
68: HyphenatedNameMapper mapper = new HyphenatedNameMapper();
69: mapper.setUpperCase(true);
70: mapper.setSeparator("_");
71: String result = mapper.mapTypeToElementName("FooBar");
72: assertEquals("FOO_BAR", result);
73: }
74:
75: /**
76: * A more "complicated" exmple
77: */
78: public void testUpperCaseLongViaProperties() {
79: HyphenatedNameMapper mapper = new HyphenatedNameMapper(true,
80: "__");
81: String result = mapper.mapTypeToElementName("FooBarFooBar");
82: assertEquals("FOO__BAR__FOO__BAR", result);
83:
84: }
85:
86: public void testBeanWithAdd() throws Exception {
87: //
88: // simple test this one
89: // a problem was reported when introspecting classes with 'add' properties
90: // when using the HyphenatedNameMapper
91: // basically, the test is that no exception is thrown
92: //
93: XMLIntrospector introspector = new XMLIntrospector();
94: introspector.getConfiguration().setElementNameMapper(
95: new HyphenatedNameMapper());
96: introspector.introspect(new ArrayList());
97: }
98: }
|