1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  
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          BeanDescriptor bd = new BeanDescriptor(getClass());
55          String result = mapper.mapTypeToElementName(bd.getName());
56          assertEquals("test_hyphenated_name_mapper", result);
57      }
58  
59      public void testUpperCase()  {
60          HyphenatedNameMapper mapper = new HyphenatedNameMapper(true, "_");
61          String result = mapper.mapTypeToElementName("FooBar");
62          assertEquals("FOO_BAR", result);
63      }
64      
65      public void testUpperCaseViaProperties()  {
66          HyphenatedNameMapper mapper = new HyphenatedNameMapper();
67          mapper.setUpperCase(true);
68          mapper.setSeparator("_");
69          String result = mapper.mapTypeToElementName("FooBar");
70          assertEquals("FOO_BAR", result);
71      }
72      
73      /**
74       * A more "complicated" exmple
75       */
76      public void testUpperCaseLongViaProperties() {
77          HyphenatedNameMapper mapper = new HyphenatedNameMapper(true, "__");
78          String result = mapper.mapTypeToElementName("FooBarFooBar");
79          assertEquals("FOO__BAR__FOO__BAR", result);
80  
81      }
82       
83      public void testBeanWithAdd() throws Exception {	
84          //
85          // simple test this one
86          // a problem was reported when introspecting classes with 'add' properties
87          // when using the HyphenatedNameMapper
88          // basically, the test is that no exception is thrown
89          //
90          XMLIntrospector introspector = new XMLIntrospector();
91          introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
92          introspector.introspect(new ArrayList());
93      }
94  }