Search in sources :

Example 46 with Expression

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

the class EvaluationTests method expectFail.

private void expectFail(ExpressionParser parser, EvaluationContext eContext, String expressionString, SpelMessage messageCode) {
    try {
        Expression e = parser.parseExpression(expressionString);
        SpelUtilities.printAbstractSyntaxTree(System.out, e);
        e.getValue(eContext);
        fail();
    } catch (SpelEvaluationException see) {
        see.printStackTrace();
        assertEquals(messageCode, see.getMessageCode());
    }
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression)

Example 47 with Expression

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

the class EvaluationTests method limitCollectionGrowing.

@Test
public void limitCollectionGrowing() throws Exception {
    TestClass instance = new TestClass();
    StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true, 3));
    Expression expression = parser.parseExpression("foo[2]");
    expression.setValue(ctx, "2");
    assertThat(instance.getFoo().size(), equalTo(3));
    expression = parser.parseExpression("foo[3]");
    try {
        expression.setValue(ctx, "3");
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode());
        assertThat(instance.getFoo().size(), equalTo(3));
    }
}
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) Test(org.junit.Test)

Example 48 with Expression

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

the class EvaluationTests method increment01root.

// For now I am making #this not assignable
@Test
public void increment01root() {
    Integer i = 42;
    StandardEvaluationContext ctx = new StandardEvaluationContext(i);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = parser.parseExpression("#this++");
    assertEquals(42, i.intValue());
    try {
        e.getValue(ctx, Integer.class);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode());
    }
}
Also used : BigInteger(java.math.BigInteger) 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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 49 with Expression

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

the class EvaluationTests method collectionGrowingViaIndexer.

/**
	 * This test is checking that with the changes for 9751 that the refactoring in Indexer is
	 * coping correctly for references beyond collection boundaries.
	 */
@Test
public void collectionGrowingViaIndexer() {
    Spr9751 instance = new Spr9751();
    // Add a new element to the list
    StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = parser.parseExpression("listOfStrings[++index3]='def'");
    e.getValue(ctx);
    assertEquals(2, instance.listOfStrings.size());
    assertEquals("def", instance.listOfStrings.get(1));
    // Check reference beyond end of collection
    ctx = new StandardEvaluationContext(instance);
    parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    e = parser.parseExpression("listOfStrings[0]");
    String value = e.getValue(ctx, String.class);
    assertEquals("abc", value);
    e = parser.parseExpression("listOfStrings[1]");
    value = e.getValue(ctx, String.class);
    assertEquals("def", value);
    e = parser.parseExpression("listOfStrings[2]");
    value = e.getValue(ctx, String.class);
    assertEquals("", value);
    // Now turn off growing and reference off the end
    ctx = new StandardEvaluationContext(instance);
    parser = new SpelExpressionParser(new SpelParserConfiguration(false, false));
    e = parser.parseExpression("listOfStrings[3]");
    try {
        e.getValue(ctx, String.class);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, see.getMessageCode());
    }
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 50 with Expression

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

the class ExpressionLanguageScenarioTests method testScenario_UsingStandardInfrastructure.

/**
	 * Scenario: using the standard infrastructure and running simple expression evaluation.
	 */
@Test
public void testScenario_UsingStandardInfrastructure() {
    try {
        // Create a parser
        SpelExpressionParser parser = new SpelExpressionParser();
        // Parse an expression
        Expression expr = parser.parseRaw("new String('hello world')");
        // Evaluate it using a 'standard' context
        Object value = expr.getValue();
        // They are reusable
        value = expr.getValue();
        assertEquals("hello world", value);
        assertEquals(String.class, value.getClass());
    } catch (EvaluationException ee) {
        ee.printStackTrace();
        fail("Unexpected Exception: " + ee.getMessage());
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail("Unexpected Exception: " + pe.getMessage());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) ParseException(org.springframework.expression.ParseException) Test(org.junit.Test)

Aggregations

Expression (org.springframework.expression.Expression)299 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)159 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)78 EvaluationContext (org.springframework.expression.EvaluationContext)53 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)19 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 Map (java.util.Map)14 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 LinkedHashMap (java.util.LinkedHashMap)11 ParseException (org.springframework.expression.ParseException)11 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9