DataConverter { public List<Object[]> convert(Object data, boolean isVarargs, Class<?>[] parameterTypes, ConverterContext context) { checkNotNull(parameterTypes, "'parameterTypes' must not be null"); checkArgument(parameterTypes.length != 0, "'parameterTypes' must not be empty"); if (data instanceof Object[][]) { return convert((Object[][]) data, isVarargs, parameterTypes, context); } else if (data instanceof String[]) { checkState(context != null, "'context' must not be null for 'String[]' data"); return convert((String[]) data, isVarargs, parameterTypes, context); } else if (data instanceof Object[]) { return convert((Object[]) data, isVarargs, parameterTypes, context); } else if (data instanceof Iterable) { @SuppressWarnings("rawtypes") Iterable iterableData = (Iterable) data; return convert(iterableData, isVarargs, parameterTypes, context); } throw new ClassCastException( String.format("Cannot cast to either Object[][], Object[], String[], or Iterable because data was: %s", data)); }  List<Object[]> convert(Object data, boolean isVarargs, Class<?>[] parameterTypes, ConverterContext context);  }
@Test public void testConvertShouldThrowNullPointerExceptionIfParameterTypesIsNull() { Object data = null; Class<?>[] parameterTypes = null; expectedException.expect(NullPointerException.class); expectedException.expectMessage("'parameterTypes' must not be null"); underTest.convert(data, false, parameterTypes, context); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionIfParameterTypesIsEmpty() { Object data = null; Class<?>[] parameterTypes = new Class<?>[0]; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("'parameterTypes' must not be empty"); underTest.convert(data, false, parameterTypes, context); }
@Test public void testConvertShouldThrowIllegalStateExceptionIfConverterContextIsNullForString() { String[] data = { "" }; Class<?>[] parameterTypes = new Class<?>[] { Object.class }; expectedException.expect(IllegalStateException.class); expectedException.expectMessage("'context' must not be null for 'String[]' data"); underTest.convert(data, false, parameterTypes, null); }
@Test public void testConvertShouldThrowClassCastExceptionIfDataIsNull() { Object data = null; Class<?>[] parameterTypes = new Class<?>[] { String.class }; expectedException.expect(ClassCastException.class); expectedException.expectMessage( "Cannot cast to either Object[][], Object[], String[], or Iterable because data was: null"); underTest.convert(data, false, parameterTypes, context); }
@Test public void testConvertShouldThrowClassCastExceptionIfDataIsNotConvertable() { Object data = "not convertable"; Class<?>[] parameterTypes = new Class<?>[] { Integer.class }; expectedException.expect(ClassCastException.class); expectedException.expectMessage( "Cannot cast to either Object[][], Object[], String[], or Iterable because data was: " + data); underTest.convert(data, false, parameterTypes, context); }
@Test public void testConvertShouldCallObjectArrayConverterOnlyOnceForObjectArrayArrayWithOneElement() { Object[][] data = new Object[][] { { 1 } }; Class<?>[] parameterTypes = new Class<?>[] { int.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, context); assertThat(result).hasSize(1); verify(objectArrayConverter).convert(data[0], false, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldCallObjectArrayConverterMultipleTimesForObjectArrayArrayWithMultipleElements() { Object[][] data = new Object[][] { { "11", 22L, 3.3 }, { "44", 55L, 6.6 }, { "77", 88L, 9.9 } }; Class<?>[] parameterTypes = new Class<?>[] { String.class, long.class, double.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, context); assertThat(result).hasSize(3); InOrder inOrder = inOrder(objectArrayConverter, singleArgConverter, stringConverter); inOrder.verify(objectArrayConverter).convert(data[0], false, parameterTypes); inOrder.verify(objectArrayConverter).convert(data[1], false, parameterTypes); inOrder.verify(objectArrayConverter).convert(data[2], false, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldCallSingleArgConverterOnlyOnceForObjectArrayWithSingleElement() { Object[] data = new Object[] { 88.99 }; Class<?>[] parameterTypes = new Class<?>[] { Object.class }; List<Object[]> result = underTest.convert(data, true, parameterTypes, context); assertThat(result).hasSize(1); verify(singleArgConverter).convert(data[0], true, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldCallSingleArgConverterMultipleTimesForObjectArrayWithMultipleElements() { Object[] data = new Object[] { "12", 34L, 5.6 }; Class<?>[] parameterTypes = new Class<?>[] { Object.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, context); assertThat(result).hasSize(3); InOrder inOrder = inOrder(objectArrayConverter, singleArgConverter, stringConverter); inOrder.verify(singleArgConverter).convert(data[0], false, parameterTypes); inOrder.verify(singleArgConverter).convert(data[1], false, parameterTypes); inOrder.verify(singleArgConverter).convert(data[2], false, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldCallSingleArgConverterOnlyOnceForListOfObjectWithSingleElement() { List<Object> data = Collections.<Object>singletonList(88); Class<?>[] parameterTypes = new Class<?>[] { Object.class }; List<Object[]> result = underTest.convert(data, true, parameterTypes, context); assertThat(result).hasSize(1); verify(singleArgConverter).convert(data.get(0), true, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldCallSingleArgConverterMultipleTimesForListOfObjectWithMultipleElements() { List<Object> data = Arrays.<Object>asList("12", 34L, 5.6); Class<?>[] parameterTypes = new Class<?>[] { Object.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, context); assertThat(result).hasSize(3); InOrder inOrder = inOrder(objectArrayConverter, singleArgConverter, stringConverter); inOrder.verify(singleArgConverter).convert(data.get(0), false, parameterTypes); inOrder.verify(singleArgConverter).convert(data.get(1), false, parameterTypes); inOrder.verify(singleArgConverter).convert(data.get(2), false, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldCallStringConverterOnlyOnceForStringArrayWithOneElement() { String[] data = new String[] { "foo,true" }; Class<?>[] parameterTypes = new Class<?>[] { String.class, boolean.class }; List<Object[]> result = underTest.convert(data, true, parameterTypes, context); assertThat(result).hasSize(1); verify(stringConverter).convert(data[0], true, parameterTypes, context, 0); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertCallStringConverterMultipleTimesForStringArrayWithMultipleElements() { String[] data = new String[] { "1, 2, 3, 4.0, e", "6, 7, 8, 9.0, i" }; Class<?>[] parameterTypes = new Class<?>[] { byte.class, int.class, long.class, double.class, char.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, context); assertThat(result).hasSize(2); InOrder inOrder = inOrder(objectArrayConverter, singleArgConverter, stringConverter); inOrder.verify(stringConverter).convert(data[0], false, parameterTypes, context, 0); inOrder.verify(stringConverter).convert(data[1], false, parameterTypes, context, 1); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }