org.springframework.expression.spel.standard.SpelExpressionParser Java Examples
The following examples show how to use
org.springframework.expression.spel.standard.SpelExpressionParser.
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: SpelExceptionTests.java From java-technology-stack with MIT License | 6 votes |
@Test @SuppressWarnings("serial") public void spelExpressionMapWithVariables() { ExpressionParser parser = new SpelExpressionParser(); Expression spelExpression = parser.parseExpression("#aMap['one'] eq 1"); StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.setVariables(new HashMap<String, Object>() { { put("aMap", new HashMap<String, Integer>() { { put("one", 1); put("two", 2); put("three", 3); } }); } }); boolean result = spelExpression.getValue(ctx, Boolean.class); assertTrue(result); }
Example #2
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void SPR9735() { Item item = new Item(); item.setName("parent"); Item item1 = new Item(); item1.setName("child1"); Item item2 = new Item(); item2.setName("child2"); item.add(item1); item.add(item2); ExpressionParser parser = new SpelExpressionParser(); EvaluationContext context = new StandardEvaluationContext(); Expression exp = parser.parseExpression("#item[0].name"); context.setVariable("item", item); assertEquals("child1", exp.getValue(context)); }
Example #3
Source File: MethodInvocationTests.java From java-technology-stack with MIT License | 6 votes |
/** * Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped. */ @Test public void testMethodThrowingException_SPR6941() { // Test method on inventor: throwException() // On 1 it will throw an IllegalArgumentException // On 2 it will throw a RuntimeException // On 3 it will exit normally // In each case it increments the Inventor field 'counter' when invoked SpelExpressionParser parser = new SpelExpressionParser(); Expression expr = parser.parseExpression("throwException(#bar)"); context.setVariable("bar", 2); try { expr.getValue(context); fail(); } catch (Exception ex) { if (ex instanceof SpelEvaluationException) { fail("Should not be a SpelEvaluationException: " + ex); } // normal } }
Example #4
Source File: SpelDocumentationTests.java From spring-analysis-note with MIT License | 6 votes |
@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); }
Example #5
Source File: TemplateExpressionParsingTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testParsingSimpleTemplateExpression04() throws Exception { SpelExpressionParser parser = new SpelExpressionParser(); Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT); Object o = expr.getValue(); assertEquals("hello world", o.toString()); expr = parser.parseExpression("", DEFAULT_TEMPLATE_PARSER_CONTEXT); o = expr.getValue(); assertEquals("", o.toString()); expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT); o = expr.getValue(); assertEquals("abc", o.toString()); expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT); o = expr.getValue((Object)null); assertEquals("abc", o.toString()); }
Example #6
Source File: MapAccessTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testGetValuePerformance() throws Exception { Assume.group(TestGroup.PERFORMANCE); Map<String, String> map = new HashMap<>(); map.put("key", "value"); EvaluationContext context = new StandardEvaluationContext(map); ExpressionParser spelExpressionParser = new SpelExpressionParser(); Expression expr = spelExpressionParser.parseExpression("#root['key']"); StopWatch s = new StopWatch(); s.start(); for (int i = 0; i < 10000; i++) { expr.getValue(context); } s.stop(); assertThat(s.getTotalTimeMillis(), lessThan(200L)); }
Example #7
Source File: IndexingTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void indexIntoGenericPropertyContainingGrowingList2() { List<String> property2 = new ArrayList<>(); this.property2 = property2; SpelParserConfiguration configuration = new SpelParserConfiguration(true, true); SpelExpressionParser parser = new SpelExpressionParser(configuration); Expression expression = parser.parseExpression("property2"); assertEquals("java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property2, expression.getValue(this)); expression = parser.parseExpression("property2[0]"); try { assertEquals("bar", expression.getValue(this)); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1053E")); } }
Example #8
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void SPR10452() { SpelParserConfiguration configuration = new SpelParserConfiguration(false, false); ExpressionParser parser = new SpelExpressionParser(configuration); StandardEvaluationContext context = new StandardEvaluationContext(); Expression spel = parser.parseExpression("#enumType.values()"); context.setVariable("enumType", ABC.class); Object result = spel.getValue(context); assertNotNull(result); assertTrue(result.getClass().isArray()); assertEquals(ABC.A, Array.get(result, 0)); assertEquals(ABC.B, Array.get(result, 1)); assertEquals(ABC.C, Array.get(result, 2)); context.setVariable("enumType", XYZ.class); result = spel.getValue(context); assertNotNull(result); assertTrue(result.getClass().isArray()); assertEquals(XYZ.X, Array.get(result, 0)); assertEquals(XYZ.Y, Array.get(result, 1)); assertEquals(XYZ.Z, Array.get(result, 2)); }
Example #9
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 #10
Source File: IndexingTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void setGenericPropertyContainingListAutogrow() { List<Integer> property = new ArrayList<>(); this.property = property; SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property[0]"); try { expression.setValue(this, "4"); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1053E")); } }
Example #11
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
/** * We add property accessors in the order: * First, Second, Third, Fourth. * They are not utilized in this order; preventing a priority or order of operations * in evaluation of SPEL expressions for a given context. */ @Test public void propertyAccessorOrder_SPR8211() { ExpressionParser expressionParser = new SpelExpressionParser(); StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new ContextObject()); evaluationContext.addPropertyAccessor(new TestPropertyAccessor("firstContext")); evaluationContext.addPropertyAccessor(new TestPropertyAccessor("secondContext")); evaluationContext.addPropertyAccessor(new TestPropertyAccessor("thirdContext")); evaluationContext.addPropertyAccessor(new TestPropertyAccessor("fourthContext")); assertEquals("first", expressionParser.parseExpression("shouldBeFirst").getValue(evaluationContext)); assertEquals("second", expressionParser.parseExpression("shouldBeSecond").getValue(evaluationContext)); assertEquals("third", expressionParser.parseExpression("shouldBeThird").getValue(evaluationContext)); assertEquals("fourth", expressionParser.parseExpression("shouldBeFourth").getValue(evaluationContext)); }
Example #12
Source File: EvaluationTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void incdecTogether() { Spr9751 helper = new Spr9751(); StandardEvaluationContext ctx = new StandardEvaluationContext(helper); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression e; // 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]); }
Example #13
Source File: TemplateExpressionParsingTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testParsingSimpleTemplateExpression04() throws Exception { SpelExpressionParser parser = new SpelExpressionParser(); Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT); Object o = expr.getValue(); assertEquals("hello world", o.toString()); expr = parser.parseExpression("", DEFAULT_TEMPLATE_PARSER_CONTEXT); o = expr.getValue(); assertEquals("", o.toString()); expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT); o = expr.getValue(); assertEquals("abc", o.toString()); expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT); o = expr.getValue((Object)null); assertEquals("abc", o.toString()); }
Example #14
Source File: IndexingTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void indexIntoGenericPropertyContainingGrowingList() { List<String> property = new ArrayList<>(); this.property = property; SpelParserConfiguration configuration = new SpelParserConfiguration(true, true); SpelExpressionParser parser = new SpelExpressionParser(configuration); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property[0]"); try { assertEquals("bar", expression.getValue(this)); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1053E")); } }
Example #15
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 #16
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
/** * Test the ability to subclass the ReflectiveMethodResolver and change how it * determines the set of methods for a type. */ @Test public void customStaticFunctions_SPR9038() { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); List<MethodResolver> methodResolvers = new ArrayList<>(); methodResolvers.add(new ReflectiveMethodResolver() { @Override protected Method[] getMethods(Class<?> type) { try { return new Method[] {Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE)}; } catch (NoSuchMethodException ex) { return new Method[0]; } } }); context.setMethodResolvers(methodResolvers); Expression expression = parser.parseExpression("parseInt('-FF', 16)"); Integer result = expression.getValue(context, "", Integer.class); assertEquals(-255, result.intValue()); }
Example #17
Source File: EvaluationTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException { ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression e = parser.parseExpression("list[0]"); TestClass testClass = new TestClass(); Object o = e.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()); }
Example #18
Source File: SelectionAndProjectionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void selectLastItemInList() throws Exception { Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]"); EvaluationContext context = new StandardEvaluationContext(new ListTestBean()); Object value = expression.getValue(context); assertTrue(value instanceof Integer); assertEquals(4, value); }
Example #19
Source File: ExpressionEvaluatorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void unavailableReturnValue() { EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE); try { new SpelExpressionParser().parseExpression("#result").getValue(context); fail("Should have failed to parse expression, result not available"); } catch (VariableNotAvailableException e) { assertEquals("wrong variable name", "result", e.getName()); } }
Example #20
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void SPR9486_floatGreaterThanEqualDouble() { Boolean expectedResult = -10.21f >= -10.2; ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Expression expression = parser.parseExpression("-10.21f >= -10.2"); Boolean result = expression.getValue(context, null, Boolean.class); assertEquals(expectedResult, result); }
Example #21
Source File: SelectionAndProjectionTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void selectFirstItemInSet() throws Exception { Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]"); EvaluationContext context = new StandardEvaluationContext(new SetTestBean()); Object value = expression.getValue(context); assertTrue(value instanceof Integer); assertEquals(0, value); }
Example #22
Source File: SpelReproTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void SPR9486_floatLessThanDouble() { Boolean expectedNumber = -10.21f < -10.2; ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Expression expression = parser.parseExpression("-10.21f < -10.2"); Boolean result = expression.getValue(context, null, Boolean.class); assertEquals(expectedNumber, result); }
Example #23
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void SPR9486_floatLessThanOrEqualDouble() { Boolean expectedNumber = -10.21f <= -10.2; ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Expression expression = parser.parseExpression("-10.21f <= -10.2"); Boolean result = expression.getValue(context, null, Boolean.class); assertEquals(expectedNumber, result); }
Example #24
Source File: SpelReproTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void SPR9486_floatLessThanOrEqualDouble() { Boolean expectedNumber = -10.21f <= -10.2; ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Expression expression = parser.parseExpression("-10.21f <= -10.2"); Boolean result = expression.getValue(context, null, Boolean.class); assertEquals(expectedNumber, result); }
Example #25
Source File: SpelDocumentationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testFunctions() throws Exception { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod("reverseString", String.class)); String helloWorldReversed = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class); assertEquals("dlrow olleh",helloWorldReversed); }
Example #26
Source File: IndexingTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void indexIntoGenericPropertyContainingList() { List<String> property = new ArrayList<>(); property.add("bar"); this.property = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property[0]"); assertEquals("bar", expression.getValue(this)); }
Example #27
Source File: IndexingTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void setGenericPropertyContainingMap() { Map<String, String> property = new HashMap<>(); property.put("foo", "bar"); this.property = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property['foo']"); assertEquals("bar", expression.getValue(this)); expression.setValue(this, "baz"); assertEquals("baz", expression.getValue(this)); }
Example #28
Source File: SpelReproTests.java From java-technology-stack with MIT License | 5 votes |
/** * Should be accessing (setting) Goo.wibble field because 'bar' variable evaluates to * "wibble" */ @Test public void indexingAsAPropertyAccess_SPR6968_4() { Goo g = Goo.instance; StandardEvaluationContext context = new StandardEvaluationContext(g); context.setVariable("bar", "wibble"); Expression expr = null; expr = new SpelExpressionParser().parseRaw("instance[#bar]='world'"); // will access the field 'wibble' and not use a getter expr.getValue(context, String.class); assertEquals("world", g.wibble); expr.getValue(context, String.class); // will be using the cached accessor this time assertEquals("world", g.wibble); }
Example #29
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void SPR10486() { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Spr10486 rootObject = new Spr10486(); Expression classNameExpression = parser.parseExpression("class.name"); Expression nameExpression = parser.parseExpression("name"); assertThat(classNameExpression.getValue(context, rootObject), equalTo((Object) Spr10486.class.getName())); assertThat(nameExpression.getValue(context, rootObject), equalTo((Object) "name")); }
Example #30
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void SPR9486_addFloatWithDouble() { Number expectedNumber = 10.21f + 10.2; ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Expression expression = parser.parseExpression("10.21f + 10.2"); Number result = expression.getValue(context, null, Number.class); assertEquals(expectedNumber, result); }