@Override public String getValue(Object rootObject) throws EvaluationException { StringBuilder sb = new StringBuilder(); for (Expression expression : this.expressions) { String value = expression.getValue(rootObject, String.class); if (value != null) { sb.append(value); } } return sb.toString(); }
@Test public void indexIntoPropertyContainingListOfList() { List<List<Integer>> property = new ArrayList<>(); property.add(Arrays.asList(3)); this.parameterizedListOfList = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("parameterizedListOfList[0]"); assertEquals("java.util.Arrays$ArrayList<java.lang.Integer>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property.get(0), expression.getValue(this)); expression = parser.parseExpression("parameterizedListOfList[0][0]"); assertEquals(3, expression.getValue(this)); }
@Test public void testOtherOperators() throws Exception { // evaluates to false boolean falseValue = parser.parseExpression("'xyz' instanceof T(int)").getValue(Boolean.class); assertFalse(falseValue); // evaluates to true boolean trueValue = parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class); assertTrue(trueValue); //evaluates to false falseValue = parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class); assertFalse(falseValue); }
@SuppressWarnings("unchecked") @Test public void resolveCollectionElementType() { listNotGeneric = new ArrayList(2); listNotGeneric.add(5); listNotGeneric.add(6); SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("listNotGeneric"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals("5,6", expression.getValue(this, String.class)); }
@Test public void ternaryWithBooleanReturn_SPR12271() { expression = parser.parseExpression("T(Boolean).TRUE?'abc':'def'"); assertEquals("abc", expression.getValue()); assertCanCompile(expression); assertEquals("abc", expression.getValue()); expression = parser.parseExpression("T(Boolean).FALSE?'abc':'def'"); assertEquals("def", expression.getValue()); assertCanCompile(expression); assertEquals("def", expression.getValue()); }
@Test public void indexIntoPropertyContainingList() { List<Integer> property = new ArrayList<>(); property.add(3); this.parameterizedList = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("parameterizedList"); assertEquals("java.util.ArrayList<java.lang.Integer>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("parameterizedList[0]"); assertEquals(3, expression.getValue(this)); }
@Test public void testResolvingString() { Class<?> stringClass = parser.parseExpression("T(String)").getValue(Class.class); assertEquals(String.class, stringClass); }
@Test public void emptyList() { listOfScalarNotGeneric = new ArrayList(); SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("listOfScalarNotGeneric"); assertEquals("java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals("", expression.getValue(this, String.class)); }
@Test public void methodReference_simpleInstanceMethodOneArgReturnPrimitive2() throws Exception { expression = parser.parseExpression("charAt(2)"); char resultI = expression.getValue("abc", Character.TYPE); assertEquals('c', resultI); assertCanCompile(expression); char resultC = expression.getValue("abc", Character.TYPE); assertEquals('c', resultC); }
@Test public void testAccessingPropertyOfClass() { Expression expression = parser.parseExpression("name"); Object value = expression.getValue(new StandardEvaluationContext(String.class)); assertEquals("java.lang.String", value); }
@Test(expected = SpelEvaluationException.class) public void noGetClassAccess() { EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); parser.parseExpression("'a'.class.name").getValue(context); }
@Test @SuppressWarnings("unchecked") public void SPR11494() { Expression exp = new SpelExpressionParser().parseExpression("T(java.util.Arrays).asList('a','b')"); List<String> list = (List<String>) exp.getValue(); assertThat(list.size(), is(2)); }
@Test public void methodReference_literalArguments_int() throws Exception { Expression expression = parser.parseExpression("'abcd'.substring(1,3)"); String resultI = expression.getValue(new TestClass1(), String.class); assertCanCompile(expression); String resultC = expression.getValue(new TestClass1(), String.class); assertEquals("bc", resultI); assertEquals("bc", resultC); }
@Test public void SPR9486_subtractFloatWithDouble() { 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); }
@Test public void SPR9486_floatLessThanOrEqualFloat() { Boolean expectedNumber = -10.21f <= -10.22f; ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Expression expression = parser.parseExpression("-10.21f <= -10.22f"); Boolean result = expression.getValue(context, null, Boolean.class); assertEquals(expectedNumber, result); }
@Test public void SPR10210() { StandardEvaluationContext context = new StandardEvaluationContext(); context.setVariable("bridgeExample", new org.springframework.expression.spel.spr10210.D()); Expression parseExpression = parser.parseExpression("#bridgeExample.bridgeMethod()"); parseExpression.getValue(context); }
@Test public void nullLiteral() throws Exception { expression = parser.parseExpression("null"); Object resultI = expression.getValue(new TestClass1(), Object.class); assertCanCompile(expression); Object resultC = expression.getValue(new TestClass1(), Object.class); assertEquals(null, resultI); assertEquals(null, resultC); assertEquals(null, resultC); }
@Test @SuppressWarnings("rawtypes") public void SPR12522() { SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("T(java.util.Arrays).asList('')"); Object value = expression.getValue(); assertTrue(value instanceof List); assertTrue(((List) value).isEmpty()); }
private void checkTemplateParsing(String expression, ParserContext context, String expectedValue) { SpelExpressionParser parser = new SpelExpressionParser(); Expression expr = parser.parseExpression(expression, context); assertEquals(expectedValue, expr.getValue(TestScenarioCreator.getTestEvaluationContext())); }
@Test public void SPR11609() { StandardEvaluationContext sec = new StandardEvaluationContext(); sec.addPropertyAccessor(new MapAccessor()); Expression exp = new SpelExpressionParser().parseExpression( "T(org.springframework.expression.spel.SpelReproTests$MapWithConstant).X"); assertEquals(1, exp.getValue(sec)); }