1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.betwixt.strategy;
18  
19  import java.beans.BeanDescriptor;
20  import java.util.ArrayList;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.apache.commons.betwixt.XMLIntrospector;
27  
28  /*** Test harness for the HyphenatedNameMapper
29    *
30    * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
31    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32    * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
33    * @version $Revision: 1.9 $
34    */
35  public class TestHyphenatedNameMapper extends TestCase {
36      
37      public static Test suite() {
38          return new TestSuite(TestHyphenatedNameMapper.class);
39      }
40      
41      public TestHyphenatedNameMapper(String testName) {
42          super(testName);
43      }
44      
45      public void testLowerCase()  {
46          HyphenatedNameMapper mapper = new HyphenatedNameMapper();
47          String result = mapper.mapTypeToElementName("FooBar");
48          assertEquals("foo-bar", result);
49      }
50      
51      public void testLowerCaseViaBeanDescriptor() {
52          HyphenatedNameMapper mapper = new HyphenatedNameMapper(false, "_");
53          BeanDescriptor bd = new BeanDescriptor(getClass());
54          String result = mapper.mapTypeToElementName(bd.getName());
55          assertEquals("test_hyphenated_name_mapper", result);
56      }
57  
58      public void testUpperCase()  {
59          HyphenatedNameMapper mapper = new HyphenatedNameMapper(true, "_");
60          String result = mapper.mapTypeToElementName("FooBar");
61          assertEquals("FOO_BAR", result);
62      }
63      
64      public void testUpperCaseViaProperties()  {
65          HyphenatedNameMapper mapper = new HyphenatedNameMapper();
66          mapper.setUpperCase(true);
67          mapper.setSeparator("_");
68          String result = mapper.mapTypeToElementName("FooBar");
69          assertEquals("FOO_BAR", result);
70      }
71      
72      /***
73       * A more "complicated" exmple
74       */
75      public void testUpperCaseLongViaProperties() {
76          HyphenatedNameMapper mapper = new HyphenatedNameMapper(true, "__");
77          String result = mapper.mapTypeToElementName("FooBarFooBar");
78          assertEquals("FOO__BAR__FOO__BAR", result);
79  
80      }
81       
82      public void testBeanWithAdd() throws Exception {	
83          //
84          // simple test this one
85          // a problem was reported when introspecting classes with 'add' properties
86          // when using the HyphenatedNameMapper
87          // basically, the test is that no exception is thrown
88          //
89          XMLIntrospector introspector = new XMLIntrospector();
90          introspector.setElementNameMapper(new HyphenatedNameMapper());
91          introspector.introspect(new ArrayList());
92      }
93  }