use of org.springframework.context.expression.MapAccessor in project spring-boot by spring-projects.
the class BindingPreparationTests method testExpressionLists.
@Test
@Ignore("Work in progress")
public void testExpressionLists() throws Exception {
TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
// map.put("foo", Arrays.asList("bar"));
target.setNested(map);
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(target);
context.addPropertyAccessor(new MapAccessor());
Expression expression = parser.parseExpression("nested.foo");
assertThat(expression.getValue(context)).isNotNull();
}
use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.
the class IntegrationSimpleEvaluationContextFactoryBean method getObject.
@Override
public SimpleEvaluationContext getObject() throws Exception {
Collection<PropertyAccessor> accessors = getPropertyAccessors().values();
PropertyAccessor[] accessorArray = accessors.toArray(new PropertyAccessor[accessors.size() + 2]);
accessorArray[accessors.size()] = new MapAccessor();
accessorArray[accessors.size() + 1] = DataBindingPropertyAccessor.forReadOnlyAccess();
SimpleEvaluationContext evaluationContext = SimpleEvaluationContext.forPropertyAccessors(accessorArray).withTypeConverter(getTypeConverter()).withInstanceMethods().build();
for (Entry<String, Method> functionEntry : getFunctions().entrySet()) {
evaluationContext.setVariable(functionEntry.getKey(), functionEntry.getValue());
}
return evaluationContext;
}
use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.
the class ObjectToMapTransformerParserTests method testObjectToSpelMapTransformer.
@SuppressWarnings("unchecked")
@Test
public void testObjectToSpelMapTransformer() {
Employee employee = this.buildEmployee();
StandardEvaluationContext context = new StandardEvaluationContext(employee);
context.addPropertyAccessor(new MapAccessor());
ExpressionParser parser = new SpelExpressionParser();
Message<Employee> message = MessageBuilder.withPayload(employee).build();
directInput.send(message);
Message<Map<String, Object>> outputMessage = (Message<Map<String, Object>>) output.receive();
Map<String, Object> transformedMap = outputMessage.getPayload();
assertNotNull(outputMessage.getPayload());
for (String key : transformedMap.keySet()) {
Expression expression = parser.parseExpression(key);
Object valueFromTheMap = transformedMap.get(key);
Object valueFromExpression = expression.getValue(context);
assertEquals(valueFromTheMap, valueFromExpression);
}
}
use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.
the class ObjectToMapTransformerTests method testObjectToSpelMapTransformer.
@SuppressWarnings("unchecked")
@Test
public void testObjectToSpelMapTransformer() throws IOException {
Employee employee = this.buildEmployee();
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new MapAccessor());
ExpressionParser parser = new SpelExpressionParser();
ObjectToMapTransformer transformer = new ObjectToMapTransformer();
Message<Employee> message = MessageBuilder.withPayload(employee).build();
Message<?> transformedMessage = transformer.transform(message);
Map<String, Object> transformedMap = (Map<String, Object>) transformedMessage.getPayload();
assertNotNull(transformedMap);
Object valueFromTheMap = null;
Object valueFromExpression = null;
Expression expression = null;
expression = parser.parseExpression("departments[0]");
valueFromTheMap = transformedMap.get("departments[0]");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.address.coordinates");
valueFromTheMap = transformedMap.get("person.address.coordinates");
valueFromExpression = expression.getValue(context, employee, Map.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.akaNames[0]");
valueFromTheMap = transformedMap.get("person.akaNames[0]");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("testMapInMapData.internalMapA.bar");
valueFromTheMap = transformedMap.get("testMapInMapData.internalMapA.bar");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("companyAddress.street");
valueFromTheMap = transformedMap.get("companyAddress.street");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.lname");
valueFromTheMap = transformedMap.get("person.lname");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.address.mapWithListData.mapWithListTestData[1]");
valueFromTheMap = transformedMap.get("person.address.mapWithListData.mapWithListTestData[1]");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("companyAddress.city");
valueFromTheMap = transformedMap.get("companyAddress.city");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.akaNames[2]");
valueFromTheMap = transformedMap.get("person.akaNames[2]");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.child");
valueFromTheMap = transformedMap.get("person.child");
valueFromExpression = expression.getValue(context, employee, String.class);
assertNull(valueFromTheMap);
assertNull(valueFromExpression);
expression = parser.parseExpression("testMapInMapData.internalMapA.foo");
valueFromTheMap = transformedMap.get("testMapInMapData.internalMapA.foo");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.address.city");
valueFromTheMap = transformedMap.get("person.address.city");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("companyAddress.coordinates.latitude[0]");
valueFromTheMap = transformedMap.get("companyAddress.coordinates.latitude[0]");
valueFromExpression = expression.getValue(context, employee, Integer.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("person.remarks[1].baz");
valueFromTheMap = transformedMap.get("person.remarks[1].baz");
valueFromExpression = expression.getValue(context, employee, String.class);
assertEquals(valueFromTheMap, valueFromExpression);
expression = parser.parseExpression("listOfDates[0][1]");
valueFromTheMap = new Date((Long) transformedMap.get("listOfDates[0][1]"));
valueFromExpression = expression.getValue(context, employee, Date.class);
assertEquals(valueFromTheMap, valueFromExpression);
}
use of org.springframework.context.expression.MapAccessor in project spring-boot-admin by codecentric.
the class TelegramNotifier method getText.
@Nullable
protected String getText(InstanceEvent event, Instance instance) {
Map<String, Object> root = new HashMap<>();
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
return message.getValue(context, String.class);
}
Aggregations