2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.test.el;
19
20 import javax.el.ELContext;
21 import javax.el.PropertyNotWritableException;
22 import javax.el.ValueExpression;
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import org.apache.shale.test.base.AbstractJsfTestCase;
26 import org.apache.shale.test.mock.MockApplication12;
27
28 /***
29 * <p>Test case for <code>MockExpressionFactory.</p>
30 */
31 public class MockExpressionFactoryTestCase extends AbstractJsfTestCase {
32
33
34
35
36
37
38 public MockExpressionFactoryTestCase(String name) {
39 super(name);
40 }
41
42
43
44
45
46
47 protected void setUp() throws Exception {
48
49 super.setUp();
50 factory = (MockExpressionFactory)
51 ((MockApplication12) application).getExpressionFactory();
52
53 }
54
55
56
57 public static Test suite() {
58
59 return (new TestSuite(MockExpressionFactoryTestCase.class));
60
61 }
62
63
64
65 protected void tearDown() throws Exception {
66
67 factory = null;
68 super.tearDown();
69
70 }
71
72
73
74
75
76 /***
77 * <p>Input values to be converted, of all the interesting types.</p>
78 */
79 private static final Object[] INPUT_VALUES = {
80 (String) null,
81 "",
82 "1234",
83 Boolean.TRUE,
84 Boolean.FALSE,
85 new Byte((byte) 123),
86 new Double(234),
87 new Float(345),
88 new Integer(456),
89 new Long(567),
90 new Short((short) 678),
91 };
92
93
94 /***
95 * <p>Output values when converted to Boolean.</p>
96 */
97 private static final Boolean[] OUTPUT_BOOLEAN = {
98 Boolean.FALSE,
99 Boolean.FALSE,
100 Boolean.FALSE,
101 Boolean.TRUE,
102 Boolean.FALSE,
103 null,
104 null,
105 null,
106 null,
107 null,
108 null
109 };
110
111
112 /***
113 * <p>Output values when converted to Integer.</p>
114 */
115 private static final Integer[] OUTPUT_INTEGER = {
116 new Integer(0),
117 new Integer(0),
118 new Integer(1234),
119 new Integer(1),
120 new Integer(0),
121 new Integer(123),
122 new Integer(234),
123 new Integer(345),
124 new Integer(456),
125 new Integer(567),
126 new Integer(678)
127 };
128
129
130 /***
131 * <p>Output values when converted to String.</p>
132 */
133 private static final String[] OUTPUT_STRING = {
134 "",
135 "",
136 "1234",
137 "true",
138 "false",
139 "123",
140 "234.0",
141 "345.0",
142 "456",
143 "567",
144 "678"
145 };
146
147
148
149
150
151 /***
152 * <p>The expression factory to be tested.</p>
153 */
154 private MockExpressionFactory factory = null;
155
156
157
158
159
160
161 public void testCoerceToBoolean() {
162
163 Object result = null;
164 for (int i = 0; i < INPUT_VALUES.length; i++) {
165 try {
166 result = factory.coerceToType(INPUT_VALUES[i], Boolean.class);
167 if ((INPUT_VALUES[i] == null)
168 || (INPUT_VALUES[i] instanceof String)
169 || (INPUT_VALUES[i] instanceof Boolean)) {
170 assertEquals("[" + i + "]", OUTPUT_BOOLEAN[i], result);
171 } else {
172 fail("[" + i + "] should have thrown IllegalArgumentException");
173 }
174 } catch (IllegalArgumentException e) {
175 if ((INPUT_VALUES[i] == null)
176 || (INPUT_VALUES[i] instanceof String)
177 || (INPUT_VALUES[i] instanceof Boolean)) {
178 fail("[" + i + "] threw IllegalArgumentException");
179 } else {
180 ;
181 }
182 }
183 }
184
185 }
186
187
188
189
190 public void testCoerceToNull() {
191
192 Object result = null;
193 for (int i = 0; i < INPUT_VALUES.length; i++) {
194 result = factory.coerceToType(INPUT_VALUES[i], null);
195 if (INPUT_VALUES[i] == null) {
196 assertNull(result);
197 } else {
198 assertTrue("[" + i + "]", result == INPUT_VALUES[i]);
199 }
200 }
201
202 }
203
204
205
206
207 public void testCoerceToObject() {
208
209 Object result = null;
210 for (int i = 0; i < INPUT_VALUES.length; i++) {
211 result = factory.coerceToType(INPUT_VALUES[i], Object.class);
212 if (INPUT_VALUES[i] == null) {
213 assertNull(result);
214 } else {
215 assertTrue("[" + i + "]", result == INPUT_VALUES[i]);
216 }
217 }
218
219 }
220
221
222
223 public void testCoerceToInteger() {
224
225 Object result = null;
226 for (int i = 0; i < INPUT_VALUES.length; i++) {
227 try {
228 result = factory.coerceToType(INPUT_VALUES[i], Integer.class);
229 if ((INPUT_VALUES[i] != null)
230 && (INPUT_VALUES[i] instanceof Boolean)) {
231 fail("[" + i + "] should have thrown IllegalArgumentException");
232 } else {
233 assertEquals("[" + i + "]", OUTPUT_INTEGER[i], result);
234 }
235 } catch (IllegalArgumentException e) {
236 if ((INPUT_VALUES[i] != null)
237 && (INPUT_VALUES[i] instanceof Boolean)) {
238 ;
239 } else {
240 fail("[" + i + "] should have thrown IllegalArgumentException");
241 }
242 }
243 }
244
245 }
246
247
248
249 public void testCoerceToString() {
250
251 Object result = null;
252 for (int i = 0; i < INPUT_VALUES.length; i++) {
253 result = factory.coerceToType(INPUT_VALUES[i], String.class);
254 assertEquals("[" + i + "]", OUTPUT_STRING[i], result);
255 }
256
257 }
258
259
260
261 public void testLiteralValueExpressionInteger() {
262
263 ELContext context = facesContext.getELContext();
264
265 ValueExpression expr = factory.createValueExpression("123", Integer.class);
266 assertEquals(Integer.class, expr.getExpectedType());
267 assertEquals(String.class, expr.getType(context));
268 assertEquals(new Integer(123), expr.getValue(context));
269 assertTrue(expr.isLiteralText());
270 assertTrue(expr.isReadOnly(context));
271 try {
272 expr.setValue(context, "234");
273 fail("Should have thrown PropertyNotWritableException");
274 } catch (PropertyNotWritableException e) {
275 ;
276 }
277
278 }
279
280
281
282 public void testLiteralValueExpressionNone() {
283
284 ELContext context = facesContext.getELContext();
285
286 ValueExpression expr = factory.createValueExpression("abc", String.class);
287 assertEquals(String.class, expr.getExpectedType());
288 assertEquals(String.class, expr.getType(context));
289 assertEquals("abc", expr.getValue(context));
290 assertTrue(expr.isLiteralText());
291 assertTrue(expr.isReadOnly(context));
292 try {
293 expr.setValue(context, "def");
294 fail("Should have thrown PropertyNotWritableException");
295 } catch (PropertyNotWritableException e) {
296 ;
297 }
298
299 }
300
301
302
303 public void testLiteralValueExpressionString() {
304
305 ELContext context = facesContext.getELContext();
306
307 ValueExpression expr = factory.createValueExpression(new Integer(123), String.class);
308 assertEquals(String.class, expr.getExpectedType());
309 assertEquals(Integer.class, expr.getType(context));
310 assertEquals("123", expr.getValue(context));
311 assertTrue(expr.isLiteralText());
312 assertTrue(expr.isReadOnly(context));
313 try {
314 expr.setValue(context, new Integer(234));
315 fail("Should have thrown PropertyNotWritableException");
316 } catch (PropertyNotWritableException e) {
317 ;
318 }
319
320 }
321
322
323
324 public void testValueExpressionString() {
325
326 request.setAttribute("org.apache.shale.test", new Integer(123));
327 ELContext context = facesContext.getELContext();
328
329 ValueExpression expr = factory.createValueExpression(context, "#{requestScope['org.apache.shale.test']}", String.class);
330 Object ref = expr.getValue(context);
331 assertNotNull(ref);
332 assertTrue(ref instanceof String);
333 assertEquals("123", ref);
334 }
335
336 public void testPristine() {
337
338 assertNotNull(factory);
339
340 }
341
342
343
344 }