This project has retired. For details please refer to its Attic page.
ELAttributeEvaluatorTest xref
View Javadoc

1   /*
2    * $Id: ELAttributeEvaluatorTest.java 816924 2009-09-19 13:45:40Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.el;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.tiles.Attribute;
29  import org.apache.tiles.Expression;
30  import org.apache.tiles.TilesApplicationContext;
31  import org.apache.tiles.context.TilesRequestContext;
32  import org.apache.tiles.el.ELAttributeEvaluator;
33  import org.apache.tiles.evaluator.el.TomcatExpressionFactoryFactory;
34  import org.easymock.EasyMock;
35  
36  /***
37   * Tests {@link ELAttributeEvaluator}.
38   *
39   * @version $Rev: 816924 $ $Date: 2009-09-19 15:45:40 +0200 (sab, 19 set 2009) $
40   */
41  public class ELAttributeEvaluatorTest extends TestCase {
42  
43      /***
44       * The evaluator to test.
45       */
46      private ELAttributeEvaluator evaluator;
47  
48      /***
49       * The request object to use.
50       */
51      private TilesRequestContext request;
52  
53      /*** {@inheritDoc} */
54      @SuppressWarnings("deprecation")
55      protected void setUp() throws Exception {
56          super.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);
75  
76          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      }
82  
83      /***
84       * Tests
85       * {@link ELAttributeEvaluator#evaluate(Attribute, TilesRequestContext)}.
86       */
87      public void 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     }
120 
121     /***
122      * Tests
123      * {@link ELAttributeEvaluator#evaluate(String, TilesRequestContext)}.
124      */
125     public void 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     }
151 
152     /***
153      * This is The Brillant Paula Bean (sic) just like it was posted to:
154      * http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx
155      * I hope that there is no copyright on it.
156      */
157     public static class PaulaBean {
158 
159         /***
160          * Paula is brillant, really.
161          */
162         private String paula = "Brillant";
163 
164         /***
165          * Returns brillant.
166          *
167          * @return "Brillant".
168          */
169         public String getPaula() {
170             return paula;
171         }
172     }
173 }