DataConverter extends com.tngtech.junit.dataprovider.convert.DataConverter { public List<Object[]> convert(Object data, boolean isVarargs, Class<?>[] parameterTypes, DataProvider dataProvider) { checkNotNull(dataProvider, "dataProvider must not be null"); ConverterContext context = new ConverterContext(objectArrayConverter, singleArgConverter, stringConverter, dataProvider.splitBy(), dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); return super.convert(data, isVarargs, parameterTypes, context); } DataConverter(); boolean canConvert(Type type); List<Object[]> convert(Object data, boolean isVarargs, Class<?>[] parameterTypes, DataProvider dataProvider); void setObjectArrayConverter(ObjectArrayConverter objectArrayConverter); void setSingleArgConverter(SingleArgConverter singleArgConverter); void setStringConverter(StringConverter stringConverter);  }
@Test(expected = NullPointerException.class) public void testConvertShouldThrowNullPointerExceptionIfParameterTypesIsNull() { Object data = null; Class<?>[] parameterTypes = null; underTest.convert(data, false, parameterTypes, dataProvider); }
@Test(expected = NullPointerException.class) public void testConvertShouldThrowNullPointerExceptionIfDataProviderIsNull() { Object data = null; Class<?>[] parameterTypes = new Class<?>[] { Object.class }; underTest.convert(data, false, parameterTypes, (DataProvider) null); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionIfParameterTypesIsEmpty() { Object data = null; Class<?>[] parameterTypes = new Class<?>[0]; underTest.convert(data, false, parameterTypes, dataProvider); }
@Test(expected = ClassCastException.class) public void testConvertShouldThrowClassCastExceptionIfDataIsNull() { Object data = null; Class<?>[] parameterTypes = new Class<?>[] { String.class }; underTest.convert(data, false, parameterTypes, dataProvider); }
@Test(expected = ClassCastException.class) public void testConvertShouldThrowClassCastExceptionIfDataIsNotConvertable() { Object data = "not convertable"; Class<?>[] parameterTypes = new Class<?>[] { Integer.class }; underTest.convert(data, false, parameterTypes, dataProvider); }
@Test public void testConvertShouldCallObjectArrayConverterOnlyOnceForObjectArrayArrayWithOneElement() { Object[][] data = new Object[][] { { 1 } }; Class<?>[] parameterTypes = new Class<?>[] { int.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, dataProvider); 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, dataProvider); 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 testConvertShouldCallObjectArrayConverterOnlyOnceForListOfListOfObjectWithOneElement() { @SuppressWarnings("unchecked") List<List<Character>> data = singletonList(singletonList('a')); Class<?>[] parameterTypes = new Class<?>[] { char.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, dataProvider); assertThat(result).hasSize(1); verify(objectArrayConverter).convert(data.get(0).toArray(), false, parameterTypes); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }
@Test public void testConvertShouldReturnMultipleElementsForListOfListOfObjectWithMultipleElements() { @SuppressWarnings("unchecked") List<List<?>> data = asList(Arrays.<Object>asList('x', "foo"), asList('y', "bar"), asList('z', "baz")); Class<?>[] parameterTypes = new Class<?>[] { char.class, String.class }; List<Object[]> result = underTest.convert(data, false, parameterTypes, dataProvider); assertThat(result).hasSize(3); InOrder inOrder = inOrder(objectArrayConverter, singleArgConverter, stringConverter); inOrder.verify(objectArrayConverter).convert(data.get(0).toArray(), false, parameterTypes); inOrder.verify(objectArrayConverter).convert(data.get(1).toArray(), false, parameterTypes); inOrder.verify(objectArrayConverter).convert(data.get(2).toArray(), 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, false, parameterTypes, dataProvider); assertThat(result).hasSize(1); verify(singleArgConverter).convert(data[0], false, 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, dataProvider); 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, false, parameterTypes, dataProvider); assertThat(result).hasSize(1); verify(singleArgConverter).convert(data.get(0), false, 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, dataProvider); 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, false, parameterTypes, dataProvider); assertThat(result).hasSize(1); verify(stringConverter).convert(eq(data[0]), eq(false), eq(parameterTypes), any(ConverterContext.class), eq(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, dataProvider); assertThat(result).hasSize(2); InOrder inOrder = inOrder(objectArrayConverter, singleArgConverter, stringConverter); inOrder.verify(stringConverter).convert(eq(data[0]), eq(false), eq(parameterTypes), any(ConverterContext.class), eq(0)); inOrder.verify(stringConverter).convert(eq(data[1]), eq(false), eq(parameterTypes), any(ConverterContext.class), eq(1)); verifyNoMoreInteractions(objectArrayConverter, singleArgConverter, stringConverter); }