|
26 | 26 | import java.util.HashMap;
|
27 | 27 | import java.util.List;
|
28 | 28 | import java.util.Map;
|
| 29 | +import java.util.Set; |
29 | 30 |
|
| 31 | +import org.junit.jupiter.api.Nested; |
30 | 32 | import org.junit.jupiter.api.Test;
|
31 | 33 |
|
32 | 34 | import org.springframework.expression.EvaluationContext;
|
|
35 | 37 | import org.springframework.expression.TypedValue;
|
36 | 38 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
37 | 39 | import org.springframework.expression.spel.support.StandardEvaluationContext;
|
| 40 | +import org.springframework.expression.spel.testresources.Person; |
38 | 41 |
|
39 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
40 | 43 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -376,18 +379,74 @@ void listOfMaps() {
|
376 | 379 | assertThat(expression.getValue(this, String.class)).isEqualTo("apple");
|
377 | 380 | }
|
378 | 381 |
|
379 |
| - @Test |
380 |
| - void nullSafeIndex() { |
381 |
| - ContextWithNullCollections testContext = new ContextWithNullCollections(); |
382 |
| - StandardEvaluationContext context = new StandardEvaluationContext(testContext); |
383 |
| - Expression expr = new SpelExpressionParser().parseRaw("nullList?.[4]"); |
384 |
| - assertThat(expr.getValue(context)).isNull(); |
| 382 | + @Nested |
| 383 | + class NullSafeIndexTests { // gh-29847 |
| 384 | + |
| 385 | + private final RootContextWithIndexedProperties rootContext = new RootContextWithIndexedProperties(); |
| 386 | + |
| 387 | + private final StandardEvaluationContext context = new StandardEvaluationContext(rootContext); |
385 | 388 |
|
386 |
| - expr = new SpelExpressionParser().parseRaw("nullArray?.[4]"); |
387 |
| - assertThat(expr.getValue(context)).isNull(); |
| 389 | + private final SpelExpressionParser parser = new SpelExpressionParser(); |
| 390 | + |
| 391 | + private Expression expression; |
| 392 | + |
| 393 | + @Test |
| 394 | + void nullSafeIndexIntoArray() { |
| 395 | + expression = parser.parseExpression("array?.[0]"); |
| 396 | + assertThat(expression.getValue(context)).isNull(); |
| 397 | + rootContext.array = new int[] {42}; |
| 398 | + assertThat(expression.getValue(context)).isEqualTo(42); |
| 399 | + } |
| 400 | + |
| 401 | + @Test |
| 402 | + void nullSafeIndexIntoList() { |
| 403 | + expression = parser.parseExpression("list?.[0]"); |
| 404 | + assertThat(expression.getValue(context)).isNull(); |
| 405 | + rootContext.list = List.of(42); |
| 406 | + assertThat(expression.getValue(context)).isEqualTo(42); |
| 407 | + } |
| 408 | + |
| 409 | + @Test |
| 410 | + void nullSafeIndexIntoSet() { |
| 411 | + expression = parser.parseExpression("set?.[0]"); |
| 412 | + assertThat(expression.getValue(context)).isNull(); |
| 413 | + rootContext.set = Set.of(42); |
| 414 | + assertThat(expression.getValue(context)).isEqualTo(42); |
| 415 | + } |
| 416 | + |
| 417 | + @Test |
| 418 | + void nullSafeIndexIntoString() { |
| 419 | + expression = parser.parseExpression("string?.[0]"); |
| 420 | + assertThat(expression.getValue(context)).isNull(); |
| 421 | + rootContext.string = "XYZ"; |
| 422 | + assertThat(expression.getValue(context)).isEqualTo("X"); |
| 423 | + } |
| 424 | + |
| 425 | + @Test |
| 426 | + void nullSafeIndexIntoMap() { |
| 427 | + expression = parser.parseExpression("map?.['enigma']"); |
| 428 | + assertThat(expression.getValue(context)).isNull(); |
| 429 | + rootContext.map = Map.of("enigma", 42); |
| 430 | + assertThat(expression.getValue(context)).isEqualTo(42); |
| 431 | + } |
| 432 | + |
| 433 | + @Test |
| 434 | + void nullSafeIndexIntoObject() { |
| 435 | + expression = parser.parseExpression("person?.['name']"); |
| 436 | + assertThat(expression.getValue(context)).isNull(); |
| 437 | + rootContext.person = new Person("Jane"); |
| 438 | + assertThat(expression.getValue(context)).isEqualTo("Jane"); |
| 439 | + } |
| 440 | + |
| 441 | + static class RootContextWithIndexedProperties { |
| 442 | + public int[] array; |
| 443 | + public List<Integer> list; |
| 444 | + public Set<Integer> set; |
| 445 | + public String string; |
| 446 | + public Map<String, Integer> map; |
| 447 | + public Person person; |
| 448 | + } |
388 | 449 |
|
389 |
| - expr = new SpelExpressionParser().parseRaw("nullMap:?.[4]"); |
390 |
| - assertThat(expr.getValue(context)).isNull(); |
391 | 450 | }
|
392 | 451 |
|
393 | 452 |
|
@@ -450,11 +509,4 @@ public Class<?>[] getSpecificTargetClasses() {
|
450 | 509 |
|
451 | 510 | }
|
452 | 511 |
|
453 |
| - |
454 |
| - static class ContextWithNullCollections { |
455 |
| - public List nullList = null; |
456 |
| - public String[] nullArray = null; |
457 |
| - public Map nullMap = null; |
458 |
| - } |
459 |
| - |
460 | 512 | }
|
0 commit comments