@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { Object argument = arguments[parameterContext.getIndex()]; Parameter parameter = parameterContext.getParameter(); Optional<ConvertWith> annotation = AnnotationUtils.findAnnotation(parameter, ConvertWith.class); // @formatter:off ArgumentConverter argumentConverter = annotation.map(ConvertWith::value) .map(clazz -> (ArgumentConverter) ReflectionUtils.newInstance(clazz)) .map(converter -> AnnotationConsumerInitializer.initialize(parameter, converter)) .orElse(DefaultArgumentConverter.INSTANCE); // @formatter:on try { return argumentConverter.convert(argument, parameterContext); } catch (Exception ex) { throw new ParameterResolutionException("参数转换出错:" + parameter.getType().getName(), ex); } }
@ParameterizedTest @CsvSource({ "JRE_VERSION_9, JRE_VERSION_160, true", "JRE_VERSION_9, JRE_VERSION_170, true", "JRE_VERSION_9, JRE_VERSION_180, true", "JRE_VERSION_9, JRE_VERSION_9, true", "JRE_VERSION_180, JRE_VERSION_160, true", "JRE_VERSION_180, JRE_VERSION_170, true", "JRE_VERSION_180, JRE_VERSION_180, true", "JRE_VERSION_170, JRE_VERSION_160, true", "JRE_VERSION_170, JRE_VERSION_170, true", "JRE_VERSION_180, JRE_VERSION_9, false", "JRE_VERSION_170, JRE_VERSION_180, false", "JRE_VERSION_170, JRE_VERSION_9, false", }) public void isAtLeast(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version1, @ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version2, boolean result) { assertEquals(version1.isAtLeast(version2), result); }
@ParameterizedTest @CsvSource({ "JRE_VERSION_160, JRE_VERSION_170, true", "JRE_VERSION_170, JRE_VERSION_180, true", "JRE_VERSION_180, JRE_VERSION_9, true", "JRE_VERSION_9, JRE_VERSION_180, false", "JRE_VERSION_170, JRE_VERSION_160, false", "JRE_VERSION_170, JRE_VERSION_170, false", "JRE_VERSION_180, JRE_VERSION_160, false", "JRE_VERSION_180, JRE_VERSION_170, false", "JRE_VERSION_180, JRE_VERSION_180, false", }) public void isBelow(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version1, @ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion version2, boolean result) { assertEquals(version1.isBelow(version2), result); }
@ParameterizedTest @EnumSource(TimeUnit.class) void testWithExplicitArgumentConversion( @ConvertWith(CustomArgumentsConverter.class) String argument) { System.out.println("Argument " + argument + " is a type of " + argument.getClass()); assertNotNull(argument); }
@ParameterizedTest @ValueSource(strings = { "java.util.List", "java.util.Collection", "java.lang.Object", }) void anySuperclass( @ConvertWith(ClassArgumentConverter.class) Class<? super List<?>> superclassOfList) { assertThat(superclassOfList).isNotNull(); }
@ParameterizedTest @ValueSource(strings = { "java.util.List", "java.util.ArrayList", }) void anySubclass(@ConvertWith(ClassArgumentConverter.class) Class<? extends List<?>> anything) { assertThat(anything).isNotNull(); }
@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) {}
@ParameterizedTest @CsvSource({ "JRE_VERSION_160, 1.6.0", "JRE_VERSION_170, 1.7.0", "JRE_VERSION_180, 1.8.0", }) public void equals(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion verConst, String verStr) { assertEquals(verConst, new JavaVersion(verStr)); }
@ParameterizedTest @CsvSource({ "JRE_VERSION_160, 1.6.0_45, 0", "JRE_VERSION_170, 1.6.0_45, 1", "JRE_VERSION_170, 1.7.0_101, 0", "JRE_VERSION_180, 1.7.0_101, 1", "JRE_VERSION_180, 1.8.0_20, 0", "JRE_VERSION_170, 1.8.0_20, -1", "JRE_VERSION_160, 1.8.0_20, -1", }) public void compareTo(@ConvertWith(JavaVersionConstantArgumentConverter.class) JavaVersion verConst, String verStr, int result) { assertEquals(Integer.signum(verConst.compareTo(new JavaVersion(verStr))), result); }
@ParameterizedTest @ValueSource(strings = { "(0/0)", "(0/1)", "(1/0)", "(1/1)", "(2/1)" }) void testPointNotNull_1(@ConvertWith(PointConverter.class) Point point) { assertNotNull(point); }