OGNLAttributeEvaluator extends AbstractAttributeEvaluator { public Object evaluate(String expression, Request request) { if (expression == null) { throw new IllegalArgumentException("The expression parameter cannot be null"); } try { return Ognl.getValue(expression, request); } catch (OgnlException e) { throw new EvaluationException("Cannot evaluate OGNL expression '" + expression + "'", e); } }  Object evaluate(String expression, Request request);  }
@Test public void testEvaluate() { Attribute attribute = new Attribute(); attribute.setExpressionObject(new Expression("requestScope.object1")); assertEquals("The value is not correct", "value", evaluator.evaluate( attribute, request)); attribute.setExpressionObject(new Expression("sessionScope.object2")); assertEquals("The value is not correct", new Integer(1), evaluator .evaluate(attribute, request)); attribute.setExpressionObject(new Expression("applicationScope.object3")); assertEquals("The value is not correct", new Float(2.0), evaluator .evaluate(attribute, request)); attribute.setExpressionObject(new Expression("object1")); assertEquals("The value is not correct", "value", evaluator.evaluate( attribute, request)); attribute.setExpressionObject(new Expression("object2")); assertEquals("The value is not correct", new Integer(1), evaluator .evaluate(attribute, request)); attribute.setExpressionObject(new Expression("object3")); assertEquals("The value is not correct", new Float(2.0), evaluator .evaluate(attribute, request)); attribute.setExpressionObject(new Expression("paulaBean.paula")); assertEquals("The value is not correct", "Brillant", evaluator .evaluate(attribute, request)); attribute.setExpressionObject(new Expression("'String literal'")); assertEquals("The value is not correct", "String literal", evaluator .evaluate(attribute, request)); attribute.setValue(new Integer(2)); assertEquals("The value is not correct", new Integer(2), evaluator .evaluate(attribute, request)); attribute.setValue("object1"); assertEquals("The value has been evaluated", "object1", evaluator .evaluate(attribute, request)); }
@Test public void testEvaluateString() { String expression = "requestScope.object1"; assertEquals("The value is not correct", "value", evaluator.evaluate( expression, request)); expression = "sessionScope.object2"; assertEquals("The value is not correct", new Integer(1), evaluator .evaluate(expression, request)); expression = "applicationScope.object3"; assertEquals("The value is not correct", new Float(2.0), evaluator .evaluate(expression, request)); expression = "object1"; assertEquals("The value is not correct", "value", evaluator.evaluate( expression, request)); expression = "object2"; assertEquals("The value is not correct", new Integer(1), evaluator .evaluate(expression, request)); expression = "object3"; assertEquals("The value is not correct", new Float(2.0), evaluator .evaluate(expression, request)); expression = "paulaBean.paula"; assertEquals("The value is not correct", "Brillant", evaluator .evaluate(expression, request)); expression = "'String literal'"; assertEquals("The value is not correct", "String literal", evaluator .evaluate(expression, request)); }
@Test(expected = IllegalArgumentException.class) public void testEvaluateNull() { evaluator.evaluate((String) null, request); }
@Test(expected = EvaluationException.class) public void testEvaluateOgnlException() { evaluator.evaluate("wrong|||!!!!yes###", request); }