Search in sources :

Example 6 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 7 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 8 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 9 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)

Example 10 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)

Aggregations

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