StringConverter extends com.tngtech.junit.dataprovider.convert.StringConverter { public Object[] convert(String data, boolean isVarargs, Class<?>[] parameterTypes, DataProvider dataProvider, int rowIdx) { ConverterContext context = new ConverterContext(dataProvider.splitBy(), dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); return super.convert(data, isVarargs, parameterTypes, context, rowIdx); }  Object[] convert(String data, boolean isVarargs, Class<?>[] parameterTypes, DataProvider dataProvider,
            int rowIdx);  }
@Test public void testConvertShouldReturnNullObjectWrappedInObjectArray() { String data = null; Class<?>[] parameterTypes = new Class<?>[] { Integer.class }; Object[] result = underTest.convert(data, false, parameterTypes, dataProvider, 1); assertThat(result).isEqualTo(new Object[] { null }); }
@Test public void testConvertShouldNotTrimNonBreakingSpaceEvenIfSettingsTrimIsTrue() { String data = "\u00A0test\u00A0"; Class<?>[] parameterTypes = new Class<?>[] { String.class }; when(dataProvider.trimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, dataProvider, 21); assertThat(result).containsExactly("\u00A0test\u00A0"); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionIfCharHasNotLengthOne() { String data = "noChar"; Class<?>[] parameterTypes = new Class<?>[] { char.class }; underTest.convert(data, false, parameterTypes, dataProvider, 40); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionIfValueOfThrowsNumberFormatException() { String data = "noInt"; Class<?>[] parameterTypes = new Class<?>[] { int.class }; underTest.convert(data, false, parameterTypes, dataProvider, 41); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionForTargetTypeConstructorWithStringArgWhichThrowsException() { String data = "noInt"; Class<?>[] parameterTypes = new Class<?>[] { BigInteger.class }; underTest.convert(data, false, parameterTypes, dataProvider, 42); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionForUnsupportedTargetType() { String data = "noObject"; Class<?>[] parameterTypes = new Class<?>[] { Object.class }; underTest.convert(data, false, parameterTypes, dataProvider, 43); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionIfEnumValuesIsInvalid() { String data = "Val1"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class }; underTest.convert(data, false, parameterTypes, dataProvider, 51); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionIfEnumValueIsInvalid() { String data = "UNKNOW_ENUM_VALUE"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class }; underTest.convert(data, false, parameterTypes, dataProvider, 51); }
@Test(expected = IllegalArgumentException.class) public void testConvertShouldThrowIllegalArgumentExceptionIfClassNameIsInvalid() { String data = "String"; Class<?>[] parameterTypes = new Class<?>[] { Class.class }; underTest.convert(data, false, parameterTypes, dataProvider, 55); }
@Test public void testConvertShouldCallCustomConvertAndReturnValueIfNotObjectNoConversion() { String data = "2016-03-11"; Class<?>[] parameterTypes = new Class<?>[] { Date.class }; StringConverter underTest = new StringConverter() { @Override protected Object customConvertValue(String str, Class<?> targetType, DataProvider dataProvider) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(str); } catch (ParseException e) { fail("Unexpected exception: " + e); return null; } } }; Object[] result = underTest.convert(data, false, parameterTypes, dataProvider, 76); GregorianCalendar expectedDate = new GregorianCalendar(); expectedDate.set(2016, Calendar.MARCH, 11, 0, 0, 0); expectedDate.set(Calendar.MILLISECOND, 0); assertThat(result).containsExactly(expectedDate.getTime()); }
@Test public void testConvertShouldCorrectlyUseConstructorWithSingleStringArgForBigInteger() { String data = "1"; Class<?>[] parameterTypes = new Class<?>[] { BigInteger.class }; Object[] result = underTest.convert(data, false, parameterTypes, dataProvider, 80); assertThat(result).containsExactly(BigInteger.ONE); }
@Test public void testConvertShouldCorrectlyUseConstructorWithSingleStringArgForFile() { String data = "home/schmida"; Class<?>[] parameterTypes = new Class<?>[] { File.class }; Object[] result = underTest.convert(data, false, parameterTypes, dataProvider, 80); assertThat(result).containsExactly(new File("home/schmida")); }
@Test public void testConvertShouldCreateEmptyVarargsArrayForMissingOnlyVarargsArgument() { String data = ""; Class<?>[] parameterTypes = new Class<?>[] { int[].class }; Object[] result = underTest.convert(data, true, parameterTypes, dataProvider, 90); assertThat(result).containsExactly(new int[0]); }
@Test public void testConvertShouldNotSplitIfSingleNonVarargArgumentIsRequired() { String data = "VAL1"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class }; Object[] result = underTest.convert(data, false, parameterTypes, dataProvider, 100); assertThat(result).containsExactly(TestEnum.VAL1); }