1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
21
21
import org .springframework .expression .Expression ;
22
22
import org .springframework .expression .spel .standard .SpelExpressionParser ;
23
+ import org .springframework .util .ObjectUtils ;
23
24
24
25
import static org .assertj .core .api .Assertions .assertThat ;
25
26
26
27
/**
27
28
* Test construction of arrays.
28
29
*
29
30
* @author Andy Clement
31
+ * @author Sam Brannen
30
32
*/
31
- public class ArrayConstructorTests extends AbstractExpressionTests {
33
+ class ArrayConstructorTests extends AbstractExpressionTests {
32
34
33
35
@ 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 () {
42
37
evaluate ("new String[]{1,2,3}[0]" , "1" , String .class );
43
38
evaluate ("new int[]{'123'}[0]" , 123 , Integer .class );
44
39
}
45
40
46
41
@ 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}" );
54
52
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 );
65
54
}
66
55
67
56
@ Test
68
- public void primitiveTypeArrayConstructorsElements () {
57
+ void primitiveTypeArrayConstructorsElements () {
69
58
evaluate ("new int[]{1,2,3,4}[0]" , 1 , Integer .class );
70
59
evaluate ("new boolean[]{true,false,true}[0]" , true , Boolean .class );
71
60
evaluate ("new char[]{'a','b','c'}[0]" , 'a' , Character .class );
@@ -78,129 +67,60 @@ public void primitiveTypeArrayConstructorsElements() {
78
67
}
79
68
80
69
@ 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
+
82
76
evaluateAndCheckError ("new char[7]{'a','c','d','e'}" , SpelMessage .INITIALIZER_LENGTH_INCORRECT );
83
77
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
+
84
81
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
+
85
87
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 );
86
94
}
87
95
88
96
@ Test
89
- public void typeArrayConstructors () {
97
+ void typeArrayConstructors () {
90
98
evaluate ("new String[]{'a','b','c','d'}[1]" , "b" , String .class );
91
99
evaluateAndCheckError ("new String[]{'a','b','c','d'}.size()" , SpelMessage .METHOD_NOT_FOUND , 30 , "size()" ,
92
100
"java.lang.String[]" );
93
101
evaluate ("new String[]{'a','b','c','d'}.length" , 4 , Integer .class );
94
102
}
95
103
96
104
@ Test
97
- public void basicArray () {
105
+ void basicArray () {
98
106
evaluate ("new String[3]" , "java.lang.String[3]{null,null,null}" , String [].class );
99
107
}
100
108
101
109
@ Test
102
- public void multiDimensionalArray () {
110
+ void multiDimensionalArrays () {
103
111
evaluate ("new String[2][2]" , "[Ljava.lang.String;[2]{[2]{null,null},[2]{null,null}}" , String [][].class );
104
112
evaluate ("new String[3][2][1]" ,
105
113
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}" ,
106
114
String [][][].class );
107
115
}
108
116
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 ) {
119
118
SpelExpressionParser parser = new SpelExpressionParser ();
120
119
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 );
204
124
}
205
125
206
126
}
0 commit comments