1 15 package org.apache.tapestry.junit.script; 16 17 import java.io.IOException ; 18 import java.util.Arrays ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.apache.hivemind.ClassResolver; 23 import org.apache.hivemind.Resource; 24 import org.apache.hivemind.impl.DefaultClassResolver; 25 import org.apache.hivemind.util.ClasspathResource; 26 import org.apache.tapestry.IRequestCycle; 27 import org.apache.tapestry.IScript; 28 import org.apache.tapestry.engine.RequestCycle; 29 import org.apache.tapestry.junit.TapestryTestCase; 30 import org.apache.tapestry.script.ScriptParser; 31 import org.apache.tapestry.script.ScriptSession; 32 import org.apache.tapestry.script.ScriptSessionImpl; 33 import org.apache.tapestry.services.ExpressionCache; 34 import org.apache.tapestry.services.ExpressionEvaluator; 35 import org.apache.tapestry.services.impl.ExpressionCacheImpl; 36 import org.apache.tapestry.services.impl.ExpressionEvaluatorImpl; 37 import org.apache.tapestry.util.xml.DocumentParseException; 38 39 45 46 public class TestScript extends TapestryTestCase 47 { 48 private MockScriptProcessor _processor = new MockScriptProcessor(); 49 50 protected static ExpressionEvaluator createExpressionEvaluator() 51 { 52 ExpressionCache cache = new ExpressionCacheImpl(); 53 ExpressionEvaluatorImpl result = new ExpressionEvaluatorImpl(); 54 result.setExpressionCache(cache); 55 56 return result; 57 } 58 59 private IScript read(String file) throws IOException , DocumentParseException 60 { 61 ClassResolver resolver = new DefaultClassResolver(); 62 ScriptParser parser = new ScriptParser(resolver, createExpressionEvaluator(), null); 63 64 String classAsPath = "/" + getClass().getName().replace('.', '/'); 65 66 Resource classLocation = new ClasspathResource(resolver, classAsPath); 67 Resource scriptLocation = classLocation.getRelativeResource(file); 68 69 return parser.parse(scriptLocation); 70 } 71 72 private IScript execute(String file, Map symbols) throws DocumentParseException, IOException 73 { 74 IScript script = read(file); 75 76 IRequestCycle cycle = (IRequestCycle) newMock(IRequestCycle.class); 77 78 replayControls(); 79 80 script.execute(cycle, _processor, symbols); 81 82 verifyControls(); 83 84 return script; 85 } 86 87 private void assertSymbol(Map symbols, String key, Object expected) 88 { 89 Object actual = symbols.get(key); 90 91 assertEquals(key, expected, actual); 92 } 93 94 97 98 public void testSimple() throws Exception 99 { 100 execute("simple.script", null); 101 102 assertEquals("body", "\nBODY\n", _processor.getBody()); 103 assertEquals("initialization", "\nINITIALIZATION\n", _processor.getInitialization()); 104 assertNull(_processor.getExternalScripts()); 105 } 106 107 112 113 public void testUnique() throws Exception 114 { 115 IScript script = read("unique.script"); 116 117 IRequestCycle cycle = new RequestCycle(); 118 119 script.execute(cycle, _processor, null); 120 script.execute(cycle, _processor, null); 121 122 assertEquals("Block1\nBlock2\nNotUnique\n\n\n\nNotUnique", _processor.getBody().trim()); 123 } 124 125 128 129 public void testEmpty() throws Exception 130 { 131 execute("empty.script", null); 132 133 assertNull("body", _processor.getBody()); 134 assertNull("initialization", _processor.getInitialization()); 135 } 136 137 141 142 public void testLet() throws Exception 143 { 144 String inputSymbol = Long.toHexString(System.currentTimeMillis()); 145 Map symbols = new HashMap (); 146 symbols.put("inputSymbol", inputSymbol); 147 148 execute("let.script", symbols); 149 150 152 String outputSymbol = "output: " + inputSymbol; 153 154 assertEquals("Output symbol", outputSymbol, symbols.get("outputSymbol")); 155 } 156 157 162 public void testUniqueLet() throws Exception 163 { 164 Map symbols = new HashMap (); 165 166 execute("unique-let.script", symbols); 167 168 assertSymbol(symbols, "alpha", "Alpha"); 169 assertSymbol(symbols, "beta", "Alpha$0"); 170 assertSymbol(symbols, "gamma", "Alpha$1"); 171 } 172 173 public void testIncludeScript() throws Exception 174 { 175 IScript script = execute("include-script.script", null); 176 177 Resource scriptLocation = script.getScriptResource(); 178 179 Resource[] expected = new Resource[] 180 { scriptLocation.getRelativeResource("first"), 181 scriptLocation.getRelativeResource("second"), 182 scriptLocation.getRelativeResource("third") }; 183 184 assertEquals("included scripts", Arrays.asList(expected), Arrays.asList(_processor 185 .getExternalScripts())); 186 } 187 188 public void testAntSyntax() throws Exception 189 { 190 Map form = new HashMap (); 191 192 form.put("name", "gallahad"); 193 194 Map component = new HashMap (); 195 component.put("form", form); 196 component.put("name", "lancelot"); 197 198 Map symbols = new HashMap (); 199 symbols.put("component", component); 200 201 execute("ant-syntax.script", symbols); 202 203 assertSymbol(symbols, "functionName", "gallahad_lancelot"); 204 assertSymbol(symbols, "incomplete1", "Incomplete: $"); 205 assertSymbol(symbols, "incomplete2", "Incomplete: ${"); 206 assertSymbol(symbols, "nopath", "This ${} ends up as literal."); 207 assertSymbol(symbols, "OGNL", "This is a brace: }."); 208 } 209 210 public void testSet() throws Exception 211 { 212 Map symbols = new HashMap (); 213 214 execute("set.script", symbols); 215 216 assertSymbol(symbols, "element2", new Character ('p')); 217 } 218 219 public void testInvalidKeyLet() throws Exception 220 { 221 try 222 { 223 execute("invalid-key-let.script", new HashMap ()); 224 225 unreachable(); 226 } 227 catch (DocumentParseException ex) 228 { 229 checkException(ex, "key"); 230 } 231 } 232 233 public void testInvalidKeySet() throws Exception 234 { 235 try 236 { 237 execute("invalid-key-set.script", new HashMap ()); 238 239 unreachable(); 240 } 241 catch (DocumentParseException ex) 242 { 243 checkException(ex, "key"); 244 } 245 } 246 247 public void testInputSymbolClass() throws Exception 248 { 249 try 250 { 251 Map symbols = new HashMap (); 252 symbols.put("input", new Integer (20)); 253 254 execute("input-symbol-class.script", symbols); 255 256 unreachable(); 257 } 258 catch (Exception ex) 259 { 260 checkException(ex, "Integer"); 261 checkException(ex, "Long"); 262 } 263 } 264 265 public void testInputSymbol() throws Exception 266 { 267 Map symbols = new HashMap (); 268 symbols.put("input", new Long (20)); 269 270 execute("input-symbol.script", symbols); 271 272 assertSymbol(symbols, "success", "Success"); 273 } 274 275 public void testInputSymbolRequired() throws Exception 276 { 277 try 278 { 279 execute("input-symbol-required.script", new HashMap ()); 280 281 unreachable(); 282 } 283 catch (Exception ex) 284 { 285 checkException(ex, "required"); 286 } 287 } 288 289 public void testInputSymbolInvalidKey() throws Exception 290 { 291 try 292 { 293 execute("input-symbol-invalid-key.script", new HashMap ()); 294 295 unreachable(); 296 } 297 catch (DocumentParseException ex) 298 { 299 checkException(ex, "key"); 300 } 301 302 } 303 304 305 306 public void testNameAppend() throws Exception 307 { 308 Map symbols = new HashMap (); 309 310 symbols.put("name", "fred"); 311 execute("name-append.script", symbols); 312 313 assertSymbol(symbols, "output", "fred$suffix"); 314 } 315 316 319 public void testCheats() throws Exception 320 { 321 IScript script = execute("simple.script", null); 322 323 ScriptSession session = new ScriptSessionImpl(script.getScriptResource(), null, null, 324 createExpressionEvaluator(), null, null); 325 assertEquals("ScriptSession[" + script.getScriptResource() + "]", session.toString()); 326 } 327 } | Popular Tags |