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

The following examples show how to use org.junit.jupiter.api.extension.ParameterContext#getIndex() . 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: RemoteActionCodeTest.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Method aggregateArguments(
    final ArgumentsAccessor arguments, final ParameterContext context)
    throws ArgumentsAggregationException {
  // Ignore CSV columns that are already used by different parameters
  final int offset = context.getIndex();
  final Class<?> remoteInterface = arguments.get(offset, Class.class);
  final String methodName = arguments.getString(offset + 1);
  final Class<?>[] argumentTypes =
      IntStream.range(offset + 2, arguments.size())
          .mapToObj(i -> arguments.get(i, Class.class))
          .toArray(Class<?>[]::new);
  try {
    return remoteInterface.getMethod(methodName, argumentTypes);
  } catch (final NoSuchMethodException e) {
    throw new ArgumentsAggregationException("Invalid method specified", e);
  }
}
 
Example 2
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 3
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 4
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 5
Source File: ParameterIndex_ParameterResolver.java    From JUnit-5-Quick-Start-Guide-and-Framework-Support with MIT License 4 votes vote down vote up
/**
 * Simple example that simply resolves the Parameter by returning the parameterIndex based on the Parameter-Context.
 */
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    return (long) parameterContext.getIndex();
}
 
Example 6
Source File: DataProviderParameterResolver.java    From junit-dataprovider with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
    Executable declaringExecutable = parameterContext.getParameter().getDeclaringExecutable();
    Method testMethod = extensionContext.getTestMethod().orElse(null);
    return declaringExecutable.equals(testMethod) && parameterContext.getIndex() < arguments.size();
}
 
Example 7
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);
}