@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<?>> inputCla@R_502_6455@ype = asCla@R_502_6455@ype(inputType);
if (!targettype.isSupertypeOf(inputCla@R_502_6455@ype)) {
throw new ArgumentConversionException(
String.format("%s is not assignable to %s",inputCla@R_502_6455@ype,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 = "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) {}
@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);
}
项目:demo-junit-5
文件:CustomArgumentConverterTest.java
@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");
}
项目:keystore-explorer
文件:JavaVersionTest.java
@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");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。