|
|
|
@ -40,6 +40,8 @@ import org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMat |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
|
|
|
|
import static org.assertj.core.api.InstanceOfAssertFactories.array; |
|
|
|
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.CLOSE; |
|
|
|
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.CLOSE; |
|
|
|
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.EXACT; |
|
|
|
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.EXACT; |
|
|
|
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.REQUIRES_CONVERSION; |
|
|
|
import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.REQUIRES_CONVERSION; |
|
|
|
@ -252,14 +254,75 @@ class ReflectionHelperTests extends AbstractExpressionTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void setupArgumentsForVarargsInvocation() { |
|
|
|
void setupArgumentsForVarargsInvocation() { |
|
|
|
Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
Object[] newArray; |
|
|
|
new Class<?>[] {String[].class}, "a", "b", "c"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(newArray).hasSize(1); |
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
Object firstParam = newArray[0]; |
|
|
|
new Class<?>[] {String[].class}, "a", "b", "c"); |
|
|
|
assertThat(firstParam.getClass().componentType()).isEqualTo(String.class); |
|
|
|
assertThat(newArray) |
|
|
|
Object[] firstParamArray = (Object[]) firstParam; |
|
|
|
.singleElement() |
|
|
|
assertThat(firstParamArray).containsExactly("a", "b", "c"); |
|
|
|
.asInstanceOf(array(String[].class)) |
|
|
|
|
|
|
|
.containsExactly("a", "b", "c"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { Object[].class }, "a", "b", "c"); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.asInstanceOf(array(Object[].class)) |
|
|
|
|
|
|
|
.containsExactly("a", "b", "c"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { Integer.class, Integer.class, String[].class }, 123, 456, "a", "b", "c"); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.satisfiesExactly( |
|
|
|
|
|
|
|
i -> assertThat(i).isEqualTo(123), |
|
|
|
|
|
|
|
i -> assertThat(i).isEqualTo(456), |
|
|
|
|
|
|
|
i -> assertThat(i).asInstanceOf(array(String[].class)).containsExactly("a", "b", "c")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { String[].class }); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.asInstanceOf(array(String[].class)) |
|
|
|
|
|
|
|
.isEmpty(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { String[].class }, new Object[] { new String[] { "a", "b", "c" } }); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.asInstanceOf(array(String[].class)) |
|
|
|
|
|
|
|
.containsExactly("a", "b", "c"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { Object[].class }, new Object[] { new String[] { "a", "b", "c" } }); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.asInstanceOf(array(Object[].class)) |
|
|
|
|
|
|
|
.containsExactly("a", "b", "c"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { String[].class }, "a"); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.asInstanceOf(array(String[].class)) |
|
|
|
|
|
|
|
.containsExactly("a"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { String[].class }, new Object[]{null}); |
|
|
|
|
|
|
|
assertThat(newArray) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.asInstanceOf(array(String[].class)) |
|
|
|
|
|
|
|
.singleElement() |
|
|
|
|
|
|
|
.isNull(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation( |
|
|
|
|
|
|
|
new Class<?>[] { Integer.class, Integer.class }, 123, 456)) |
|
|
|
|
|
|
|
.isInstanceOf(IllegalArgumentException.class) |
|
|
|
|
|
|
|
.hasMessage("Method must be varargs"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[] {}, "a", "b", "c")) |
|
|
|
|
|
|
|
.isInstanceOf(IllegalArgumentException.class) |
|
|
|
|
|
|
|
.hasMessage("Required parameter types must not be empty"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
|