AbstractObjectConverter { protected void checkIfArgumentsMatchParameterTypes(Object[] arguments, Class<?>[] parameterTypes) { checkNotNull(arguments, "'arguments' must not be null"); checkNotNull(parameterTypes, "'testMethod' must not be null"); checkArgument(parameterTypes.length >= arguments.length, "Expected at most %d arguments for test method but got %d.", parameterTypes.length, arguments.length); for (int idx = 0; idx < arguments.length; idx++) { Object object = arguments[idx]; if (object != null) { Class<?> paramType = parameterTypes[idx]; if (!paramType.isInstance(object) && !isWrappedInstance(paramType, object) && !isWideningConversion(paramType, object)) { throw new IllegalArgumentException( String.format("Parameter number %d is of type '%s' but argument given is '%s' of type '%s'", idx, paramType.getSimpleName(), object, object.getClass().getSimpleName())); } } } }  abstract Object[] convert(V data, boolean isVarargs, Class<?>[] parameterTypes);  }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldThrowNullPointerExceptionIfArgumentsIsNull() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("'arguments' must not be null"); underTest.checkIfArgumentsMatchParameterTypes(null, new Class<?>[0]); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldThrowNullPointerExceptionIfParameterTypesIsNull() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("'testMethod' must not be null"); underTest.checkIfArgumentsMatchParameterTypes(new Object[0], null); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldThrowIllegalArgumentExceptionIfMoreArgumentsThanParametersAreGiven() { Object[] arguments = new Object[] { 0, "b", false, 'd' }; Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, boolean.class }; expectedException.expect(IllegalArgumentException.class); expectedException .expectMessage("Expected at most 3 arguments for test method but got 4."); underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldEndWithoutFailureIfLengthOfArgumentsIsSmallerThanParameterTypes() { Object[] arguments = new Object[0]; Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, boolean.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldThrowIllegalArgumentExceptionIfSingleArgumentIsNotAssignableToParameterType() { Object[] arguments = new Object[] { "1" }; Class<?>[] parameterTypes = new Class<?>[] { int.class }; expectedException.expect(IllegalArgumentException.class); expectedException .expectMessage("Parameter number 0 is of type 'int' but argument given is '1' of type 'String'"); underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldThrowIllegalArgumentExceptionIfAnyArgumentTypeIsNotAssignableToParameterType() { Object[] arguments = new Object[] { 2, "2", 2l }; Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, boolean.class }; expectedException.expect(IllegalArgumentException.class); expectedException .expectMessage("Parameter number 2 is of type 'boolean' but argument given is '2' of type 'Long'"); underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldNotThrowExceptionIfSingleArgumentTypeEqualsParameterType() { Object[] arguments = new Object[] { Character.valueOf('a') }; Class<?>[] parameterTypes = new Class<?>[] { Character.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldNotThrowExceptionIfEveryArgumentTypeEqualsParameterTypesExactly() { Object[] arguments = new Object[] { "b", Boolean.FALSE }; Class<?>[] parameterTypes = new Class<?>[] { String.class, Boolean.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldNotThrowExceptionIfArgumentsIsAreAssignableToParameterTypes() { Object[] arguments = new Object[] { Long.valueOf(1l), Integer.valueOf(2) }; Class<?>[] parameterTypes = new Class<?>[] { Number.class, Number.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldNotThrowExceptionIfArgumentsAreWrappedPrimitivesOfParameterTypes() { Object[] arguments = new Object[] { Boolean.FALSE, Character.valueOf('a'), Byte.valueOf((byte) 2), Short.valueOf((short) 3), Integer.valueOf(4), Long.valueOf(5l), Float.valueOf(6.6f), Double.valueOf(7.7) }; Class<?>[] parameterTypes = new Class<?>[] { boolean.class, char.class, byte.class, short.class, int.class, long.class, float.class, double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldBeAwareOfWideningPrimitiveConversionsOfByte() { Object[] arguments = new Object[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 }; Class<?>[] parameterTypes = new Class<?>[] { short.class, int.class, long.class, float.class, double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldBeAwareOfWideningPrimitiveConversionsOfShort() { Object[] arguments = new Object[] { (short) 1, (short) 2, (short) 3, (short) 4 }; Class<?>[] parameterTypes = new Class<?>[] { int.class, long.class, float.class, double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldBeAwareOfWideningPrimitiveConversionsOfChar() { Object[] arguments = new Object[] { (char) 1, (char) 2, (char) 3, (char) 4 }; Class<?>[] parameterTypes = new Class<?>[] { int.class, long.class, float.class, double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldBeAwareOfWideningPrimitiveConversionsOfInt() { Object[] arguments = new Object[] { 1, 2, 3 }; Class<?>[] parameterTypes = new Class<?>[] { long.class, float.class, double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldBeAwareOfWideningPrimitiveConversionsOfFloat() { Object[] arguments = new Object[] { 1.1f }; Class<?>[] parameterTypes = new Class<?>[] { double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldBeAwareOfWideningPrimitiveConversionsOfLong() { Object[] arguments = new Object[] { 1l, 2l }; Class<?>[] parameterTypes = new Class<?>[] { float.class, double.class }; underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }
@Test public void testCheckIfArgumentsMatchParameterTypesShouldThrowExceptionForNonWideningConversionsOfLong() { Object[] arguments = new Object[] { (long) 1 }; Class<?>[] parameterTypes = new Class<?>[] { int.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Parameter number 0 is of type 'int' but argument given is '1' of type 'Long'"); underTest.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); }