From d34a2c5d02d7c15534d14d5f1f5649a9bcba0031 Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Wed, 22 Apr 2009 17:57:24 +0000 Subject: [PATCH] SPR-5663: test and fix: inconsistency between canRead() and read() on ReflectivePropertyResolver --- .../support/ReflectivePropertyResolver.java | 2 +- .../expression/spel/SpringEL300Tests.java | 58 ++++++++++++++----- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java b/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java index 7035d08aa97..b318d3fc34f 100644 --- a/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java +++ b/org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyResolver.java @@ -88,7 +88,7 @@ public class ReflectivePropertyResolver implements PropertyAccessor { public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { if (target == null) { - return null; + throw new AccessException("Cannot read property of null target"); } Class type = (target instanceof Class ? (Class) target : target.getClass()); diff --git a/org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java b/org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java index 001f2a7b43e..1233bad4e4b 100644 --- a/org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java +++ b/org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java @@ -16,9 +16,13 @@ package org.springframework.expression.spel; +import org.springframework.expression.AccessException; +import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.expression.ParserContext; +import org.springframework.expression.PropertyAccessor; import org.springframework.expression.spel.antlr.SpelAntlrExpressionParser; +import org.springframework.expression.spel.support.ReflectivePropertyResolver; /** * Tests based on Jiras up to the release of Spring 3.0.0 @@ -27,24 +31,11 @@ import org.springframework.expression.spel.antlr.SpelAntlrExpressionParser; */ public class SpringEL300Tests extends ExpressionTestCase { - - public static final ParserContext DOLLARSQUARE_TEMPLATE_PARSER_CONTEXT = new ParserContext() { - public String getExpressionPrefix() { - return "$["; - } - public String getExpressionSuffix() { - return "]"; - } - public boolean isTemplate() { - return true; - } - }; - - public void testNPE_5661() { + public void testNPE_SPR5661() { evaluate("joinThreeStrings('a',null,'c')", "anullc", String.class); } - public void testNPE_5673() throws Exception { + public void testNPE_SPR5673() throws Exception { ParserContext hashes = TemplateExpressionParsingTests.HASH_DELIMITED_PARSER_CONTEXT; ParserContext dollars = TemplateExpressionParsingTests.DEFAULT_TEMPLATE_PARSER_CONTEXT; @@ -77,6 +68,28 @@ public class SpringEL300Tests extends ExpressionTestCase { checkTemplateParsing("Hello ${'inner literal that''s got {[(])]}an escaped quote in it'} World","Hello inner literal that's got {[(])]}an escaped quote in it World"); checkTemplateParsingError("Hello ${","No ending suffix '}' for expression starting at character 6: ${"); } + + public void testAccessingNullPropertyViaReflection_SPR5663() throws AccessException { + PropertyAccessor propertyAccessor = new ReflectivePropertyResolver(); + EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); + assertFalse(propertyAccessor.canRead(context, null, "abc")); + assertFalse(propertyAccessor.canWrite(context, null, "abc")); + try { + propertyAccessor.read(context, null, "abc"); + fail("Should have failed with an AccessException"); + } catch (AccessException ae) { + // success + } + try { + propertyAccessor.write(context, null, "abc","foo"); + fail("Should have failed with an AccessException"); + } catch (AccessException ae) { + // success + } + } + + + // --- private void checkTemplateParsing(String expression, String expectedValue) throws Exception { checkTemplateParsing(expression,TemplateExpressionParsingTests.DEFAULT_TEMPLATE_PARSER_CONTEXT, expectedValue); @@ -95,7 +108,7 @@ public class SpringEL300Tests extends ExpressionTestCase { private void checkTemplateParsingError(String expression,ParserContext context, String expectedMessage) throws Exception { SpelAntlrExpressionParser parser = new SpelAntlrExpressionParser(); try { - Expression expr = parser.parseExpression(expression,context); + parser.parseExpression(expression,context); fail("Should have failed"); } catch (Exception e) { if (!e.getMessage().equals(expectedMessage)) { @@ -104,5 +117,18 @@ public class SpringEL300Tests extends ExpressionTestCase { assertEquals(expectedMessage,e.getMessage()); } } + + private static final ParserContext DOLLARSQUARE_TEMPLATE_PARSER_CONTEXT = new ParserContext() { + public String getExpressionPrefix() { + return "$["; + } + public String getExpressionSuffix() { + return "]"; + } + public boolean isTemplate() { + return true; + } + }; + }