use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR10417.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void SPR10417() {
List list1 = new ArrayList();
list1.add("a");
list1.add("b");
list1.add("x");
List list2 = new ArrayList();
list2.add("c");
list2.add("x");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("list1", list1);
context.setVariable("list2", list2);
// #this should be the element from list1
Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
Object result = ex.getValue(context);
assertEquals("[x]", result.toString());
// toString() should be called on the element from list1
ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
result = ex.getValue(context);
assertEquals("[x]", result.toString());
List list3 = new ArrayList();
list3.add(1);
list3.add(2);
list3.add(3);
list3.add(4);
context = new StandardEvaluationContext();
context.setVariable("list3", list3);
ex = parser.parseExpression("#list3.?[#this > 2]");
result = ex.getValue(context);
assertEquals("[3, 4]", result.toString());
ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
result = ex.getValue(context);
assertEquals("[1, 2, 3, 4]", result.toString());
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelParserTests method valueType.
@Test
public void valueType() {
SpelExpressionParser parser = new SpelExpressionParser();
EvaluationContext ctx = new StandardEvaluationContext();
Class<?> c = parser.parseRaw("2").getValueType();
assertEquals(Integer.class, c);
c = parser.parseRaw("12").getValueType(ctx);
assertEquals(Integer.class, c);
c = parser.parseRaw("null").getValueType();
assertNull(c);
c = parser.parseRaw("null").getValueType(ctx);
assertNull(c);
Object o = parser.parseRaw("null").getValue(ctx, Integer.class);
assertNull(o);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class ReflectionHelperTests method testOptimalReflectivePropertyResolver.
@Test
public void testOptimalReflectivePropertyResolver() throws Exception {
ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
Tester t = new Tester();
t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t);
// assertTrue(rpr.canRead(ctx, t, "property"));
// assertEquals("hello",rpr.read(ctx, t, "property").getValue());
// assertEquals("hello",rpr.read(ctx, t, "property").getValue()); // cached accessor used
PropertyAccessor optA = rpr.createOptimalAccessor(ctx, t, "property");
assertTrue(optA.canRead(ctx, t, "property"));
assertFalse(optA.canRead(ctx, t, "property2"));
try {
optA.canWrite(ctx, t, "property");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.canWrite(ctx, t, "property2");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
assertEquals("hello", optA.read(ctx, t, "property").getValue());
// cached accessor used
assertEquals("hello", optA.read(ctx, t, "property").getValue());
try {
optA.getSpecificTargetClasses();
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.write(ctx, t, "property", null);
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
optA = rpr.createOptimalAccessor(ctx, t, "field");
assertTrue(optA.canRead(ctx, t, "field"));
assertFalse(optA.canRead(ctx, t, "field2"));
try {
optA.canWrite(ctx, t, "field");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.canWrite(ctx, t, "field2");
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
assertEquals(3, optA.read(ctx, t, "field").getValue());
// cached accessor used
assertEquals(3, optA.read(ctx, t, "field").getValue());
try {
optA.getSpecificTargetClasses();
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
try {
optA.write(ctx, t, "field", null);
fail();
} catch (UnsupportedOperationException uoe) {
// success
}
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class ReflectionHelperTests method testReflectivePropertyResolver.
@Test
public void testReflectivePropertyResolver() throws Exception {
ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
Tester t = new Tester();
t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t);
assertTrue(rpr.canRead(ctx, t, "property"));
assertEquals("hello", rpr.read(ctx, t, "property").getValue());
// cached accessor used
assertEquals("hello", rpr.read(ctx, t, "property").getValue());
assertTrue(rpr.canRead(ctx, t, "field"));
assertEquals(3, rpr.read(ctx, t, "field").getValue());
// cached accessor used
assertEquals(3, rpr.read(ctx, t, "field").getValue());
assertTrue(rpr.canWrite(ctx, t, "property"));
rpr.write(ctx, t, "property", "goodbye");
// cached accessor used
rpr.write(ctx, t, "property", "goodbye");
assertTrue(rpr.canWrite(ctx, t, "field"));
rpr.write(ctx, t, "field", 12);
rpr.write(ctx, t, "field", 12);
// Attempted write as first activity on this field and property to drive testing
// of populating type descriptor cache
rpr.write(ctx, t, "field2", 3);
rpr.write(ctx, t, "property2", "doodoo");
assertEquals(3, rpr.read(ctx, t, "field2").getValue());
// Attempted read as first activity on this field and property (no canRead before them)
assertEquals(0, rpr.read(ctx, t, "field3").getValue());
assertEquals("doodoo", rpr.read(ctx, t, "property3").getValue());
// Access through is method
// assertEquals(0,rpr.read(ctx,t,"field3").getValue());
assertEquals(false, rpr.read(ctx, t, "property4").getValue());
assertTrue(rpr.canRead(ctx, t, "property4"));
// repro SPR-9123, ReflectivePropertyAccessor JavaBean property names compliance tests
assertEquals("iD", rpr.read(ctx, t, "iD").getValue());
assertTrue(rpr.canRead(ctx, t, "iD"));
assertEquals("id", rpr.read(ctx, t, "id").getValue());
assertTrue(rpr.canRead(ctx, t, "id"));
assertEquals("ID", rpr.read(ctx, t, "ID").getValue());
assertTrue(rpr.canRead(ctx, t, "ID"));
// note: "Id" is not a valid JavaBean name, nevertheless it is treated as "id"
assertEquals("id", rpr.read(ctx, t, "Id").getValue());
assertTrue(rpr.canRead(ctx, t, "Id"));
// repro SPR-10994
assertEquals("xyZ", rpr.read(ctx, t, "xyZ").getValue());
assertTrue(rpr.canRead(ctx, t, "xyZ"));
assertEquals("xY", rpr.read(ctx, t, "xY").getValue());
assertTrue(rpr.canRead(ctx, t, "xY"));
// SPR-10122, ReflectivePropertyAccessor JavaBean property names compliance tests - setters
rpr.write(ctx, t, "pEBS", "Test String");
assertEquals("Test String", rpr.read(ctx, t, "pEBS").getValue());
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR9495.
@Test
public void SPR9495() throws Exception {
SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);
StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");
context.setVariable("enumType", ABC.class);
Object result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(ABC.A, Array.get(result, 0));
assertEquals(ABC.B, Array.get(result, 1));
assertEquals(ABC.C, Array.get(result, 2));
context.addMethodResolver(new MethodResolver() {
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
return new MethodExecutor() {
@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
try {
Method method = XYZ.class.getMethod("values");
Object value = method.invoke(target, arguments);
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
} catch (Exception ex) {
throw new AccessException(ex.getMessage(), ex);
}
}
};
}
});
result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(XYZ.X, Array.get(result, 0));
assertEquals(XYZ.Y, Array.get(result, 1));
assertEquals(XYZ.Z, Array.get(result, 2));
}
Aggregations