@VisibleForTesting Class<?> convert(String input, TypeToken<? extends Class<?>> targetType) throws ArgumentConversionException { Class<?> inputType; try { inputType = Class.forName(input); } catch (ClassNotFoundException e) { throw new ArgumentConversionException("Could not convert: " + input, e); } TypeToken<? extends Class<?>> inputClassType = asClassType(inputType); if (!targetType.isSupertypeOf(inputClassType)) { throw new ArgumentConversionException( String.format("%s is not assignable to %s", inputClassType, targetType)); } return inputType; }
@SuppressWarnings("unchecked") @Override public Object convert(Object input, ParameterContext context) throws ArgumentConversionException { TypeToken<?> parameterType = TypeToken.of(context.getParameter().getParameterizedType()); if (parameterType.getRawType() != Class.class) { throw new ArgumentConversionException( String.format("Could not convert: %s. Invalid parameter type.", input)); } return convert(input.toString(), (TypeToken<? extends Class<?>>) parameterType); }
@ExpectFailure({ @Cause(type = ParameterResolutionException.class), @Cause(type = ArgumentConversionException.class), @Cause(type = ClassNotFoundException.class) }) @ParameterizedTest @ValueSource(strings = "123ClassDoesNotExist") void classNotFound(@ConvertWith(ClassArgumentConverter.class) Class<?> clazz) {}
@ExpectFailure({ @Cause(type = ParameterResolutionException.class), @Cause(type = ArgumentConversionException.class, message = "Invalid parameter type") }) @ParameterizedTest @ValueSource(strings = "java.lang.Object") void badParameterType(@ConvertWith(ClassArgumentConverter.class) String clazz) {}
@ExpectFailure({ @Cause(type = ParameterResolutionException.class), @Cause( type = ArgumentConversionException.class, message = "java.lang.Class<java.util.List> is not assignable to" + " java.lang.Class<java.util.Collection<?>>" ) }) @ParameterizedTest @ValueSource(strings = "java.util.List") void wrongClass(@ConvertWith(ClassArgumentConverter.class) Class<Collection<?>> clazz) {}
@ExpectFailure({ @Cause(type = ParameterResolutionException.class), @Cause(type = ArgumentConversionException.class, message = "is not assignable to") }) @ParameterizedTest @ValueSource(strings = "java.util.List") void badLowerBound( @ConvertWith(ClassArgumentConverter.class) Class<? super Collection<?>> clazz) {}
@ExpectFailure({ @Cause(type = ParameterResolutionException.class), @Cause(type = ArgumentConversionException.class, message = "is not assignable to") }) @ParameterizedTest @ValueSource(strings = "java.lang.Object") void badUpperBound( @ConvertWith(ClassArgumentConverter.class) Class<? extends Collection<?>> clazz) {}
@Test void classDoesNotExist() { ArgumentConversionException e = assertThrows(ArgumentConversionException.class, () -> converter.convert("doesnotexist", THROWABLE)); assertThat(e).hasMessageThat().contains("Could not convert: doesnotexist"); assertThat(e).hasCauseThat().isInstanceOf(ClassNotFoundException.class); }
@Override public Object convert(Object input, ParameterContext parameterContext) throws ArgumentConversionException { if (input instanceof Point) return input; if (input instanceof String) try { return Point.from((String) input); } catch (NumberFormatException ex) { String message = input + " is no correct string representation of a point."; throw new ArgumentConversionException(message, ex); } throw new ArgumentConversionException(input + " is no valid point"); }
@Override public Object convert(Object source, ParameterContext context) throws ArgumentConversionException { try { return JavaVersion.class.getField(String.valueOf(source)).get(null); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new ArgumentConversionException(e.toString()); } }
void testIncompatible(String input, TypeToken<? extends Class<?>> type) { ArgumentConversionException e = assertThrows(ArgumentConversionException.class, () -> converter.convert(input, type)); assertThat(e).hasMessageThat().contains("is not assignable to"); }