StringConverter { public Object[] convert(String data, boolean isVarargs, Class<?>[] parameterTypes, ConverterContext context, int rowIdx) { if (data == null) { return new Object[] { null }; } if (parameterTypes.length == 1) { if (isVarargs) { if (data.isEmpty()) { return new Object[] { Array.newInstance(parameterTypes[0].getComponentType(), 0) }; } } else { return new Object[] { convertValue(data, parameterTypes[0], context) }; } } String[] splitData = splitBy(data, context.getSplitBy()); checkArgumentsAndParameterCount(splitData.length, parameterTypes.length, isVarargs, rowIdx); return convert(splitData, isVarargs, parameterTypes, context); }  Object[] convert(String data, boolean isVarargs, Class<?>[] parameterTypes, ConverterContext context, int rowIdx);  }
@Test public void testConvertShouldReturnNullObjectWrappedInObjectArray() { String data = null; Class<?>[] parameterTypes = new Class<?>[] { Integer.class }; Object[] result = underTest.convert(data, false, parameterTypes, context, 1); assertThat(result).isEqualTo(new Object[] { null }); }
@Test public void testConvertShouldThrowExceptionIfNumberOfArgumentsIsGreaterThanNumberOfParameterTypesOnNonVarargMethod() { String data = "1, 2, 3"; Class<?>[] parameterTypes = new Class<?>[] { String.class, int.class }; when(context.getSplitBy()).thenReturn(","); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Test method has 2 parameters but got 3 arguments in row 2"); underTest.convert(data, false, parameterTypes, context, 2); }
@Test public void testConvertShouldReturnConvertedDataIfNumberOfArgumentsIsSmallerThanNumberOfParameterTypes() { String data = ""; Class<?>[] parameterTypes = new Class<?>[] { String.class, int.class }; when(context.getSplitBy()).thenReturn(","); Object[] result = underTest.convert(data, false, parameterTypes, context, 2); assertThat(result).containsExactly(data); }
@Test public void testConvertShouldThrowExceptionIfVarargsAndNumberOfArgumentsIsTwoLessComparedToNumberOfParameterTypes() { String data = ""; Class<?>[] parameterTypes = new Class<?>[] { long.class, boolean.class, int[].class }; when(context.getSplitBy()).thenReturn(","); expectedException.expect(IllegalArgumentException.class); expectedException .expectMessage("Varargs test method has 3 parameters but got only 1 arguments in row 3"); underTest.convert(data, true, parameterTypes, context, 3); }
@Test public void testConvertShouldCorrectlyParseAllPrimitiveTypes() { String data = "true,1,c,2,3,4,5.5,6.6"; Class<?>[] parameterTypes = new Class<?>[] { boolean.class, byte.class, char.class, short.class, int.class, long.class, float.class, double.class }; when(context.getSplitBy()).thenReturn(","); Object[] result = underTest.convert(data, false, parameterTypes, context, 10); assertThat(result).containsExactly(true, (byte) 1, 'c', (short) 2, 3, 4L, 5.5f, 6.6d); }
@Test public void testConvertShouldCorrectlyParseAllPrimitiveTypesAsJavaString() { String data = "-5;2014l;-1.234567f;-901e-3"; Class<?>[] parameterTypes = new Class<?>[] { int.class, long.class, float.class, double.class }; when(context.getSplitBy()).thenReturn(";"); Object[] result = underTest.convert(data, false, parameterTypes, context, 11); assertThat(result).containsExactly(-5, 2014l, -1.234567f, -0.901d); }
@Test public void testConvertShouldNotTrimValuesIfSettingsTrimIsFalse() { String data = " foo| bar |baz "; Class<?>[] parameterTypes = new Class<?>[] { String.class, String.class, String.class }; when(context.getSplitBy()).thenReturn("\\|"); Object[] result = underTest.convert(data, false, parameterTypes, context, 12); assertThat(result).containsExactly(" foo", " bar ", "baz "); }
@Test public void testConvertShouldTrimAndParseAllPrimitiveTypesIfSettingsTrimIsTrue() { String data = " false ; 11 ; z ; 22 ; 33 ;44 ; 55.55 ; 66.66 "; Class<?>[] parameterTypes = new Class<?>[] { boolean.class, byte.class, char.class, short.class, int.class, long.class, float.class, double.class }; when(context.getSplitBy()).thenReturn(";"); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 13); assertThat(result).containsExactly(false, (byte) 11, 'z', (short) 22, 33, 44L, 55.55f, 66.66d); }
@Test public void testConvertShouldTrimNonSpaceWhitespaceCharsIfSettingsTrimIsTrue() { String data = "\n-1f\n,\r-2\r,\t3.0d\t"; Class<?>[] parameterTypes = new Class<?>[] { float.class, int.class, double.class }; when(context.getSplitBy()).thenReturn(","); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 20); assertThat(result).containsExactly(-1f, -2, 3d); }
@Test public void testConvertShouldNotTrimNonBreakingSpaceEvenIfSettingsTrimIsTrue() { String data = "\u00A0test\u00A0"; Class<?>[] parameterTypes = new Class<?>[] { String.class }; when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 21); assertThat(result).containsExactly("\u00A0test\u00A0"); }
@Test public void testConvertShouldCorrectlyHandleLeadingEmptyString() { String data = "/true"; Class<?>[] parameterTypes = new Class<?>[] { String.class, boolean.class }; when(context.getSplitBy()).thenReturn("/"); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 30); assertThat(result).containsExactly("", true); }
@Test public void testConvertShouldCorrectlyHandleTrailingEmptyString() { String data = "1 "; Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class }; when(context.getSplitBy()).thenReturn(" "); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 31); assertThat(result).containsExactly(1, ""); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionIfCharHasNotLengthOne() { String data = "noChar"; Class<?>[] parameterTypes = new Class<?>[] { char.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("'noChar' cannot be converted to type 'char'"); underTest.convert(data, false, parameterTypes, context, 40); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionIfValueOfThrowsNumberFormatException() { String data = "noInt"; Class<?>[] parameterTypes = new Class<?>[] { int.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Cannot convert 'noInt' to type 'int'"); underTest.convert(data, false, parameterTypes, context, 41); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionForTargetTypeConstructorWithStringArgWhichThrowsException() { String data = "noInt"; Class<?>[] parameterTypes = new Class<?>[] { BigInteger.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Tried to invoke 'public java.math.BigInteger(java.lang.String)' for argument 'noInt'. Exception was: null"); underTest.convert(data, false, parameterTypes, context, 42); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionForUnsupportedTargetType() { String data = "noObject"; Class<?>[] parameterTypes = new Class<?>[] { Object.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Type 'Object' is not supported as parameter type of test methods. Supported types are primitive types and their wrappers, 'Enum' values, 'String's, and types having a single 'String' parameter constructor"); underTest.convert(data, false, parameterTypes, context, 43); }
@Test public void testConvertShouldCorrectlyParseEnum() { String data = " VAL1, VAL2 "; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class, TestEnum.class }; when(context.getSplitBy()).thenReturn(","); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 50); assertThat(result).containsExactly(TestEnum.VAL1, TestEnum.VAL2); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionIfEnumValuesIsInvalid() { String data = "Val1"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "'Val1' is not a valid value of enum 'TestEnum'. Please be aware of case sensitivity or use 'ignoreEnumCase'"); underTest.convert(data, false, parameterTypes, context, 51); }
@Test public void testConvertShouldCorrectlyParseEnumIgnoringCase() { String data = "Val1,val2"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class, TestEnum.class }; when(context.getSplitBy()).thenReturn(","); when(context.isIgnoreEnumCase()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 50); assertThat(result).containsExactly(TestEnum.VAL1, TestEnum.VAL2); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionIfEnumValueIsInvalid() { String data = "UNKNOW_ENUM_VALUE"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "'UNKNOW_ENUM_VALUE' is not a valid value of enum 'TestEnum'. Please be aware of case sensitivity or use 'ignoreEnumCase'."); underTest.convert(data, false, parameterTypes, context, 51); }
@Test public void testConvertShouldCorrectlyParseClass() { String data = " java.lang.Thread, com.tngtech.junit.dataprovider.DataProviders "; Class<?>[] parameterTypes = new Class<?>[] { Class.class, Class.class }; when(context.getSplitBy()).thenReturn(","); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 50); assertThat(result).containsExactly(Thread.class, DataProviders.class); }
@Test public void testConvertShouldThrowIllegalArgumentExceptionIfClassNameIsInvalid() { String data = "String"; Class<?>[] parameterTypes = new Class<?>[] { Class.class }; expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Unable to instantiate 'Class' for 'String'"); underTest.convert(data, false, parameterTypes, context, 55); }
@Test public void testConvertShouldCorrectlyParseAllPrimitiveWrapperTypes() { String data = "true,1,c,2,3,4,5.5,6.6"; Class<?>[] parameterTypes = new Class<?>[] { Boolean.class, Byte.class, Character.class, Short.class, Integer.class, Long.class, Float.class, Double.class }; when(context.getSplitBy()).thenReturn(","); Object[] result = underTest.convert(data, false, parameterTypes, context, 60); assertThat(result).containsExactly( Boolean.TRUE, Byte.valueOf((byte) 1), Character.valueOf('c'), Short.valueOf((short) 2), Integer.valueOf(3), Long.valueOf(4L), Float.valueOf(5.5f), Double.valueOf(6.6d)); }
@Test public void testConvertShouldParseNullValuesAsStringIfSettingsConvertNullsIsFalse() { String data = "null#null"; Class<?>[] parameterTypes = new Class<?>[] { String.class, String.class }; when(context.getSplitBy()).thenReturn("#"); Object[] result = underTest.convert(data, false, parameterTypes, context, 70); assertThat(result).containsExactly("null", "null"); }
@Test public void testConvertShouldParseNullValuesAsNullObjectIfSettingsConvertNullsIsTrue() { String data = "null,null,foo"; Class<?>[] parameterTypes = new Class<?>[] { String.class, String.class, String.class }; when(context.getSplitBy()).thenReturn(","); when(context.isConvertNulls()).thenReturn(true); Object[] result = underTest.convert(data, false, parameterTypes, context, 71); assertThat(result).containsExactly(null, null, "foo"); }
@Test public void testConvertShouldCallCustomConvertAndNotReturnValueIfObjectNoConversion() { String data = "2016-03-11"; Class<?>[] parameterTypes = new Class<?>[] { Date.class }; StringConverter underTest = new StringConverter() { @Override protected Object customConvertValue(String str, Class<?> targetType, ConverterContext dataProvider) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(str); } catch (ParseException e) { throw new UndeclaredThrowableException(e); } } }; Object[] result = underTest.convert(data, false, parameterTypes, context, 75); assertThat(result).hasSize(1); assertThat(result[0]).isInstanceOf(Date.class).isEqualTo(date(2016, Calendar.MARCH, 11)); }
@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, ConverterContext 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, context, 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, context, 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, context, 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, context, 90); assertThat(result).containsExactly(new int[0]); }
@Test public void testConvertShouldCreateEmptyVarargsArrayForLastMissingVarargsArgument() { String data = "test"; Class<?>[] parameterTypes = new Class<?>[] { String.class, int[].class }; when(context.getSplitBy()).thenReturn(","); Object[] result = underTest.convert(data, true, parameterTypes, context, 91); assertThat(result).containsExactly("test", new int[0]); }
@Test public void testConvertShouldCreateVarargsArrayForOneOnlyVarargsArguments() { String data = "1.0"; Class<?>[] parameterTypes = new Class<?>[] { double[].class }; when(context.getSplitBy()).thenReturn(","); Object[] result = underTest.convert(data, true, parameterTypes, context, 92); assertThat(result).containsExactly(new double[] { 1.0 }); }
@Test public void testConvertShouldCreateVarargsArrayForOneLastVarargsArguments() { String data = "a,2,1.0,null"; Class<?>[] parameterTypes = new Class<?>[] { char.class, byte.class, Double[].class }; when(context.getSplitBy()).thenReturn(","); when(context.isConvertNulls()).thenReturn(true); Object[] result = underTest.convert(data, true, parameterTypes, context, 93); assertThat(result).containsExactly('a', (byte) 2, new Double[] { 1.0, null }); }
@Test public void testConvertShouldCreateVarargsArrayForMultipleOnlyVarargsArguments() { String data = "1, 2, 3"; Class<?>[] parameterTypes = new Class<?>[] { long[].class }; when(context.getSplitBy()).thenReturn(","); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, true, parameterTypes, context, 94); assertThat(result).containsExactly(new long[] { 1, 2, 3 }); }
@Test public void testConvertShouldCreateVarargsArrayForMultipleLastVarargsArguments() { String data = "foobar, 1, 2, 3"; Class<?>[] parameterTypes = new Class<?>[] { String.class, long[].class }; when(context.getSplitBy()).thenReturn(","); when(context.isTrimValues()).thenReturn(true); Object[] result = underTest.convert(data, true, parameterTypes, context, 95); assertThat(result).containsExactly("foobar", new long[] { 1, 2, 3 }); }
@Test public void testConvertShouldNotSplitIfSingleNonVarargArgumentIsRequired() { String data = "VAL1"; Class<?>[] parameterTypes = new Class<?>[] { TestEnum.class }; Object[] result = underTest.convert(data, false, parameterTypes, context, 100); assertThat(result).containsExactly(TestEnum.VAL1); }