|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.expression.spel; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Nested; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.expression.Expression; |
| 27 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 28 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests which verify support for using {@link Optional} with the null-safe and |
| 35 | + * Elvis operators in SpEL expressions. |
| 36 | + * |
| 37 | + * @author Sam Brannen |
| 38 | + * @since 7.0 |
| 39 | + */ |
| 40 | +class OptionalNullSafetyTests { |
| 41 | + |
| 42 | + private final SpelExpressionParser parser = new SpelExpressionParser(); |
| 43 | + |
| 44 | + private final StandardEvaluationContext context = new StandardEvaluationContext(); |
| 45 | + |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUpContext() { |
| 49 | + context.setVariable("service", new Service()); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + void invokeMethodDirectlyOnEmptyOptional() { |
| 55 | + assertInvocationFails("#service.findJediByName('').name"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void invokeMethodDirectlyOnNonEmptyOptional() { |
| 60 | + assertInvocationFails("#service.findJediByName('Yoda').name"); |
| 61 | + } |
| 62 | + |
| 63 | + private void assertInvocationFails(String expression) { |
| 64 | + Expression expr = parser.parseExpression(expression); |
| 65 | + |
| 66 | + assertThatExceptionOfType(SpelEvaluationException.class) |
| 67 | + .isThrownBy(() -> expr.getValue(context)) |
| 68 | + .satisfies(ex -> { |
| 69 | + assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE); |
| 70 | + assertThat(ex).hasMessageContaining("Property or field 'name' cannot be found on object of type 'java.util.Optional'"); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @Nested |
| 76 | + class NullSafeTests { |
| 77 | + |
| 78 | + @Test |
| 79 | + void accessPropertyOnEmptyOptionalViaNullSafeOperator() { |
| 80 | + Expression expr = parser.parseExpression("#service.findJediByName('')?.name"); |
| 81 | + |
| 82 | + assertThat(expr.getValue(context)).isNull(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void accessPropertyOnNonEmptyOptionalViaNullSafeOperator() { |
| 87 | + Expression expr = parser.parseExpression("#service.findJediByName('Yoda')?.name"); |
| 88 | + |
| 89 | + assertThat(expr.getValue(context)).isEqualTo("Yoda"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + void accessIndexOnEmptyOptionalViaNullSafeOperator() { |
| 95 | + Expression expr = parser.parseExpression("#service.findFruitsByColor('blue')?.[1]"); |
| 96 | + |
| 97 | + assertThat(expr.getValue(context)).isNull(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + void accessIndexOnNonEmptyOptionalViaNullSafeOperator() { |
| 103 | + Expression expr = parser.parseExpression("#service.findFruitsByColor('yellow')?.[1]"); |
| 104 | + |
| 105 | + assertThat(expr.getValue(context)).isEqualTo("lemon"); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @Nested |
| 111 | + class ElvisTests { |
| 112 | + |
| 113 | + @Test |
| 114 | + void elvisOperatorOnEmptyOptional() { |
| 115 | + Expression expr = parser.parseExpression("#service.findJediByName('') ?: 'unknown'"); |
| 116 | + |
| 117 | + assertThat(expr.getValue(context)).isEqualTo("unknown"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void elvisOperatorOnNonEmptyOptional() { |
| 122 | + Expression expr = parser.parseExpression("#service.findJediByName('Yoda') ?: 'unknown'"); |
| 123 | + |
| 124 | + assertThat(expr.getValue(context)).isEqualTo(new Jedi("Yoda")); |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + record Jedi(String name) { |
| 131 | + } |
| 132 | + |
| 133 | + static class Service { |
| 134 | + |
| 135 | + public Optional<Jedi> findJediByName(String name) { |
| 136 | + return (!name.isEmpty() ? Optional.of(new Jedi(name)) : Optional.empty()); |
| 137 | + } |
| 138 | + |
| 139 | + public Optional<List<String>> findFruitsByColor(String color) { |
| 140 | + return (color.equals("yellow") ? Optional.of(List.of("banana", "lemon")) : Optional.empty()); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments