001: // Copyright 2004, 2005 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.hivemind.impl;
016:
017: import org.apache.commons.logging.LogFactory;
018: import org.apache.hivemind.ErrorHandler;
019: import org.apache.hivemind.Location;
020: import org.apache.hivemind.SymbolSource;
021: import org.apache.hivemind.test.HiveMindTestCase;
022: import org.easymock.MockControl;
023:
024: /**
025: * Tests for {@link org.apache.hivemind.impl.SymbolExpander}.
026: *
027: * @author Howard Lewis Ship
028: * @since 1.1
029: */
030: public class TestSymbolExpander extends HiveMindTestCase {
031: private class SymbolSourceFixture implements SymbolSource {
032: public String valueForSymbol(String name) {
033: return name.toUpperCase();
034: }
035: }
036:
037: private void attempt(String expected, String text) {
038: SymbolExpander e = new SymbolExpander(null,
039: new SymbolSourceFixture());
040:
041: String actual = e.expandSymbols(text, null);
042:
043: assertEquals(expected, actual);
044: }
045:
046: public void testSimple() {
047: attempt("Now is the TIME", "Now is the ${time}");
048: }
049:
050: public void testNoSymbols() {
051: attempt("No symbols in here", "No symbols in here");
052: }
053:
054: public void testFalseStart() {
055: attempt("The cost of the ITEM is $1,000.",
056: "The cost of the ${item} is $1,000.");
057: }
058:
059: public void testNestedBraces() {
060: attempt("Nested {BRACES}", "Nested ${{braces}}");
061: }
062:
063: public void testEmptySymbol() {
064: attempt("An empty ${} symbol", "An empty ${} symbol");
065: }
066:
067: public void testTrailingDollar() {
068: attempt("SYMBOL Ends with $", "${symbol} Ends with $");
069: }
070:
071: public void testEndsWithPartialSymbol() {
072: attempt("SYMBOL Ends with ${partial",
073: "${symbol} Ends with ${partial");
074: }
075:
076: public void testMissingSymbol() {
077: ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
078: Location l = newLocation();
079:
080: MockControl control = newControl(SymbolSource.class);
081: SymbolSource source = (SymbolSource) control.getMock();
082:
083: // Training
084:
085: source.valueForSymbol("symbol");
086: control.setReturnValue(null);
087:
088: eh.error(LogFactory.getLog(SymbolExpander.class), ImplMessages
089: .noSuchSymbol("symbol"), l, null);
090:
091: replayControls();
092:
093: SymbolExpander e = new SymbolExpander(eh, source);
094:
095: String actual = e.expandSymbols("Unknown ${symbol}", l);
096:
097: assertEquals("Unknown ${symbol}", actual);
098:
099: verifyControls();
100: }
101:
102: public void testEscaped() {
103: attempt("This is a SYMBOL, this is ${not}.",
104: "This is a ${symbol}, this is $${not}.");
105: }
106:
107: public void testEscapedAtStart() {
108: attempt("${not-a-symbol}", "$${not-a-symbol}");
109: }
110: }
|