Search in sources :

Example 96 with ExpressionParser

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");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 97 with ExpressionParser

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");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 98 with ExpressionParser

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();
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 99 with ExpressionParser

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]");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 100 with ExpressionParser

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");
}
Also used : ParserContext(cn.taketoday.expression.ParserContext) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) Proxy(java.lang.reflect.Proxy) ListIterator(java.util.ListIterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MethodParameter(cn.taketoday.core.MethodParameter) MethodExecutor(cn.taketoday.expression.MethodExecutor) HashMap(java.util.HashMap) PropertyAccessor(cn.taketoday.expression.PropertyAccessor) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) EvaluationException(cn.taketoday.expression.EvaluationException) Expression(cn.taketoday.expression.Expression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) Map(java.util.Map) TypeDescriptor(cn.taketoday.core.TypeDescriptor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) Method(java.lang.reflect.Method) EvaluationContext(cn.taketoday.expression.EvaluationContext) LinkedHashSet(java.util.LinkedHashSet) ExpressionException(cn.taketoday.expression.ExpressionException) BeanResolver(cn.taketoday.expression.BeanResolver) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Properties(java.util.Properties) Iterator(java.util.Iterator) StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Collection(java.util.Collection) ReflectivePropertyAccessor(cn.taketoday.expression.spel.support.ReflectivePropertyAccessor) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ExpressionParser(cn.taketoday.expression.ExpressionParser) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Reserver(cn.taketoday.expression.spel.testresources.le.div.mod.reserved.Reserver) List(java.util.List) ObjectUtils(cn.taketoday.util.ObjectUtils) Nullable(cn.taketoday.lang.Nullable) TypedValue(cn.taketoday.expression.TypedValue) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) AccessException(cn.taketoday.expression.AccessException) Collections(java.util.Collections) MethodResolver(cn.taketoday.expression.MethodResolver) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Aggregations

ExpressionParser (cn.taketoday.expression.ExpressionParser)132 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)132 Test (org.junit.jupiter.api.Test)132 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)112 Expression (cn.taketoday.expression.Expression)110 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)72 EvaluationContext (cn.taketoday.expression.EvaluationContext)16 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)10 Map (java.util.Map)10 EvaluationException (cn.taketoday.expression.EvaluationException)6 MethodResolver (cn.taketoday.expression.MethodResolver)6 ReflectiveMethodResolver (cn.taketoday.expression.spel.support.ReflectiveMethodResolver)6 Method (java.lang.reflect.Method)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 TreeMap (java.util.TreeMap)6 MethodParameter (cn.taketoday.core.MethodParameter)4 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4 AccessException (cn.taketoday.expression.AccessException)4