Skip to content

Commit d3fc545

Browse files
Store all extension data in ExtensionContext
- use own namespace - remove some of protected methods - all methods needs an execution context in parameter - use ThreadLocal for static getBasedir It will be a brake change for project which extends this extension For project which only use extension should be not breaking
1 parent 7e1c9fd commit d3fc545

File tree

2 files changed

+54
-103
lines changed

2 files changed

+54
-103
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>plexus-testing</artifactId>
12-
<version>1.7.1-SNAPSHOT</version>
12+
<version>2.0.0-SNAPSHOT</version>
1313

1414
<name>Plexus Testing</name>
1515
<description>Library to help testing plexus components</description>
@@ -36,7 +36,7 @@
3636

3737
<properties>
3838
<versions.eclipse.sisu>0.9.0.M4</versions.eclipse.sisu>
39-
<project.build.outputTimestamp>2025-10-14T21:16:53Z</project.build.outputTimestamp>
39+
<project.build.outputTimestamp>2025-10-19T08:15:10Z</project.build.outputTimestamp>
4040
</properties>
4141

4242
<dependencies>

src/main/java/org/codehaus/plexus/testing/PlexusExtension.java

Lines changed: 52 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,15 @@
3636
*/
3737

3838
import java.io.File;
39-
import java.io.InputStream;
4039
import java.util.Collections;
40+
import java.util.Optional;
4141

4242
import org.codehaus.plexus.ContainerConfiguration;
4343
import org.codehaus.plexus.DefaultContainerConfiguration;
4444
import org.codehaus.plexus.DefaultPlexusContainer;
4545
import org.codehaus.plexus.PlexusConstants;
4646
import org.codehaus.plexus.PlexusContainer;
4747
import org.codehaus.plexus.PlexusContainerException;
48-
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
49-
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
50-
import org.codehaus.plexus.configuration.PlexusConfiguration;
5148
import org.codehaus.plexus.context.Context;
5249
import org.codehaus.plexus.context.DefaultContext;
5350
import org.junit.jupiter.api.extension.AfterEachCallback;
@@ -66,15 +63,13 @@
6663
* @author Guillaume Nodet
6764
*/
6865
public class PlexusExtension implements BeforeEachCallback, AfterEachCallback {
69-
private ExtensionContext context;
70-
private PlexusContainer container;
7166

72-
private static String basedir;
67+
private static final ExtensionContext.Namespace PLEXUS_EXTENSION =
68+
ExtensionContext.Namespace.create("PlexusExtension");
7369

74-
/**
75-
* The base directory for the test instance
76-
*/
77-
private String testBasedir;
70+
public static final String BASEDIR_KEY = "basedir";
71+
72+
private static final ThreadLocal<ExtensionContext> extensionContextThreadLocal = new ThreadLocal<>();
7873

7974
static {
8075
if (System.getProperty("guice_custom_class_loading", "").trim().isEmpty()) {
@@ -84,41 +79,33 @@ public class PlexusExtension implements BeforeEachCallback, AfterEachCallback {
8479

8580
@Override
8681
public void beforeEach(ExtensionContext context) throws Exception {
82+
extensionContextThreadLocal.set(context);
83+
setTestBasedir(getDefaultBasedir(), context);
8784

88-
setContext(context);
89-
90-
getContainer().addComponent(getContainer(), PlexusContainer.class.getName());
91-
92-
((DefaultPlexusContainer) getContainer())
85+
((DefaultPlexusContainer) getContainer(context))
9386
.addPlexusInjector(
9487
Collections.emptyList(), binder -> binder.requestInjection(context.getRequiredTestInstance()));
9588
}
9689

97-
protected void setContext(ExtensionContext context) {
98-
this.context = context;
99-
}
100-
101-
protected void setupContainer() {
90+
private PlexusContainer setupContainer(ExtensionContext context) {
10291
// ----------------------------------------------------------------------------
10392
// Context Setup
10493
// ----------------------------------------------------------------------------
10594

106-
DefaultContext context = new DefaultContext();
107-
108-
context.put("basedir", getTestBasedir());
109-
110-
customizeContext(context);
95+
DefaultContext plexusContext = new DefaultContext();
96+
plexusContext.put("basedir", getTestBasedir(context));
97+
customizeContext(plexusContext);
11198

112-
boolean hasPlexusHome = context.contains("plexus.home");
99+
boolean hasPlexusHome = plexusContext.contains("plexus.home");
113100

114101
if (!hasPlexusHome) {
115-
File f = getTestFile("target/plexus-home");
102+
File f = new File(getTestBasedir(context), "target/plexus-home");
116103

117104
if (!f.isDirectory()) {
118105
f.mkdir();
119106
}
120107

121-
context.put("plexus.home", f.getAbsolutePath());
108+
plexusContext.put("plexus.home", f.getAbsolutePath());
122109
}
123110

124111
// ----------------------------------------------------------------------------
@@ -128,25 +115,29 @@ protected void setupContainer() {
128115
String config = getCustomConfigurationName();
129116

130117
ContainerConfiguration containerConfiguration =
131-
new DefaultContainerConfiguration().setName("test").setContext(context.getContextData());
118+
new DefaultContainerConfiguration().setName("test").setContext(plexusContext.getContextData());
132119

133120
if (config != null) {
134121
containerConfiguration.setContainerConfiguration(config);
135122
} else {
136-
String resource = getConfigurationName(null);
137-
123+
String resource = getConfigurationName(context);
138124
containerConfiguration.setContainerConfiguration(resource);
139125
}
140126

141127
customizeContainerConfiguration(containerConfiguration);
142-
testInstanceCustomizeContainerConfiguration(containerConfiguration);
128+
testInstanceCustomizeContainerConfiguration(containerConfiguration, context);
143129

130+
PlexusContainer container;
144131
try {
145132
container = new DefaultPlexusContainer(containerConfiguration);
133+
container.addComponent(container, PlexusContainer.class.getName());
146134
} catch (PlexusContainerException e) {
147135
throw new IllegalArgumentException("Failed to create plexus container.", e);
148136
}
149-
testInstanceCustomizeContainer(container);
137+
testInstanceCustomizeContainer(container, context);
138+
context.getStore(PLEXUS_EXTENSION).put(PlexusContainer.class, container);
139+
140+
return container;
150141
}
151142

152143
/**
@@ -160,14 +151,15 @@ protected void customizeContainerConfiguration(ContainerConfiguration containerC
160151
containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
161152
}
162153

163-
private void testInstanceCustomizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
154+
private void testInstanceCustomizeContainerConfiguration(
155+
ContainerConfiguration containerConfiguration, ExtensionContext context) {
164156
Object testInstance = context.getRequiredTestInstance();
165157
if (testInstance instanceof PlexusTestConfiguration) {
166158
((PlexusTestConfiguration) testInstance).customizeConfiguration(containerConfiguration);
167159
}
168160
}
169161

170-
private void testInstanceCustomizeContainer(PlexusContainer container) {
162+
private void testInstanceCustomizeContainer(PlexusContainer container, ExtensionContext context) {
171163
Object testInstance = context.getRequiredTestInstance();
172164
if (testInstance instanceof PlexusTestConfiguration) {
173165
((PlexusTestConfiguration) testInstance).customizeContainer(container);
@@ -176,28 +168,30 @@ private void testInstanceCustomizeContainer(PlexusContainer container) {
176168

177169
protected void customizeContext(Context context) {}
178170

179-
protected PlexusConfiguration customizeComponentConfiguration() {
180-
return null;
181-
}
182-
183171
@Override
184172
public void afterEach(ExtensionContext context) throws Exception {
173+
PlexusContainer container =
174+
context.getStore(PLEXUS_EXTENSION).remove(PlexusContainer.class, PlexusContainer.class);
185175
if (container != null) {
186176
container.dispose();
187-
188-
container = null;
189177
}
178+
context.getStore(PLEXUS_EXTENSION).remove("testBasedir", String.class);
179+
extensionContextThreadLocal.remove();
190180
}
191181

192182
/**
193183
* The base directory for the test instance. By default, this is the same as the basedir.
194184
*
185+
* @param context the test execution context
186+
*
195187
* @return the testBasedir
196188
* @since 1.7.0
197189
*/
198-
protected String getTestBasedir() {
190+
protected String getTestBasedir(ExtensionContext context) {
191+
String testBasedir = context.getStore(PLEXUS_EXTENSION).get(BASEDIR_KEY, String.class);
199192
if (testBasedir == null) {
200-
testBasedir = getBasedir();
193+
testBasedir = getDefaultBasedir();
194+
context.getStore(PLEXUS_EXTENSION).put(BASEDIR_KEY, testBasedir);
201195
}
202196
return testBasedir;
203197
}
@@ -206,28 +200,22 @@ protected String getTestBasedir() {
206200
* Set the base directory for the test instance. By default, this is the same as the basedir.
207201
*
208202
* @param testBasedir the testBasedir for the test instance
203+
* @param context the test execution context
209204
* @since 1.7.0
210205
*/
211-
protected void setTestBasedir(String testBasedir) {
212-
this.testBasedir = testBasedir;
206+
protected void setTestBasedir(String testBasedir, ExtensionContext context) {
207+
context.getStore(PLEXUS_EXTENSION).put(BASEDIR_KEY, testBasedir);
213208
}
214209

215-
public PlexusContainer getContainer() {
210+
public PlexusContainer getContainer(ExtensionContext context) {
211+
PlexusContainer container =
212+
context.getStore(PLEXUS_EXTENSION).get(PlexusContainer.class, PlexusContainer.class);
216213
if (container == null) {
217-
setupContainer();
214+
return setupContainer(context);
218215
}
219-
220216
return container;
221217
}
222218

223-
protected InputStream getConfiguration() throws Exception {
224-
return getConfiguration(null);
225-
}
226-
227-
protected InputStream getConfiguration(String subname) throws Exception {
228-
return getResourceAsStream(getConfigurationName(subname));
229-
}
230-
231219
protected String getCustomConfigurationName() {
232220
return null;
233221
}
@@ -238,10 +226,9 @@ protected String getCustomConfigurationName() {
238226
* this will produce a resource name of org/foo/FunTest.xml which would be used to
239227
* configure the Plexus container before running your test.
240228
*
241-
* @param subname the subname
242229
* @return A configruation name
243230
*/
244-
protected String getConfigurationName(String subname) {
231+
protected String getConfigurationName(ExtensionContext context) {
245232
Class<?> testClass = context.getRequiredTestClass();
246233
for (Class<?> clazz = testClass; clazz != null; clazz = clazz.getSuperclass()) {
247234
String name = clazz.getName().replace('.', '/') + ".xml";
@@ -252,40 +239,6 @@ protected String getConfigurationName(String subname) {
252239
return null;
253240
}
254241

255-
protected InputStream getResourceAsStream(String resource) {
256-
return context.getRequiredTestClass().getResourceAsStream(resource);
257-
}
258-
259-
protected ClassLoader getClassLoader() {
260-
return context.getRequiredTestClass().getClassLoader();
261-
}
262-
263-
// ----------------------------------------------------------------------
264-
// Container access
265-
// ----------------------------------------------------------------------
266-
267-
@SuppressWarnings("unchecked")
268-
protected <T> T lookup(String componentKey) throws ComponentLookupException {
269-
return (T) getContainer().lookup(componentKey);
270-
}
271-
272-
@SuppressWarnings("unchecked")
273-
protected <T> T lookup(String role, String roleHint) throws ComponentLookupException {
274-
return (T) getContainer().lookup(role, roleHint);
275-
}
276-
277-
protected <T> T lookup(Class<T> componentClass) throws ComponentLookupException {
278-
return getContainer().lookup(componentClass);
279-
}
280-
281-
protected <T> T lookup(Class<T> componentClass, String roleHint) throws ComponentLookupException {
282-
return getContainer().lookup(componentClass, roleHint);
283-
}
284-
285-
protected void release(Object component) throws ComponentLifecycleException {
286-
getContainer().release(component);
287-
}
288-
289242
// ----------------------------------------------------------------------
290243
// Helper methods for sub classes
291244
// ----------------------------------------------------------------------
@@ -312,12 +265,8 @@ public static String getTestPath(String basedir, String path) {
312265
return getTestFile(basedir, path).getAbsolutePath();
313266
}
314267

315-
public static String getBasedir() {
316-
if (basedir != null) {
317-
return basedir;
318-
}
319-
320-
basedir = System.getProperty("basedir");
268+
private static String getDefaultBasedir() {
269+
String basedir = System.getProperty("basedir");
321270

322271
if (basedir == null) {
323272
basedir = new File("").getAbsolutePath();
@@ -326,8 +275,10 @@ public static String getBasedir() {
326275
return basedir;
327276
}
328277

329-
public String getTestConfiguration() {
330-
return getTestConfiguration(context.getRequiredTestClass());
278+
public static String getBasedir() {
279+
return Optional.ofNullable(extensionContextThreadLocal.get())
280+
.map(ec -> ec.getStore(PLEXUS_EXTENSION).get(BASEDIR_KEY, String.class))
281+
.orElseGet(PlexusExtension::getDefaultBasedir);
331282
}
332283

333284
public static String getTestConfiguration(Class<?> clazz) {

0 commit comments

Comments
 (0)