2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // ------------------------------------------------------------ Constructors
35  
36  
37      // Construct a new instance of this test case.
38      public MockExpressionFactoryTestCase(String name) {
39          super(name);
40      }
41  
42  
43      // ---------------------------------------------------- Overall Test Methods
44  
45  
46      // Set up instance variables required by this test case.
47      protected void setUp() throws Exception {
48  
49          super.setUp();
50          factory = (MockExpressionFactory)
51            ((MockApplication12) application).getExpressionFactory();
52  
53      }
54  
55  
56      // Return the tests included in this test case.
57      public static Test suite() {
58  
59          return (new TestSuite(MockExpressionFactoryTestCase.class));
60  
61      }
62  
63  
64      // Tear down instance variables required by this test case.
65      protected void tearDown() throws Exception {
66  
67          factory = null;
68          super.tearDown();
69  
70      }
71  
72  
73      // -------------------------------------------------------- Static Variables
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     // ------------------------------------------------------ Instance Variables
149 
150 
151     /***
152      * <p>The expression factory to be tested.</p>
153      */
154     private MockExpressionFactory factory = null;
155 
156 
157     // ------------------------------------------------- Individual Test Methods
158 
159 
160     // Test coercion when expectedType is passed as Boolean.
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                     ; // Expected result
181                 }
182             }
183         }
184 
185     }
186 
187 
188     // Test coercion when expectedType is passed as null.  We should
189     // get the original object back
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     // Test coercion when expectedType is Object.  We should
206     // get the original object back.
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     // Test coercion when expectedType is Integer
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                     ; // Expected result
239                 } else {
240                     fail("[" + i + "] should have thrown IllegalArgumentException");
241                 }
242             }
243         }
244 
245     }
246 
247 
248     // Test coercion when expectedType is String.
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     // Test ValueExpression that wraps a literal String object and conversion to Integer
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             ; // Expected result
276         }
277 
278     }
279 
280 
281     // Test ValueExpression that wraps a literal String object and no conversion
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             ; // Expected result
297         }
298 
299     }
300 
301 
302     // Test ValueExpression that wraps a literal Integer object and conversion to String
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             ; // Expected result
318         }
319 
320     }
321 
322 
323     // Test ValueExpression
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 }