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 junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  /** Test harness for the BadCharacterReplacingNMapper
25    *
26    * @author Robert Burrell Donkin
27    * @version $Revision: 438373 $
28    */
29  public class TestBadCharacterReplacingNMapper extends TestCase {
30      
31          
32      public static Test suite() {
33          return new TestSuite(TestBadCharacterReplacingNMapper.class);
34      }
35      
36      public TestBadCharacterReplacingNMapper(String testName) {
37          super(testName);
38      }
39      
40      public void testNoReplacementBadFirstNoChainedMapper() {
41          String name="$LoadsOfMoney";
42          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(null);
43          String out = mapper.mapTypeToElementName(name);
44          assertEquals("Expected", "LoadsOfMoney", out);
45      }
46      
47      public void testNoReplacementBadFirstWithChainedMapper() {
48          String name="$LOADSŁOF$MONEY";
49          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(new PlainMapper());
50          String out = mapper.mapTypeToElementName(name);
51          assertEquals("Expected", "LOADSOFMONEY", out);
52      }
53      
54      public void testNoReplacementGoodFirstNoChainedMapper() {
55          String name="L$oads%OfMone$y$";
56          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(null);
57          String out = mapper.mapTypeToElementName(name);
58          assertEquals("Expected", "LoadsOfMoney", out);
59      }
60      
61      public void testNoReplacementGoodFirstWithChainedMapper() {
62          String name="LOADSOFMONE$$Y";
63          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(new PlainMapper());
64          String out = mapper.mapTypeToElementName(name);
65          assertEquals("Expected", "LOADSOFMONEY", out);
66      }
67      
68      public void testReplacementBadFirstNoChainedMapper() {
69          String name="$LoadsOfMoney$";
70          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(null);
71          mapper.setReplacement(new Character('_'));
72          String out = mapper.mapTypeToElementName(name);
73          assertEquals("Expected", "_LoadsOfMoney_", out);
74      }
75      
76      public void testReplacementBadFirstWithChainedMapper() {
77          String name="$LOADSŁOF$MONEY";
78          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(new PlainMapper());
79          mapper.setReplacement(new Character('_'));
80          String out = mapper.mapTypeToElementName(name);
81          assertEquals("Expected", "_LOADS_OF_MONEY", out);
82      }
83      
84      public void testReplacementGoodFirstNoChainedMapper() {
85          String name="L$$$$$oads%OfMone$y$";
86          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(null);
87          mapper.setReplacement(new Character('_'));
88          String out = mapper.mapTypeToElementName(name);
89          assertEquals("Expected", "L_____oads_OfMone_y_", out);
90      }
91      
92      public void testReplacementGoodFirstWithChainedMapper() {
93          String name="L$OADSOFMONE$$$$$Y";
94          BadCharacterReplacingNMapper mapper = new BadCharacterReplacingNMapper(new PlainMapper());
95          mapper.setReplacement(new Character('_'));
96          String out = mapper.mapTypeToElementName(name);
97          assertEquals("Expected", "L_OADSOFMONE_____Y", out);
98      }
99      
100     private class PlainMapper implements NameMapper {
101         public String mapTypeToElementName(String typeName) {
102             return typeName;
103         }
104     }
105 }