1 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 30 public class TestSymbolExpander extends HiveMindTestCase 31 { 32 private class SymbolSourceFixture implements SymbolSource 33 { 34 public String valueForSymbol(String name) 35 { 36 return name.toUpperCase(); 37 } 38 } 39 40 private void attempt(String expected, String text) 41 { 42 SymbolExpanderImpl e = new SymbolExpanderImpl(null, new SymbolSource[] {new SymbolSourceFixture()}); 43 44 String 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 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 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 |