Skip to content

Commit fab5b4f

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 fab5b4f

File tree

2 files changed

+53
-103
lines changed

2 files changed

+53
-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: 51 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,32 @@ public class PlexusExtension implements BeforeEachCallback, AfterEachCallback {
8479

8580
@Override
8681
public void beforeEach(ExtensionContext context) throws Exception {
82+
extensionContextThreadLocal.set(context);
8783

88-
setContext(context);
89-
90-
getContainer().addComponent(getContainer(), PlexusContainer.class.getName());
91-
92-
((DefaultPlexusContainer) getContainer())
84+
((DefaultPlexusContainer) getContainer(context))
9385
.addPlexusInjector(
9486
Collections.emptyList(), binder -> binder.requestInjection(context.getRequiredTestInstance()));
9587
}
9688

97-
protected void setContext(ExtensionContext context) {
98-
this.context = context;
99-
}
100-
101-
protected void setupContainer() {
89+
private PlexusContainer setupContainer(ExtensionContext context) {
10290
// ----------------------------------------------------------------------------
10391
// Context Setup
10492
// ----------------------------------------------------------------------------
10593

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

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

114100
if (!hasPlexusHome) {
115-
File f = getTestFile("target/plexus-home");
101+
File f = new File(getTestBasedir(context), "target/plexus-home");
116102

117103
if (!f.isDirectory()) {
118104
f.mkdir();
119105
}
120106

121-
context.put("plexus.home", f.getAbsolutePath());
107+
plexusContext.put("plexus.home", f.getAbsolutePath());
122108
}
123109

124110
// ----------------------------------------------------------------------------
@@ -128,25 +114,29 @@ protected void setupContainer() {
128114
String config = getCustomConfigurationName();
129115

130116
ContainerConfiguration containerConfiguration =
131-
new DefaultContainerConfiguration().setName("test").setContext(context.getContextData());
117+
new DefaultContainerConfiguration().setName("test").setContext(plexusContext.getContextData());
132118

133119
if (config != null) {
134120
containerConfiguration.setContainerConfiguration(config);
135121
} else {
136-
String resource = getConfigurationName(null);
137-
122+
String resource = getConfigurationName(context);
138123
containerConfiguration.setContainerConfiguration(resource);
139124
}
140125

141126
customizeContainerConfiguration(containerConfiguration);
142-
testInstanceCustomizeContainerConfiguration(containerConfiguration);
127+
testInstanceCustomizeContainerConfiguration(containerConfiguration, context);
143128

129+
PlexusContainer container;
144130
try {
145131
container = new DefaultPlexusContainer(containerConfiguration);
132+
container.addComponent(container, PlexusContainer.class.getName());
146133
} catch (PlexusContainerException e) {
147134
throw new IllegalArgumentException("Failed to create plexus container.", e);
148135
}
149-
testInstanceCustomizeContainer(container);
136+
testInstanceCustomizeContainer(container, context);
137+
context.getStore(PLEXUS_EXTENSION).put(PlexusContainer.class, container);
138+
139+
return container;
150140
}
151141

152142
/**
@@ -160,14 +150,15 @@ protected void customizeContainerConfiguration(ContainerConfiguration containerC
160150
containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
161151
}
162152

163-
private void testInstanceCustomizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
153+
private void testInstanceCustomizeContainerConfiguration(
154+
ContainerConfiguration containerConfiguration, ExtensionContext context) {
164155
Object testInstance = context.getRequiredTestInstance();
165156
if (testInstance instanceof PlexusTestConfiguration) {
166157
((PlexusTestConfiguration) testInstance).customizeConfiguration(containerConfiguration);
167158
}
168159
}
169160

170-
private void testInstanceCustomizeContainer(PlexusContainer container) {
161+
private void testInstanceCustomizeContainer(PlexusContainer container, ExtensionContext context) {
171162
Object testInstance = context.getRequiredTestInstance();
172163
if (testInstance instanceof PlexusTestConfiguration) {
173164
((PlexusTestConfiguration) testInstance).customizeContainer(container);
@@ -176,28 +167,30 @@ private void testInstanceCustomizeContainer(PlexusContainer container) {
176167

177168
protected void customizeContext(Context context) {}
178169

179-
protected PlexusConfiguration customizeComponentConfiguration() {
180-
return null;
181-
}
182-
183170
@Override
184171
public void afterEach(ExtensionContext context) throws Exception {
172+
PlexusContainer container =
173+
context.getStore(PLEXUS_EXTENSION).remove(PlexusContainer.class, PlexusContainer.class);
185174
if (container != null) {
186175
container.dispose();
187-
188-
container = null;
189176
}
177+
context.getStore(PLEXUS_EXTENSION).remove("testBasedir", String.class);
178+
extensionContextThreadLocal.remove();
190179
}
191180

192181
/**
193182
* The base directory for the test instance. By default, this is the same as the basedir.
194183
*
184+
* @param context the test execution context
185+
*
195186
* @return the testBasedir
196187
* @since 1.7.0
197188
*/
198-
protected String getTestBasedir() {
189+
protected String getTestBasedir(ExtensionContext context) {
190+
String testBasedir = context.getStore(PLEXUS_EXTENSION).get(BASEDIR_KEY, String.class);
199191
if (testBasedir == null) {
200-
testBasedir = getBasedir();
192+
testBasedir = getDefaultBasedir();
193+
context.getStore(PLEXUS_EXTENSION).put(BASEDIR_KEY, testBasedir);
201194
}
202195
return testBasedir;
203196
}
@@ -206,28 +199,22 @@ protected String getTestBasedir() {
206199
* Set the base directory for the test instance. By default, this is the same as the basedir.
207200
*
208201
* @param testBasedir the testBasedir for the test instance
202+
* @param context the test execution context
209203
* @since 1.7.0
210204
*/
211-
protected void setTestBasedir(String testBasedir) {
212-
this.testBasedir = testBasedir;
205+
protected void setTestBasedir(String testBasedir, ExtensionContext context) {
206+
context.getStore(PLEXUS_EXTENSION).put(BASEDIR_KEY, testBasedir);
213207
}
214208

215-
public PlexusContainer getContainer() {
209+
public PlexusContainer getContainer(ExtensionContext context) {
210+
PlexusContainer container =
211+
context.getStore(PLEXUS_EXTENSION).get(PlexusContainer.class, PlexusContainer.class);
216212
if (container == null) {
217-
setupContainer();
213+
return setupContainer(context);
218214
}
219-
220215
return container;
221216
}
222217

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-
231218
protected String getCustomConfigurationName() {
232219
return null;
233220
}
@@ -238,10 +225,9 @@ protected String getCustomConfigurationName() {
238225
* this will produce a resource name of org/foo/FunTest.xml which would be used to
239226
* configure the Plexus container before running your test.
240227
*
241-
* @param subname the subname
242228
* @return A configruation name
243229
*/
244-
protected String getConfigurationName(String subname) {
230+
protected String getConfigurationName(ExtensionContext context) {
245231
Class<?> testClass = context.getRequiredTestClass();
246232
for (Class<?> clazz = testClass; clazz != null; clazz = clazz.getSuperclass()) {
247233
String name = clazz.getName().replace('.', '/') + ".xml";
@@ -252,40 +238,6 @@ protected String getConfigurationName(String subname) {
252238
return null;
253239
}
254240

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-
289241
// ----------------------------------------------------------------------
290242
// Helper methods for sub classes
291243
// ----------------------------------------------------------------------
@@ -312,12 +264,8 @@ public static String getTestPath(String basedir, String path) {
312264
return getTestFile(basedir, path).getAbsolutePath();
313265
}
314266

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

322270
if (basedir == null) {
323271
basedir = new File("").getAbsolutePath();
@@ -326,8 +274,10 @@ public static String getBasedir() {
326274
return basedir;
327275
}
328276

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

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

0 commit comments

Comments
 (0)