use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method methodReference_simpleInstanceMethodOneArgReturnPrimitive1.
@Test
public void methodReference_simpleInstanceMethodOneArgReturnPrimitive1() throws Exception {
Expression expression = parser.parseExpression("indexOf('b')");
int resultI = expression.getValue("abc", Integer.TYPE);
assertCanCompile(expression);
int resultC = expression.getValue("abc", Integer.TYPE);
assertEquals(1, resultI);
assertEquals(1, resultC);
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method ternaryOperator_SPR15192.
@Test
public void ternaryOperator_SPR15192() {
SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
Expression exp;
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("map", Collections.singletonMap("foo", "qux"));
exp = new SpelExpressionParser(configuration).parseExpression("bar(#map['foo'] != null ? #map['foo'] : 'qux')");
assertEquals("QUX", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("QUX", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("3==3?3:'foo'");
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("3!=3?3:'foo'");
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// When the condition is a double slot primitive
exp = new SpelExpressionParser(configuration).parseExpression("3==3?3L:'foo'");
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("3", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("3!=3?3L:'foo'");
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// When the condition is an empty string
exp = new SpelExpressionParser(configuration).parseExpression("''==''?'abc':4L");
assertEquals("abc", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("abc", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// null condition
exp = new SpelExpressionParser(configuration).parseExpression("3==3?null:4L");
assertEquals(null, exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals(null, exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// variable access returning primitive
exp = new SpelExpressionParser(configuration).parseExpression("#x==#x?50:'foo'");
context.setVariable("x", 50);
assertEquals("50", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("50", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
exp = new SpelExpressionParser(configuration).parseExpression("#x!=#x?50:'foo'");
context.setVariable("x", null);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("foo", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
// variable access returning array
exp = new SpelExpressionParser(configuration).parseExpression("#x==#x?'1,2,3':'foo'");
context.setVariable("x", new int[] { 1, 2, 3 });
assertEquals("1,2,3", exp.getValue(context, new Foo(), String.class));
assertCanCompile(exp);
assertEquals("1,2,3", exp.getValue(context, new Foo(), String.class));
assertIsCompiled(exp);
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method variableReference_root.
@Test
public void variableReference_root() throws Exception {
String s = "hello";
Expression expression = parser.parseExpression("#root");
String resultI = expression.getValue(s, String.class);
assertCanCompile(expression);
String resultC = expression.getValue(s, String.class);
assertEquals(s, resultI);
assertEquals(s, resultC);
expression = parser.parseExpression("#root");
int i = (Integer) expression.getValue(42);
assertEquals(42, i);
assertCanCompile(expression);
i = (Integer) expression.getValue(42);
assertEquals(42, i);
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class PerformanceTests method testPerformanceOfMethodAccess.
@Test
public void testPerformanceOfMethodAccess() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
long starttime = 0;
long endtime = 0;
// warmup
for (int i = 0; i < ITERATIONS; i++) {
Expression expr = parser.parseExpression("getPlaceOfBirth().getCity()");
if (expr == null) {
fail("Parser returned null for expression");
}
expr.getValue(eContext);
}
starttime = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Expression expr = parser.parseExpression("getPlaceOfBirth().getCity()");
if (expr == null) {
fail("Parser returned null for expression");
}
expr.getValue(eContext);
}
endtime = System.currentTimeMillis();
long freshParseTime = endtime - starttime;
if (DEBUG) {
System.out.println("MethodExpression: Time for parsing and evaluation x 10000: " + freshParseTime + "ms");
}
Expression expr = parser.parseExpression("getPlaceOfBirth().getCity()");
if (expr == null) {
fail("Parser returned null for expression");
}
starttime = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
expr.getValue(eContext);
}
endtime = System.currentTimeMillis();
long reuseTime = endtime - starttime;
if (DEBUG) {
System.out.println("MethodExpression: Time for just evaluation x 10000: " + reuseTime + "ms");
}
if (reuseTime > freshParseTime) {
System.out.println("Fresh parse every time, ITERATIONS iterations = " + freshParseTime + "ms");
System.out.println("Reuse SpelExpression, ITERATIONS iterations = " + reuseTime + "ms");
fail("Should have been quicker to reuse!");
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class PropertyAccessTests method testAddingSpecificPropertyAccessor.
@Test
public // Adding a new property accessor just for a particular type
void testAddingSpecificPropertyAccessor() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
// Even though this property accessor is added after the reflection one, it specifically
// names the String class as the type it is interested in so is chosen in preference to
// any 'default' ones
ctx.addPropertyAccessor(new StringyPropertyAccessor());
Expression expr = parser.parseRaw("new String('hello').flibbles");
Integer i = expr.getValue(ctx, Integer.class);
assertEquals((int) i, 7);
// The reflection one will be used for other properties...
expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
Object o = expr.getValue(ctx);
assertNotNull(o);
expr = parser.parseRaw("new String('hello').flibbles");
expr.setValue(ctx, 99);
i = expr.getValue(ctx, Integer.class);
assertEquals((int) i, 99);
// Cannot set it to a string value
try {
expr.setValue(ctx, "not allowed");
fail("Should not have been allowed");
} catch (EvaluationException ex) {
// success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
// System.out.println(e.getMessage());
}
}
Aggregations