KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > variable > test > VariableExpanderTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.variable.test;
8
9
10 import junit.framework.TestCase;
11
12 import com.inversoft.util.variable.ExpanderException;
13 import com.inversoft.util.variable.ExpanderStrategy;
14 import com.inversoft.util.variable.VariableExpander;
15
16
17 /**
18  * This class is the JUnit test case for the variable
19  * package.
20  *
21  * @author Brian Pontarelli
22  * @since 1.0
23  * @version 1.0
24  */

25 public class VariableExpanderTest extends TestCase {
26
27     /**
28      * Constructs a new <code>VariableExpanderTest</code>.
29      */

30     public VariableExpanderTest(String JavaDoc name) {
31         super(name);
32     }
33
34
35     /**
36      * Tests with a single variable
37      */

38     public void testSingle() {
39         helperGood("${variable1}", "foo", new SimpleStrategy());
40         helperGood("Inside ${variable1}", "Inside foo", new SimpleStrategy());
41         helperGood("${variable1} outside", "foo outside", new SimpleStrategy());
42         helperGood("Inside ${variable1} outside", "Inside foo outside", new SimpleStrategy());
43     }
44
45     /**
46      * Tests with a empty string
47      */

48     public void testEmpty() {
49         try {
50             assertNull(VariableExpander.expand((String JavaDoc) null, new SimpleStrategy()));
51             assertEquals("", VariableExpander.expand("", new SimpleStrategy()));
52         } catch (ExpanderException ee) {
53             fail("Shouldn't have failed");
54         }
55     }
56
57     /**
58      * Tests with two variables
59      */

60     public void testDouble() {
61         helperGood("${variable1} ${variable2}", "foo foo", new SimpleStrategy());
62         helperGood("Inside ${variable1} middle ${variable2} outside",
63             "Inside foo middle foo outside",
64             new SimpleStrategy());
65         helperGood("Inside ${variable1} ${variable2} outside",
66             "Inside foo foo outside",
67             new SimpleStrategy());
68     }
69
70     /**
71      * Tests the expand (String, Strategy) for long variable names
72      */

73     public void testStringLongVariableNames() {
74         helperGood("Inside ${long variable with spaces} ${long_variable{{{${} outside",
75             "Inside foo foo outside",
76             new LongNamesStrategy());
77     }
78
79     /**
80      * Tests the expand (String, Strategy) for invalid variables
81      */

82     public void testInvalidSingle() {
83         helperGood("variable1}",
84             "variable1}",
85             new SimpleStrategy());
86         helperGood("Inside variable1}",
87             "Inside variable1}",
88             new SimpleStrategy());
89         helperGood("Inside variable1} outside",
90             "Inside variable1} outside",
91             new SimpleStrategy());
92
93         helperGood("${variable1",
94             "${variable1",
95             new SimpleStrategy());
96         helperGood("Inside ${variable1",
97             "Inside ${variable1",
98             new SimpleStrategy());
99         helperGood("Inside ${variable1 outside",
100             "Inside ${variable1 outside",
101             new SimpleStrategy());
102
103         helperGood("$a{variable1}",
104             "$a{variable1}",
105             new SimpleStrategy());
106         helperGood("Inside $a{variable1}",
107             "Inside $a{variable1}",
108             new SimpleStrategy());
109         helperGood("Inside $a{variable1} outside",
110             "Inside $a{variable1} outside",
111             new SimpleStrategy());
112
113         helperGood("{variable1}",
114             "{variable1}",
115             new SimpleStrategy());
116         helperGood("Inside {variable1}",
117             "Inside {variable1}",
118             new SimpleStrategy());
119         helperGood("Inside {variable1} outside",
120             "Inside {variable1} outside",
121             new SimpleStrategy());
122
123         helperGood("}${variable1",
124             "}${variable1",
125             new SimpleStrategy());
126         helperGood("${variable1 ${foo ${bar",
127             "${variable1 ${foo ${bar",
128             new SimpleStrategy());
129     }
130
131     /**
132      * Tests for invalid variables
133      */

134     public void testInvalidDouble() {
135         helperGood("variable1} variable2}",
136             "variable1} variable2}",
137             new SimpleStrategy());
138         helperGood("Inside variable1} variable2}",
139             "Inside variable1} variable2}",
140             new SimpleStrategy());
141         helperGood("Inside variable1} variable2} outside",
142             "Inside variable1} variable2} outside",
143             new SimpleStrategy());
144         helperGood("Inside variable1} middle variable2} outside",
145             "Inside variable1} middle variable2} outside",
146             new SimpleStrategy());
147
148         helperGood("${variable1 ${variable2",
149             "${variable1 ${variable2",
150             new SimpleStrategy());
151         helperGood("Inside ${variable1 ${variable2",
152             "Inside ${variable1 ${variable2",
153             new SimpleStrategy());
154         helperGood("Inside ${variable1 ${variable2 outside",
155             "Inside ${variable1 ${variable2 outside",
156             new SimpleStrategy());
157         helperGood("Inside ${variable1 middle ${variable2 outside",
158             "Inside ${variable1 middle ${variable2 outside",
159             new SimpleStrategy());
160
161         helperGood("$a{variable1} $a{variable2}",
162             "$a{variable1} $a{variable2}",
163             new SimpleStrategy());
164         helperGood("Inside $a{variable1} $a{variable2}",
165             "Inside $a{variable1} $a{variable2}",
166             new SimpleStrategy());
167         helperGood("Inside $a{variable1} $a{variable2} outside",
168             "Inside $a{variable1} $a{variable2} outside",
169             new SimpleStrategy());
170         helperGood("Inside $a{variable1} middle $a{variable2} outside",
171             "Inside $a{variable1} middle $a{variable2} outside",
172             new SimpleStrategy());
173
174         helperGood("{variable1} {variable2}",
175             "{variable1} {variable2}",
176             new SimpleStrategy());
177         helperGood("Inside {variable1} {variable2}",
178             "Inside {variable1} {variable2}",
179             new SimpleStrategy());
180         helperGood("Inside {variable1} {variable2} outside",
181             "Inside {variable1} {variable2} outside",
182             new SimpleStrategy());
183         helperGood("Inside {variable1} middle {variable2} outside",
184             "Inside {variable1} middle {variable2} outside",
185             new SimpleStrategy());
186
187         helperGood("}${variable1 ${variable2",
188             "}${variable1 ${variable2",
189             new SimpleStrategy());
190         helperGood("${variable1 ${foo ${bar",
191             "${variable1 ${foo ${bar",
192             new SimpleStrategy());
193         helperGood("$}{variable1 $a{variable2} ${variable3{",
194             "$}{variable1 $a{variable2} ${variable3{",
195             new SimpleStrategy());
196     }
197
198     /**
199      * Tests the expand (String, Strategy) for invalid strategy
200      */

201     public void testStringFailureStrategy() {
202
203         try {
204             ExpanderStrategy strat = new FailureStrategy();
205             String JavaDoc str = "Inside ${variable} ${variable} outside";
206             /*String value = */VariableExpander.expand(str, strat);
207
208             fail("Should have failed");
209         } catch (ExpanderException ee) {
210             assertTrue("Should have message of 'Message' but is '" + ee.getMessage() +
211                 "'", ee.getMessage().equals("Message"));
212         }
213     }
214
215     /**
216      * Tests the expand (StringBuffer, Strategy) for invalid strategy
217      */

218     public void testStringBufferFailureStrategy() {
219
220         try {
221             ExpanderStrategy strat = new FailureStrategy();
222             StringBuffer JavaDoc str = new StringBuffer JavaDoc("Inside ${long variable with spaces} ${long_variable{{{${} outside");
223             /*String value = */VariableExpander.expand(str, strat);
224
225             fail("Should have failed");
226         } catch (ExpanderException ee) {
227             assertTrue("Should have message of 'Message' but is '" + ee.getMessage() +
228                 "'", ee.getMessage().equals("Message"));
229         }
230     }
231
232     /**
233      * Tests the assert statements
234      */

235     public void testAsserts() {
236         try {
237             StringBuffer JavaDoc str = new StringBuffer JavaDoc("");
238             VariableExpander.expand(str, null);
239             fail("Should have failed");
240         } catch (ExpanderException ee) {
241             fail("Should have asserted");
242         } catch (AssertionError JavaDoc ae) {
243             // Expected
244
}
245     }
246
247     /**
248      * Helper
249      */

250     private void helperGood(String JavaDoc in, String JavaDoc expected, ExpanderStrategy strat) {
251         try {
252             String JavaDoc value = VariableExpander.expand(in, strat);
253             assertTrue("Should be '" + expected + "' but is '" + value + "'",
254                 value.equals(expected));
255
256             strat = (ExpanderStrategy) strat.getClass().newInstance();
257             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(in);
258             value = VariableExpander.expand(strBuf, strat);
259             assertTrue("Should be '" + expected + "' but is '" + value + "'",
260                 value.equals(expected));
261         } catch (ExpanderException ee) {
262             fail(ee.toString());
263         } catch (Exception JavaDoc e) {
264             fail(e.toString());
265         }
266     }
267
268
269
270
271     /**
272      * This is the strategy for simple tests
273      */

274     public static class SimpleStrategy implements ExpanderStrategy {
275         private int count = 1;
276
277         public String JavaDoc expand(String JavaDoc name) {
278             assertTrue("Should be 'variable" + count + "' but is '" + name + "'",
279                 name.equals("variable" + count));
280             count++;
281             return "foo";
282         }
283     }
284
285     /**
286      * This is the strategy for simple tests
287      */

288     public static class LongNamesStrategy implements ExpanderStrategy {
289         private int count = 1;
290
291         public String JavaDoc expand(String JavaDoc name) {
292             if (count == 1) {
293                 assertTrue("Should be 'long variable with spaces' but is '" +
294                     name + "'", name.equals("long variable with spaces"));
295             } else {
296                 assertTrue("Should be 'long_variable{{{${' but is '" +
297                     name + "'", name.equals("long_variable{{{${"));
298             }
299             count++;
300             return "foo";
301         }
302     }
303
304     /**
305      * This is the strategy for simple tests
306      */

307     public static class FailureStrategy implements ExpanderStrategy {
308         public String JavaDoc expand(String JavaDoc name) throws ExpanderException {
309             throw new ExpanderException("Message");
310         }
311     }
312 }
313
314
Popular Tags