use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectFirstItemInList.
@Test
public void selectFirstItemInList() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof Integer);
assertEquals(0, value);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectionWithPrimitiveArray.
@Test
public void selectionWithPrimitiveArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("ints.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context);
assertTrue(value.getClass().isArray());
TypedValue typedValue = new TypedValue(value);
assertEquals(Integer.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
Integer[] array = (Integer[]) value;
assertEquals(5, array.length);
assertEquals(new Integer(0), array[0]);
assertEquals(new Integer(1), array[1]);
assertEquals(new Integer(2), array[2]);
assertEquals(new Integer(3), array[3]);
assertEquals(new Integer(4), array[4]);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectFirstItemInSet.
@Test
public void selectFirstItemInSet() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof Integer);
assertEquals(0, value);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectFirstItemInMap.
@Test
@SuppressWarnings("unchecked")
public 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);
assertEquals(1, colorsMap.size());
assertEquals("beige", colorsMap.keySet().iterator().next());
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class DefaultSubscriptionRegistry method filterSubscriptions.
private MultiValueMap<String, String> filterSubscriptions(MultiValueMap<String, String> allMatches, Message<?> message) {
if (!this.selectorHeaderInUse) {
return allMatches;
}
EvaluationContext context = null;
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(allMatches.size());
for (String sessionId : allMatches.keySet()) {
for (String subId : allMatches.get(sessionId)) {
SessionSubscriptionInfo info = this.subscriptionRegistry.getSubscriptions(sessionId);
if (info == null) {
continue;
}
Subscription sub = info.getSubscription(subId);
if (sub == null) {
continue;
}
Expression expression = sub.getSelectorExpression();
if (expression == null) {
result.add(sessionId, subId);
continue;
}
if (context == null) {
context = new StandardEvaluationContext(message);
context.getPropertyAccessors().add(new SimpMessageHeaderPropertyAccessor());
}
try {
if (expression.getValue(context, boolean.class)) {
result.add(sessionId, subId);
}
} catch (SpelEvaluationException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to evaluate selector: " + ex.getMessage());
}
} catch (Throwable ex) {
logger.debug("Failed to evaluate selector", ex);
}
}
}
return result;
}
Aggregations