下面列出了怎么用org.junit.jupiter.params.converter.ArgumentConversionException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object convert(Object input, ParameterContext parameterContext)
throws ArgumentConversionException {
if (null == input) {
return null;
}
if (!(input instanceof String)) {
throw new ArgumentConversionException(input + " is not a string");
}
var inputString = (String) input;
if (0 == inputString.length()) {
return Key.newBuilder().build();
} else {
return Key.newBuilder().setEd25519(ByteString.copyFrom(inputString.getBytes())).build();
}
}
/**
* Convert the supplied {@code source} object according to the supplied
* {@code context}.
*
* @param source the source object to convert; may be {@code null}
* @param context the parameter context where the converted object will be
* used; never {@code null}
* @return the converted object; may be {@code null} but only if the target
* type is a reference type
* @throws ArgumentConversionException if an error occurs during the
* conversion
*/
@Override
public Object convert(Object source, ParameterContext context) {
if (!(source instanceof JsonObject)) {
throw new ArgumentConversionException("Not a JsonObject");
}
JsonObject json = (JsonObject) source;
String name = context.getParameter().getName();
Class<?> type = context.getParameter().getType();
if (type == String.class) {
return json.getString(name);
} else if (type == int.class) {
return json.getInt(name);
} else if (type == boolean.class) {
return json.getBoolean(name);
}
throw new ArgumentConversionException("Can't convert to type: '" + type.getName() + "'");
}
@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;
}
@Override
public Object convert(Object source, ParameterContext context) throws ArgumentConversionException {
if (!(source instanceof String))
throw new IllegalArgumentException("The argument should be a string: " + source);
try {
String[] parts = ((String) source).split("/");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);
return LocalDate.of(year, month, day);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to convert", e);
}
}
@Override
public Object convert(Object input, ParameterContext parameterContext)
throws ArgumentConversionException {
if (null == input) {
return null;
}
if (!(input instanceof String)) {
throw new ArgumentConversionException(input + " is not a string");
}
var parts = ((String) input).split("\\.");
return TopicID.newBuilder().setShardNum(Long.parseLong(parts[0])).setRealmNum(Long.parseLong(parts[1]))
.setTopicNum(Long.parseLong(parts[2])).build();
}
@Override
public Object convert(Object input, ParameterContext parameterContext)
throws ArgumentConversionException {
if (null == input) {
return null;
}
if (!(input instanceof String)) {
throw new ArgumentConversionException(input + " is not a string");
}
return Instant.parse((String) input);
}
@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");
}
@Override
public Object convert(Object source, ParameterContext context) throws ArgumentConversionException {
if (source instanceof String)
return new NoFactory(Integer.parseInt((String) source));
throw new ArgumentConversionException(source + " could not be converted.");
}