1/*2 * $Id: ELAttributeEvaluatorTest.java 816924 2009-09-19 13:45:40Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.el;
2223import java.util.HashMap;
24import java.util.Map;
2526import junit.framework.TestCase;
2728import org.apache.tiles.Attribute;
29import org.apache.tiles.Expression;
30import org.apache.tiles.TilesApplicationContext;
31import org.apache.tiles.context.TilesRequestContext;
32import org.apache.tiles.el.ELAttributeEvaluator;
33import org.apache.tiles.evaluator.el.TomcatExpressionFactoryFactory;
34import org.easymock.EasyMock;
3536/***37 * Tests {@link ELAttributeEvaluator}.38 *39 * @version $Rev: 816924 $ $Date: 2009-09-19 15:45:40 +0200 (sab, 19 set 2009) $40 */41publicclassELAttributeEvaluatorTestextends TestCase {
4243/***44 * The evaluator to test.45 */46private ELAttributeEvaluator evaluator;
4748/***49 * The request object to use.50 */51private TilesRequestContext request;
5253/*** {@inheritDoc} */54 @SuppressWarnings("deprecation")
55protectedvoid setUp() throws Exception {
56super.setUp();
57 evaluator = new ELAttributeEvaluator();
58 Map<String, Object> requestScope = new HashMap<String, Object>();
59 Map<String, Object> sessionScope = new HashMap<String, Object>();
60 Map<String, Object> applicationScope = new HashMap<String, Object>();
61 requestScope.put("object1", "value");
62 sessionScope.put("object2", new Integer(1));
63 applicationScope.put("object3", new Float(2.0));
64 requestScope.put("paulaBean", new PaulaBean());
65 request = EasyMock.createMock(TilesRequestContext.class);
66 EasyMock.expect(request.getRequestScope()).andReturn(requestScope)
67 .anyTimes();
68 EasyMock.expect(request.getSessionScope()).andReturn(sessionScope)
69 .anyTimes();
70 TilesApplicationContext applicationContext = EasyMock
71 .createMock(TilesApplicationContext.class);
72 EasyMock.expect(applicationContext.getApplicationScope()).andReturn(
73 applicationScope).anyTimes();
74 EasyMock.replay(request, applicationContext);
7576 evaluator.setApplicationContext(applicationContext);
77 Map<String, String> params = new HashMap<String, String>();
78 params.put(ELAttributeEvaluator.EXPRESSION_FACTORY_FACTORY_INIT_PARAM,
79 TomcatExpressionFactoryFactory.class.getName());
80 evaluator.init(params);
81 }
8283/***84 * Tests85 * {@link ELAttributeEvaluator#evaluate(Attribute, TilesRequestContext)}.86 */87publicvoid testEvaluate() {
88 Attribute attribute = new Attribute();
89 attribute.setExpressionObject(new Expression("${requestScope.object1}"));
90 assertEquals("The value is not correct", "value", evaluator.evaluate(
91 attribute, request));
92 attribute.setExpressionObject(new Expression("${sessionScope.object2}"));
93 assertEquals("The value is not correct", new Integer(1), evaluator
94 .evaluate(attribute, request));
95 attribute.setExpressionObject(new Expression("${applicationScope.object3}"));
96 assertEquals("The value is not correct", new Float(2.0), evaluator
97 .evaluate(attribute, request));
98 attribute.setExpressionObject(new Expression("${object1}"));
99 assertEquals("The value is not correct", "value", evaluator.evaluate(
100 attribute, request));
101 attribute.setExpressionObject(new Expression("${object2}"));
102 assertEquals("The value is not correct", new Integer(1), evaluator
103 .evaluate(attribute, request));
104 attribute.setExpressionObject(new Expression("${object3}"));
105 assertEquals("The value is not correct", new Float(2.0), evaluator
106 .evaluate(attribute, request));
107 attribute.setExpressionObject(new Expression("${paulaBean.paula}"));
108 assertEquals("The value is not correct", "Brillant", evaluator
109 .evaluate(attribute, request));
110 attribute.setExpressionObject(new Expression("String literal"));
111 assertEquals("The value is not correct", "String literal", evaluator
112 .evaluate(attribute, request));
113 attribute.setValue(new Integer(2));
114 assertEquals("The value is not correct", new Integer(2), evaluator
115 .evaluate(attribute, request));
116 attribute.setValue("${object1}");
117 assertEquals("The value has been evaluated", "${object1}", evaluator
118 .evaluate(attribute, request));
119 }
120121/***122 * Tests123 * {@link ELAttributeEvaluator#evaluate(String, TilesRequestContext)}.124 */125publicvoid testEvaluateString() {
126 String expression = "${requestScope.object1}";
127 assertEquals("The value is not correct", "value", evaluator.evaluate(
128 expression, request));
129 expression = "${sessionScope.object2}";
130 assertEquals("The value is not correct", new Integer(1), evaluator
131 .evaluate(expression, request));
132 expression = "${applicationScope.object3}";
133 assertEquals("The value is not correct", new Float(2.0), evaluator
134 .evaluate(expression, request));
135 expression = "${object1}";
136 assertEquals("The value is not correct", "value", evaluator.evaluate(
137 expression, request));
138 expression = "${object2}";
139 assertEquals("The value is not correct", new Integer(1), evaluator
140 .evaluate(expression, request));
141 expression = "${object3}";
142 assertEquals("The value is not correct", new Float(2.0), evaluator
143 .evaluate(expression, request));
144 expression = "${paulaBean.paula}";
145 assertEquals("The value is not correct", "Brillant", evaluator
146 .evaluate(expression, request));
147 expression = "String literal";
148 assertEquals("The value is not correct", expression, evaluator
149 .evaluate(expression, request));
150 }
151152/***153 * This is The Brillant Paula Bean (sic) just like it was posted to:154 * http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx155 * I hope that there is no copyright on it.156 */157publicstaticclass PaulaBean {
158159/***160 * Paula is brillant, really.161 */162private String paula = "Brillant";
163164/***165 * Returns brillant.166 *167 * @return "Brillant".168 */169public String getPaula() {
170return paula;
171 }
172 }
173 }