1 15 package org.apache.tapestry.script; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Location; 19 import org.apache.hivemind.test.HiveMindTestCase; 20 import org.easymock.MockControl; 21 22 29 public class TestIfToken extends HiveMindTestCase 30 { 31 public void testEvaluateTrue() 32 { 33 MockControl sc = newControl(ScriptSession.class); 34 ScriptSession s = (ScriptSession) sc.getMock(); 35 36 IScriptToken nested = (IScriptToken) newMock(IScriptToken.class); 37 38 s.evaluate("EXPRESSION", Boolean .class); 39 sc.setReturnValue(Boolean.TRUE); 40 41 StringBuffer buffer = new StringBuffer (); 42 43 nested.write(buffer, s); 44 45 replayControls(); 46 47 IfToken t = new IfToken(true, "EXPRESSION", null); 48 t.addToken(nested); 49 50 t.write(buffer, s); 51 52 verifyControls(); 53 } 54 55 public void testEvaluateFalse() 56 { 57 MockControl sc = newControl(ScriptSession.class); 58 ScriptSession s = (ScriptSession) sc.getMock(); 59 60 IScriptToken nested = (IScriptToken) newMock(IScriptToken.class); 61 62 s.evaluate("EXPRESSION", Boolean .class); 63 sc.setReturnValue(Boolean.FALSE); 64 65 StringBuffer buffer = new StringBuffer (); 66 67 replayControls(); 68 69 IfToken t = new IfToken(true, "EXPRESSION", null); 70 t.addToken(nested); 71 72 t.write(buffer, s); 73 74 verifyControls(); 75 } 76 77 public void testEvaluateMatch() 78 { 79 MockControl sc = newControl(ScriptSession.class); 80 ScriptSession s = (ScriptSession) sc.getMock(); 81 82 IScriptToken nested = (IScriptToken) newMock(IScriptToken.class); 83 84 s.evaluate("EXPRESSION", Boolean .class); 85 sc.setReturnValue(Boolean.FALSE); 86 87 StringBuffer buffer = new StringBuffer (); 88 89 nested.write(buffer, s); 90 91 replayControls(); 92 93 IfToken t = new IfToken(false, "EXPRESSION", null); 94 t.addToken(nested); 95 96 t.write(buffer, s); 97 98 verifyControls(); 99 } 100 101 public void testEvaluateFailure() 102 { 103 Location l = fabricateLocation(8); 104 Throwable inner = new ApplicationRuntimeException("Simulated error."); 105 106 MockControl sc = newControl(ScriptSession.class); 107 ScriptSession s = (ScriptSession) sc.getMock(); 108 109 s.evaluate("EXPRESSION", Boolean .class); 110 sc.setThrowable(inner); 111 112 replayControls(); 113 114 IfToken t = new IfToken(false, "EXPRESSION", l); 115 116 try 117 { 118 t.write(null, s); 119 120 unreachable(); 121 } 122 catch (ApplicationRuntimeException ex) 123 { 124 assertEquals(inner.getMessage(), ex.getMessage()); 125 assertSame(l, ex.getLocation()); 126 assertSame(inner, ex.getRootCause()); 127 } 128 } 129 } | Popular Tags |