Search in sources :

Example 1 with ParseException

use of cn.taketoday.expression.ParseException in project today-infrastructure by TAKETODAY.

the class SetValueTests method setValue.

protected void setValue(String expression, Object value) {
    try {
        Expression e = parser.parseExpression(expression);
        assertThat(e).isNotNull();
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        assertThat(e.isWritable(lContext)).as("Expression is not writeable but should be").isTrue();
        e.setValue(lContext, value);
        assertThat(e.getValue(lContext, value.getClass())).as("Retrieved value was not equal to set value").isEqualTo(value);
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError("Unexpected Exception: " + ex.getMessage(), ex);
    }
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException)

Example 2 with ParseException

use of cn.taketoday.expression.ParseException in project today-framework by TAKETODAY.

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();
        assertThat(value).isEqualTo("hello world");
        assertThat(value.getClass()).isEqualTo(String.class);
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError(ex.getMessage(), ex);
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException) Test(org.junit.jupiter.api.Test)

Example 3 with ParseException

use of cn.taketoday.expression.ParseException in project today-framework by TAKETODAY.

the class ExpressionLanguageScenarioTests method testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem.

/**
 * Scenario: using your own java methods and calling them from the expression
 */
@Test
public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException {
    try {
        // Create a parser
        SpelExpressionParser parser = new SpelExpressionParser();
        // Use the standard evaluation context
        StandardEvaluationContext ctx = new StandardEvaluationContext();
        ctx.registerFunction("repeat", ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat", String.class));
        Expression expr = parser.parseRaw("#repeat('hello')");
        Object value = expr.getValue(ctx);
        assertThat(value).isEqualTo("hellohello");
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError(ex.getMessage(), ex);
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException) Test(org.junit.jupiter.api.Test)

Example 4 with ParseException

use of cn.taketoday.expression.ParseException in project today-framework by TAKETODAY.

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);
        assertThat(e).isNotNull();
        if (DEBUG) {
            SpelUtilities.printAbstractSyntaxTree(System.out, e);
        }
        StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
        assertThat(e.isWritable(lContext)).as("Expression is not writeable but should be").isTrue();
        e.setValue(lContext, value);
        Object a = expectedValue;
        Object b = e.getValue(lContext);
        assertThat(a).isEqualTo(b);
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError("Unexpected Exception: " + ex.getMessage(), ex);
    }
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException)

Example 5 with ParseException

use of cn.taketoday.expression.ParseException in project today-infrastructure by TAKETODAY.

the class ExpressionLanguageScenarioTests method testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem.

/**
 * Scenario: using your own java methods and calling them from the expression
 */
@Test
public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException {
    try {
        // Create a parser
        SpelExpressionParser parser = new SpelExpressionParser();
        // Use the standard evaluation context
        StandardEvaluationContext ctx = new StandardEvaluationContext();
        ctx.registerFunction("repeat", ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat", String.class));
        Expression expr = parser.parseRaw("#repeat('hello')");
        Object value = expr.getValue(ctx);
        assertThat(value).isEqualTo("hellohello");
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError(ex.getMessage(), ex);
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException) Test(org.junit.jupiter.api.Test)

Aggregations

EvaluationException (cn.taketoday.expression.EvaluationException)6 Expression (cn.taketoday.expression.Expression)6 ParseException (cn.taketoday.expression.ParseException)6 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 Test (org.junit.jupiter.api.Test)4