use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR5847.
@Test
public void SPR5847() throws Exception {
StandardEvaluationContext eContext = new StandardEvaluationContext(new TestProperties());
String name = null;
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("jdbcProperties['username']");
name = expr.getValue(eContext, String.class);
assertEquals("Dave", name);
expr = new SpelExpressionParser().parseRaw("jdbcProperties[username]");
name = expr.getValue(eContext, String.class);
assertEquals("Dave", name);
// MapAccessor required for this to work
expr = new SpelExpressionParser().parseRaw("jdbcProperties.username");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext, String.class);
assertEquals("Dave", name);
// --- dotted property names
// lookup foo on the root, then bar on that, then use that as the key into
// jdbcProperties
expr = new SpelExpressionParser().parseRaw("jdbcProperties[foo.bar]");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext, String.class);
assertEquals("Dave2", name);
// key is foo.bar
expr = new SpelExpressionParser().parseRaw("jdbcProperties['foo.bar']");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext, String.class);
assertEquals("Elephant", name);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext 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));
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR9486_floatNotEqDouble.
@Test
public void SPR9486_floatNotEqDouble() {
Boolean expectedResult = 10.215f != 10.2109;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = parser.parseExpression("10.215f != 10.2109");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals(expectedResult, result);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR9486_floatLessThanDouble.
@Test
public void SPR9486_floatLessThanDouble() {
Boolean expectedNumber = -10.21f < -10.2;
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = parser.parseExpression("-10.21f < -10.2");
Boolean result = expression.getValue(context, null, Boolean.class);
assertEquals(expectedNumber, result);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method array.
@Test
public void array() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = null;
Object result = null;
expression = parser.parseExpression("new java.lang.Long[0].class");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [Ljava.lang.Long;", result.toString());
expression = parser.parseExpression("T(java.lang.Long[])");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [Ljava.lang.Long;", result.toString());
expression = parser.parseExpression("T(java.lang.String[][][])");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [[[Ljava.lang.String;", result.toString());
assertEquals("T(java.lang.String[][][])", ((SpelExpression) expression).toStringAST());
expression = parser.parseExpression("new int[0].class");
result = expression.getValue(context, "");
assertEquals("Equal assertion failed: ", "class [I", result.toString());
expression = parser.parseExpression("T(int[][])");
result = expression.getValue(context, "");
assertEquals("class [[I", result.toString());
}
Aggregations