Java Code Examples for org.junit.jupiter.api.extension.ExtensionContext.Store#put()

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext.Store#put() . 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: GuiceExtension.java    From bobcat with Apache License 2.0 6 votes vote down vote up
/**
 * Create {@link Injector} or get existing one from test context
 */
private static Optional<Injector> getOrCreateInjector(ExtensionContext context)
    throws NoSuchMethodException, InstantiationException, IllegalAccessException,
    InvocationTargetException {

  Optional<AnnotatedElement> optionalAnnotatedElement = context.getElement();
  if (!optionalAnnotatedElement.isPresent()) {
    return Optional.empty();
  }

  AnnotatedElement element = optionalAnnotatedElement.get();
  Store store = context.getStore(NAMESPACE);

  Injector injector = store.get(element, Injector.class);
  if (injector == null) {
    injector = createInjector(context);
    store.put(element, injector);
  }

  return Optional.of(injector);
}
 
Example 2
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Override
public void beforeAll(ExtensionContext context) {
    Class<?> testClass = context.getTestClass()
        .orElseThrow(() -> new ExtensionConfigurationException("TestcontainersExtension is only supported for classes."));

    Store store = context.getStore(NAMESPACE);
    List<StoreAdapter> sharedContainersStoreAdapters = findSharedContainers(testClass);

    sharedContainersStoreAdapters.forEach(adapter -> store.getOrComputeIfAbsent(adapter.getKey(), k -> adapter.start()));

    List<TestLifecycleAware> lifecycleAwareContainers = sharedContainersStoreAdapters
        .stream()
        .filter(this::isTestLifecycleAware)
        .map(lifecycleAwareAdapter -> (TestLifecycleAware) lifecycleAwareAdapter.container)
        .collect(toList());

    store.put(SHARED_LIFECYCLE_AWARE_CONTAINERS, lifecycleAwareContainers);
    signalBeforeTestToContainers(lifecycleAwareContainers, testDescriptionFrom(context));
}
 
Example 3
Source File: GuiceExtension.java    From junit5-extensions with MIT License 5 votes vote down vote up
/**
 * Returns an injector for the given context if and only if the given context has an {@link
 * ExtensionContext#getElement() annotated element}.
 */
private static Optional<Injector> getOrCreateInjector(ExtensionContext context)
    throws NoSuchMethodException,
    InstantiationException,
    IllegalAccessException,
    InvocationTargetException {
  if (!context.getElement().isPresent()) {
    return Optional.empty();
  }
  AnnotatedElement element = context.getElement().get();
  Store store = context.getStore(NAMESPACE);
  Injector injector = store.get(element, Injector.class);
  boolean sharedInjector = isSharedInjector(context);
  Set<Class<? extends Module>> moduleClasses = Collections.emptySet();
  if (injector == null && sharedInjector) {
    moduleClasses = getContextModuleTypes(context);
    injector = INJECTOR_CACHE.get(moduleClasses);
  }
  if (injector == null) {
    injector = createInjector(context);
    store.put(element, injector);
    if (sharedInjector && !moduleClasses.isEmpty()) {
      INJECTOR_CACHE.put(moduleClasses, injector);
    }
  }
  return Optional.of(injector);
}
 
Example 4
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public void beforeEach(final ExtensionContext context) {
    Store store = context.getStore(NAMESPACE);

    List<TestLifecycleAware> lifecycleAwareContainers = collectParentTestInstances(context).parallelStream()
        .flatMap(this::findRestartContainers)
        .peek(adapter -> store.getOrComputeIfAbsent(adapter.getKey(), k -> adapter.start()))
        .filter(this::isTestLifecycleAware)
        .map(lifecycleAwareAdapter -> (TestLifecycleAware) lifecycleAwareAdapter.container)
        .collect(toList());

    store.put(LOCAL_LIFECYCLE_AWARE_CONTAINERS, lifecycleAwareContainers);
    signalBeforeTestToContainers(lifecycleAwareContainers, testDescriptionFrom(context));
}
 
Example 5
Source File: CaptureSystemOutExtension.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Override {@code System.out} stream with custom out stream.
 *
 * @param store The extension store.
 */
private static void overrideSystemOut(Store store) {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	PrintStream outStream = new PrintStream(out);
	System.setOut(outStream);
	store.put(TEMPORARY_OUT_STORE_KEY, out);
}
 
Example 6
Source File: AbstractUseDataProviderArgumentProvider.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult,
        ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");

    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);

    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(
                new DefaultJupiterConfiguration(emptyConfigurationParameters()));
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context,
                extensionRegistry, InvocationInterceptor::interceptTestFactoryMethod);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;

    } catch (Exception e) {
        throw new ParameterResolutionException(
                String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(),
                        e.getMessage()),
                e);
    }
}
 
Example 7
Source File: UseDataProviderInvocationContextProvider.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the test data from given dataprovider method.
 *
 * @param dataProviderMethod the dataprovider method that gives the parameters; never {@code null}
 * @param cacheDataProviderResult determines if the dataprovider result should be cached using
 *            {@code dataProviderMethod} as key
 * @param context the execution context to use to create a {@link TestInfo} if required; never {@code null}
 *
 * @return a list of methods, each method bound to a parameter combination returned by the dataprovider
 * @throws NullPointerException if and only if one of the given arguments is {@code null}
 */
protected Object invokeDataProviderMethodToRetrieveData(Method dataProviderMethod, boolean cacheDataProviderResult,
        ExtensionContext context) {
    checkNotNull(dataProviderMethod, "'dataProviderMethod' must not be null");
    checkNotNull(context, "'context' must not be null");

    Store store = context.getRoot().getStore(NAMESPACE_USE_DATAPROVIDER);

    Object cached = store.get(dataProviderMethod);
    if (cached != null) {
        return cached;
    }
    try {
        // TODO how to not require junit-jupiter-engine dependency and reuse already existing ExtensionRegistry?
        ExtensionRegistry extensionRegistry = createRegistryWithDefaultExtensions(
                new DefaultJupiterConfiguration(emptyConfigurationParameters()));
        Object data = executableInvoker.invoke(dataProviderMethod, context.getTestInstance().orElse(null), context,
                extensionRegistry, InvocationInterceptor::interceptTestFactoryMethod);
        if (cacheDataProviderResult) {
            store.put(dataProviderMethod, data);
        }
        return data;

    } catch (Exception e) {
        throw new ParameterResolutionException(
                String.format("Exception while invoking dataprovider method '%s': %s", dataProviderMethod.getName(),
                        e.getMessage()),
                e);
    }
}
 
Example 8
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Override
public void postProcessTestInstance(final Object testInstance, final ExtensionContext context) {
    Store store = context.getStore(NAMESPACE);
    store.put(TEST_INSTANCE, testInstance);
}
 
Example 9
Source File: CaptureSystemOutExtension.java    From junit-servers with MIT License 2 votes vote down vote up
/**
 * Save origin {@code System.out} stream in the test store, to be able
 * to restore it after each test.
 *
 * @param store The extension store.
 */
private static void storeOriginalSystemOut(Store store) {
	PrintStream originalOut = System.out;
	store.put(ORIGINAL_OUT_STORE_KEY, originalOut);
}