Java Code Examples for org.springframework.expression.spel.standard.SpelExpressionParser#parseRaw()
The following examples show how to use
org.springframework.expression.spel.standard.SpelExpressionParser#parseRaw() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScenariosForSpringSecurity.java From java-technology-stack with MIT License | 6 votes |
@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()); } }
Example 2
Source File: ScenariosForSpringSecurity.java From spring-analysis-note with MIT License | 6 votes |
@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); }
Example 3
Source File: ScenariosForSpringSecurity.java From spring-analysis-note with MIT License | 6 votes |
@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); }
Example 4
Source File: ExpressionLanguageScenarioTests.java From java-technology-stack with MIT License | 6 votes |
/** * Scenario: using the standard context but adding your own variables */ @Test public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.setVariable("favouriteColour","blue"); List<Integer> primes = new ArrayList<>(); primes.addAll(Arrays.asList(2,3,5,7,11,13,17)); ctx.setVariable("primes",primes); Expression expr = parser.parseRaw("#favouriteColour"); Object value = expr.getValue(ctx); assertEquals("blue", value); expr = parser.parseRaw("#primes.get(1)"); value = expr.getValue(ctx); assertEquals(3, value); // all prime numbers > 10 from the list (using selection ?{...}) expr = parser.parseRaw("#primes.?[#this>10]"); value = expr.getValue(ctx); assertEquals("[11, 13, 17]", value.toString()); }
Example 5
Source File: ExpressionLanguageScenarioTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testScenario_AddingYourOwnPropertyResolvers_2() throws Exception { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.addPropertyAccessor(new VegetableColourAccessor()); Expression expr = parser.parseRaw("pea"); Object value = expr.getValue(ctx); assertEquals(Color.green, value); try { expr.setValue(ctx, Color.blue); fail("Should not be allowed to set peas to be blue !"); } catch (SpelEvaluationException ee) { assertEquals(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, ee.getMessageCode()); } }
Example 6
Source File: ExpressionLanguageScenarioTests.java From java-technology-stack with MIT License | 6 votes |
/** * Scenario: using the standard infrastructure and running simple expression evaluation. */ @Test public void testScenario_UsingStandardInfrastructure() { try { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Parse an expression Expression expr = parser.parseRaw("new String('hello world')"); // Evaluate it using a 'standard' context Object value = expr.getValue(); // They are reusable value = expr.getValue(); assertEquals("hello world", value); assertEquals(String.class, value.getClass()); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
Example 7
Source File: ExpressionLanguageScenarioTests.java From java-technology-stack with MIT License | 6 votes |
/** * Scenario: using your own java methods and calling them from the expression */ @Test public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException { try { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class)); Expression expr = parser.parseRaw("#repeat('hello')"); Object value = expr.getValue(ctx); assertEquals("hellohello", value); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
Example 8
Source File: ExpressionLanguageScenarioTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Scenario: using your own java methods and calling them from the expression */ @Test public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException { try { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class)); Expression expr = parser.parseRaw("#repeat('hello')"); Object value = expr.getValue(ctx); assertEquals("hellohello", value); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
Example 9
Source File: ScenariosForSpringSecurity.java From java-technology-stack with MIT License | 6 votes |
@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); }
Example 10
Source File: ExpressionLanguageScenarioTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Scenario: using the standard context but adding your own variables */ @Test public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.setVariable("favouriteColour","blue"); List<Integer> primes = new ArrayList<>(); primes.addAll(Arrays.asList(2,3,5,7,11,13,17)); ctx.setVariable("primes",primes); Expression expr = parser.parseRaw("#favouriteColour"); Object value = expr.getValue(ctx); assertEquals("blue", value); expr = parser.parseRaw("#primes.get(1)"); value = expr.getValue(ctx); assertEquals(3, value); // all prime numbers > 10 from the list (using selection ?{...}) expr = parser.parseRaw("#primes.?[#this>10]"); value = expr.getValue(ctx); assertEquals("[11, 13, 17]", value.toString()); }
Example 11
Source File: ScenariosForSpringSecurity.java From java-technology-stack with MIT License | 6 votes |
@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); }
Example 12
Source File: SpelReproTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void reservedWordProperties_SPR9862() { StandardEvaluationContext context = new StandardEvaluationContext(); SpelExpressionParser parser = new SpelExpressionParser(); SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST"); Object value = expression.getValue(context); assertEquals(value, Reserver.CONST); }
Example 13
Source File: ScenariosForSpringSecurity.java From java-technology-stack with MIT License | 5 votes |
@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); }
Example 14
Source File: PropertyAccessTests.java From java-technology-stack with MIT License | 5 votes |
@Test // Adding a new property accessor just for a particular type public void testAddingSpecificPropertyAccessor() { 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(7, (int) i); // 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(99, (int) i); // 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()); } }
Example 15
Source File: PropertyAccessTests.java From spring-analysis-note with MIT License | 5 votes |
@Test // Adding a new property accessor just for a particular type public void testAddingSpecificPropertyAccessor() { 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(7, (int) i); // 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(99, (int) i); // 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()); } }
Example 16
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void greaterThanWithNulls_SPR7840() { List<D> list = new ArrayList<>(); list.add(new D("aaa")); list.add(new D("bbb")); list.add(new D(null)); list.add(new D("ccc")); list.add(new D(null)); list.add(new D("zzz")); StandardEvaluationContext context = new StandardEvaluationContext(list); SpelExpressionParser parser = new SpelExpressionParser(); String el1 = "#root.?[a < 'hhh']"; SpelExpression exp = parser.parseRaw(el1); Object value = exp.getValue(context); assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]", value.toString()); String el2 = "#root.?[a > 'hhh']"; SpelExpression exp2 = parser.parseRaw(el2); Object value2 = exp2.getValue(context); assertEquals("[D(zzz)]", value2.toString()); // trim out the nulls first String el3 = "#root.?[a!=null].?[a < 'hhh']"; SpelExpression exp3 = parser.parseRaw(el3); Object value3 = exp3.getValue(context); assertEquals("[D(aaa), D(bbb), D(ccc)]", value3.toString()); }
Example 17
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void projectionTypeDescriptors_3() { StandardEvaluationContext context = new StandardEvaluationContext(new C()); SpelExpressionParser parser = new SpelExpressionParser(); String el1 = "ms.![key.equals('abc')]"; SpelExpression exp = parser.parseRaw(el1); List<?> value = (List<?>) exp.getValue(context); // value is list containing [true,false] assertEquals(Boolean.class, value.get(0).getClass()); TypeDescriptor evaluated = exp.getValueTypeDescriptor(context); assertEquals(null, evaluated.getElementTypeDescriptor()); }
Example 18
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void projectionTypeDescriptors_1() { StandardEvaluationContext context = new StandardEvaluationContext(new C()); SpelExpressionParser parser = new SpelExpressionParser(); String el1 = "ls.![#this.equals('abc')]"; SpelExpression exp = parser.parseRaw(el1); List<?> value = (List<?>) exp.getValue(context); // value is list containing [true,false] assertEquals(Boolean.class, value.get(0).getClass()); TypeDescriptor evaluated = exp.getValueTypeDescriptor(context); assertEquals(null, evaluated.getElementTypeDescriptor()); }
Example 19
Source File: ScenariosForSpringSecurity.java From spring-analysis-note with MIT License | 5 votes |
@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); }
Example 20
Source File: SpelReproTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void projectionTypeDescriptors_1() { StandardEvaluationContext context = new StandardEvaluationContext(new C()); SpelExpressionParser parser = new SpelExpressionParser(); String el1 = "ls.![#this.equals('abc')]"; SpelExpression exp = parser.parseRaw(el1); List<?> value = (List<?>) exp.getValue(context); // value is list containing [true,false] assertEquals(Boolean.class, value.get(0).getClass()); TypeDescriptor evaluated = exp.getValueTypeDescriptor(context); assertEquals(null, evaluated.getElementTypeDescriptor()); }