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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations