use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ComparatorTests method customComparatorWorksWithEquality.
@Test
public void customComparatorWorksWithEquality() {
final StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setTypeComparator(customComparator);
ExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("'1' == 1");
assertThat(expr.getValue(ctx, Boolean.class)).isTrue();
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class CachedMethodExecutorTests method testCachedExecutionForTarget.
@Test
public void testCachedExecutionForTarget() {
Expression expression = this.parser.parseExpression("#var.echo(42)");
assertMethodExecution(expression, new RootObject(), "int: 42");
assertMethodExecution(expression, new RootObject(), "int: 42");
assertMethodExecution(expression, new BaseObject(), "String: 42");
assertMethodExecution(expression, new RootObject(), "int: 42");
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ConstructorInvocationTests method testConstructorThrowingException_SPR6760.
@Test
public void testConstructorThrowingException_SPR6760() {
// Test ctor on inventor:
// On 1 it will throw an IllegalArgumentException
// On 2 it will throw a RuntimeException
// On 3 it will exit normally
// In each case it increments the Tester field 'counter' when invoked
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("new cn.taketoday.expression.spel.ConstructorInvocationTests$Tester(#bar).i");
// Normal exit
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
eContext.setRootObject(new Tester());
eContext.setVariable("bar", 3);
Object o = expr.getValue(eContext);
assertThat(o).isEqualTo(3);
assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(1);
// Now the expression has cached that throwException(int) is the right thing to
// call. Let's change 'bar' to be a PlaceOfBirth which indicates the cached
// reference is out of date.
eContext.setVariable("bar", new PlaceOfBirth("London"));
o = expr.getValue(eContext);
assertThat(o).isEqualTo(0);
// That confirms the logic to mark the cached reference stale and retry is working
// Now let's cause the method to exit via exception and ensure it doesn't cause
// a retry.
// First, switch back to throwException(int)
eContext.setVariable("bar", 3);
o = expr.getValue(eContext);
assertThat(o).isEqualTo(3);
assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(2);
// 4 will make it throw a checked exception - this will be wrapped by spel on the
// way out
eContext.setVariable("bar", 4);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext)).withMessageContaining("Tester");
// A problem occurred whilst attempting to construct an object of type
// 'cn.taketoday.expression.spel.ConstructorInvocationTests$Tester'
// using arguments '(java.lang.Integer)'
// If counter is 4 then the method got called twice!
assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(3);
// 1 will make it throw a RuntimeException - SpEL will let this through
eContext.setVariable("bar", 1);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext)).isNotInstanceOf(SpelEvaluationException.class);
// A problem occurred whilst attempting to construct an object of type
// 'cn.taketoday.expression.spel.ConstructorInvocationTests$Tester'
// using arguments '(java.lang.Integer)'
// If counter is 5 then the method got called twice!
assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(4);
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ExpressionLanguageScenarioTests method testScenario_AddingYourOwnPropertyResolvers_1.
/**
* Scenario: add a property resolver that will get called in the resolver chain, this one only supports reading.
*/
@Test
public void testScenario_AddingYourOwnPropertyResolvers_1() throws Exception {
// Create a parser
SpelExpressionParser parser = new SpelExpressionParser();
// Use the standard evaluation context
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new FruitColourAccessor());
Expression expr = parser.parseRaw("orange");
Object value = expr.getValue(ctx);
assertThat(value).isEqualTo(Color.orange);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expr.setValue(ctx, Color.blue)).satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL));
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ExpressionLanguageScenarioTests method testScenario_DefiningVariablesThatWillBeAccessibleInExpressions.
/**
* Scenario: using the standard context but adding your own variables
*/
@Test
public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception {
// Create a parser
SpelExpressionParser parser = new SpelExpressionParser();
// Use the standard evaluation context
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("favouriteColour", "blue");
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17);
ctx.setVariable("primes", primes);
Expression expr = parser.parseRaw("#favouriteColour");
Object value = expr.getValue(ctx);
assertThat(value).isEqualTo("blue");
expr = parser.parseRaw("#primes.get(1)");
value = expr.getValue(ctx);
assertThat(value).isEqualTo(3);
// all prime numbers > 10 from the list (using selection ?{...})
expr = parser.parseRaw("#primes.?[#this>10]");
value = expr.getValue(ctx);
assertThat(value.toString()).isEqualTo("[11, 13, 17]");
}
Aggregations