|
21 | 21 | import java.util.Set;
|
22 | 22 |
|
23 | 23 | import org.assertj.core.api.ThrowableTypeAssert;
|
24 |
| -import org.junit.jupiter.api.Disabled; |
25 | 24 | import org.junit.jupiter.api.Test;
|
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.ValueSource; |
26 | 27 |
|
27 | 28 | import org.springframework.expression.EvaluationException;
|
28 | 29 | import org.springframework.expression.Expression;
|
29 | 30 | import org.springframework.expression.ParseException;
|
30 | 31 | import org.springframework.expression.spel.testresources.PlaceOfBirth;
|
| 32 | +import org.springframework.util.ObjectUtils; |
31 | 33 |
|
32 | 34 | import static org.assertj.core.api.Assertions.assertThat;
|
33 | 35 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 36 | +import static org.springframework.expression.spel.SpelMessage.TYPE_CONVERSION_ERROR; |
34 | 37 |
|
35 | 38 | /**
|
36 | 39 | * Tests for assignment, setValue(), and isWritable() expressions.
|
@@ -164,21 +167,19 @@ void setArrayElementToPrimitiveFromSingleElementWrapperList() {
|
164 | 167 | setValue("arrayContainer.doubles[1]", List.of(42D), 42D);
|
165 | 168 | }
|
166 | 169 |
|
167 |
| - @Disabled("Disabled due to bug in Indexer.setArrayElement() regarding primitive/wrapper types") |
168 |
| - @Test |
169 |
| - void setArrayElementToPrimitiveFromEmptyCollectionFails() { |
170 |
| - List<Object> emptyList = List.of(); |
171 |
| - // TODO These fail because CollectionToObjectConverter returns null. |
172 |
| - // It currently throws: java.lang.IllegalStateException: Null conversion result for index [[]]. |
173 |
| - // Whereas, it should throw a SpelEvaluationException. |
174 |
| - setValueAndExpectError("arrayContainer.booleans[1]", emptyList); |
175 |
| - setValueAndExpectError("arrayContainer.chars[1]", emptyList); |
176 |
| - setValueAndExpectError("arrayContainer.shorts[1]", emptyList); |
177 |
| - setValueAndExpectError("arrayContainer.bytes[1]", emptyList); |
178 |
| - setValueAndExpectError("arrayContainer.ints[1]", emptyList); |
179 |
| - setValueAndExpectError("arrayContainer.longs[1]", emptyList); |
180 |
| - setValueAndExpectError("arrayContainer.floats[1]", emptyList); |
181 |
| - setValueAndExpectError("arrayContainer.doubles[1]", emptyList); |
| 170 | + @ParameterizedTest |
| 171 | + @ValueSource(strings = { |
| 172 | + "arrayContainer.booleans[1]", |
| 173 | + "arrayContainer.chars[1]", |
| 174 | + "arrayContainer.shorts[1]", |
| 175 | + "arrayContainer.bytes[1]", |
| 176 | + "arrayContainer.ints[1]", |
| 177 | + "arrayContainer.longs[1]", |
| 178 | + "arrayContainer.floats[1]", |
| 179 | + "arrayContainer.doubles[1]" |
| 180 | + }) |
| 181 | + void setArrayElementToPrimitiveFromEmptyCollectionFailsWithTypeConversionError(String expression) { |
| 182 | + setValueAndExpectError(expression, List.of(), TYPE_CONVERSION_ERROR); |
182 | 183 | }
|
183 | 184 |
|
184 | 185 | @Test
|
@@ -272,6 +273,40 @@ private void setValueAndExpectError(String expression, Object value) {
|
272 | 273 | assertThatSpelEvaluationException().isThrownBy(() -> e.setValue(context, value));
|
273 | 274 | }
|
274 | 275 |
|
| 276 | + /** |
| 277 | + * Call setValue() but expect it to fail. |
| 278 | + * @see #evaluateAndCheckError(org.springframework.expression.ExpressionParser, String, Class, SpelMessage, Object...) |
| 279 | + */ |
| 280 | + private void setValueAndExpectError(String expression, Object value, SpelMessage expectedMessage, |
| 281 | + Object... otherProperties) { |
| 282 | + |
| 283 | + Expression expr = parser.parseExpression(expression); |
| 284 | + assertThat(expr).as("expression").isNotNull(); |
| 285 | + |
| 286 | + if (DEBUG) { |
| 287 | + SpelUtilities.printAbstractSyntaxTree(System.out, expr); |
| 288 | + } |
| 289 | + |
| 290 | + assertThatSpelEvaluationException() |
| 291 | + .isThrownBy(() -> expr.setValue(context, value)) |
| 292 | + .satisfies(ex -> { |
| 293 | + assertThat(ex.getMessageCode()).isEqualTo(expectedMessage); |
| 294 | + if (!ObjectUtils.isEmpty(otherProperties)) { |
| 295 | + // first one is expected position of the error within the string |
| 296 | + int pos = (Integer) otherProperties[0]; |
| 297 | + assertThat(ex.getPosition()).as("position").isEqualTo(pos); |
| 298 | + if (otherProperties.length > 1) { |
| 299 | + // Check inserts match |
| 300 | + Object[] inserts = ex.getInserts(); |
| 301 | + assertThat(inserts).as("inserts").hasSizeGreaterThanOrEqualTo(otherProperties.length - 1); |
| 302 | + Object[] expectedInserts = new Object[inserts.length]; |
| 303 | + System.arraycopy(otherProperties, 1, expectedInserts, 0, expectedInserts.length); |
| 304 | + assertThat(inserts).as("inserts").containsExactly(expectedInserts); |
| 305 | + } |
| 306 | + } |
| 307 | + }); |
| 308 | + } |
| 309 | + |
275 | 310 | private void setValue(String expression, Object value) {
|
276 | 311 | Class<?> expectedType = value.getClass();
|
277 | 312 | try {
|
|
0 commit comments