Search in sources :

Example 41 with Expression

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

the class AbstractExpressionTests method evaluate.

/**
	 * Evaluate an expression and check that the actual result matches the
	 * expectedValue and the class of the result matches the expectedClassOfResult.
	 * @param expression the expression to evaluate
	 * @param expectedValue the expected result for evaluating the expression
	 * @param expectedResultType the expected class of the evaluation result
	 */
public void evaluate(String expression, Object expectedValue, Class<?> expectedResultType) {
    Expression expr = parser.parseExpression(expression);
    if (expr == null) {
        fail("Parser returned null for expression");
    }
    if (DEBUG) {
        SpelUtilities.printAbstractSyntaxTree(System.out, expr);
    }
    Object value = expr.getValue(eContext);
    // Check the return value
    if (value == null) {
        if (expectedValue == null) {
            // no point doing other checks
            return;
        }
        assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
    }
    Class<?> resultType = value.getClass();
    assertEquals("Type of the actual result was not as expected.  Expected '" + expectedResultType + "' but result was of type '" + resultType + "'", expectedResultType, resultType);
    if (expectedValue instanceof String) {
        assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, AbstractExpressionTests.stringValueOf(value));
    } else {
        assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
    }
}
Also used : Expression(org.springframework.expression.Expression)

Example 42 with Expression

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

the class AbstractExpressionTests method parseAndCheckError.

/**
	 * Parse the specified expression and ensure the expected message comes out.
	 * The message may have inserts and they will be checked if otherProperties is specified.
	 * The first entry in otherProperties should always be the position.
	 * @param expression the expression to evaluate
	 * @param expectedMessage the expected message
	 * @param otherProperties the expected inserts within the message
	 */
protected void parseAndCheckError(String expression, SpelMessage expectedMessage, Object... otherProperties) {
    try {
        Expression expr = parser.parseExpression(expression);
        SpelUtilities.printAbstractSyntaxTree(System.out, expr);
        fail("Parsing should have failed!");
    } catch (ParseException pe) {
        SpelParseException ex = (SpelParseException) pe;
        if (ex.getMessageCode() != expectedMessage) {
            assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
        }
        if (otherProperties != null && otherProperties.length != 0) {
            // first one is expected position of the error within the string
            int pos = ((Integer) otherProperties[0]).intValue();
            assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
            if (otherProperties.length > 1) {
                // Check inserts match
                Object[] inserts = ex.getInserts();
                if (inserts == null) {
                    inserts = new Object[0];
                }
                if (inserts.length < otherProperties.length - 1) {
                    fail("Cannot check " + (otherProperties.length - 1) + " properties of the exception, it only has " + inserts.length + " inserts");
                }
                for (int i = 1; i < otherProperties.length; i++) {
                    if (!inserts[i - 1].equals(otherProperties[i])) {
                        fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '" + inserts[i - 1] + "'");
                    }
                }
            }
        }
    }
}
Also used : Expression(org.springframework.expression.Expression) ParseException(org.springframework.expression.ParseException)

Example 43 with Expression

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

the class AbstractExpressionTests method evaluateAndCheckError.

/**
	 * Evaluate the specified expression and ensure the expected message comes out.
	 * The message may have inserts and they will be checked if otherProperties is specified.
	 * The first entry in otherProperties should always be the position.
	 * @param expression the expression to evaluate
	 * @param expectedReturnType ask the expression return value to be of this type if possible
	 * ({@code null} indicates don't ask for conversion)
	 * @param expectedMessage the expected message
	 * @param otherProperties the expected inserts within the message
	 */
protected void evaluateAndCheckError(String expression, Class<?> expectedReturnType, SpelMessage expectedMessage, Object... otherProperties) {
    try {
        Expression expr = parser.parseExpression(expression);
        if (expr == null) {
            fail("Parser returned null for expression");
        }
        if (expectedReturnType != null) {
            expr.getValue(eContext, expectedReturnType);
        } else {
            expr.getValue(eContext);
        }
        fail("Should have failed with message " + expectedMessage);
    } catch (EvaluationException ee) {
        SpelEvaluationException ex = (SpelEvaluationException) ee;
        if (ex.getMessageCode() != expectedMessage) {
            assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
        }
        if (otherProperties != null && otherProperties.length != 0) {
            // first one is expected position of the error within the string
            int pos = ((Integer) otherProperties[0]).intValue();
            assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
            if (otherProperties.length > 1) {
                // Check inserts match
                Object[] inserts = ex.getInserts();
                if (inserts == null) {
                    inserts = new Object[0];
                }
                if (inserts.length < otherProperties.length - 1) {
                    fail("Cannot check " + (otherProperties.length - 1) + " properties of the exception, it only has " + inserts.length + " inserts");
                }
                for (int i = 1; i < otherProperties.length; i++) {
                    if (otherProperties[i] == null) {
                        if (inserts[i - 1] != null) {
                            fail("Insert does not match, expected 'null' but insert value was '" + inserts[i - 1] + "'");
                        }
                    } else if (inserts[i - 1] == null) {
                        if (otherProperties[i] != null) {
                            fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was 'null'");
                        }
                    } else if (!inserts[i - 1].equals(otherProperties[i])) {
                        fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '" + inserts[i - 1] + "'");
                    }
                }
            }
        }
    }
}
Also used : Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException)

Example 44 with Expression

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

the class ArrayConstructorTests method evaluateArrayBuildingExpression.

private String evaluateArrayBuildingExpression(String expression, String expectedToString) {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression e = parser.parseExpression(expression);
    Object o = e.getValue();
    assertNotNull(o);
    assertTrue(o.getClass().isArray());
    StringBuilder s = new StringBuilder();
    s.append('[');
    if (o instanceof int[]) {
        int[] array = (int[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof boolean[]) {
        boolean[] array = (boolean[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof char[]) {
        char[] array = (char[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof long[]) {
        long[] array = (long[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof short[]) {
        short[] array = (short[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof double[]) {
        double[] array = (double[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof float[]) {
        float[] array = (float[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else if (o instanceof byte[]) {
        byte[] array = (byte[]) o;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                s.append(',');
            }
            s.append(array[i]);
        }
    } else {
        fail("Not supported " + o.getClass());
    }
    s.append(']');
    assertEquals(expectedToString, s.toString());
    return s.toString();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression)

Example 45 with Expression

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

the class EvaluationTests method increment02prefix.

@Test
public void increment02prefix() {
    Spr9751 helper = new Spr9751();
    StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = null;
    // BigDecimal
    e = parser.parseExpression("++bd");
    assertTrue(new BigDecimal("2").equals(helper.bd));
    BigDecimal return_bd = e.getValue(ctx, BigDecimal.class);
    assertTrue(new BigDecimal("3").equals(return_bd));
    assertTrue(new BigDecimal("3").equals(helper.bd));
    // double
    e = parser.parseExpression("++ddd");
    assertEquals(2.0d, helper.ddd, 0d);
    double return_ddd = e.getValue(ctx, Double.TYPE);
    assertEquals(3.0d, return_ddd, 0d);
    assertEquals(3.0d, helper.ddd, 0d);
    // float
    e = parser.parseExpression("++fff");
    assertEquals(3.0f, helper.fff, 0d);
    float return_fff = e.getValue(ctx, Float.TYPE);
    assertEquals(4.0f, return_fff, 0d);
    assertEquals(4.0f, helper.fff, 0d);
    // long
    e = parser.parseExpression("++lll");
    assertEquals(66666L, helper.lll);
    long return_lll = e.getValue(ctx, Long.TYPE);
    assertEquals(66667L, return_lll);
    assertEquals(66667L, helper.lll);
    // int
    e = parser.parseExpression("++iii");
    assertEquals(42, helper.iii);
    int return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(43, return_iii);
    assertEquals(43, helper.iii);
    return_iii = e.getValue(ctx, Integer.TYPE);
    assertEquals(44, return_iii);
    assertEquals(44, helper.iii);
    // short
    e = parser.parseExpression("++sss");
    assertEquals(15, helper.sss);
    int return_sss = (Integer) e.getValue(ctx);
    assertEquals(16, return_sss);
    assertEquals(16, helper.sss);
}
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) BigDecimal(java.math.BigDecimal) 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