1 package org.apache.torque.generator.control;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }