Java Code Examples for org.junit.jupiter.api.extension.ParameterContext#getParameter()

The following examples show how to use org.junit.jupiter.api.extension.ParameterContext#getParameter() . 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: JunitServerExtension.java    From junit-servers with MIT License 6 votes vote down vote up
@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;
}
 
Example 2
Source File: JunitServerExtension.java    From junit-servers with MIT License 6 votes vote down vote up
@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;
}
 
Example 3
Source File: TestParametersSupport.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
@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();
}
 
Example 4
Source File: SpringExtension.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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());
}
 
Example 5
Source File: SpringExtension.java    From tutorials with MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: TestParametersSupport.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: SpringExtension.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: GuiceExtension.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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);
}
 
Example 9
Source File: GuiceExtension.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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();
}
 
Example 10
Source File: SpringExtension.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: SeleniumExtension.java    From selenium-jupiter with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 12
Source File: TempDirectoryExtension.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@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());
}
 
Example 13
Source File: SlimeExtension.java    From Slime with MIT License 4 votes vote down vote up
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    Parameter parameter = parameterContext.getParameter();

    return parameter.getType().equals(SlimeFile.class);
}
 
Example 14
Source File: CaptureSystemOutExtension.java    From junit-servers with MIT License 4 votes vote down vote up
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
	Parameter parameter = parameterContext.getParameter();
	Class<?> parameterType = parameter.getType();
	return CaptureSystemOut.class.isAssignableFrom(parameterType);
}
 
Example 15
Source File: WireMockExtension.java    From junit-servers with MIT License 4 votes vote down vote up
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
	Parameter parameter = parameterContext.getParameter();
	Class<?> parameterType = parameter.getType();
	return WireMockServer.class.isAssignableFrom(parameterType);
}
 
Example 16
Source File: SpringExtension.java    From tutorials with MIT License 4 votes vote down vote up
@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);
}
 
Example 17
Source File: SpringExtension.java    From spring-test-junit5 with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: SpringExtension.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: SpringExtension.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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()));
}