下面列出了org.junit.jupiter.api.extension.ExtensionConfigurationException#org.junit.jupiter.api.extension.ParameterResolutionException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
if (redisServer == null) {
String localhost = InetAddress.getLoopbackAddress().getHostAddress();
redisServer = RedisServer
.builder()
.setting("bind " + localhost)
.setting("maxmemory 128mb")
.setting("maxmemory-policy allkeys-lru")
.setting("appendonly no")
.setting("save \"\"")
.port(findFreePort())
.build();
Runtime.getRuntime().addShutdownHook(shutdownThread);
redisServer.start();
}
return redisServer.ports().get(0);
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Configuration config = new Configuration();
Class<?> declaringClass = parameterContext.getDeclaringExecutable().getDeclaringClass();
WithHibernateSession annotation = getAnnotation(declaringClass);
for (Class<?> modelClass : annotation.models()) {
config.addAnnotatedClass(modelClass);
}
config.setProperty(DIALECT, H2Dialect.class.getName());
config.setProperty(DRIVER, Driver.class.getName());
config.setProperty(URL, "jdbc:h2:mem:./" + UUID.randomUUID().toString());
config.setProperty(HBM2DDL_AUTO, "create");
config.setProperty(CURRENT_SESSION_CONTEXT_CLASS, "managed");
config.setProperty(GENERATE_STATISTICS, "false");
SessionFactory sessionFactory = config.buildSessionFactory();
openSession(sessionFactory, extensionContext);
return sessionFactory;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
if (parameterContext.getParameter().getType() == Configuration.class) return configuration;
if (parameterContext.getParameter().getType() == Sources.class) return getTestRawSource();
for (Field field : Configuration.class.getDeclaredFields()) {
if (field.getType() == parameterContext.getParameter().getType()) {
try {
field.setAccessible(true);
return field.get(configuration);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
}
// 実装ミスでもなければここには来ない
throw new AssertionError();
}
/**
* We don't actually have to resolve the parameter (thus the default values in the implementation)
* since the class instance that is passed to JUnit isn't really used.
* The actual test instance that is used is the one that is pulled from Arc, which of course will already have its
* constructor parameters properly resolved
*/
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
String className = parameterContext.getParameter().getType().getName();
switch (className) {
case "boolean":
return false;
case "byte":
case "short":
case "int":
return 0;
case "long":
return 0L;
case "float":
return 0.0f;
case "double":
return 0.0d;
case "char":
return '\u0000';
default:
return null;
}
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {
FlowableDmnTestHelper flowableTestHelper = getTestHelper(context);
if (parameterContext.isAnnotated(DmnDeploymentId.class)) {
return flowableTestHelper.getDeploymentIdFromDeploymentAnnotation();
}
Class<?> parameterType = parameterContext.getParameter().getType();
DmnEngine dmnEngine = flowableTestHelper.getDmnEngine();
if (parameterType.isInstance(dmnEngine)) {
return dmnEngine;
} else if (FlowableDmnTestHelper.class.equals(parameterType)) {
return flowableTestHelper;
}
try {
return DmnEngine.class.getDeclaredMethod("get" + parameterType.getSimpleName()).invoke(dmnEngine);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
throw new ParameterResolutionException("Could not find service " + parameterType, ex);
}
}
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
throws ParameterResolutionException {
List<ParameterFactory> validFactories =
getSupportedFactories(parameterContext.getParameter()).collect(toList());
if (validFactories.size() > 1) {
throw new ParameterResolutionException(
String.format("Too many factories: %s for parameter: %s",
validFactories,
parameterContext.getParameter()));
}
return Iterables.getOnlyElement(validFactories)
.getParameterValue(parameterContext.getParameter());
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {
FlowableCmmnTestHelper flowableTestHelper = getTestHelper(context);
if (parameterContext.isAnnotated(CmmnDeploymentId.class)) {
return flowableTestHelper.getDeploymentIdFromDeploymentAnnotation();
}
Class<?> parameterType = parameterContext.getParameter().getType();
CmmnEngine cmmnEngine = flowableTestHelper.getCmmnEngine();
if (parameterType.isInstance(cmmnEngine)) {
return cmmnEngine;
} else if (FlowableCmmnTestHelper.class.equals(parameterType)) {
return flowableTestHelper;
}
try {
return CmmnEngine.class.getDeclaredMethod("get" + parameterType.getSimpleName()).invoke(cmmnEngine);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
throw new ParameterResolutionException("Could not find service " + parameterType, ex);
}
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {
FlowableTestHelper flowableTestHelper = getTestHelper(context);
if (parameterContext.isAnnotated(DeploymentId.class)) {
return flowableTestHelper.getDeploymentIdFromDeploymentAnnotation();
}
Class<?> parameterType = parameterContext.getParameter().getType();
ProcessEngine processEngine = flowableTestHelper.getProcessEngine();
if (parameterType.isInstance(processEngine)) {
return processEngine;
} else if (FlowableTestHelper.class.equals(parameterType)) {
return flowableTestHelper;
} else if (FlowableMockSupport.class.equals(parameterType)) {
return flowableTestHelper.getMockSupport();
}
try {
return ProcessEngine.class.getDeclaredMethod("get" + parameterType.getSimpleName()).invoke(processEngine);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
throw new ParameterResolutionException("Could not find service " + parameterType, ex);
}
}
@Override
public Object resolveParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
checkExtensionContext(extensionContext);
CloseableTestKit closeableTestKit =
getStore(extensionContext).get(TESTKIT_KEY, CloseableTestKit.class);
TestKit testKit;
if (closeableTestKit == null) {
testKit = buildTestKit(parameterContext, extensionContext);
getStore(extensionContext).put(TESTKIT_KEY, new CloseableTestKit(testKit));
} else {
// Throw an exception if TestKit was already instantiated in this context, but user tries to
// reconfigure it
if (annotationsUsed(parameterContext)) {
throw new ParameterResolutionException("TestKit was parameterized with annotations after"
+ " being instantiated in " + extensionContext.getDisplayName());
}
testKit = closeableTestKit.getTestKit();
}
return testKit;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
if (tempDirectory == null) {
try {
tempDirectory = createTempDirectory(extensionContext.getRequiredTestClass().getSimpleName());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return tempDirectory;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
if (vertx == null) {
System.setProperty("vertx.disableFileCPResolving", "true");
vertx = Vertx.vertx();
}
return vertx;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
if (tempDirectory == null) {
try {
tempDirectory = createTempDirectory(extensionContext.getRequiredTestClass().getSimpleName());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return tempDirectory;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
if (vertx == null) {
System.setProperty("vertx.disableFileCPResolving", "true");
vertx = Vertx.vertx();
}
return vertx;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
if (parameterContext.getParameter().getType() == Configuration.class) return true;
if (parameterContext.getParameter().getType() == Sources.class) return true;
if (parameterContext.getParameter().getType() == AnalyzedImplementation.class) return true;
for (Field field : Configuration.class.getDeclaredFields()) {
if (field.getType() == parameterContext.getParameter().getType()) {
return true;
}
}
return false;
}
@SuppressWarnings("PMD.AvoidUncheckedExceptionsInSignatures")
@Override
public BrokerProperties resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
String[] properties = getValueHolderProperties(extensionContext);
return new BrokerProperties(properties);
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
AnnotatedElement annotatedElement = extensionContext.getElement().orElse(null);
if (annotatedElement == null) return null;
UITest annotation = annotatedElement.getAnnotation(UITest.class);
FXMLLoader loader = new FXMLLoader(getClass().getResource(annotation.value()));
try {
return loader.load();
} catch (IOException e) {
return null;
}
}
@Override
public boolean supportsParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
// don't blindly support a common type like `Integer`
// instead it should be annotated with `@Randomized` or something
Class<?> targetType = parameterContext.getParameter().getType();
return targetType == Integer.class || targetType == int.class;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
// we did our checks in supportsParameter() method, now we can do simple resolution
if (getContainerFromStore(extensionContext) != null) {
List<Annotation> qualifiers = resolveQualifiers(parameterContext, getContainerFromStore(extensionContext).getBeanManager());
return getContainerFromStore(extensionContext)
.select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()])).get();
}
return null;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
// if weld container isn't up yet or if its not Method, we don't resolve it
if (getContainerFromStore(extensionContext) == null || (!(parameterContext.getDeclaringExecutable() instanceof Method))) {
return false;
}
List<Annotation> qualifiers = resolveQualifiers(parameterContext, getContainerFromStore(extensionContext).getBeanManager());
// if we require explicit parameter injection (via global settings or annotation) and there are no qualifiers we don't resolve it
if ((getExplicitInjectionInfoFromStore(extensionContext) || (methodRequiresExplicitParamInjection(parameterContext))) && qualifiers.isEmpty()) {
return false;
} else {
return getContainerFromStore(extensionContext).select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()]))
.isResolvable();
}
}
@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) {}
@Override
public Object resolveParameter(
ParameterContext parameterContext, ExtensionContext context)
throws ParameterResolutionException {
return randomByUniqueId(context)
.computeIfAbsent(context.getUniqueId(), SeededRandom::create);
}
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
Key<?> key = getKey(extensionContext.getTestClass(), parameter);
Injector injector = getInjectorForParameterResolution(extensionContext)
.orElseThrow(() ->
new ParameterResolutionException(
String.format(
"Could not create injector for: %s It has no annotated element.",
extensionContext.getDisplayName())));
return injector.getInstance(key);
}
@ExpectFailure(
@Cause(
type = ParameterResolutionException.class,
message = "Discovered multiple competing ParameterResolvers"
)
)
@IncludeModule(TestModule.class)
@ParameterizedTest
@ValueSource(strings = "valueSourceString")
void explicitBindingStringShouldConflictWithValueSource(String value) {
}
@ExpectFailure(
@Cause(
type = ParameterResolutionException.class,
message = "Discovered multiple competing ParameterResolvers"
)
)
@IncludeModule(TestModule.class)
@ParameterizedTest
@ValueSource(strings = "valueSourceString")
void explicitBindingStringShouldConflictWithValueSource2(@SomeBindingAnnotation String value) {
}
@ExpectFailure(
@Cause(
type = ParameterResolutionException.class,
message = "Discovered multiple competing ParameterResolvers"
)
)
@IncludeModule(TestModule.class)
@ParameterizedTest
@ValueSource(strings = "valueSourceString")
void explicitBindingStringShouldConflictWithValueSource3(@SomeQualifyingAnnotation String value) {
}
@ExpectFailure(
@Cause(
type = ParameterResolutionException.class,
message = "Discovered multiple competing ParameterResolvers"
)
)
@IncludeModule(TestModule.class)
@ParameterizedTest
@ValueSource(strings = "valueSourceString")
void explicitBindingStringShouldConflictWithValueSource4(@Named("named") String value) {
}