KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > ExpressionEvaluationUtilsTests


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

16
17 package org.springframework.web.util;
18
19 import javax.servlet.jsp.PageContext JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.mock.web.MockPageContext;
24
25 /**
26  * @author Aled Arendsen
27  * @since 16.09.2003
28  */

29 public class ExpressionEvaluationUtilsTests extends TestCase {
30
31     public void testIsExpressionLanguage() {
32         // should be true
33
String JavaDoc expr = "${bla}";
34         assertTrue(ExpressionEvaluationUtils.isExpressionLanguage(expr));
35         
36         // should be true
37
expr = "bla${bla}";
38         assertTrue(ExpressionEvaluationUtils.isExpressionLanguage(expr));
39         
40         // should be false
41
expr = "bla{bla";
42         assertFalse(ExpressionEvaluationUtils.isExpressionLanguage(expr));
43         
44         // should be false
45
expr = "bla$b{";
46         assertFalse(ExpressionEvaluationUtils.isExpressionLanguage(expr));
47         
48         // ok, tested enough ;-)
49
}
50
51     public void testEvaluate() throws Exception JavaDoc {
52         PageContext JavaDoc ctx = new MockPageContext();
53         
54         ctx.setAttribute("bla", "blie");
55         String JavaDoc expr = "${bla}";
56         
57         Object JavaDoc o = ExpressionEvaluationUtils.evaluate("test", expr, String JavaDoc.class, ctx);
58         assertEquals(o, "blie");
59         
60         assertEquals(new String JavaDoc("test"),
61                      ExpressionEvaluationUtils.evaluate("test", "test", Float JavaDoc.class, ctx));
62     }
63
64     public void testEvaluateString() throws Exception JavaDoc {
65         PageContext JavaDoc ctx = new MockPageContext();
66         
67         ctx.setAttribute("bla", "blie");
68         String JavaDoc expr = "${bla}";
69         Object JavaDoc o = ExpressionEvaluationUtils.evaluateString("test", expr, ctx);
70         assertEquals(o, "blie");
71         
72         assertEquals("blie", ExpressionEvaluationUtils.evaluateString("test", "blie", ctx));
73     }
74
75     public void testEvaluateInteger() throws Exception JavaDoc {
76         PageContext JavaDoc ctx = new MockPageContext();
77         
78         ctx.setAttribute("bla", new Integer JavaDoc(1));
79         String JavaDoc expr = "${bla}";
80         
81         int i = ExpressionEvaluationUtils.evaluateInteger("test", expr, ctx);
82         assertEquals(i, 1);
83         
84         assertEquals(21, ExpressionEvaluationUtils.evaluateInteger("test", "21", ctx));
85     }
86
87     public void testEvaluateBoolean() throws Exception JavaDoc {
88         PageContext JavaDoc ctx = new MockPageContext();
89         
90         ctx.setAttribute("bla", new Boolean JavaDoc(true));
91         String JavaDoc expr = "${bla}";
92         
93         boolean b = ExpressionEvaluationUtils.evaluateBoolean("test", expr, ctx);
94         assertEquals(b, true);
95         
96         assertEquals(true, ExpressionEvaluationUtils.evaluateBoolean("test", "true", ctx));
97     }
98
99 }
100
Popular Tags