Package org.springframework.expression

Examples of org.springframework.expression.ExpressionParser


  @Test
  public void decrement04() {
    Integer i = 42;
    StandardEvaluationContext ctx = new StandardEvaluationContext(i);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    try {
      Expression e =  parser.parseExpression("--1");
      e.getValue(ctx,Integer.class);
      fail();
    } catch (SpelEvaluationException see) {
      assertEquals(SpelMessage.NOT_ASSIGNABLE,see.getMessageCode());
    }
    try {
      Expression e =  parser.parseExpression("1--");
      e.getValue(ctx,Integer.class);
      fail();
    } catch (SpelEvaluationException see) {
      assertEquals(SpelMessage.NOT_ASSIGNABLE,see.getMessageCode());
    }
View Full Code Here


  @Test
  public void incdecTogether() {
    Spr9751 helper = new Spr9751();
    StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = null;

    // index1 is 2 at the start - the 'intArray[#root.index1++]' should not be evaluated twice!
    // intArray[2] is 3
    e = parser.parseExpression("intArray[#root.index1++]++");
    e.getValue(ctx,Integer.class);
    assertEquals(3,helper.index1);
    assertEquals(4,helper.intArray[2]);

    // index1 is 3 intArray[3] is 4
    e =  parser.parseExpression("intArray[#root.index1++]--");
    assertEquals(4,e.getValue(ctx,Integer.class).intValue());
    assertEquals(4,helper.index1);
    assertEquals(3,helper.intArray[3]);

    // index1 is 4, intArray[3] is 3
    e =  parser.parseExpression("intArray[--#root.index1]++");
    assertEquals(3,e.getValue(ctx,Integer.class).intValue());
    assertEquals(3,helper.index1);
    assertEquals(4,helper.intArray[3]);
  }
View Full Code Here

  // Verify how all the nodes behave with assignment (++, --, =)
  @Test
  public void incrementAllNodeTypes() throws SecurityException, NoSuchMethodException {
    Spr9751 helper = new Spr9751();
    StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression e = null;

    // BooleanLiteral
    expectFailNotAssignable(parser, ctx, "true++");
    expectFailNotAssignable(parser, ctx, "--false");
    expectFailSetValueNotSupported(parser, ctx, "true=false");

    // IntLiteral
    expectFailNotAssignable(parser, ctx, "12++");
    expectFailNotAssignable(parser, ctx, "--1222");
    expectFailSetValueNotSupported(parser, ctx, "12=16");

    // LongLiteral
    expectFailNotAssignable(parser, ctx, "1.0d++");
    expectFailNotAssignable(parser, ctx, "--3.4d");
    expectFailSetValueNotSupported(parser, ctx, "1.0d=3.2d");

    // NullLiteral
    expectFailNotAssignable(parser, ctx, "null++");
    expectFailNotAssignable(parser, ctx, "--null");
    expectFailSetValueNotSupported(parser, ctx, "null=null");
    expectFailSetValueNotSupported(parser, ctx, "null=123");

    // OpAnd
    expectFailNotAssignable(parser, ctx, "(true && false)++");
    expectFailNotAssignable(parser, ctx, "--(false AND true)");
    expectFailSetValueNotSupported(parser, ctx, "(true && false)=(false && true)");

    // OpDivide
    expectFailNotAssignable(parser, ctx, "(3/4)++");
    expectFailNotAssignable(parser, ctx, "--(2/5)");
    expectFailSetValueNotSupported(parser, ctx, "(1/2)=(3/4)");

    // OpEq
    expectFailNotAssignable(parser, ctx, "(3==4)++");
    expectFailNotAssignable(parser, ctx, "--(2==5)");
    expectFailSetValueNotSupported(parser, ctx, "(1==2)=(3==4)");

    // OpGE
    expectFailNotAssignable(parser, ctx, "(3>=4)++");
    expectFailNotAssignable(parser, ctx, "--(2>=5)");
    expectFailSetValueNotSupported(parser, ctx, "(1>=2)=(3>=4)");

    // OpGT
    expectFailNotAssignable(parser, ctx, "(3>4)++");
    expectFailNotAssignable(parser, ctx, "--(2>5)");
    expectFailSetValueNotSupported(parser, ctx, "(1>2)=(3>4)");

    // OpLE
    expectFailNotAssignable(parser, ctx, "(3<=4)++");
    expectFailNotAssignable(parser, ctx, "--(2<=5)");
    expectFailSetValueNotSupported(parser, ctx, "(1<=2)=(3<=4)");

    // OpLT
    expectFailNotAssignable(parser, ctx, "(3<4)++");
    expectFailNotAssignable(parser, ctx, "--(2<5)");
    expectFailSetValueNotSupported(parser, ctx, "(1<2)=(3<4)");

    // OpMinus
    expectFailNotAssignable(parser, ctx, "(3-4)++");
    expectFailNotAssignable(parser, ctx, "--(2-5)");
    expectFailSetValueNotSupported(parser, ctx, "(1-2)=(3-4)");

    // OpModulus
    expectFailNotAssignable(parser, ctx, "(3%4)++");
    expectFailNotAssignable(parser, ctx, "--(2%5)");
    expectFailSetValueNotSupported(parser, ctx, "(1%2)=(3%4)");

    // OpMultiply
    expectFailNotAssignable(parser, ctx, "(3*4)++");
    expectFailNotAssignable(parser, ctx, "--(2*5)");
    expectFailSetValueNotSupported(parser, ctx, "(1*2)=(3*4)");

    // OpNE
    expectFailNotAssignable(parser, ctx, "(3!=4)++");
    expectFailNotAssignable(parser, ctx, "--(2!=5)");
    expectFailSetValueNotSupported(parser, ctx, "(1!=2)=(3!=4)");

    // OpOr
    expectFailNotAssignable(parser, ctx, "(true || false)++");
    expectFailNotAssignable(parser, ctx, "--(false OR true)");
    expectFailSetValueNotSupported(parser, ctx, "(true || false)=(false OR true)");

    // OpPlus
    expectFailNotAssignable(parser, ctx, "(3+4)++");
    expectFailNotAssignable(parser, ctx, "--(2+5)");
    expectFailSetValueNotSupported(parser, ctx, "(1+2)=(3+4)");

    // RealLiteral
    expectFailNotAssignable(parser, ctx, "1.0d++");
    expectFailNotAssignable(parser, ctx, "--2.0d");
    expectFailSetValueNotSupported(parser, ctx, "(1.0d)=(3.0d)");
    expectFailNotAssignable(parser, ctx, "1.0f++");
    expectFailNotAssignable(parser, ctx, "--2.0f");
    expectFailSetValueNotSupported(parser, ctx, "(1.0f)=(3.0f)");

    // StringLiteral
    expectFailNotAssignable(parser, ctx, "'abc'++");
    expectFailNotAssignable(parser, ctx, "--'def'");
    expectFailSetValueNotSupported(parser, ctx, "'abc'='def'");

    // Ternary
    expectFailNotAssignable(parser, ctx, "(true?true:false)++");
    expectFailNotAssignable(parser, ctx, "--(true?true:false)");
    expectFailSetValueNotSupported(parser, ctx, "(true?true:false)=(true?true:false)");

    // TypeReference
    expectFailNotAssignable(parser, ctx, "T(String)++");
    expectFailNotAssignable(parser, ctx, "--T(Integer)");
    expectFailSetValueNotSupported(parser, ctx, "T(String)=T(Integer)");

    // OperatorBetween
    expectFailNotAssignable(parser, ctx, "(3 between {1,5})++");
    expectFailNotAssignable(parser, ctx, "--(3 between {1,5})");
    expectFailSetValueNotSupported(parser, ctx, "(3 between {1,5})=(3 between {1,5})");

    // OperatorInstanceOf
    expectFailNotAssignable(parser, ctx, "(type instanceof T(String))++");
    expectFailNotAssignable(parser, ctx, "--(type instanceof T(String))");
    expectFailSetValueNotSupported(parser, ctx, "(type instanceof T(String))=(type instanceof T(String))");

    // Elvis
    expectFailNotAssignable(parser, ctx, "(true?:false)++");
    expectFailNotAssignable(parser, ctx, "--(true?:false)");
    expectFailSetValueNotSupported(parser, ctx, "(true?:false)=(true?:false)");

    // OpInc
    expectFailNotAssignable(parser, ctx, "(iii++)++");
    expectFailNotAssignable(parser, ctx, "--(++iii)");
    expectFailSetValueNotSupported(parser, ctx, "(iii++)=(++iii)");

    // OpDec
    expectFailNotAssignable(parser, ctx, "(iii--)++");
    expectFailNotAssignable(parser, ctx, "--(--iii)");
    expectFailSetValueNotSupported(parser, ctx, "(iii--)=(--iii)");

    // OperatorNot
    expectFailNotAssignable(parser, ctx, "(!true)++");
    expectFailNotAssignable(parser, ctx, "--(!false)");
    expectFailSetValueNotSupported(parser, ctx, "(!true)=(!false)");

    // OperatorPower
    expectFailNotAssignable(parser, ctx, "(iii^2)++");
    expectFailNotAssignable(parser, ctx, "--(iii^2)");
    expectFailSetValueNotSupported(parser, ctx, "(iii^2)=(iii^3)");

    // Assign
    // iii=42
    e = parser.parseExpression("iii=iii++");
    assertEquals(42,helper.iii);
    int return_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(42,helper.iii);
    assertEquals(42,return_iii);

    // Identifier
    e = parser.parseExpression("iii++");
    assertEquals(42,helper.iii);
    return_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(42,return_iii);
    assertEquals(43,helper.iii);

    e = parser.parseExpression("--iii");
    assertEquals(43,helper.iii);
    return_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(42,return_iii);
    assertEquals(42,helper.iii);

    e = parser.parseExpression("iii=99");
    assertEquals(42,helper.iii);
    return_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(99,return_iii);
    assertEquals(99,helper.iii);

    // CompoundExpression
    // foo.iii == 99
    e = parser.parseExpression("foo.iii++");
    assertEquals(99,helper.foo.iii);
    int return_foo_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(99,return_foo_iii);
    assertEquals(100,helper.foo.iii);

    e = parser.parseExpression("--foo.iii");
    assertEquals(100,helper.foo.iii);
    return_foo_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(99,return_foo_iii);
    assertEquals(99,helper.foo.iii);

    e = parser.parseExpression("foo.iii=999");
    assertEquals(99,helper.foo.iii);
    return_foo_iii = e.getValue(ctx,Integer.TYPE);
    assertEquals(999,return_foo_iii);
    assertEquals(999,helper.foo.iii);

    // ConstructorReference
    expectFailNotAssignable(parser, ctx, "(new String('abc'))++");
    expectFailNotAssignable(parser, ctx, "--(new String('abc'))");
    expectFailSetValueNotSupported(parser, ctx, "(new String('abc'))=(new String('abc'))");

    // MethodReference
    expectFailNotIncrementable(parser, ctx, "m()++");
    expectFailNotDecrementable(parser, ctx, "--m()");
    expectFailSetValueNotSupported(parser, ctx, "m()=m()");

    // OperatorMatches
    expectFailNotAssignable(parser, ctx, "('abc' matches '^a..')++");
    expectFailNotAssignable(parser, ctx, "--('abc' matches '^a..')");
    expectFailSetValueNotSupported(parser, ctx, "('abc' matches '^a..')=('abc' matches '^a..')");

    // Selection
    ctx.registerFunction("isEven", Spr9751.class.getDeclaredMethod("isEven", Integer.TYPE));

    expectFailNotIncrementable(parser, ctx, "({1,2,3}.?[#isEven(#this)])++");
    expectFailNotDecrementable(parser, ctx, "--({1,2,3}.?[#isEven(#this)])");
    expectFailNotAssignable(parser, ctx, "({1,2,3}.?[#isEven(#this)])=({1,2,3}.?[#isEven(#this)])");

    // slightly diff here because return value isn't a list, it is a single entity
    expectFailNotAssignable(parser, ctx, "({1,2,3}.^[#isEven(#this)])++");
    expectFailNotAssignable(parser, ctx, "--({1,2,3}.^[#isEven(#this)])");
    expectFailNotAssignable(parser, ctx, "({1,2,3}.^[#isEven(#this)])=({1,2,3}.^[#isEven(#this)])");

    expectFailNotAssignable(parser, ctx, "({1,2,3}.$[#isEven(#this)])++");
    expectFailNotAssignable(parser, ctx, "--({1,2,3}.$[#isEven(#this)])");
    expectFailNotAssignable(parser, ctx, "({1,2,3}.$[#isEven(#this)])=({1,2,3}.$[#isEven(#this)])");

    // FunctionReference
    expectFailNotAssignable(parser, ctx, "#isEven(3)++");
    expectFailNotAssignable(parser, ctx, "--#isEven(4)");
    expectFailSetValueNotSupported(parser, ctx, "#isEven(3)=#isEven(5)");

    // VariableReference
    ctx.setVariable("wibble", "hello world");
    expectFailNotIncrementable(parser, ctx, "#wibble++");
    expectFailNotDecrementable(parser, ctx, "--#wibble");
    e = parser.parseExpression("#wibble=#wibble+#wibble");
    String s = e.getValue(ctx,String.class);
    assertEquals("hello worldhello world",s);
    assertEquals("hello worldhello world",ctx.lookupVariable("wibble"));

    ctx.setVariable("wobble", 3);
    e = parser.parseExpression("#wobble++");
    assertEquals(3,((Integer)ctx.lookupVariable("wobble")).intValue());
    int r = e.getValue(ctx,Integer.TYPE);
    assertEquals(3,r);
    assertEquals(4,((Integer)ctx.lookupVariable("wobble")).intValue());

    e = parser.parseExpression("--#wobble");
    assertEquals(4,((Integer)ctx.lookupVariable("wobble")).intValue());
    r = e.getValue(ctx,Integer.TYPE);
    assertEquals(3,r);
    assertEquals(3,((Integer)ctx.lookupVariable("wobble")).intValue());

    e = parser.parseExpression("#wobble=34");
    assertEquals(3,((Integer)ctx.lookupVariable("wobble")).intValue());
    r = e.getValue(ctx,Integer.TYPE);
    assertEquals(34,r);
    assertEquals(34,((Integer)ctx.lookupVariable("wobble")).intValue());

    // Projection
    expectFailNotIncrementable(parser, ctx, "({1,2,3}.![#isEven(#this)])++"); // projection would be {false,true,false}
    expectFailNotDecrementable(parser, ctx, "--({1,2,3}.![#isEven(#this)])"); // projection would be {false,true,false}
    expectFailNotAssignable(parser, ctx, "({1,2,3}.![#isEven(#this)])=({1,2,3}.![#isEven(#this)])");

    // InlineList
    expectFailNotAssignable(parser, ctx, "({1,2,3})++");
    expectFailNotAssignable(parser, ctx, "--({1,2,3})");
    expectFailSetValueNotSupported(parser, ctx, "({1,2,3})=({1,2,3})");

    // InlineMap
    expectFailNotAssignable(parser, ctx, "({'a':1,'b':2,'c':3})++");
    expectFailNotAssignable(parser, ctx, "--({'a':1,'b':2,'c':3})");
    expectFailSetValueNotSupported(parser, ctx, "({'a':1,'b':2,'c':3})=({'a':1,'b':2,'c':3})");

    // BeanReference
    ctx.setBeanResolver(new MyBeanResolver());
    expectFailNotAssignable(parser, ctx, "@foo++");
    expectFailNotAssignable(parser, ctx, "--@foo");
    expectFailSetValueNotSupported(parser, ctx, "@foo=@bar");

    // PropertyOrFieldReference
    helper.iii = 42;
    e = parser.parseExpression("iii++");
    assertEquals(42,helper.iii);
    r = e.getValue(ctx,Integer.TYPE);
    assertEquals(42,r);
    assertEquals(43,helper.iii);

    e = parser.parseExpression("--iii");
    assertEquals(43,helper.iii);
    r = e.getValue(ctx,Integer.TYPE);
    assertEquals(42,r);
    assertEquals(42,helper.iii);

    e = parser.parseExpression("iii=100");
    assertEquals(42,helper.iii);
    r = e.getValue(ctx,Integer.TYPE);
    assertEquals(100,r);
    assertEquals(100,helper.iii);

View Full Code Here

*/
public class EvaluationTests extends AbstractExpressionTests {

  @Test
  public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
    ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression expression = parser.parseExpression("list[0]");
    TestClass testClass = new TestClass();
    Object o = null;
    o = expression.getValue(new StandardEvaluationContext(testClass));
    assertEquals("", o);
    o = parser.parseExpression("list[3]").getValue(new StandardEvaluationContext(testClass));
    assertEquals("", o);
    assertEquals(4, testClass.list.size());
    try {
      o = parser.parseExpression("list2[3]").getValue(new StandardEvaluationContext(testClass));
      fail();
    } catch (EvaluationException ee) {
      ee.printStackTrace();
      // success!
    }
    o = parser.parseExpression("foo[3]").getValue(new StandardEvaluationContext(testClass));
    assertEquals("", o);
    assertEquals(4, testClass.getFoo().size());
  }
View Full Code Here

    c.set(1856, 7, 9);

    //  The constructor arguments are name, birthday, and nationaltiy.
    Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");

    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("name");

    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(tesla);

    String name = (String) exp.getValue(context);
View Full Code Here

    assertEquals("Nikola Tesla",name);
  }

  @Test
  public void testEqualityCheck() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();

    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(tesla);

    Expression exp = parser.parseExpression("name == 'Nikola Tesla'");
    boolean isEqual = exp.getValue(context, Boolean.class)// evaluates to true
    assertTrue(isEqual);
  }
View Full Code Here

  }

  // Section 7.5
  @Test
  public void testLiterals() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();

    String helloWorld = (String) parser.parseExpression("'Hello World'").getValue(); // evals to "Hello World"
    assertEquals("Hello World",helloWorld);

    double avogadrosNumber  = (Double) parser.parseExpression("6.0221415E+23").getValue();
    assertEquals(6.0221415E+23, avogadrosNumber, 0);

    int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue()// evals to 2147483647
    assertEquals(Integer.MAX_VALUE,maxValue);

    boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
    assertTrue(trueValue);

    Object nullValue = parser.parseExpression("null").getValue();
    assertNull(nullValue);
  }
View Full Code Here

    assertEquals("SmilJan",city);
  }

  @Test
  public void testPropertyNavigation() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();

    // Inventions Array
    StandardEvaluationContext teslaContext = TestScenarioCreator.getTestEvaluationContext();
//    teslaContext.setRootObject(tesla);

    // evaluates to "Induction motor"
    String invention = parser.parseExpression("inventions[3]").getValue(teslaContext, String.class);
    assertEquals("Induction motor",invention);

    // Members List
    StandardEvaluationContext societyContext = new StandardEvaluationContext();
    IEEE ieee = new IEEE();
    ieee.Members[0]= tesla;
    societyContext.setRootObject(ieee);

    // evaluates to "Nikola Tesla"
    String name = parser.parseExpression("Members[0].Name").getValue(societyContext, String.class);
    assertEquals("Nikola Tesla",name);

    // List and Array navigation
    // evaluates to "Wireless communication"
    invention = parser.parseExpression("Members[0].Inventions[6]").getValue(societyContext, String.class);
    assertEquals("Wireless communication",invention);
  }
View Full Code Here

    // create an array of integers
    List<Integer> primes = new ArrayList<Integer>();
    primes.addAll(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);
    assertEquals("[11, 13, 17]",primesGreaterThanTen.toString());
  }
View Full Code Here

  // 7.5.9

  @Test
  public void testFunctions() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();

    context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod(
        "reverseString", new Class[] { String.class }));

    String helloWorldReversed = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class);
    assertEquals("dlrow olleh",helloWorldReversed);
  }
View Full Code Here

TOP

Related Classes of org.springframework.expression.ExpressionParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.