KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > script > TestIfToken


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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 /**
23  * Tests {@link org.apache.tapestry.script.IfToken}, the basis for the <if> and
24  * <if-not> elements.
25  *
26  * @author Howard M. Lewis Ship
27  * @since 4.0
28  */

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 JavaDoc.class);
39         sc.setReturnValue(Boolean.TRUE);
40
41         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc.class);
63         sc.setReturnValue(Boolean.FALSE);
64
65         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc.class);
85         sc.setReturnValue(Boolean.FALSE);
86
87         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc inner = new ApplicationRuntimeException("Simulated error.");
105
106         MockControl sc = newControl(ScriptSession.class);
107         ScriptSession s = (ScriptSession) sc.getMock();
108
109         s.evaluate("EXPRESSION", Boolean JavaDoc.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