use of cn.taketoday.expression.ExpressionParser in project today-framework by TAKETODAY.
the class MapAccessTests method testVariableMapAccess.
@Test
public void testVariableMapAccess() throws Exception {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
ctx.setVariable("day", "saturday");
Expression expr = parser.parseExpression("testMap[#day]");
Object value = expr.getValue(ctx, String.class);
assertThat(value).isEqualTo("samstag");
}
use of cn.taketoday.expression.ExpressionParser in project today-framework by TAKETODAY.
the class SelectionAndProjectionTests method selectFirstItemInMap.
@Test
@SuppressWarnings("unchecked")
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);
assertThat(colorsMap.size()).isEqualTo(1);
assertThat(colorsMap.keySet().iterator().next()).isEqualTo("beige");
}
use of cn.taketoday.expression.ExpressionParser in project today-infrastructure by TAKETODAY.
the class SpelDocumentationTests method literals.
// Section 7.5
@Test
void literals() throws Exception {
ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
assertThat(helloWorld).isEqualTo("Hello World");
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
assertThat(avogadrosNumber).isCloseTo(6.0221415E+23, within((double) 0));
// evals to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
assertThat(maxValue).isEqualTo(Integer.MAX_VALUE);
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
assertThat(trueValue).isTrue();
Object nullValue = parser.parseExpression("null").getValue();
assertThat(nullValue).isNull();
}
use of cn.taketoday.expression.ExpressionParser in project today-infrastructure by TAKETODAY.
the class SpelDocumentationTests method specialVariables.
@Test
@SuppressWarnings("unchecked")
void specialVariables() throws Exception {
// create an array of integers
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17);
// create parser and set variable 'primes' as the array of integers
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("primes", primes);
// all prime numbers > 10 from the list (using selection ?{...})
List<Integer> primesGreaterThanTen = (List<Integer>) parser.parseExpression("#primes.?[#this>10]").getValue(context);
assertThat(primesGreaterThanTen.toString()).isEqualTo("[11, 13, 17]");
}
use of cn.taketoday.expression.ExpressionParser in project today-infrastructure by TAKETODAY.
the class SpelReproTests method testCompiledExpressionForProxy_SPR16191.
@Test
void testCompiledExpressionForProxy_SPR16191() {
SpelExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null));
Expression expression = expressionParser.parseExpression("#target.process(#root)");
VarargsReceiver receiver = new VarargsReceiver();
VarargsInterface proxy = (VarargsInterface) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { VarargsInterface.class }, (proxy1, method, args) -> method.invoke(receiver, args));
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
evaluationContext.setVariable("target", proxy);
String result = expression.getValue(evaluationContext, "foo", String.class);
result = expression.getValue(evaluationContext, "foo", String.class);
assertThat(result).isEqualTo("OK");
}
Aggregations