KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > binding > TestExpressionBinding


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.binding;
16
17 import org.apache.tapestry.BindingException;
18 import org.apache.tapestry.IComponent;
19 import org.apache.tapestry.coerce.ValueConverter;
20 import org.apache.tapestry.services.ExpressionCache;
21 import org.apache.tapestry.services.ExpressionEvaluator;
22 import org.easymock.MockControl;
23
24 /**
25  * Tests for {@link org.apache.tapestry.binding.ExpressionBinding}.
26  *
27  * @author Howard M. Lewis Ship
28  * @since 4.0
29  */

30 public class TestExpressionBinding extends BindingTestCase
31 {
32
33     public void testInvariant()
34     {
35         MockControl evc = newControl(ExpressionEvaluator.class);
36         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
37
38         MockControl ecc = newControl(ExpressionCache.class);
39         ExpressionCache ec = (ExpressionCache) ecc.getMock();
40
41         MockControl cc = newControl(IComponent.class);
42         IComponent component = (IComponent) cc.getMock();
43
44         Object JavaDoc compiled = new Object JavaDoc();
45
46         Object JavaDoc expressionValue = "EXPRESSION-VALUE";
47
48         ValueConverter vc = newValueConverter();
49
50         ec.getCompiledExpression("exp");
51         ecc.setReturnValue(compiled);
52
53         ev.isConstant("exp");
54         evc.setReturnValue(true);
55
56         ev.readCompiled(component, compiled);
57         evc.setReturnValue(expressionValue);
58
59         component.getExtendedId();
60         cc.setReturnValue("Foo/bar.baz");
61
62         replayControls();
63
64         ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component,
65                 "exp", ev, ec);
66
67         assertEquals(true, b.isInvariant());
68
69         // A second time, to test the 'already initialized'
70
// code path.
71

72         assertEquals(true, b.isInvariant());
73
74         // Get the object, which should be cached.
75

76         assertSame(expressionValue, b.getObject());
77
78         assertSame(component, b.getComponent());
79
80         assertEquals("ExpressionBinding[Foo/bar.baz exp]", b.toString());
81
82         verifyControls();
83     }
84
85     public void testVariant()
86     {
87         MockControl evc = newControl(ExpressionEvaluator.class);
88         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
89
90         MockControl ecc = newControl(ExpressionCache.class);
91         ExpressionCache ec = (ExpressionCache) ecc.getMock();
92
93         IComponent component = newComponent();
94         Object JavaDoc compiled = new Object JavaDoc();
95
96         Object JavaDoc expressionValue1 = new Object JavaDoc();
97         Object JavaDoc expressionValue2 = new Object JavaDoc();
98
99         ValueConverter vc = newValueConverter();
100
101         ec.getCompiledExpression("exp");
102         ecc.setReturnValue(compiled);
103
104         ev.isConstant("exp");
105         evc.setReturnValue(false);
106
107         ev.readCompiled(component, compiled);
108         evc.setReturnValue(expressionValue1);
109
110         ev.readCompiled(component, compiled);
111         evc.setReturnValue(expressionValue2);
112
113         replayControls();
114
115         ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component,
116                 "exp", ev, ec);
117
118         assertEquals(false, b.isInvariant());
119
120         // Check that the expression is re-evaluated on
121
// each call to getObject().
122

123         assertSame(expressionValue1, b.getObject());
124
125         assertSame(expressionValue2, b.getObject());
126
127         verifyControls();
128     }
129
130     public void testSetObject()
131     {
132         MockControl evc = newControl(ExpressionEvaluator.class);
133         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
134
135         MockControl ecc = newControl(ExpressionCache.class);
136         ExpressionCache ec = (ExpressionCache) ecc.getMock();
137
138         IComponent component = newComponent();
139         Object JavaDoc compiled = new Object JavaDoc();
140
141         ValueConverter vc = newValueConverter();
142
143         ec.getCompiledExpression("exp");
144         ecc.setReturnValue(compiled);
145
146         ev.isConstant("exp");
147         evc.setReturnValue(false);
148
149         Object JavaDoc newValue = new Object JavaDoc();
150
151         ev.writeCompiled(component, compiled, newValue);
152
153         replayControls();
154
155         ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component,
156                 "exp", ev, ec);
157
158         b.setObject(newValue);
159
160         verifyControls();
161     }
162
163     public void testSetObjectInvariant()
164     {
165         MockControl evc = newControl(ExpressionEvaluator.class);
166         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
167
168         MockControl ecc = newControl(ExpressionCache.class);
169         ExpressionCache ec = (ExpressionCache) ecc.getMock();
170
171         IComponent component = newComponent("Foo/bar.baz");
172         Object JavaDoc compiled = new Object JavaDoc();
173
174         ValueConverter vc = newValueConverter();
175
176         ec.getCompiledExpression("exp");
177         ecc.setReturnValue(compiled);
178
179         ev.isConstant("exp");
180         evc.setReturnValue(true);
181
182         replayControls();
183
184         ExpressionBinding b = new ExpressionBinding("parameter foo", fabricateLocation(1), vc, component,
185                 "exp", ev, ec);
186
187         try
188         {
189             b.setObject(new Object JavaDoc());
190             unreachable();
191         }
192         catch (BindingException ex)
193         {
194             assertEquals(
195                     "Binding for parameter foo (ExpressionBinding[Foo/bar.baz exp]) may not be updated.",
196                     ex.getMessage());
197         }
198
199         verifyControls();
200     }
201
202     public void testSetObjectFailure()
203     {
204         MockControl evc = newControl(ExpressionEvaluator.class);
205         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
206
207         MockControl ecc = newControl(ExpressionCache.class);
208         ExpressionCache ec = (ExpressionCache) ecc.getMock();
209
210         IComponent component = newComponent();
211         Object JavaDoc compiled = new Object JavaDoc();
212
213         ValueConverter vc = newValueConverter();
214
215         ec.getCompiledExpression("exp");
216         ecc.setReturnValue(compiled);
217
218         ev.isConstant("exp");
219         evc.setReturnValue(false);
220
221         Object JavaDoc newValue = new Object JavaDoc();
222
223         RuntimeException JavaDoc innerException = new RuntimeException JavaDoc("Failure");
224
225         ev.writeCompiled(component, compiled, newValue);
226         evc.setThrowable(innerException);
227
228         replayControls();
229
230         ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component,
231                 "exp", ev, ec);
232
233         try
234         {
235             b.setObject(newValue);
236             unreachable();
237         }
238         catch (BindingException ex)
239         {
240             assertEquals("Failure", ex.getMessage());
241             assertSame(innerException, ex.getRootCause());
242         }
243
244         verifyControls();
245     }
246
247     public void testCompileExpressionFailure()
248     {
249         MockControl evc = newControl(ExpressionEvaluator.class);
250         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
251
252         MockControl ecc = newControl(ExpressionCache.class);
253         ExpressionCache ec = (ExpressionCache) ecc.getMock();
254
255         IComponent component = newComponent();
256         ValueConverter vc = newValueConverter();
257
258         Throwable JavaDoc innerException = new RuntimeException JavaDoc("Failure");
259
260         ec.getCompiledExpression("exp");
261         ecc.setThrowable(innerException);
262
263         replayControls();
264
265         ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component,
266                 "exp", ev, ec);
267
268         try
269         {
270             b.isInvariant();
271             unreachable();
272         }
273         catch (BindingException ex)
274         {
275             assertEquals("Failure", ex.getMessage());
276             assertSame(innerException, ex.getRootCause());
277         }
278
279         verifyControls();
280     }
281
282     public void testResolveExpressionFailure()
283     {
284         MockControl evc = newControl(ExpressionEvaluator.class);
285         ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock();
286
287         MockControl ecc = newControl(ExpressionCache.class);
288         ExpressionCache ec = (ExpressionCache) ecc.getMock();
289
290         IComponent component = newComponent();
291         Object JavaDoc compiled = new Object JavaDoc();
292
293         ValueConverter vc = newValueConverter();
294
295         ec.getCompiledExpression("exp");
296         ecc.setReturnValue(compiled);
297
298         ev.isConstant("exp");
299         evc.setReturnValue(false);
300
301         Throwable JavaDoc innerException = new RuntimeException JavaDoc("Failure");
302
303         ev.readCompiled(component, compiled);
304         evc.setThrowable(innerException);
305
306         replayControls();
307
308         ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component,
309                 "exp", ev, ec);
310
311         try
312         {
313             b.getObject();
314             unreachable();
315         }
316         catch (BindingException ex)
317         {
318             assertEquals("Failure", ex.getMessage());
319             assertSame(innerException, ex.getRootCause());
320         }
321
322         verifyControls();
323     }
324 }
Popular Tags