SPR-7335: support for expression inline lists and array construction
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
|
||||
/**
|
||||
* Test construction of arrays.
|
||||
*
|
||||
* @author Andy Clement
|
||||
*/
|
||||
public class ArrayConstructorTests extends ExpressionTestCase {
|
||||
|
||||
@Test
|
||||
public void testSimpleArrayWithInitializer() {
|
||||
evaluateArrayBuildingExpression("new int[]{1,2,3}", "[1,2,3]");
|
||||
evaluateArrayBuildingExpression("new int[]{}", "[]");
|
||||
evaluate("new int[]{}.length", "0", Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversion() {
|
||||
evaluate("new String[]{1,2,3}[0]", "1", String.class);
|
||||
evaluate("new int[]{'123'}[0]", 123, Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultidimensionalArrays() {
|
||||
evaluateAndCheckError("new int[][]{{1,2},{3,4}}", SpelMessage.MULTIDIM_ARRAY_INITIALIZER_NOT_SUPPORTED);
|
||||
evaluateAndCheckError("new int[3][]", SpelMessage.MISSING_ARRAY_DIMENSION);
|
||||
evaluateAndCheckError("new int[]", SpelMessage.MISSING_ARRAY_DIMENSION);
|
||||
evaluateAndCheckError("new String[]", SpelMessage.MISSING_ARRAY_DIMENSION);
|
||||
evaluateAndCheckError("new int[][1]", SpelMessage.MISSING_ARRAY_DIMENSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeArrayConstructors() {
|
||||
evaluateArrayBuildingExpression("new int[]{1,2,3,4}", "[1,2,3,4]");
|
||||
evaluateArrayBuildingExpression("new boolean[]{true,false,true}", "[true,false,true]");
|
||||
evaluateArrayBuildingExpression("new char[]{'a','b','c'}", "[a,b,c]");
|
||||
evaluateArrayBuildingExpression("new long[]{1,2,3,4,5}", "[1,2,3,4,5]");
|
||||
evaluateArrayBuildingExpression("new short[]{2,3,4,5,6}", "[2,3,4,5,6]");
|
||||
evaluateArrayBuildingExpression("new double[]{1d,2d,3d,4d}", "[1.0,2.0,3.0,4.0]");
|
||||
evaluateArrayBuildingExpression("new float[]{1f,2f,3f,4f}", "[1.0,2.0,3.0,4.0]");
|
||||
evaluateArrayBuildingExpression("new byte[]{1,2,3,4}", "[1,2,3,4]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeArrayConstructorsElements() {
|
||||
evaluate("new int[]{1,2,3,4}[0]", 1, Integer.class);
|
||||
evaluate("new boolean[]{true,false,true}[0]", true, Boolean.class);
|
||||
evaluate("new char[]{'a','b','c'}[0]", 'a', Character.class);
|
||||
evaluate("new long[]{1,2,3,4,5}[0]", 1L, Long.class);
|
||||
evaluate("new short[]{2,3,4,5,6}[0]", (short) 2, Short.class);
|
||||
evaluate("new double[]{1d,2d,3d,4d}[0]", (double) 1, Double.class);
|
||||
evaluate("new float[]{1f,2f,3f,4f}[0]", (float) 1, Float.class);
|
||||
evaluate("new byte[]{1,2,3,4}[0]", (byte) 1, Byte.class);
|
||||
evaluate("new String(new char[]{'h','e','l','l','o'})", "hello", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorCases() {
|
||||
evaluateAndCheckError("new char[7]{'a','c','d','e'}", SpelMessage.INITIALIZER_LENGTH_INCORRECT);
|
||||
evaluateAndCheckError("new char[3]{'a','c','d','e'}", SpelMessage.INITIALIZER_LENGTH_INCORRECT);
|
||||
evaluateAndCheckError("new char[2]{'hello','world'}", SpelMessage.TYPE_CONVERSION_ERROR);
|
||||
evaluateAndCheckError("new String('a','c','d')", SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeArrayConstructors() {
|
||||
evaluate("new String[]{'a','b','c','d'}[1]", "b", String.class);
|
||||
evaluateAndCheckError("new String[]{'a','b','c','d'}.size()", SpelMessage.METHOD_NOT_FOUND, 30, "size()",
|
||||
"java.lang.String[]");
|
||||
evaluate("new String[]{'a','b','c','d'}.length", 4, Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicArray() {
|
||||
evaluate("new String[3]", "java.lang.String[3]{null,null,null}", String[].class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiDimensionalArray() {
|
||||
evaluate("new String[2][2]", "[Ljava.lang.String;[2]{[2]{null,null},[2]{null,null}}", String[][].class);
|
||||
evaluate("new String[3][2][1]",
|
||||
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
|
||||
String[][][].class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorInvocation03() {
|
||||
evaluateAndCheckError("new String[]", SpelMessage.MISSING_ARRAY_DIMENSION);
|
||||
}
|
||||
|
||||
public void testConstructorInvocation04() {
|
||||
evaluateAndCheckError("new Integer[3]{'3','ghi','5'}", SpelMessage.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, 4);
|
||||
}
|
||||
|
||||
private String evaluateArrayBuildingExpression(String expression, String expectedToString) {
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
Expression e = parser.parseExpression(expression);
|
||||
Object o = e.getValue();
|
||||
Assert.assertNotNull(o);
|
||||
Assert.assertTrue(o.getClass().isArray());
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append('[');
|
||||
if (o instanceof int[]) {
|
||||
int[] array = (int[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof boolean[]) {
|
||||
boolean[] array = (boolean[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof char[]) {
|
||||
char[] array = (char[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof long[]) {
|
||||
long[] array = (long[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof short[]) {
|
||||
short[] array = (short[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof double[]) {
|
||||
double[] array = (double[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof float[]) {
|
||||
float[] array = (float[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else if (o instanceof byte[]) {
|
||||
byte[] array = (byte[]) o;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(',');
|
||||
}
|
||||
s.append(array[i]);
|
||||
}
|
||||
} else {
|
||||
Assert.fail("Not supported " + o.getClass());
|
||||
}
|
||||
s.append(']');
|
||||
Assert.assertEquals(expectedToString, s.toString());
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -338,14 +338,17 @@ public abstract class ExpressionTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String stringValueOf(Object value) {
|
||||
return stringValueOf(value, false);
|
||||
}
|
||||
/**
|
||||
* Produce a nice string representation of the input object.
|
||||
*
|
||||
* @param value object to be formatted
|
||||
* @return a nice string
|
||||
*/
|
||||
public static String stringValueOf(Object value) {
|
||||
public static String stringValueOf(Object value, boolean isNested) {
|
||||
// do something nice for arrays
|
||||
if (value == null) {
|
||||
return "null";
|
||||
@@ -378,9 +381,27 @@ public abstract class ExpressionTestCase {
|
||||
throw new RuntimeException("Please implement support for type " + primitiveType.getName()
|
||||
+ " in ExpressionTestCase.stringValueOf()");
|
||||
}
|
||||
} else if (value.getClass().getComponentType().isArray()) {
|
||||
List<Object> l = Arrays.asList((Object[]) value);
|
||||
if (!isNested) {
|
||||
sb.append(value.getClass().getComponentType().getName());
|
||||
}
|
||||
sb.append("[").append(l.size()).append("]{");
|
||||
int i = 0;
|
||||
for (Object object : l) {
|
||||
if (i > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
i++;
|
||||
sb.append(stringValueOf(object, true));
|
||||
}
|
||||
sb.append("}");
|
||||
} else {
|
||||
List<Object> l = Arrays.asList((Object[]) value);
|
||||
sb.append(value.getClass().getComponentType().getName()).append("[").append(l.size()).append("]{");
|
||||
if (!isNested) {
|
||||
sb.append(value.getClass().getComponentType().getName());
|
||||
}
|
||||
sb.append("[").append(l.size()).append("]{");
|
||||
int i = 0;
|
||||
for (Object object : l) {
|
||||
if (i > 0) {
|
||||
|
||||
@@ -16,15 +16,17 @@
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
/**
|
||||
* These are tests for language features that are not yet considered 'live'. Either missing implementation or documentation.
|
||||
* These are tests for language features that are not yet considered 'live'. Either missing implementation or
|
||||
* documentation.
|
||||
*
|
||||
* Where implementation is missing the tests are commented out.
|
||||
*
|
||||
@@ -37,71 +39,73 @@ public class InProgressTests extends ExpressionTestCase {
|
||||
evaluate("1 between listOneFive", "true", Boolean.class);
|
||||
// evaluate("1 between {1, 5}", "true", Boolean.class); // no inline list building at the moment
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsBetweenErrors01() {
|
||||
evaluateAndCheckError("1 between T(String)", SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 10);
|
||||
evaluateAndCheckError("1 between T(String)", SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 10);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsBetweenErrors03() {
|
||||
evaluateAndCheckError("1 between listOfNumbersUpToTen", SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 10);
|
||||
evaluateAndCheckError("1 between listOfNumbersUpToTen",
|
||||
SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST, 10);
|
||||
}
|
||||
|
||||
// PROJECTION
|
||||
|
||||
// PROJECTION
|
||||
@Test
|
||||
public void testProjection01() {
|
||||
evaluate("listOfNumbersUpToTen.![#this<5?'y':'n']","[y, y, y, y, n, n, n, n, n, n]",ArrayList.class);
|
||||
evaluate("listOfNumbersUpToTen.![#this<5?'y':'n']", "[y, y, y, y, n, n, n, n, n, n]", ArrayList.class);
|
||||
// inline list creation not supported at the moment
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{#isEven(#this)}", "[n, y, n, y, n, y, n, y, n, y]", ArrayList.class);
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.!{#isEven(#this)}", "[n, y, n, y, n, y, n, y, n, y]", ArrayList.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testProjection02() {
|
||||
// inline map creation not supported at the moment
|
||||
// evaluate("#{'a':'y','b':'n','c':'y'}.![value=='y'?key:null].nonnull().sort()", "[a, c]", ArrayList.class);
|
||||
evaluate("mapOfNumbersUpToTen.![key>5?value:null]", "[null, null, null, null, null, six, seven, eight, nine, ten]", ArrayList.class);
|
||||
evaluate("mapOfNumbersUpToTen.![key>5?value:null]",
|
||||
"[null, null, null, null, null, six, seven, eight, nine, ten]", ArrayList.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testProjection05() {
|
||||
evaluateAndCheckError("'abc'.![true]", SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE);
|
||||
evaluateAndCheckError("null.![true]", SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE);
|
||||
evaluate("null?.![true]", null, null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testProjection06() throws Exception {
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'.![true]");
|
||||
Assert.assertEquals("'abc'.![true]",expr.toStringAST());
|
||||
SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.![true]");
|
||||
Assert.assertEquals("'abc'.![true]", expr.toStringAST());
|
||||
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
}
|
||||
|
||||
// SELECTION
|
||||
|
||||
|
||||
@Test
|
||||
public void testSelection02() {
|
||||
evaluate("testMap.keySet().?[#this matches '.*o.*']", "[monday]", ArrayList.class);
|
||||
evaluate("testMap.keySet().?[#this matches '.*r.*'].contains('saturday')", "true", Boolean.class);
|
||||
evaluate("testMap.keySet().?[#this matches '.*r.*'].size()", "3", Integer.class);
|
||||
evaluate("testMap.keySet().?[#this matches '.*o.*']", "[monday]", ArrayList.class);
|
||||
evaluate("testMap.keySet().?[#this matches '.*r.*'].contains('saturday')", "true", Boolean.class);
|
||||
evaluate("testMap.keySet().?[#this matches '.*r.*'].size()", "3", Integer.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSelectionError_NonBooleanSelectionCriteria() {
|
||||
evaluateAndCheckError("listOfNumbersUpToTen.?['nonboolean']",
|
||||
SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
|
||||
}
|
||||
|
||||
public void testSelectionError_NonBooleanSelectionCriteria() {
|
||||
evaluateAndCheckError("listOfNumbersUpToTen.?['nonboolean']",
|
||||
SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelection03() {
|
||||
evaluate("mapOfNumbersUpToTen.?[key>5].size()", "5", Integer.class);
|
||||
// evaluate("listOfNumbersUpToTen.?{#this>5}", "5", ArrayList.class);
|
||||
// evaluate("listOfNumbersUpToTen.?{#this>5}", "5", ArrayList.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSelection04() {
|
||||
evaluateAndCheckError("mapOfNumbersUpToTen.?['hello'].size()",SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
|
||||
evaluateAndCheckError("mapOfNumbersUpToTen.?['hello'].size()",
|
||||
SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -131,23 +135,25 @@ public class InProgressTests extends ExpressionTestCase {
|
||||
|
||||
@Test
|
||||
public void testSelectionLast02() {
|
||||
evaluate("mapOfNumbersUpToTen.$[key>5]", "{10=ten}", HashMap.class);
|
||||
evaluate("mapOfNumbersUpToTen.$[key>5].size()", "1", Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectionAST() throws Exception {
|
||||
SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'.^[true]");
|
||||
Assert.assertEquals("'abc'.^[true]",expr.toStringAST());
|
||||
SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.^[true]");
|
||||
Assert.assertEquals("'abc'.^[true]", expr.toStringAST());
|
||||
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
expr = (SpelExpression)parser.parseExpression("'abc'.?[true]");
|
||||
Assert.assertEquals("'abc'.?[true]",expr.toStringAST());
|
||||
expr = (SpelExpression) parser.parseExpression("'abc'.?[true]");
|
||||
Assert.assertEquals("'abc'.?[true]", expr.toStringAST());
|
||||
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
expr = (SpelExpression)parser.parseExpression("'abc'.$[true]");
|
||||
Assert.assertEquals("'abc'.$[true]",expr.toStringAST());
|
||||
expr = (SpelExpression) parser.parseExpression("'abc'.$[true]");
|
||||
Assert.assertEquals("'abc'.$[true]", expr.toStringAST());
|
||||
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
|
||||
}
|
||||
|
||||
// Constructor invocation
|
||||
|
||||
|
||||
// public void testPrimitiveTypeArrayConstructors() {
|
||||
// evaluate("new int[]{1,2,3,4}.count()", 4, Integer.class);
|
||||
// evaluate("new boolean[]{true,false,true}.count()", 3, Integer.class);
|
||||
@@ -254,52 +260,41 @@ public class InProgressTests extends ExpressionTestCase {
|
||||
// evaluate("(#answer=42;#answer)", "42", Integer.class, true);
|
||||
// evaluate("($answer=42;$answer)", "42", Integer.class, true);
|
||||
// }
|
||||
//
|
||||
|
||||
// // inline map creation
|
||||
// @Test
|
||||
// public void testInlineMapCreation01() {
|
||||
// evaluate("#{'key1':'Value 1', 'today':'Monday'}", "{key1=Value 1, today=Monday}", HashMap.class);
|
||||
// }
|
||||
//
|
||||
// public void testRelOperatorsIs02() {
|
||||
// evaluate("{1, 2, 3, 4, 5} instanceof T(List)", "true", Boolean.class);
|
||||
// }
|
||||
// @Test
|
||||
// public void testInlineMapCreation02() {
|
||||
// // "{2=February, 1=January, 3=March}", HashMap.class);
|
||||
// evaluate("#{1:'January', 2:'February', 3:'March'}.size()", 3, Integer.class);
|
||||
// }
|
||||
//
|
||||
// public void testRelOperatorsIs03() {
|
||||
// evaluate("{1, 2, 3, 4, 5} instanceof T(List)", "true", Boolean.class);
|
||||
// }
|
||||
// @Test
|
||||
// public void testInlineMapCreation03() {
|
||||
// evaluate("#{'key1':'Value 1', 'today':'Monday'}['key1']", "Value 1", String.class);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testInlineMapCreation04() {
|
||||
// evaluate("#{1:'January', 2:'February', 3:'March'}[3]", "March", String.class);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testInlineMapCreation05() {
|
||||
// evaluate("#{1:'January', 2:'February', 3:'March'}.get(2)", "February", String.class);
|
||||
// }
|
||||
|
||||
// set construction
|
||||
@Test
|
||||
public void testSetConstruction01() {
|
||||
evaluate("new java.util.HashSet().addAll({'a','b','c'})", "true", Boolean.class);
|
||||
}
|
||||
|
||||
//
|
||||
// inline list creation
|
||||
// public void testInlineListCreation01() {
|
||||
// evaluate("{1, 2, 3, 4, 5}", "[1, 2, 3, 4, 5]", ArrayList.class);
|
||||
// }
|
||||
//
|
||||
// public void testInlineListCreation02() {
|
||||
// evaluate("{'abc', 'xyz'}", "[abc, xyz]", ArrayList.class);
|
||||
// }
|
||||
//
|
||||
// // inline map creation
|
||||
// public void testInlineMapCreation01() {
|
||||
// evaluate("#{'key1':'Value 1', 'today':'Monday'}", "{key1=Value 1, today=Monday}", HashMap.class);
|
||||
// }
|
||||
//
|
||||
// public void testInlineMapCreation02() {
|
||||
// evaluate("#{1:'January', 2:'February', 3:'March'}.size()", 3, Integer.class);// "{2=February, 1=January,
|
||||
// // 3=March}", HashMap.class);
|
||||
// }
|
||||
//
|
||||
// public void testInlineMapCreation03() {
|
||||
// evaluate("#{'key1':'Value 1', 'today':'Monday'}['key1']", "Value 1", String.class);
|
||||
// }
|
||||
//
|
||||
// public void testInlineMapCreation04() {
|
||||
// evaluate("#{1:'January', 2:'February', 3:'March'}[3]", "March", String.class);
|
||||
// }
|
||||
//
|
||||
// public void testInlineMapCreation05() {
|
||||
// evaluate("#{1:'January', 2:'February', 3:'March'}.get(2)", "February", String.class);
|
||||
// }
|
||||
//
|
||||
// // set construction
|
||||
// public void testSetConstruction01() {
|
||||
// evaluate("new HashSet().addAll({'a','b','c'})", "true", Boolean.class);
|
||||
// }
|
||||
//
|
||||
// public void testConstructorInvocation02() {
|
||||
// evaluate("new String[3]", "java.lang.String[3]{null,null,null}", String[].class);
|
||||
// }
|
||||
@@ -311,7 +306,8 @@ public class InProgressTests extends ExpressionTestCase {
|
||||
// public void testConstructorInvocation04() {
|
||||
// evaluateAndCheckError("new String[3]{'abc',3,'def'}", SpelMessages.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, 4);
|
||||
// }
|
||||
// // array construction
|
||||
// array construction
|
||||
// @Test
|
||||
// public void testArrayConstruction01() {
|
||||
// evaluate("new int[] {1, 2, 3, 4, 5}", "int[5]{1,2,3,4,5}", int[].class);
|
||||
// }
|
||||
@@ -418,13 +414,13 @@ public class InProgressTests extends ExpressionTestCase {
|
||||
// public void testSelectionUsingIndex() {
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{$index > 5 }", "[7, 8, 9, 10]", ArrayList.class);
|
||||
// }
|
||||
//public void testSelection01() {
|
||||
// inline list creation not supported:
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{#isEven(#this) == 'y'}", "[2, 4, 6, 8, 10]", ArrayList.class);
|
||||
//}
|
||||
// public void testSelection01() {
|
||||
// inline list creation not supported:
|
||||
// evaluate("{1,2,3,4,5,6,7,8,9,10}.?{#isEven(#this) == 'y'}", "[2, 4, 6, 8, 10]", ArrayList.class);
|
||||
// }
|
||||
//
|
||||
// public void testSelectionUsingIndex() {
|
||||
// evaluate("listOfNumbersUpToTen.?[#index > 5 ]", "[7, 8, 9, 10]", ArrayList.class);
|
||||
//}
|
||||
// public void testSelectionUsingIndex() {
|
||||
// evaluate("listOfNumbersUpToTen.?[#index > 5 ]", "[7, 8, 9, 10]", ArrayList.class);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.expression.spel.ast.InlineList;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
|
||||
/**
|
||||
* Test usage of inline lists.
|
||||
*
|
||||
* @author Andy Clement
|
||||
* @since 3.0.4
|
||||
*/
|
||||
public class ListTests extends ExpressionTestCase {
|
||||
|
||||
// if the list is full of literals then it will be of the type unmodifiableClass rather than ArrayList
|
||||
Class<?> unmodifiableClass = Collections.unmodifiableList(new ArrayList<Object>()).getClass();
|
||||
|
||||
@Test
|
||||
public void testInlineListCreation01() {
|
||||
evaluate("{1, 2, 3, 4, 5}", "[1, 2, 3, 4, 5]", unmodifiableClass);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListCreation02() {
|
||||
evaluate("{'abc', 'xyz'}", "[abc, xyz]", unmodifiableClass);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListCreation03() {
|
||||
evaluate("{}", "[]", unmodifiableClass);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListCreation04() {
|
||||
evaluate("{'abc'=='xyz'}", "[false]", ArrayList.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListAndNesting() {
|
||||
evaluate("{{1,2,3},{4,5,6}}", "[[1, 2, 3], [4, 5, 6]]", unmodifiableClass);
|
||||
evaluate("{{1,'2',3},{4,{'a','b'},5,6}}", "[[1, 2, 3], [4, [a, b], 5, 6]]", unmodifiableClass);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListError() {
|
||||
parseAndCheckError("{'abc'", SpelMessage.OOD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsIs02() {
|
||||
evaluate("{1, 2, 3, 4, 5} instanceof T(java.util.List)", "true", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListCreation05() {
|
||||
evaluate("3 between {1,5}", "true", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListCreation06() {
|
||||
evaluate("8 between {1,5}", "false", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListAndProjectionSelection() {
|
||||
evaluate("{1,2,3,4,5,6}.![#this>3]", "[false, false, false, true, true, true]", ArrayList.class);
|
||||
evaluate("{1,2,3,4,5,6}.?[#this>3]", "[4, 5, 6]", ArrayList.class);
|
||||
evaluate("{1,2,3,4,5,6,7,8,9,10}.?[#isEven(#this) == 'y']", "[2, 4, 6, 8, 10]", ArrayList.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetConstruction01() {
|
||||
evaluate("new java.util.HashSet().addAll({'a','b','c'})", "true", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsBetween01() {
|
||||
evaluate("32 between {32, 42}", "true", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsBetween02() {
|
||||
evaluate("'efg' between {'abc', 'xyz'}", "true", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsBetween03() {
|
||||
evaluate("42 between {32, 42}", "true", Boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRelOperatorsBetweenErrors02() {
|
||||
evaluateAndCheckError("'abc' between {5,7}", SpelMessage.NOT_COMPARABLE, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstantRepresentation1() {
|
||||
checkConstantList("{1,2,3,4,5}", true);
|
||||
checkConstantList("{'abc'}", true);
|
||||
checkConstantList("{}", true);
|
||||
checkConstantList("{#a,2,3}", false);
|
||||
checkConstantList("{1,2,Integer.valueOf(4)}", false);
|
||||
checkConstantList("{1,2,{#a}}", false);
|
||||
}
|
||||
|
||||
private void checkConstantList(String expressionText, boolean expectedToBeConstant) {
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
|
||||
SpelNode node = expression.getAST();
|
||||
Assert.assertTrue(node instanceof InlineList);
|
||||
InlineList inlineList = (InlineList) node;
|
||||
if (expectedToBeConstant) {
|
||||
Assert.assertTrue(inlineList.isConstant());
|
||||
} else {
|
||||
Assert.assertFalse(inlineList.isConstant());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineListWriting() {
|
||||
// list should be unmodifiable
|
||||
try {
|
||||
evaluate("{1, 2, 3, 4, 5}[0]=6", "[1, 2, 3, 4, 5]", unmodifiableClass);
|
||||
Assert.fail();
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -432,7 +432,12 @@ public class ParsingTests {
|
||||
public void testTypeReferences02() {
|
||||
parseCheck("T(String)");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInlineList1() {
|
||||
parseCheck("{1,2,3,4}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the supplied expression and then create a string representation of the resultant AST, it should be the same
|
||||
* as the original expression.
|
||||
|
||||
@@ -17,13 +17,12 @@
|
||||
package org.springframework.expression.spel.standard;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.ExpressionException;
|
||||
import org.springframework.expression.ParseException;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.expression.spel.SpelMessage;
|
||||
import org.springframework.expression.spel.SpelNode;
|
||||
import org.springframework.expression.spel.SpelParseException;
|
||||
@@ -111,16 +110,6 @@ public class SpelParserTests {
|
||||
|
||||
@Test
|
||||
public void generalExpressions() throws Exception {
|
||||
try {
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
parser.parseRaw("new String[3]");
|
||||
Assert.fail();
|
||||
} catch (ParseException e) {
|
||||
Assert.assertTrue(e instanceof SpelParseException);
|
||||
SpelParseException spe = (SpelParseException)e;
|
||||
Assert.assertEquals(SpelMessage.MISSING_CONSTRUCTOR_ARGS,spe.getMessageCode());
|
||||
Assert.assertEquals(10,spe.getPosition());
|
||||
}
|
||||
try {
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
parser.parseRaw("new String");
|
||||
|
||||
Reference in New Issue
Block a user