KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > TestSymbolExpander


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import org.apache.commons.logging.LogFactory;
18 import org.apache.hivemind.ErrorHandler;
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.SymbolSource;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.easymock.MockControl;
23
24 /**
25  * Tests for {@link org.apache.hivemind.impl.SymbolExpanderImpl}.
26  *
27  * @author Howard Lewis Ship
28  * @since 1.1
29  */

30 public class TestSymbolExpander extends HiveMindTestCase
31 {
32     private class SymbolSourceFixture implements SymbolSource
33     {
34         public String JavaDoc valueForSymbol(String JavaDoc name)
35         {
36             return name.toUpperCase();
37         }
38     }
39
40     private void attempt(String JavaDoc expected, String JavaDoc text)
41     {
42         SymbolExpanderImpl e = new SymbolExpanderImpl(null, new SymbolSource[] {new SymbolSourceFixture()});
43
44         String JavaDoc actual = e.expandSymbols(text, null);
45
46         assertEquals(expected, actual);
47     }
48
49     public void testSimple()
50     {
51         attempt("Now is the TIME", "Now is the ${time}");
52     }
53
54     public void testNoSymbols()
55     {
56         attempt("No symbols in here", "No symbols in here");
57     }
58
59     public void testFalseStart()
60     {
61         attempt("The cost of the ITEM is $1,000.", "The cost of the ${item} is $1,000.");
62     }
63
64     public void testNestedBraces()
65     {
66         attempt("Nested {BRACES}", "Nested ${{braces}}");
67     }
68
69     public void testEmptySymbol()
70     {
71         attempt("An empty ${} symbol", "An empty ${} symbol");
72     }
73
74     public void testTrailingDollar()
75     {
76         attempt("SYMBOL Ends with $", "${symbol} Ends with $");
77     }
78
79     public void testEndsWithPartialSymbol()
80     {
81         attempt("SYMBOL Ends with ${partial", "${symbol} Ends with ${partial");
82     }
83
84     public void testMissingSymbol()
85     {
86         ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
87         Location l = newLocation();
88
89         MockControl control = newControl(SymbolSource.class);
90         SymbolSource source = (SymbolSource) control.getMock();
91
92         // Training
93

94         source.valueForSymbol("symbol");
95         control.setReturnValue(null);
96
97         eh.error(
98             LogFactory.getLog(SymbolExpanderImpl.class),
99             XmlImplMessages.noSuchSymbol("symbol"),
100             l,
101             null);
102
103         replayControls();
104
105         SymbolExpanderImpl e = new SymbolExpanderImpl(eh, new SymbolSource[] {source});
106
107         String JavaDoc actual = e.expandSymbols("Unknown ${symbol}", l);
108
109         assertEquals("Unknown ${symbol}", actual);
110
111         verifyControls();
112     }
113
114     public void testEscaped()
115     {
116         attempt("This is a SYMBOL, this is ${not}.", "This is a ${symbol}, this is $${not}.");
117     }
118     
119     public void testEscapedAtStart()
120     {
121         attempt("${not-a-symbol}", "$${not-a-symbol}");
122     }
123
124     public void testSystemPropertiesSymbolSource()
125     {
126         SymbolSource s = new SystemPropertiesSymbolSource();
127
128         assertEquals(System.getProperty("user.home"), s.valueForSymbol("user.home"));
129     }
130     
131 }
132
Popular Tags