下面列出了org.junit.jupiter.api.extension.ParameterContext#getParameter() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
@SuppressWarnings("checkstyle:ReturnCount")
public Object resolveParameter(final ParameterContext parameterContext,
final ExtensionContext extensionContext) throws ParameterResolutionException {
final Parameter parameter = parameterContext.getParameter();
final Class<?> type = parameter.getType();
if (ClientSupport.class.equals(type)) {
return getClient(extensionContext);
}
final DropwizardTestSupport<?> support = Preconditions.checkNotNull(getSupport(extensionContext));
if (Application.class.isAssignableFrom(type)) {
return support.getApplication();
}
if (ObjectMapper.class.equals(type)) {
return support.getObjectMapper();
}
return InjectorLookup.getInjector(support.getApplication())
.map(it -> it.getInstance(getKey(parameter)))
.get();
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
final Parameter parameter = parameterContext.getParameter();
final Class<?> parameterClass = parameter.getType();
// Fast case: a perfect match.
if (RESOLVERS.containsKey(parameterClass)) {
return true;
}
for (Class<?> klass : RESOLVERS.keySet()) {
if (klass.isAssignableFrom(parameterClass)) {
return true;
}
}
return false;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
final Parameter parameter = parameterContext.getParameter();
final Class<?> parameterClass = parameter.getType();
final EmbeddedServerRunner serverAdapter = findEmbeddedServerAdapterInStore(extensionContext);
// Fast case: a perfect match.
if (RESOLVERS.containsKey(parameterClass)) {
return RESOLVERS.get(parameterClass).resolve(parameterContext, serverAdapter);
}
for (Class<?> klass : RESOLVERS.keySet()) {
if (klass.isAssignableFrom(parameterClass)) {
return RESOLVERS.get(klass).resolve(parameterContext, serverAdapter);
}
}
// Should not happen since Junit framework will call this method if, and only if, the
// method `supportsParameter` has previously returned `true`.
return null;
}
/**
* Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by
* retrieving the corresponding dependency from the test's {@link ApplicationContext}.
* <p>Delegates to {@link ParameterResolutionDelegate#resolveDependency}.
* @see #supportsParameter
* @see ParameterResolutionDelegate#resolveDependency
*/
@Override
@Nullable
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
int index = parameterContext.getIndex();
Class<?> testClass = extensionContext.getRequiredTestClass();
ApplicationContext applicationContext = getApplicationContext(extensionContext);
return ParameterResolutionDelegate.resolveDependency(parameter, index, testClass,
applicationContext.getAutowireCapableBeanFactory());
}
/**
* Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by
* retrieving the corresponding dependency from the test's {@link ApplicationContext}.
* <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}.
* @see #supportsParameter
* @see ParameterAutowireUtils#resolveDependency
*/
@Override
@Nullable
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
int index = parameterContext.getIndex();
Class<?> testClass = extensionContext.getRequiredTestClass();
ApplicationContext applicationContext = getApplicationContext(extensionContext);
return ParameterAutowireUtils.resolveDependency(parameter, index, testClass, applicationContext);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
if (getBindingAnnotations(parameter).size() > 1) {
return false;
}
Key<?> key = getKey(
extensionContext.getTestClass(),
parameter);
Optional<Injector> optInjector = getInjectorForParameterResolution(extensionContext);
return optInjector.filter(injector -> {
// Do not bind String without explicit bindings.
if (key.equals(Key.get(String.class)) && injector.getExistingBinding(key) == null) {
return false;
}
try {
injector.getInstance(key);
return true;
} catch (ConfigurationException | ProvisionException e) {
// If we throw a ParameterResolutionException here instead of returning false, we'll block
// other ParameterResolvers from being able to work.
return false;
}
}).isPresent();
}
@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);
}
/**
* Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by
* retrieving the corresponding dependency from the test's {@link ApplicationContext}.
* <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}.
* @see #supportsParameter
* @see ParameterAutowireUtils#resolveDependency
*/
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
Class<?> testClass = extensionContext.getRequiredTestClass();
ApplicationContext applicationContext = getApplicationContext(extensionContext);
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
}
@Override
@SuppressWarnings("checkstyle:ReturnCount")
public boolean supportsParameter(final ParameterContext parameterContext,
final ExtensionContext extensionContext) throws ParameterResolutionException {
final Parameter parameter = parameterContext.getParameter();
if (parameter.getAnnotations().length > 0) {
if (AnnotationSupport.isAnnotated(parameter, Jit.class)) {
return true;
} else if (!isQualifierAnnotation(parameter.getAnnotations())) {
// if any other annotation declared on the parameter - skip it (possibly other extension's parameter)
return false;
}
}
final Class<?> type = parameter.getType();
if (Application.class.isAssignableFrom(type) || Configuration.class.isAssignableFrom(type)) {
// special case when exact app or configuration class used
return true;
} else {
for (Class<?> cls : supportedClasses) {
if (type.equals(cls)) {
return true;
}
}
}
// declared guice binding (by class only)
return getInjector(extensionContext)
.map(it -> it.getExistingBinding(getKey(parameter)) != null)
.orElse(false);
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
Class<?> testClass = extensionContext.getTestClass()
.get();
ApplicationContext applicationContext = getApplicationContext(extensionContext);
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
return parameter.getType().equals(SlimeFile.class);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
return parameter.getAnnotation(TempDirectory.class) != null && Path.class.equals(parameter.getType());
}
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
String contextId = extensionContext.getUniqueId();
Parameter parameter = parameterContext.getParameter();
int index = parameterContext.getIndex();
log.trace("Context id {}", contextId);
if (isSingleSession(extensionContext)
&& driverHandlerMap.containsKey(contextId)) {
List<DriverHandler> list = driverHandlerMap.get(contextId);
if (index < list.size()) {
Object obj = list.get(index).getObject();
if (obj != null) {
log.trace("Returning index {}: {}", index, obj);
return obj;
}
}
}
Class<?> type = parameter.getType();
String url = null;
Browser browser = null;
// Check template
if (isGeneric(type) && !browserListMap.isEmpty()) {
browser = getBrowser(contextId, index);
}
Optional<String> urlFromAnnotation = getUrlFromAnnotation(parameter,
extensionContext);
if (urlFromAnnotation.isPresent() && browser != null) {
browser.setUrl(urlFromAnnotation.get());
}
if (browser != null) {
type = templateHandlerMap.get(browser.getType());
url = browser.getUrl();
}
// Handler
Class<?> constructorClass = handlerMap.containsKey(type.getName())
? handlerMap.get(type.getName())
: OtherDriverHandler.class;
boolean isRemote = constructorClass.equals(RemoteDriverHandler.class);
if (url != null && !url.isEmpty()) {
constructorClass = RemoteDriverHandler.class;
isRemote = true;
}
// WebDriverManager
runWebDriverManagerIfNeded(type, isRemote);
DriverHandler driverHandler = getDriverHandler(parameterContext,
extensionContext, parameter, type, browser, constructorClass,
isRemote);
return resolveHandler(parameter, driverHandler);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
Class<?> parameterType = parameter.getType();
return CaptureSystemOut.class.isAssignableFrom(parameterType);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
Class<?> parameterType = parameter.getType();
return WireMockServer.class.isAssignableFrom(parameterType);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
Executable executable = parameter.getDeclaringExecutable();
return ((executable instanceof Constructor) && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
}
/**
* Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
* should be autowired from the test's {@link ApplicationContext}.
* <p>A parameter is considered to be autowirable if one of the following
* conditions is {@code true}.
* <ol>
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
* executable} is a {@link Constructor} and
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class)}
* returns {@code true}.</li>
* <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
* <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
* </ol>
* <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
* with {@code @Autowired} or automatically autowirable (see {@link TestConstructor}),
* Spring will assume the responsibility for resolving all parameters in the
* constructor. Consequently, no other registered {@link ParameterResolver}
* will be able to resolve parameters.
* @see #resolveParameter
* @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
* @see ParameterResolutionDelegate#isAutowirable
*/
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
Executable executable = parameter.getDeclaringExecutable();
Class<?> testClass = extensionContext.getRequiredTestClass();
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass) ||
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}
/**
* Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
* should be autowired from the test's {@link ApplicationContext}.
* <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
* that is annotated with {@link Autowired @Autowired} and otherwise delegates to
* {@link ParameterAutowireUtils#isAutowirable}.
* <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
* that is annotated with {@code @Autowired}, Spring will assume the responsibility
* for resolving all parameters in the constructor. Consequently, no other registered
* {@link ParameterResolver} will be able to resolve parameters.
* @see #resolveParameter
* @see ParameterAutowireUtils#isAutowirable
*/
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
int index = parameterContext.getIndex();
Executable executable = parameter.getDeclaringExecutable();
return (executable instanceof Constructor &&
AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
ParameterAutowireUtils.isAutowirable(parameter, index);
}
/**
* Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
* should be autowired from the test's {@link ApplicationContext}.
* <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
* that is annotated with {@link Autowired @Autowired} and otherwise delegates to
* {@link ParameterAutowireUtils#isAutowirable}.
* <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
* that is annotated with {@code @Autowired}, Spring will assume the responsibility
* for resolving all parameters in the constructor. Consequently, no other registered
* {@link ParameterResolver} will be able to resolve parameters.
* @see #resolveParameter
* @see ParameterAutowireUtils#isAutowirable
*/
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
Executable executable = parameter.getDeclaringExecutable();
return (executable instanceof Constructor &&
AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
ParameterAutowireUtils.isAutowirable(parameter);
}