use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class PropertyAccessTests method propertyAccessWithArrayIndexOutOfBounds.
@Test
void propertyAccessWithArrayIndexOutOfBounds() {
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
Expression expression = parser.parseExpression("stringArrayOfThreeItems[3]");
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expression.getValue(context, new Inventor())).extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS);
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class PropertyAccessTests method propertyReadOnlyWithRecordStyle.
@Test
void propertyReadOnlyWithRecordStyle() {
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
Expression expr = parser.parseExpression("name");
RecordPerson target1 = new RecordPerson("p1");
assertThat(expr.getValue(context, target1)).isEqualTo("p1");
RecordPerson target2 = new RecordPerson("p2");
assertThat(expr.getValue(context, target2)).isEqualTo("p2");
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target2));
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class PropertyAccessTests method shouldAlwaysUsePropertyAccessorFromEvaluationContext.
@Test
void shouldAlwaysUsePropertyAccessorFromEvaluationContext() {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("name");
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Ollie")));
assertThat(expression.getValue(context)).isEqualTo("Ollie");
context = new StandardEvaluationContext();
context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Jens")));
assertThat(expression.getValue(context)).isEqualTo("Jens");
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class SelectionAndProjectionTests method selectLastItemInArray.
@Test
void selectLastItemInArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context);
assertThat(value).isInstanceOf(Integer.class);
assertThat(value).isEqualTo(4);
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class SelectionAndProjectionTests method selectFirstItemInMap.
@Test
@SuppressWarnings("unchecked")
void selectFirstItemInMap() {
EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("colors.^[key.startsWith('b')]");
Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
assertThat(colorsMap.size()).isEqualTo(1);
assertThat(colorsMap.keySet().iterator().next()).isEqualTo("beige");
}
Aggregations