|
| 1 | +import org.junit.jupiter.api.Test; |
| 2 | +import org.springframework.expression.Expression; |
| 3 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 4 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 5 | +import org.springframework.util.ReflectionUtils; |
| 6 | +import static org.assertj.core.api.Assertions.assertThat; |
| 7 | + |
| 8 | +import java.lang.reflect.Method; |
| 9 | + |
| 10 | +public class SpelVarargArrayTest { |
| 11 | + @Test |
| 12 | + public void arrayFlattenedAsVararg_Before_6_1_10() { |
| 13 | + assertThat(evaluateExpression("#objectVarargDemo({'World', 123})")).isEqualTo("There are 2 argument(s): World 123"); |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + public void arrayFlattenedAsVararg_After_6_1_11() { |
| 18 | + assertThat(evaluateExpression("#objectVarargDemo({'World', 123})")).isEqualTo("There are 1 argument(s): [World, 123]"); |
| 19 | + } |
| 20 | + |
| 21 | + private static final Method demoMethod = |
| 22 | + ReflectionUtils.findMethod(SpelVarargArrayTest.class, "objectVarargDemo", Object[].class); |
| 23 | + |
| 24 | + private static Object evaluateExpression(String expression) { |
| 25 | + SpelExpressionParser parser = new SpelExpressionParser(); |
| 26 | + Expression exp = parser.parseExpression(expression); |
| 27 | + StandardEvaluationContext context = new StandardEvaluationContext(); |
| 28 | + context.registerFunction("objectVarargDemo", demoMethod); |
| 29 | + return exp.getValue(context); |
| 30 | + } |
| 31 | + |
| 32 | + static String objectVarargDemo(Object... args) { |
| 33 | + StringBuilder sb = new StringBuilder(); |
| 34 | + sb.append(String.format("There are %d argument(s): ", args.length)); |
| 35 | + for (Object arg : args) { |
| 36 | + sb.append(arg).append(" "); |
| 37 | + } |
| 38 | + return sb.toString().trim(); |
| 39 | + } |
| 40 | +} |
0 commit comments