001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import java.util.Arrays;
018: import java.util.Collections;
019: import java.util.List;
020:
021: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
022: import org.apache.tapestry.ioc.services.SymbolProvider;
023: import org.apache.tapestry.ioc.services.SymbolSource;
024: import org.testng.annotations.Test;
025:
026: public class SymbolSourceImplTest extends IOCInternalTestCase {
027: @Test
028: public void expand_symbols_no_symbols_present() {
029: String input = "A jolly good time.";
030:
031: List<SymbolProvider> providers = Collections.emptyList();
032:
033: SymbolSource source = new SymbolSourceImpl(providers);
034:
035: // Not just equal, but the same.
036:
037: assertSame(source.expandSymbols(input), input);
038: }
039:
040: @Test
041: public void simple_expansions() {
042: SymbolProvider provider = mockSymbolProvider();
043:
044: List<SymbolProvider> providers = Arrays.asList(provider);
045:
046: train_valueForSymbol(provider, "barney", "Barney");
047: train_valueForSymbol(provider, "dino", "Dino");
048:
049: replay();
050:
051: SymbolSource source = new SymbolSourceImpl(providers);
052:
053: assertEquals(
054: source
055: .expandSymbols("Fred's friends are ${barney} and ${dino}."),
056: "Fred's friends are Barney and Dino.");
057:
058: verify();
059: }
060:
061: @Test
062: public void undefined_symbol() {
063:
064: SymbolProvider provider = mockSymbolProvider();
065:
066: List<SymbolProvider> providers = Arrays.asList(provider);
067:
068: train_valueForSymbol(provider, "barney", null);
069:
070: replay();
071:
072: SymbolSource source = new SymbolSourceImpl(providers);
073:
074: try {
075: source.valueForSymbol("barney");
076: unreachable();
077: } catch (RuntimeException ex) {
078: assertEquals(ex.getMessage(),
079: "Symbol 'barney' is not defined.");
080: }
081:
082: verify();
083: }
084:
085: @Test
086: public void missing_brace() {
087: SymbolProvider provider = mockSymbolProvider();
088:
089: List<SymbolProvider> providers = Arrays.asList(provider);
090:
091: replay();
092:
093: SymbolSource source = new SymbolSourceImpl(providers);
094:
095: try {
096: source.expandSymbols("Unmatched ${this");
097: unreachable();
098: } catch (RuntimeException ex) {
099: assertEquals(ex.getMessage(),
100: "Input string 'Unmatched ${this' is missing a symbol closing brace.");
101: }
102:
103: verify();
104: }
105:
106: @Test
107: public void indirect_expansions() {
108: SymbolProvider provider = mockSymbolProvider();
109:
110: List<SymbolProvider> providers = Arrays.asList(provider);
111:
112: train_valueForSymbol(provider, "fred.friends",
113: "${barney} and ${dino}");
114: train_valueForSymbol(provider, "barney", "Barney");
115: train_valueForSymbol(provider, "dino", "Dino");
116:
117: replay();
118:
119: SymbolSource source = new SymbolSourceImpl(providers);
120:
121: assertEquals(source
122: .expandSymbols("Fred's friends are ${fred.friends}."),
123: "Fred's friends are Barney and Dino.");
124:
125: verify();
126: }
127:
128: @Test
129: public void undefined_symbol_in_path() {
130:
131: SymbolProvider provider = mockSymbolProvider();
132:
133: List<SymbolProvider> providers = Arrays.asList(provider);
134:
135: train_valueForSymbol(provider, "barney",
136: "Barney (whose friends are ${barney.friends})");
137: train_valueForSymbol(provider, "barney.friends",
138: "${fred} and ${betty}");
139: train_valueForSymbol(provider, "fred", null);
140:
141: replay();
142:
143: SymbolSource source = new SymbolSourceImpl(providers);
144:
145: try {
146: source.valueForSymbol("barney");
147: unreachable();
148: } catch (RuntimeException ex) {
149: assertEquals(ex.getMessage(),
150: "Symbol 'fred' is not defined (in barney --> barney.friends --> fred). ");
151: }
152:
153: verify();
154: }
155:
156: @Test
157: public void missing_brace_in_path() {
158:
159: SymbolProvider provider = mockSymbolProvider();
160:
161: List<SymbolProvider> providers = Arrays.asList(provider);
162:
163: train_valueForSymbol(provider, "barney",
164: "Barney (whose friends are ${barney.friends})");
165: train_valueForSymbol(provider, "barney.friends",
166: "${fred} and ${betty");
167: train_valueForSymbol(provider, "fred", "Fred");
168:
169: replay();
170:
171: SymbolSource source = new SymbolSourceImpl(providers);
172:
173: try {
174: source.valueForSymbol("barney");
175: unreachable();
176: } catch (RuntimeException ex) {
177: assertEquals(
178: ex.getMessage(),
179: "Input string '${fred} and ${betty' is missing a symbol closing brace (in barney --> barney.friends).");
180: }
181:
182: verify();
183: }
184:
185: @Test
186: public void providers_searched_in_order() {
187: SymbolProvider provider1 = mockSymbolProvider();
188: SymbolProvider provider2 = mockSymbolProvider();
189:
190: List<SymbolProvider> providers = Arrays.asList(provider1,
191: provider2);
192:
193: train_valueForSymbol(provider1, "fred.friends",
194: "${barney} and ${dino}");
195: train_valueForSymbol(provider1, "barney", null);
196: train_valueForSymbol(provider2, "barney", "Barney");
197: train_valueForSymbol(provider1, "dino", null);
198: train_valueForSymbol(provider2, "dino", "Dino");
199:
200: replay();
201:
202: SymbolSource source = new SymbolSourceImpl(providers);
203:
204: assertEquals(source
205: .expandSymbols("Fred's friends are ${fred.friends}."),
206: "Fred's friends are Barney and Dino.");
207:
208: verify();
209: }
210:
211: @Test
212: public void symbols_are_cached() {
213: SymbolProvider provider = mockSymbolProvider();
214:
215: List<SymbolProvider> providers = Arrays.asList(provider);
216:
217: train_valueForSymbol(provider, "fred",
218: "Fred's friends are ${barney} and ${dino}.");
219: train_valueForSymbol(provider, "barney", "Barney");
220: train_valueForSymbol(provider, "dino", "Dino");
221:
222: replay();
223:
224: SymbolSource source = new SymbolSourceImpl(providers);
225:
226: assertEquals(source.valueForSymbol("fred"),
227: "Fred's friends are Barney and Dino.");
228:
229: verify();
230:
231: replay();
232:
233: // This time, comes out of the cache.
234:
235: assertEquals(source.valueForSymbol("fred"),
236: "Fred's friends are Barney and Dino.");
237:
238: verify();
239: }
240:
241: @Test
242: public void recursive_symbols_fail() {
243: SymbolProvider provider = mockSymbolProvider();
244:
245: List<SymbolProvider> providers = Arrays.asList(provider);
246:
247: train_valueForSymbol(provider, "fred",
248: "Fred (whose friends are ${fred.friends})");
249: train_valueForSymbol(provider, "fred.friends",
250: "${barney} and ${dino}");
251: train_valueForSymbol(provider, "barney",
252: "Barney (whose friends are ${barney.friends})");
253: train_valueForSymbol(provider, "barney.friends",
254: "${fred} and ${betty}");
255:
256: replay();
257:
258: SymbolSource source = new SymbolSourceImpl(providers);
259:
260: try {
261: source.valueForSymbol("fred");
262: unreachable();
263: } catch (RuntimeException ex) {
264: assertEquals(
265: ex.getMessage(),
266: "Symbol 'fred' is defined in terms of itself (fred --> fred.friends --> barney --> barney.friends --> fred).");
267: }
268:
269: verify();
270: }
271:
272: @Test
273: public void integration_test() {
274: SymbolSource source = getService(SymbolSource.class);
275:
276: // SystemPropertiesSymbolProvider is available by default
277:
278: String userName = System.getProperty("user.name");
279:
280: assertEquals(source.valueForSymbol("user.name"), userName);
281: }
282:
283: protected final void train_valueForSymbol(SymbolProvider provider,
284: String symbolName, String value) {
285: expect(provider.valueForSymbol(symbolName)).andReturn(value);
286: }
287:
288: protected final SymbolProvider mockSymbolProvider() {
289: return newMock(SymbolProvider.class);
290: }
291:
292: }
|