diff --git a/pom.xml b/pom.xml index 1c97977..4558aa7 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ plexus-testing - 2.0.0 + 2.0.1 Plexus Testing Library to help testing plexus components @@ -18,7 +18,7 @@ scm:git:https://github.com/codehaus-plexus/plexus-testing.git ${project.scm.connection} - plexus-testing-2.0.0 + plexus-testing-2.0.1 https://github.com/codehaus-plexus/plexus-testing @@ -36,7 +36,7 @@ 0.9.0.M4 - 2025-10-20T16:08:27Z + 2025-10-27T21:16:48Z diff --git a/src/main/java/org/codehaus/plexus/testing/PlexusExtension.java b/src/main/java/org/codehaus/plexus/testing/PlexusExtension.java index 0c15235..c45e782 100644 --- a/src/main/java/org/codehaus/plexus/testing/PlexusExtension.java +++ b/src/main/java/org/codehaus/plexus/testing/PlexusExtension.java @@ -64,6 +64,23 @@ */ public class PlexusExtension implements BeforeEachCallback, AfterEachCallback { + private static class PlexusClosableWrapper implements AutoCloseable { + private final PlexusContainer container; + + PlexusClosableWrapper(PlexusContainer container) { + this.container = container; + } + + @Override + public void close() { + container.dispose(); + } + + public PlexusContainer get() { + return container; + } + } + private static final ExtensionContext.Namespace PLEXUS_EXTENSION = ExtensionContext.Namespace.create("PlexusExtension"); @@ -79,7 +96,6 @@ public class PlexusExtension implements BeforeEachCallback, AfterEachCallback { @Override public void beforeEach(ExtensionContext context) throws Exception { - extensionContextThreadLocal.set(context); setTestBasedir(getDefaultBasedir(), context); ((DefaultPlexusContainer) getContainer(context)) @@ -88,6 +104,12 @@ public void beforeEach(ExtensionContext context) throws Exception { } private PlexusContainer setupContainer(ExtensionContext context) { + // Store the context in a thread local for static access + // must be done hear as this method is always executed + extensionContextThreadLocal.set(context); + context.getStore(PLEXUS_EXTENSION) + .put("threadLocalCloseable", (AutoCloseable) extensionContextThreadLocal::remove); + // ---------------------------------------------------------------------------- // Context Setup // ---------------------------------------------------------------------------- @@ -135,7 +157,7 @@ private PlexusContainer setupContainer(ExtensionContext context) { throw new IllegalArgumentException("Failed to create plexus container.", e); } testInstanceCustomizeContainer(container, context); - context.getStore(PLEXUS_EXTENSION).put(PlexusContainer.class, container); + context.getStore(PLEXUS_EXTENSION).put(PlexusClosableWrapper.class, new PlexusClosableWrapper(container)); return container; } @@ -170,13 +192,7 @@ protected void customizeContext(Context context) {} @Override public void afterEach(ExtensionContext context) throws Exception { - PlexusContainer container = - context.getStore(PLEXUS_EXTENSION).remove(PlexusContainer.class, PlexusContainer.class); - if (container != null) { - container.dispose(); - } - context.getStore(PLEXUS_EXTENSION).remove("testBasedir", String.class); - extensionContextThreadLocal.remove(); + // empty method, not used } /** @@ -208,12 +224,12 @@ protected void setTestBasedir(String testBasedir, ExtensionContext context) { } public PlexusContainer getContainer(ExtensionContext context) { - PlexusContainer container = - context.getStore(PLEXUS_EXTENSION).get(PlexusContainer.class, PlexusContainer.class); + PlexusClosableWrapper container = + context.getStore(PLEXUS_EXTENSION).get(PlexusClosableWrapper.class, PlexusClosableWrapper.class); if (container == null) { return setupContainer(context); } - return container; + return container.get(); } protected String getCustomConfigurationName() {