Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup ExtensionContext Namespace API #1013

Merged
merged 1 commit into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions documentation/src/docs/asciidoc/release-notes-5.0.0-RC3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ on GitHub.
`junit.jupiter.conditions.deactivate`.
* The `junit.extensions.autodetection.enabled` configuration parameter has been renamed
to `junit.jupiter.extensions.autodetection.enabled`.
* The default, global extension namespace constant in `ExtensionContext` is now named
`Namespace.GLOBAL` instead of `Namespace.DEFAULT`.
* The default `getStore()` method was removed from the `ExtensionContext` interface.
Use the explicit call `getStore(Namespace.GLOBAL)` instead.

===== New Features and Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,15 @@ default void publishReportEntry(String key, String value) {
this.publishReportEntry(Collections.singletonMap(key, value));
}

/**
* Get the {@link Store} for the default, global {@link Namespace}.
*
* @return the default, global {@code Store}; never {@code null}
* @see #getStore(Namespace)
* @see Namespace#DEFAULT
*/
default Store getStore() {
return getStore(Namespace.DEFAULT);
}

/**
* Get the {@link Store} for the supplied {@link Namespace}.
*
* <p>Use {@code getStore(Namespace.GLOBAL)} to get the default, global {@link Namespace}.
*
* @param namespace the {@code Namespace} to get the store for; never {@code null}
* @return the store in which to put and get objects for other invocations
* working in the same namespace; never {@code null}
* @see Namespace#GLOBAL
*/
Store getStore(Namespace namespace);

Expand Down Expand Up @@ -406,7 +398,7 @@ class Namespace {
* The default, global namespace which allows access to stored data from
* all extensions.
*/
public static final Namespace DEFAULT = Namespace.create(new Object());
public static final Namespace GLOBAL = Namespace.create(new Object());

/**
* Create a namespace which restricts access to data to all extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.engine.descriptor.ClassExtensionContext;
import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
import org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor;
Expand Down Expand Up @@ -160,16 +161,19 @@ void reportEntriesArePublishedToExecutionContext() {

extensionContext.publishReportEntry(map1);
extensionContext.publishReportEntry(map2);
extensionContext.publishReportEntry("3rd key", "third value");

ArgumentCaptor<ReportEntry> entryCaptor = ArgumentCaptor.forClass(ReportEntry.class);
Mockito.verify(engineExecutionListener, Mockito.times(2)).reportingEntryPublished(
Mockito.verify(engineExecutionListener, Mockito.times(3)).reportingEntryPublished(
Mockito.eq(classTestDescriptor), entryCaptor.capture());

ReportEntry reportEntry1 = entryCaptor.getAllValues().get(0);
ReportEntry reportEntry2 = entryCaptor.getAllValues().get(1);
ReportEntry reportEntry3 = entryCaptor.getAllValues().get(2);

assertEquals(map1, reportEntry1.getKeyValuePairs());
assertEquals(map2, reportEntry2.getKeyValuePairs());
assertEquals("third value", reportEntry3.getKeyValuePairs().get("3rd key"));
}

@Test
Expand All @@ -180,8 +184,8 @@ void usingStore() {
MethodExtensionContext childContext = new MethodExtensionContext(parentContext, null, methodTestDescriptor,
new OuterClass(), new ThrowableCollector());

ExtensionContext.Store childStore = childContext.getStore();
ExtensionContext.Store parentStore = parentContext.getStore();
ExtensionContext.Store childStore = childContext.getStore(Namespace.GLOBAL);
ExtensionContext.Store parentStore = parentContext.getStore(Namespace.GLOBAL);

final Object key1 = "key 1";
final String value1 = "a value";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private String newValue(String key) {

private Store reset() {
count.set(0);
return new NamespaceAwareStore(new ExtensionValuesStore(null), Namespace.DEFAULT);
return new NamespaceAwareStore(new ExtensionValuesStore(null), Namespace.GLOBAL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ static class B extends Parent {
static class OnlyIncrementCounterOnce implements BeforeAllCallback {
@Override
public void beforeAll(ExtensionContext context) throws Exception {
getRoot(context).getStore().getOrComputeIfAbsent("counter", key -> Parent.counter.incrementAndGet());
ExtensionContext.Store store = getRoot(context).getStore(ExtensionContext.Namespace.GLOBAL);
store.getOrComputeIfAbsent("counter", key -> Parent.counter.incrementAndGet());
}

private ExtensionContext getRoot(ExtensionContext context) {
Expand Down