@ -234,26 +234,33 @@ public class MethodInvocationTests extends AbstractExpressionTests {
@@ -234,26 +234,33 @@ public class MethodInvocationTests extends AbstractExpressionTests {
@Test
public void testVarargsInvocation01 ( ) {
// Calling 'public int aVarargsMethod(String... strings)' - returns number of arguments
evaluate ( "aVarargsMethod('a','b','c')" , 3 , Integer . class ) ;
evaluate ( "aVarargsMethod('a')" , 1 , Integer . class ) ;
evaluate ( "aVarargsMethod()" , 0 , Integer . class ) ;
evaluate ( "aVarargsMethod(1,2,3)" , 3 , Integer . class ) ; // all need converting to strings
evaluate ( "aVarargsMethod(1)" , 1 , Integer . class ) ; // needs string conversion
evaluate ( "aVarargsMethod(1,'a',3.0d)" , 3 , Integer . class ) ; // first and last need conversion
evaluate ( "aVarargsMethod(new String[]{'a','b','c'})" , 3 , Integer . class ) ;
// Calling 'public String aVarargsMethod(String... strings)'
evaluate ( "aVarargsMethod('a','b','c')" , "[a, b, c]" , String . class ) ;
evaluate ( "aVarargsMethod('a')" , "[a]" , String . class ) ;
evaluate ( "aVarargsMethod()" , "[]" , String . class ) ;
evaluate ( "aVarargsMethod(1,2,3)" , "[1, 2, 3]" , String . class ) ; // all need converting to strings
evaluate ( "aVarargsMethod(1)" , "[1]" , String . class ) ; // needs string conversion
evaluate ( "aVarargsMethod(1,'a',3.0d)" , "[1, a, 3.0]" , String . class ) ; // first and last need conversion
evaluate ( "aVarargsMethod(new String[]{'a','b','c'})" , "[a, b, c]" , String . class ) ;
evaluate ( "aVarargsMethod(new String[]{})" , "[]" , String . class ) ;
evaluate ( "aVarargsMethod(null)" , "[null]" , String . class ) ;
evaluate ( "aVarargsMethod(null,'a')" , "[null, a]" , String . class ) ;
evaluate ( "aVarargsMethod('a',null,'b')" , "[a, null, b]" , String . class ) ;
}
@Test
public void testVarargsInvocation02 ( ) {
// Calling 'public int aVarargsMethod2(int i, String... strings)' - returns int + length_of_strings
evaluate ( "aVarargsMethod2(5,'a','b','c')" , 8 , Integer . class ) ;
evaluate ( "aVarargsMethod2(2,'a')" , 3 , Integer . class ) ;
evaluate ( "aVarargsMethod2(4)" , 4 , Integer . class ) ;
evaluate ( "aVarargsMethod2(8,2,3)" , 10 , Integer . class ) ;
evaluate ( "aVarargsMethod2(9)" , 9 , Integer . class ) ;
evaluate ( "aVarargsMethod2(2,'a',3.0d)" , 4 , Integer . class ) ;
evaluate ( "aVarargsMethod2(8,new String[]{'a','b','c'})" , 11 , Integer . class ) ;
// Calling 'public String aVarargsMethod2(int i, String... strings)'
evaluate ( "aVarargsMethod2(5,'a','b','c')" , "5-[a, b, c]" , String . class ) ;
evaluate ( "aVarargsMethod2(2,'a')" , "2-[a]" , String . class ) ;
evaluate ( "aVarargsMethod2(4)" , "4-[]" , String . class ) ;
evaluate ( "aVarargsMethod2(8,2,3)" , "8-[2, 3]" , String . class ) ;
evaluate ( "aVarargsMethod2(2,'a',3.0d)" , "2-[a, 3.0]" , String . class ) ;
evaluate ( "aVarargsMethod2(8,new String[]{'a','b','c'})" , "8-[a, b, c]" , String . class ) ;
evaluate ( "aVarargsMethod2(8,new String[]{})" , "8-[]" , String . class ) ;
evaluate ( "aVarargsMethod2(8,null)" , "8-[null]" , String . class ) ;
evaluate ( "aVarargsMethod2(8,null,'a')" , "8-[null, a]" , String . class ) ;
evaluate ( "aVarargsMethod2(8,'a',null,'b')" , "8-[a, null, b]" , String . class ) ;
}
@Test
@ -284,6 +291,25 @@ public class MethodInvocationTests extends AbstractExpressionTests {
@@ -284,6 +291,25 @@ public class MethodInvocationTests extends AbstractExpressionTests {
evaluate ( "aVarargsMethod3('foo', 'bar,baz')" , "foo-bar,baz" , String . class ) ;
}
@Test
public void testVarargsOptionalInvocation ( ) {
// Calling 'public String optionalVarargsMethod(Optional<String>... values)'
evaluate ( "optionalVarargsMethod()" , "[]" , String . class ) ;
evaluate ( "optionalVarargsMethod(new String[0])" , "[]" , String . class ) ;
evaluate ( "optionalVarargsMethod('a')" , "[Optional[a]]" , String . class ) ;
evaluate ( "optionalVarargsMethod('a','b','c')" , "[Optional[a], Optional[b], Optional[c]]" , String . class ) ;
evaluate ( "optionalVarargsMethod(9)" , "[Optional[9]]" , String . class ) ;
evaluate ( "optionalVarargsMethod(2,3)" , "[Optional[2], Optional[3]]" , String . class ) ;
evaluate ( "optionalVarargsMethod('a',3.0d)" , "[Optional[a], Optional[3.0]]" , String . class ) ;
evaluate ( "optionalVarargsMethod(new String[]{'a','b','c'})" , "[Optional[a], Optional[b], Optional[c]]" , String . class ) ;
// The following should actually evaluate to [Optional.empty] instead of [null],
// but ReflectionHelper.convertArguments() currently does not provide explicit
// Optional support for a single argument passed to a varargs array.
evaluate ( "optionalVarargsMethod(null)" , "[null]" , String . class ) ;
evaluate ( "optionalVarargsMethod(null,'a')" , "[Optional.empty, Optional[a]]" , String . class ) ;
evaluate ( "optionalVarargsMethod('a',null,'b')" , "[Optional[a], Optional.empty, Optional[b]]" , String . class ) ;
}
@Test
public void testInvocationOnNullContextObject ( ) {
evaluateAndCheckError ( "null.toString()" , SpelMessage . METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED ) ;