View Javadoc

1   package org.apache.torque.generator.control;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import org.apache.torque.generator.BaseTest;
25  import org.apache.torque.generator.configuration.UnitConfiguration;
26  import org.apache.torque.generator.control.ControllerState;
27  import org.apache.torque.generator.control.TokenReplacer;
28  import org.apache.torque.generator.option.Option;
29  import org.apache.torque.generator.option.OptionImpl;
30  import org.apache.torque.generator.option.Options;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  public class TokenReplacerTest extends BaseTest
35  {
36      private TokenReplacer tokenReplacer;
37  
38      @Before
39      public void setUp()
40      {
41          Options options = new Options();
42          {
43              Option optionWithoutNamespace = new OptionImpl(
44                      "optionWithoutNamespace",
45                      "ValueWithoutNamespace");
46              options.setGlobalOption(optionWithoutNamespace);
47          }
48          {
49              Option optionWithNamespace = new OptionImpl(
50                      "org.apache.torque.generator.optionWithNamespace",
51                      "ValueWithNamespace");
52              options.setGlobalOption(optionWithNamespace);
53          }
54          {
55              Option optionWithSpecialChars = new OptionImpl(
56                      "opt${}i\\on",
57                      "ValueWith${}Special\\Chars");
58              options.setGlobalOption(optionWithSpecialChars);
59          }
60  
61          UnitConfiguration unitConfiguration = new UnitConfiguration();
62          unitConfiguration.setOptions(options);
63          ControllerState controllerState = new ControllerState();
64          controllerState.setUnitConfiguration(unitConfiguration);
65          tokenReplacer = new TokenReplacer(controllerState);
66      }
67  
68      @Test
69      public void testNoToken() throws Exception
70      {
71          String input = "No Token in this String";
72          String output = tokenReplacer.process(input);
73          assertEquals(input, output);
74      }
75  
76      @Test
77      public void testTokenWithoutNamespace() throws Exception
78      {
79          String input = "Prefix${option:optionWithoutNamespace}Postfix";
80          String output = tokenReplacer.process(input);
81          assertEquals("PrefixValueWithoutNamespacePostfix", output);
82      }
83  
84      @Test
85      public void testTokenWithNamespace() throws Exception
86      {
87          String input
88              = "Prefix${option:org.apache.torque.generator.optionWithNamespace}Postfix";
89          String output = tokenReplacer.process(input);
90          assertEquals("PrefixValueWithNamespacePostfix", output);
91      }
92  
93      @Test
94      public void testEscapingOutsideToken() throws Exception
95      {
96          String input = "Pre\\fixXXXPostfix";
97          String output = tokenReplacer.process(input);
98          assertEquals("PrefixXXXPostfix", output);
99      }
100 
101     @Test
102     public void testEscapingInToken() throws Exception
103     {
104         String input
105                 = "Prefix${option:option\\W\\ithoutNamespace}Postfix";
106         String output = tokenReplacer.process(input);
107         assertEquals("PrefixValueWithoutNamespacePostfix", output);
108     }
109 
110     @Test
111     public void testDoubleEscapingOutsideToken() throws Exception
112     {
113         String input = "Prefix\\\\Postfix";
114         String output = tokenReplacer.process(input);
115         assertEquals("Prefix\\Postfix", output);
116     }
117 
118     @Test
119     public void testTokenWithSpecialChars() throws Exception
120     {
121         String input
122                 = "Prefix${option:opt${\\}i\\\\on}Postfix";
123         String output = tokenReplacer.process(input);
124         assertEquals("PrefixValueWith${}Special\\CharsPostfix", output);
125     }
126 
127     @Test
128     public void testEscapingTokenStart1() throws Exception
129     {
130         String input
131                 = "Prefix\\${XXX}Postfix";
132         String output = tokenReplacer.process(input);
133         assertEquals("Prefix${XXX}Postfix", output);
134     }
135 
136     @Test
137     public void testEscapingTokenStart2() throws Exception
138     {
139         String input
140                 = "Prefix$\\{XXX}Postfix";
141         String output = tokenReplacer.process(input);
142         assertEquals("Prefix${XXX}Postfix", output);
143     }
144 }