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

1   /*
2    * $Id: MVELAttributeEvaluatorTest.java 817009 2009-09-20 11:26:26Z 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.mvel;
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.context.TilesRequestContextHolder;
33  import org.apache.tiles.mvel.MVELAttributeEvaluator;
34  import org.apache.tiles.mvel.TilesContextBeanVariableResolverFactory;
35  import org.apache.tiles.mvel.TilesContextVariableResolverFactory;
36  import org.easymock.EasyMock;
37  import org.mvel2.integration.VariableResolverFactory;
38  
39  /***
40   * Tests {@link MVELAttributeEvaluator}.
41   *
42   * @version $Rev: 817009 $ $Date: 2009-09-20 13:26:26 +0200 (dom, 20 set 2009) $$
43   */
44  public class MVELAttributeEvaluatorTest extends TestCase {
45  
46      /***
47       * The evaluator to test.
48       */
49      private MVELAttributeEvaluator evaluator;
50  
51      /***
52       * The request object to use.
53       */
54      private TilesRequestContext request;
55  
56      /*** {@inheritDoc} */
57      protected void setUp() throws Exception {
58          super.setUp();
59          TilesRequestContextHolder requestHolder = new TilesRequestContextHolder();
60          VariableResolverFactory variableResolverFactory = new TilesContextVariableResolverFactory(
61                  requestHolder);
62          variableResolverFactory
63                  .setNextFactory(new TilesContextBeanVariableResolverFactory(
64                          requestHolder));
65          evaluator = new MVELAttributeEvaluator(requestHolder,
66                  variableResolverFactory);
67          Map<String, Object> requestScope = new HashMap<String, Object>();
68          Map<String, Object> sessionScope = new HashMap<String, Object>();
69          Map<String, Object> applicationScope = new HashMap<String, Object>();
70          requestScope.put("object1", "value");
71          sessionScope.put("object2", new Integer(1));
72          applicationScope.put("object3", new Float(2.0));
73          requestScope.put("paulaBean", new PaulaBean());
74          request = EasyMock.createMock(TilesRequestContext.class);
75          EasyMock.expect(request.getRequestScope()).andReturn(requestScope)
76                  .anyTimes();
77          EasyMock.expect(request.getSessionScope()).andReturn(sessionScope)
78                  .anyTimes();
79          TilesApplicationContext applicationContext = EasyMock
80                  .createMock(TilesApplicationContext.class);
81          EasyMock.expect(request.getApplicationContext()).andReturn(
82                  applicationContext).anyTimes();
83          EasyMock.expect(applicationContext.getApplicationScope()).andReturn(
84                  applicationScope).anyTimes();
85          EasyMock.replay(request, applicationContext);
86      }
87  
88      /***
89       * Tests
90       * {@link MVELAttributeEvaluator#evaluate(Attribute, TilesRequestContext)}.
91       */
92      public void testEvaluate() {
93          Attribute attribute = new Attribute();
94          attribute.setExpressionObject(new Expression("requestScope.object1"));
95          assertEquals("The value is not correct", "value", evaluator.evaluate(
96                  attribute, request));
97          attribute.setExpressionObject(new Expression("sessionScope.object2"));
98          assertEquals("The value is not correct", new Integer(1), evaluator
99                  .evaluate(attribute, request));
100         attribute.setExpressionObject(new Expression("applicationScope.object3"));
101         assertEquals("The value is not correct", new Float(2.0), evaluator
102                 .evaluate(attribute, request));
103         attribute.setExpressionObject(new Expression("object1"));
104         assertEquals("The value is not correct", "value", evaluator.evaluate(
105                 attribute, request));
106         attribute.setExpressionObject(new Expression("object2"));
107         assertEquals("The value is not correct", new Integer(1), evaluator
108                 .evaluate(attribute, request));
109         attribute.setExpressionObject(new Expression("object3"));
110         assertEquals("The value is not correct", new Float(2.0), evaluator
111                 .evaluate(attribute, request));
112         attribute.setExpressionObject(new Expression("paulaBean.paula"));
113         assertEquals("The value is not correct", "Brillant", evaluator
114                 .evaluate(attribute, request));
115         attribute.setExpressionObject(new Expression("'String literal'"));
116         assertEquals("The value is not correct", "String literal", evaluator
117                 .evaluate(attribute, request));
118         attribute.setValue(new Integer(2));
119         assertEquals("The value is not correct", new Integer(2), evaluator
120                 .evaluate(attribute, request));
121         attribute.setValue("object1");
122         assertEquals("The value has been evaluated", "object1", evaluator
123                 .evaluate(attribute, request));
124     }
125 
126     /***
127      * Tests {@link MVELAttributeEvaluator#evaluate(String, TilesRequestContext)}.
128      */
129     public void testEvaluateString() {
130         String expression = "requestScope.object1";
131         assertEquals("The value is not correct", "value", evaluator.evaluate(
132                 expression, request));
133         expression = "sessionScope.object2";
134         assertEquals("The value is not correct", new Integer(1), evaluator
135                 .evaluate(expression, request));
136         expression = "applicationScope.object3";
137         assertEquals("The value is not correct", new Float(2.0), evaluator
138                 .evaluate(expression, request));
139         expression = "object1";
140         assertEquals("The value is not correct", "value", evaluator.evaluate(
141                 expression, request));
142         expression = "object2";
143         assertEquals("The value is not correct", new Integer(1), evaluator
144                 .evaluate(expression, request));
145         expression = "object3";
146         assertEquals("The value is not correct", new Float(2.0), evaluator
147                 .evaluate(expression, request));
148         expression = "paulaBean.paula";
149         assertEquals("The value is not correct", "Brillant", evaluator
150                 .evaluate(expression, request));
151         expression = "'String literal'";
152         assertEquals("The value is not correct", "String literal", evaluator
153                 .evaluate(expression, request));
154     }
155 
156     /***
157      * This is The Brillant Paula Bean (sic) just like it was posted to:
158      * http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx I hope that
159      * there is no copyright on it.
160      */
161     public static class PaulaBean {
162 
163         /***
164          * Paula is brillant, really.
165          */
166         private String paula = "Brillant";
167 
168         /***
169          * Returns brillant.
170          *
171          * @return "Brillant".
172          */
173         public String getPaula() {
174             return paula;
175         }
176     }
177 }