@Test public void testRootObject() throws Exception { GregorianCalendar c = new GregorianCalendar(); 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); assertEquals("Nikola Tesla",name); }
@Test public void testAssignment() throws Exception { Inventor inventor = new Inventor(); StandardEvaluationContext inventorContext = new StandardEvaluationContext(); inventorContext.setRootObject(inventor); parser.parseExpression("foo").setValue(inventorContext, "Alexander Seovic2"); assertEquals("Alexander Seovic2",parser.parseExpression("foo").getValue(inventorContext,String.class)); // alternatively String aleks = parser.parseExpression("foo = 'Alexandar Seovic'").getValue(inventorContext, String.class); assertEquals("Alexandar Seovic",parser.parseExpression("foo").getValue(inventorContext,String.class)); assertEquals("Alexandar Seovic",aleks); }
@Test public void testConstructors() throws Exception { StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); Inventor einstein = parser.parseExpression("new org.springframework.expression.spel.testresources.Inventor('Albert Einstein',new java.util.Date(), 'German')").getValue(Inventor.class); assertEquals("Albert Einstein", einstein.getName()); //create new inventor instance within add method of List parser.parseExpression("Members2.add(new org.springframework.expression.spel.testresources.Inventor('Albert Einstein', 'German'))").getValue(societyContext); }
@Test public void testMethodInvocation2() throws Exception { // string literal, evaluates to "bc" String c = parser.parseExpression("'abc'.substring(1, 3)").getValue(String.class); assertEquals("bc",c); StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); // evaluates to true boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(societyContext, Boolean.class); assertTrue(isMember); }
@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); }
@SuppressWarnings("unchecked") @Test public void testSelection() throws Exception { StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); List<Inventor> list = (List<Inventor>) parser.parseExpression("Members2.?[nationality == 'Serbian']").getValue(societyContext); assertEquals(1,list.size()); assertEquals("Nikola Tesla",list.get(0).getName()); }
/** * Create the root context object, an Inventor instance. Non-qualified property * and method references will be resolved against this context object. * @param testContext the evaluation context in which to set the root object */ private static void setupRootContextObject(StandardEvaluationContext testContext) { GregorianCalendar c = new GregorianCalendar(); c.set(1856, 7, 9); Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian"); tesla.setPlaceOfBirth(new PlaceOfBirth("SmilJan")); tesla.setInventions(new String[] { "Telephone repeater", "Rotating magnetic field principle", "Polyphase alternating-current system", "Induction motor", "Alternating-current power transmission", "Tesla coil transformer", "Wireless communication", "Radio", "Fluorescent lights" }); testContext.setRootObject(tesla); }
@Test public void testTernary() throws Exception { String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class); assertEquals("falseExp",falseString); StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); parser.parseExpression("Name").setValue(societyContext, "IEEE"); societyContext.setVariable("queryName", "Nikola Tesla"); String expression = "isMember(#queryName)? #queryName + ' is a member of the ' " + "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'"; String queryResultString = parser.parseExpression(expression).getValue(societyContext, String.class); assertEquals("Nikola Tesla is a member of the IEEE Society",queryResultString); // queryResultString = "Nikola Tesla is a member of the IEEE Society" }
@Test public void testDictionaryAccess() throws Exception { StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); // Officer's Dictionary Inventor pupin = parser.parseExpression("officers['president']").getValue(societyContext, Inventor.class); assertNotNull(pupin); // evaluates to "Idvor" String city = parser.parseExpression("officers['president'].PlaceOfBirth.city").getValue(societyContext, String.class); assertNotNull(city); // setting values Inventor i = parser.parseExpression("officers['advisors'][0]").getValue(societyContext,Inventor.class); assertEquals("Nikola Tesla",i.getName()); parser.parseExpression("officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext, "Croatia"); Inventor i2 = parser.parseExpression("reverse[0]['advisors'][0]").getValue(societyContext,Inventor.class); assertEquals("Nikola Tesla",i2.getName()); }
@Test public void testLogicalOperators() throws Exception { StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); // -- AND -- // evaluates to false boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class); assertFalse(falseValue); // evaluates to true String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')"; boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class); // -- OR -- // evaluates to true trueValue = parser.parseExpression("true or false").getValue(Boolean.class); assertTrue(trueValue); // evaluates to true expression = "isMember('Nikola Tesla') or isMember('Albert Einstien')"; trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class); assertTrue(trueValue); // -- NOT -- // evaluates to false falseValue = parser.parseExpression("!true").getValue(Boolean.class); assertFalse(falseValue); // -- AND and NOT -- expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')"; falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class); assertFalse(falseValue); }
@Test public void testVariables() throws Exception { Inventor tesla = new Inventor("Nikola Tesla", "Serbian"); StandardEvaluationContext context = new StandardEvaluationContext(); context.setVariable("newName", "Mike Tesla"); context.setRootObject(tesla); parser.parseExpression("foo = #newName").getValue(context); assertEquals("Mike Tesla",tesla.getFoo()); }
@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); }
@Before public void createContext() { user = mock(Authentication.class); root = new MethodSecurityExpressionRoot(user); ctx = new StandardEvaluationContext(); ctx.setRootObject(root); trustResolver = mock(AuthenticationTrustResolver.class); root.setTrustResolver(trustResolver); }
@Test public void testScenario03_Arithmetic() throws Exception { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); // Might be better with a as a variable although it would work as a property too... // Variable references using a '#' Expression expr = parser.parseRaw("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); Boolean value = null; ctx.setVariable("a",1.0d); // referenced as #a in the expression ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it value = expr.getValue(ctx,Boolean.class); assertTrue(value); ctx.setRootObject(new Manager("Luke")); ctx.setVariable("a",1.043d); value = expr.getValue(ctx,Boolean.class); assertFalse(value); }
@Test public void indexIntoGenericPropertyContainingMapObject() { Map<String, Map<String, String>> property = new HashMap<>(); Map<String, String> map = new HashMap<>(); map.put("foo", "bar"); property.put("property", map); SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new MapAccessor()); context.setRootObject(property); Expression expression = parser.parseExpression("property"); assertEquals("java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(context).toString()); assertEquals(map, expression.getValue(context)); assertEquals(map, expression.getValue(context, Map.class)); expression = parser.parseExpression("property['foo']"); assertEquals("bar", expression.getValue(context)); }
@Test public void testScenario01_Roles() throws Exception { try { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); Expression expr = parser.parseRaw("hasAnyRole('MANAGER','TELLER')"); ctx.setRootObject(new Person("Ben")); Boolean value = expr.getValue(ctx,Boolean.class); assertFalse(value); ctx.setRootObject(new Manager("Luke")); value = expr.getValue(ctx,Boolean.class); assertTrue(value); } catch (EvaluationException ee) { ee.printStackTrace(); fail("Unexpected SpelException: " + ee.getMessage()); } }
@Test public void testScenario02_ComparingNames() throws Exception { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.addPropertyAccessor(new SecurityPrincipalAccessor()); // Multiple options for supporting this expression: "p.name == principal.name" // (1) If the right person is the root context object then "name==principal.name" is good enough Expression expr = parser.parseRaw("name == principal.name"); ctx.setRootObject(new Person("Andy")); Boolean value = expr.getValue(ctx,Boolean.class); assertTrue(value); ctx.setRootObject(new Person("Christian")); value = expr.getValue(ctx,Boolean.class); assertFalse(value); // (2) Or register an accessor that can understand 'p' and return the right person expr = parser.parseRaw("p.name == principal.name"); PersonAccessor pAccessor = new PersonAccessor(); ctx.addPropertyAccessor(pAccessor); ctx.setRootObject(null); pAccessor.setPerson(new Person("Andy")); value = expr.getValue(ctx,Boolean.class); assertTrue(value); pAccessor.setPerson(new Person("Christian")); value = expr.getValue(ctx,Boolean.class); assertFalse(value); }
@Test public void testRootContextObject() { ExpressionState state = getState(); assertEquals(Inventor.class, state.getRootContextObject().getValue().getClass()); // although the root object is being set on the evaluation context, the value in the 'state' remains what it was when constructed ((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null); assertEquals(Inventor.class, state.getRootContextObject().getValue().getClass()); // assertEquals(null, state.getRootContextObject().getValue()); state = new ExpressionState(new StandardEvaluationContext()); assertEquals(TypedValue.NULL, state.getRootContextObject()); ((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null); assertEquals(null, state.getRootContextObject().getValue()); }
@Test public void testScenario04_ControllingWhichMethodsRun() throws Exception { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it); ctx.addMethodResolver(new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM // Might be better with a as a variable although it would work as a property too... // Variable references using a '#' // SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); Expression expr = parser.parseRaw("(hasRole(3) or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); Boolean value = null; ctx.setVariable("a",1.0d); // referenced as #a in the expression value = expr.getValue(ctx,Boolean.class); assertTrue(value); // ctx.setRootObject(new Manager("Luke")); // ctx.setVariable("a",1.043d); // value = (Boolean)expr.getValue(ctx,Boolean.class); // assertFalse(value); }
SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); context.setRootObject(new TestObject()); LocalFilter filter = new LocalFilter(); context.registerMethodFilter(TestObject.class,filter); context.setRootObject(new String("abc")); expr = (SpelExpression) parser.parseExpression("charAt(0)"); result = expr.getValue(context, String.class); context.setRootObject(new TestObject()); expr = (SpelExpression) parser.parseExpression("doit(1)"); result = expr.getValue(context, String.class);