KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestExpressionEvaluator


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.services.impl;
16
17 import java.lang.reflect.Method JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import ognl.TypeConverter;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.test.HiveMindTestCase;
25 import org.apache.tapestry.Tapestry;
26 import org.apache.tapestry.services.ExpressionCache;
27 import org.apache.tapestry.services.ExpressionEvaluator;
28 import org.apache.tapestry.spec.IApplicationSpecification;
29 import org.easymock.AbstractMatcher;
30 import org.easymock.MockControl;
31
32 /**
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36 public class TestExpressionEvaluator extends HiveMindTestCase
37 {
38     private ExpressionEvaluator create()
39     {
40         ExpressionCache cache = new ExpressionCacheImpl();
41
42         ExpressionEvaluatorImpl result = new ExpressionEvaluatorImpl();
43
44         result.setExpressionCache(cache);
45
46         return result;
47     }
48
49     private static class NullMeansIgnoreMatcher extends AbstractMatcher
50     {
51         public boolean matches(Object JavaDoc[] expected, Object JavaDoc[] actual)
52         {
53             for (int i = 0; i < expected.length; i++)
54             {
55                 if (expected[i] == null)
56                     continue;
57
58                 if (!argumentMatches(expected[i], actual[i]))
59                     return false;
60             }
61
62             return true;
63         }
64
65     }
66
67     public static class Fixture
68     {
69         private String JavaDoc _value;
70
71         public Fixture()
72         {
73         }
74
75         public Fixture(String JavaDoc value)
76         {
77             _value = value;
78         }
79
80         public String JavaDoc getValue()
81         {
82             return _value;
83         }
84
85         public void setValue(String JavaDoc value)
86         {
87             _value = value;
88         }
89     }
90
91     public void testRead()
92     {
93         Fixture f = new Fixture("Foo");
94
95         ExpressionEvaluator ee = create();
96
97         assertEquals("Foo", ee.read(f, "value"));
98     }
99
100     public void testReadFail()
101     {
102         Fixture f = new Fixture();
103
104         ExpressionEvaluator ee = create();
105
106         try
107         {
108             ee.read(f, "bar");
109             unreachable();
110         }
111         catch (ApplicationRuntimeException ex)
112         {
113             assertExceptionSubstring(ex, "Unable to read OGNL expression");
114             assertSame(f, ex.getComponent());
115         }
116     }
117
118     public void testWrite()
119     {
120         Fixture f = new Fixture("Foo");
121
122         ExpressionEvaluator ee = create();
123
124         ee.write(f, "value", "Bar");
125
126         assertEquals("Bar", f.getValue());
127     }
128
129     public void testWriteFail()
130     {
131         Fixture f = new Fixture();
132
133         ExpressionEvaluator ee = create();
134
135         try
136         {
137             ee.write(f, "class", "Foo");
138             unreachable();
139         }
140         catch (ApplicationRuntimeException ex)
141         {
142             assertExceptionSubstring(ex, "Unable to update OGNL expression");
143             assertSame(f, ex.getComponent());
144         }
145     }
146
147     public void testIsConstant()
148     {
149         ExpressionEvaluator ee = create();
150
151         assertEquals(true, ee.isConstant("true"));
152         assertEquals(true, ee.isConstant("'OGNL'"));
153         assertEquals(false, ee.isConstant("foo.bar"));
154         assertEquals(false, ee.isConstant("bar()"));
155         assertEquals(true, ee.isConstant("@org.apache.tapestry.Tapestry@HOME_SERVICE"));
156     }
157
158     public void testIsConstantFail()
159     {
160         ExpressionEvaluator ee = create();
161
162         try
163         {
164             ee.isConstant("@foo@BAR");
165             unreachable();
166         }
167         catch (ApplicationRuntimeException ex)
168         {
169             assertExceptionSubstring(ex, "Error evaluating OGNL expression");
170         }
171
172     }
173
174     public void testTypeConverter() throws Exception JavaDoc
175     {
176         MockControl asc = newControl(IApplicationSpecification.class);
177         IApplicationSpecification as = (IApplicationSpecification) asc.getMock();
178
179         MockControl tcc = newControl(TypeConverter.class);
180         TypeConverter tc = (TypeConverter) tcc.getMock();
181
182         // Training
183

184         as.checkExtension(Tapestry.OGNL_TYPE_CONVERTER);
185         asc.setReturnValue(true);
186
187         as.getExtension(Tapestry.OGNL_TYPE_CONVERTER, TypeConverter.class);
188         asc.setReturnValue(tc);
189
190         replayControls();
191
192         ExpressionCache cache = new ExpressionCacheImpl();
193
194         ExpressionEvaluatorImpl ee = new ExpressionEvaluatorImpl();
195
196         ee.setExpressionCache(cache);
197         ee.setApplicationSpecification(as);
198         ee.setContributions(Collections.EMPTY_LIST);
199         
200         ee.initializeService();
201
202         verifyControls();
203
204         Fixture f = new Fixture();
205
206         Method JavaDoc m = Fixture.class.getMethod("setValue", new Class JavaDoc[]
207         { String JavaDoc.class });
208
209         Date JavaDoc d = new Date JavaDoc();
210
211         // Training
212

213         // Since we have no idea what OGNL will stuff into that Map parameter,
214
// we just ignore it.
215
tc.convertValue(null, f, m, "value", d, String JavaDoc.class);
216         tcc.setMatcher(new NullMeansIgnoreMatcher());
217
218         tcc.setReturnValue("FROM-TYPE-CONVERTER");
219
220         replayControls();
221
222         ee.write(f, "value", d);
223
224         assertEquals("FROM-TYPE-CONVERTER", f.getValue());
225
226         verifyControls();
227     }
228 }
Popular Tags