/tiles-2.2.2/src/tiles-ognl/src/test/java/org/apache/tiles/ognl/OGNLAttributeEvaluatorTest.java

# · Java · 196 lines · 131 code · 13 blank · 52 comment · 0 complexity · 904df850a895ae9bf31db0efdf92347a MD5 · raw file

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