Search in sources :

Example 11 with EvaluationException

use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.

the class IndexingTests method setGenericPropertyContainingListAutogrow.

@Test
public void setGenericPropertyContainingListAutogrow() {
    List<Integer> property = new ArrayList<>();
    this.property = property;
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression expression = parser.parseExpression("property");
    assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
    assertEquals(property, expression.getValue(this));
    expression = parser.parseExpression("property[0]");
    try {
        expression.setValue(this, "4");
    } catch (EvaluationException ex) {
        assertTrue(ex.getMessage().startsWith("EL1053E"));
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 12 with EvaluationException

use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.

the class IndexingTests method indexIntoGenericPropertyContainingGrowingList2.

@Test
public void indexIntoGenericPropertyContainingGrowingList2() {
    List<String> property2 = new ArrayList<>();
    this.property2 = property2;
    SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
    SpelExpressionParser parser = new SpelExpressionParser(configuration);
    Expression expression = parser.parseExpression("property2");
    assertEquals("java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
    assertEquals(property2, expression.getValue(this));
    expression = parser.parseExpression("property2[0]");
    try {
        assertEquals("bar", expression.getValue(this));
    } catch (EvaluationException ex) {
        assertTrue(ex.getMessage().startsWith("EL1053E"));
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 13 with EvaluationException

use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.

the class StandardTypeLocatorTests method testImports.

@Test
public void testImports() throws EvaluationException {
    StandardTypeLocator locator = new StandardTypeLocator();
    assertEquals(Integer.class, locator.findType("java.lang.Integer"));
    assertEquals(String.class, locator.findType("java.lang.String"));
    List<String> prefixes = locator.getImportPrefixes();
    assertEquals(1, prefixes.size());
    assertTrue(prefixes.contains("java.lang"));
    assertFalse(prefixes.contains("java.util"));
    assertEquals(Boolean.class, locator.findType("Boolean"));
    try {
        locator.findType("URL");
        fail("Should have failed");
    } catch (EvaluationException ee) {
        SpelEvaluationException sEx = (SpelEvaluationException) ee;
        assertEquals(SpelMessage.TYPE_NOT_FOUND, sEx.getMessageCode());
    }
    locator.registerImport("java.net");
    assertEquals(java.net.URL.class, locator.findType("URL"));
}
Also used : StandardTypeLocator(org.springframework.expression.spel.support.StandardTypeLocator) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 14 with EvaluationException

use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.

the class SpelReproTests method SPR5899.

@Test
public void SPR5899() throws Exception {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
    Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
    assertEquals(12, expr.getValue(eContext));
    expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
    assertEquals(null, expr.getValue(eContext));
    try {
        expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
        expr.getValue();
        fail("Should have failed to find a method to which it could pass null");
    } catch (EvaluationException see) {
    // success
    }
    eContext.setTypeLocator(new MyTypeLocator());
    // varargs
    expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
    assertEquals("ab", expr.getValue(eContext));
    // varargs 2 - null is packed into the varargs
    expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
    assertEquals("anullc", expr.getValue(eContext));
    // check we can find the ctor ok
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
    assertEquals("instance", expr.getValue(eContext));
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
    assertEquals("instance", expr.getValue(eContext));
    // ctor varargs
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
    assertEquals("instance", expr.getValue(eContext));
    // ctor varargs 2
    expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
    assertEquals("instance", expr.getValue(eContext));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 15 with EvaluationException

use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.

the class SetValueTests method setValue.

/**
	 * For use when coercion is happening during a setValue().  The expectedValue should be
	 * the coerced form of the value.
	 */
protected void setValue(String expression, Object value, Object expectedValue) {
    try {
        Expression e = parser.parseExpression(expression);
        if (e == null) {
            fail("Parser returned null for expression");
        }
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        assertTrue("Expression is not writeable but should be", e.isWritable(lContext));
        e.setValue(lContext, value);
        Object a = expectedValue;
        Object b = e.getValue(lContext);
        if (!a.equals(b)) {
            fail("Not the same: [" + a + "] type=" + a.getClass() + "  [" + b + "] type=" + b.getClass());
        //				assertEquals("Retrieved value was not equal to set value", expectedValue, e.getValue(lContext));
        }
    } catch (EvaluationException ee) {
        ee.printStackTrace();
        fail("Unexpected Exception: " + ee.getMessage());
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail("Unexpected Exception: " + pe.getMessage());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) ParseException(org.springframework.expression.ParseException)

Aggregations

EvaluationException (org.springframework.expression.EvaluationException)25 Expression (org.springframework.expression.Expression)16 Test (org.junit.Test)14 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)11 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)10 ArrayList (java.util.ArrayList)5 AccessException (org.springframework.expression.AccessException)5 ParseException (org.springframework.expression.ParseException)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)4 MethodParameter (org.springframework.core.MethodParameter)3 TypeConverter (org.springframework.expression.TypeConverter)3 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 PropertyAccessor (org.springframework.expression.PropertyAccessor)2 CompilablePropertyAccessor (org.springframework.expression.spel.CompilablePropertyAccessor)2 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)2 StandardTypeLocator (org.springframework.expression.spel.support.StandardTypeLocator)2 Constructor (java.lang.reflect.Constructor)1