org.junit.jupiter.params.converter.ArgumentConversionException Java Examples

The following examples show how to use org.junit.jupiter.params.converter.ArgumentConversionException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyConverter.java    From hedera-mirror-node with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #2
Source File: SlashyDateConverter.java    From tutorials with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #3
Source File: ClassArgumentConverter.java    From junit5-extensions with MIT License 6 votes vote down vote up
@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;
}
 
Example #4
Source File: JsonConverter.java    From junit-json-params with Apache License 2.0 6 votes vote down vote up
/**
 * 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() + "'");
}
 
Example #5
Source File: ClassArgumentConverter.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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);
}
 
Example #6
Source File: InstantConverter.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: UsageExampleTests.java    From junit5-extensions with MIT License 5 votes vote down vote up
@ExpectFailure({
    @Cause(type = ParameterResolutionException.class),
    @Cause(type = ArgumentConversionException.class),
    @Cause(type = ClassNotFoundException.class)
})
@ParameterizedTest
@ValueSource(strings = "123ClassDoesNotExist")
void classNotFound(@ConvertWith(ClassArgumentConverter.class) Class<?> clazz) {}
 
Example #8
Source File: UsageExampleTests.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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) {}
 
Example #9
Source File: UsageExampleTests.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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) {}
 
Example #10
Source File: UsageExampleTests.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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) {}
 
Example #11
Source File: UsageExampleTests.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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) {}
 
Example #12
Source File: ClassArgumentConverterTest.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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);
}
 
Example #13
Source File: CustomArgumentConverterTest.java    From demo-junit-5 with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
@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");
}
 
Example #14
Source File: JavaVersionTest.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
@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());
	}
}
 
Example #15
Source File: TopicIdConverter.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #16
Source File: ClassArgumentConverterTest.java    From junit5-extensions with MIT License 4 votes vote down vote up
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");
}
 
Example #17
Source File: ArgumentAggregatorTest.java    From demo-junit-5 with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
@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.");
}