Skip to content

Commit 4fcfa5b

Browse files
committed
Update and simplify ArrayConstructorTests
1 parent ed06a6d commit 4fcfa5b

File tree

1 file changed

+45
-125
lines changed

1 file changed

+45
-125
lines changed
Lines changed: 45 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,52 +20,41 @@
2020

2121
import org.springframework.expression.Expression;
2222
import org.springframework.expression.spel.standard.SpelExpressionParser;
23+
import org.springframework.util.ObjectUtils;
2324

2425
import static org.assertj.core.api.Assertions.assertThat;
2526

2627
/**
2728
* Test construction of arrays.
2829
*
2930
* @author Andy Clement
31+
* @author Sam Brannen
3032
*/
31-
public class ArrayConstructorTests extends AbstractExpressionTests {
33+
class ArrayConstructorTests extends AbstractExpressionTests {
3234

3335
@Test
34-
public void simpleArrayWithInitializer() {
35-
evaluateArrayBuildingExpression("new int[]{1,2,3}", "[1,2,3]");
36-
evaluateArrayBuildingExpression("new int[]{}", "[]");
37-
evaluate("new int[]{}.length", "0", Integer.class);
38-
}
39-
40-
@Test
41-
public void conversion() {
36+
void conversion() {
4237
evaluate("new String[]{1,2,3}[0]", "1", String.class);
4338
evaluate("new int[]{'123'}[0]", 123, Integer.class);
4439
}
4540

4641
@Test
47-
public void multidimensionalArrays() {
48-
evaluateAndCheckError("new int[][]{{1,2},{3,4}}", SpelMessage.MULTIDIM_ARRAY_INITIALIZER_NOT_SUPPORTED);
49-
evaluateAndCheckError("new int[3][]", SpelMessage.MISSING_ARRAY_DIMENSION);
50-
evaluateAndCheckError("new int[]", SpelMessage.MISSING_ARRAY_DIMENSION);
51-
evaluateAndCheckError("new String[]", SpelMessage.MISSING_ARRAY_DIMENSION);
52-
evaluateAndCheckError("new int[][1]", SpelMessage.MISSING_ARRAY_DIMENSION);
53-
}
42+
void primitiveTypeArrayConstructors() {
43+
evaluateArrayBuildingExpression("new int[]{}", "{}");
44+
evaluateArrayBuildingExpression("new int[]{1,2,3,4}", "{1, 2, 3, 4}");
45+
evaluateArrayBuildingExpression("new boolean[]{true,false,true}", "{true, false, true}");
46+
evaluateArrayBuildingExpression("new char[]{'a','b','c'}", "{'a', 'b', 'c'}");
47+
evaluateArrayBuildingExpression("new long[]{1,2,3,4,5}", "{1, 2, 3, 4, 5}");
48+
evaluateArrayBuildingExpression("new short[]{2,3,4,5,6}", "{2, 3, 4, 5, 6}");
49+
evaluateArrayBuildingExpression("new double[]{1d,2d,3d,4d}", "{1.0, 2.0, 3.0, 4.0}");
50+
evaluateArrayBuildingExpression("new float[]{1f,2f,3f,4f}", "{1.0, 2.0, 3.0, 4.0}");
51+
evaluateArrayBuildingExpression("new byte[]{1,2,3,4}", "{1, 2, 3, 4}");
5452

55-
@Test
56-
public void primitiveTypeArrayConstructors() {
57-
evaluateArrayBuildingExpression("new int[]{1,2,3,4}", "[1,2,3,4]");
58-
evaluateArrayBuildingExpression("new boolean[]{true,false,true}", "[true,false,true]");
59-
evaluateArrayBuildingExpression("new char[]{'a','b','c'}", "[a,b,c]");
60-
evaluateArrayBuildingExpression("new long[]{1,2,3,4,5}", "[1,2,3,4,5]");
61-
evaluateArrayBuildingExpression("new short[]{2,3,4,5,6}", "[2,3,4,5,6]");
62-
evaluateArrayBuildingExpression("new double[]{1d,2d,3d,4d}", "[1.0,2.0,3.0,4.0]");
63-
evaluateArrayBuildingExpression("new float[]{1f,2f,3f,4f}", "[1.0,2.0,3.0,4.0]");
64-
evaluateArrayBuildingExpression("new byte[]{1,2,3,4}", "[1,2,3,4]");
53+
evaluate("new int[]{}.length", "0", Integer.class);
6554
}
6655

6756
@Test
68-
public void primitiveTypeArrayConstructorsElements() {
57+
void primitiveTypeArrayConstructorsElements() {
6958
evaluate("new int[]{1,2,3,4}[0]", 1, Integer.class);
7059
evaluate("new boolean[]{true,false,true}[0]", true, Boolean.class);
7160
evaluate("new char[]{'a','b','c'}[0]", 'a', Character.class);
@@ -78,129 +67,60 @@ public void primitiveTypeArrayConstructorsElements() {
7867
}
7968

8069
@Test
81-
public void errorCases() {
70+
void errorCases() {
71+
evaluateAndCheckError("new int[]", SpelMessage.MISSING_ARRAY_DIMENSION);
72+
evaluateAndCheckError("new String[]", SpelMessage.MISSING_ARRAY_DIMENSION);
73+
evaluateAndCheckError("new int[3][]", SpelMessage.MISSING_ARRAY_DIMENSION);
74+
evaluateAndCheckError("new int[][1]", SpelMessage.MISSING_ARRAY_DIMENSION);
75+
8276
evaluateAndCheckError("new char[7]{'a','c','d','e'}", SpelMessage.INITIALIZER_LENGTH_INCORRECT);
8377
evaluateAndCheckError("new char[3]{'a','c','d','e'}", SpelMessage.INITIALIZER_LENGTH_INCORRECT);
78+
79+
evaluateAndCheckError("new int[][]{{1,2},{3,4}}", SpelMessage.MULTIDIM_ARRAY_INITIALIZER_NOT_SUPPORTED);
80+
8481
evaluateAndCheckError("new char[2]{'hello','world'}", SpelMessage.TYPE_CONVERSION_ERROR);
82+
// Could conceivably be a SpelMessage.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, but it appears
83+
// that SpelMessage.INCORRECT_ELEMENT_TYPE_FOR_ARRAY is not actually (no longer?) used
84+
// in the code base.
85+
evaluateAndCheckError("new Integer[3]{'3','ghi','5'}", SpelMessage.TYPE_CONVERSION_ERROR);
86+
8587
evaluateAndCheckError("new String('a','c','d')", SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM);
88+
// Root cause: java.lang.OutOfMemoryError: Requested array size exceeds VM limit
89+
evaluateAndCheckError("new java.util.ArrayList(T(java.lang.Integer).MAX_VALUE)", SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM);
90+
91+
int threshold = 256 * 1024; // ConstructorReference.MAX_ARRAY_ELEMENTS
92+
evaluateAndCheckError("new int[T(java.lang.Integer).MAX_VALUE]", SpelMessage.MAX_ARRAY_ELEMENTS_THRESHOLD_EXCEEDED, 0, threshold);
93+
evaluateAndCheckError("new int[1024 * 1024][1024 * 1024]", SpelMessage.MAX_ARRAY_ELEMENTS_THRESHOLD_EXCEEDED, 0, threshold);
8694
}
8795

8896
@Test
89-
public void typeArrayConstructors() {
97+
void typeArrayConstructors() {
9098
evaluate("new String[]{'a','b','c','d'}[1]", "b", String.class);
9199
evaluateAndCheckError("new String[]{'a','b','c','d'}.size()", SpelMessage.METHOD_NOT_FOUND, 30, "size()",
92100
"java.lang.String[]");
93101
evaluate("new String[]{'a','b','c','d'}.length", 4, Integer.class);
94102
}
95103

96104
@Test
97-
public void basicArray() {
105+
void basicArray() {
98106
evaluate("new String[3]", "java.lang.String[3]{null,null,null}", String[].class);
99107
}
100108

101109
@Test
102-
public void multiDimensionalArray() {
110+
void multiDimensionalArrays() {
103111
evaluate("new String[2][2]", "[Ljava.lang.String;[2]{[2]{null,null},[2]{null,null}}", String[][].class);
104112
evaluate("new String[3][2][1]",
105113
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
106114
String[][][].class);
107115
}
108116

109-
@Test
110-
public void constructorInvocation03() {
111-
evaluateAndCheckError("new String[]", SpelMessage.MISSING_ARRAY_DIMENSION);
112-
}
113-
114-
public void constructorInvocation04() {
115-
evaluateAndCheckError("new Integer[3]{'3','ghi','5'}", SpelMessage.INCORRECT_ELEMENT_TYPE_FOR_ARRAY, 4);
116-
}
117-
118-
private String evaluateArrayBuildingExpression(String expression, String expectedToString) {
117+
private void evaluateArrayBuildingExpression(String expression, String expectedToString) {
119118
SpelExpressionParser parser = new SpelExpressionParser();
120119
Expression e = parser.parseExpression(expression);
121-
Object o = e.getValue();
122-
assertThat(o).isNotNull();
123-
assertThat(o.getClass().isArray()).isTrue();
124-
StringBuilder s = new StringBuilder();
125-
s.append('[');
126-
if (o instanceof int[]) {
127-
int[] array = (int[]) o;
128-
for (int i = 0; i < array.length; i++) {
129-
if (i > 0) {
130-
s.append(',');
131-
}
132-
s.append(array[i]);
133-
}
134-
}
135-
else if (o instanceof boolean[]) {
136-
boolean[] array = (boolean[]) o;
137-
for (int i = 0; i < array.length; i++) {
138-
if (i > 0) {
139-
s.append(',');
140-
}
141-
s.append(array[i]);
142-
}
143-
}
144-
else if (o instanceof char[]) {
145-
char[] array = (char[]) o;
146-
for (int i = 0; i < array.length; i++) {
147-
if (i > 0) {
148-
s.append(',');
149-
}
150-
s.append(array[i]);
151-
}
152-
}
153-
else if (o instanceof long[]) {
154-
long[] array = (long[]) o;
155-
for (int i = 0; i < array.length; i++) {
156-
if (i > 0) {
157-
s.append(',');
158-
}
159-
s.append(array[i]);
160-
}
161-
}
162-
else if (o instanceof short[]) {
163-
short[] array = (short[]) o;
164-
for (int i = 0; i < array.length; i++) {
165-
if (i > 0) {
166-
s.append(',');
167-
}
168-
s.append(array[i]);
169-
}
170-
}
171-
else if (o instanceof double[]) {
172-
double[] array = (double[]) o;
173-
for (int i = 0; i < array.length; i++) {
174-
if (i > 0) {
175-
s.append(',');
176-
}
177-
s.append(array[i]);
178-
}
179-
}
180-
else if (o instanceof float[]) {
181-
float[] array = (float[]) o;
182-
for (int i = 0; i < array.length; i++) {
183-
if (i > 0) {
184-
s.append(',');
185-
}
186-
s.append(array[i]);
187-
}
188-
}
189-
else if (o instanceof byte[]) {
190-
byte[] array = (byte[]) o;
191-
for (int i = 0; i < array.length; i++) {
192-
if (i > 0) {
193-
s.append(',');
194-
}
195-
s.append(array[i]);
196-
}
197-
}
198-
else {
199-
throw new IllegalStateException("Not supported " + o.getClass());
200-
}
201-
s.append(']');
202-
assertThat(s.toString()).isEqualTo(expectedToString);
203-
return s.toString();
120+
Object array = e.getValue();
121+
assertThat(array).isNotNull();
122+
assertThat(array.getClass().isArray()).isTrue();
123+
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo(expectedToString);
204124
}
205125

206126
}

0 commit comments

Comments
 (0)