1 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 25 public class VariableExpanderTest extends TestCase { 26 27 30 public VariableExpanderTest(String name) { 31 super(name); 32 } 33 34 35 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 48 public void testEmpty() { 49 try { 50 assertNull(VariableExpander.expand((String ) null, new SimpleStrategy())); 51 assertEquals("", VariableExpander.expand("", new SimpleStrategy())); 52 } catch (ExpanderException ee) { 53 fail("Shouldn't have failed"); 54 } 55 } 56 57 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 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 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 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 201 public void testStringFailureStrategy() { 202 203 try { 204 ExpanderStrategy strat = new FailureStrategy(); 205 String str = "Inside ${variable} ${variable} outside"; 206 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 218 public void testStringBufferFailureStrategy() { 219 220 try { 221 ExpanderStrategy strat = new FailureStrategy(); 222 StringBuffer str = new StringBuffer ("Inside ${long variable with spaces} ${long_variable{{{${} outside"); 223 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 235 public void testAsserts() { 236 try { 237 StringBuffer str = new StringBuffer (""); 238 VariableExpander.expand(str, null); 239 fail("Should have failed"); 240 } catch (ExpanderException ee) { 241 fail("Should have asserted"); 242 } catch (AssertionError ae) { 243 } 245 } 246 247 250 private void helperGood(String in, String expected, ExpanderStrategy strat) { 251 try { 252 String 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 strBuf = new StringBuffer (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 e) { 264 fail(e.toString()); 265 } 266 } 267 268 269 270 271 274 public static class SimpleStrategy implements ExpanderStrategy { 275 private int count = 1; 276 277 public String expand(String name) { 278 assertTrue("Should be 'variable" + count + "' but is '" + name + "'", 279 name.equals("variable" + count)); 280 count++; 281 return "foo"; 282 } 283 } 284 285 288 public static class LongNamesStrategy implements ExpanderStrategy { 289 private int count = 1; 290 291 public String expand(String 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 307 public static class FailureStrategy implements ExpanderStrategy { 308 public String expand(String name) throws ExpanderException { 309 throw new ExpanderException("Message"); 310 } 311 } 312 } 313 314 | Popular Tags |