下面列出了org.junit.jupiter.api.extension.ExtensionContext.Store#get() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
/**
* 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);
}
private boolean isSpringTestContextEnabled(ExtensionContext extensionContext) {
if (!extensionContext.getTestClass().isPresent()) {
return false;
}
Store springStore = extensionContext.getRoot().getStore(Namespace.create(SpringExtension.class));
return springStore != null && springStore.get(extensionContext.getTestClass().get()) != null;
}
private static Optional<io.micronaut.context.ApplicationContext> getMicronautApplicationContext(ExtensionContext extensionContext) {
Store micronautStore = extensionContext.getRoot().getStore(Namespace.create(MicronautJunit5Extension.class));
if (micronautStore != null) {
try {
io.micronaut.context.ApplicationContext appContext = (io.micronaut.context.ApplicationContext) micronautStore.get(io.micronaut.context.ApplicationContext.class);
if (appContext != null) {
return Optional.of(appContext);
}
} catch (ClassCastException ex) {
}
}
return Optional.empty();
}
@Override
public void afterEach(ExtensionContext context) {
Store store = getStore(context);
WireMockServer wireMockServer = store.get(STORE_KEY, WireMockServer.class);
try {
wireMockServer.stop();
}
finally {
store.remove(STORE_KEY);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Store store = getStore(extensionContext);
ByteArrayOutputStream out = store.get(TEMPORARY_OUT_STORE_KEY, ByteArrayOutputStream.class);
return new CaptureSystemOut(out);
}
/**
* Flush and close the previously created temporary {@code System.out} stream.
*
* @param store The extension store.
* @throws Exception If an error occurred while closing the stream.
*/
private static void closeTemporaryOut(Store store) throws Exception {
ByteArrayOutputStream out = store.get(TEMPORARY_OUT_STORE_KEY, ByteArrayOutputStream.class);
out.flush();
out.close();
}