3636 */
3737
3838import java .io .File ;
39- import java .io .InputStream ;
4039import java .util .Collections ;
4140
4241import org .codehaus .plexus .ContainerConfiguration ;
4544import org .codehaus .plexus .PlexusConstants ;
4645import org .codehaus .plexus .PlexusContainer ;
4746import 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 ;
5147import org .codehaus .plexus .context .Context ;
5248import org .codehaus .plexus .context .DefaultContext ;
5349import org .junit .jupiter .api .extension .AfterEachCallback ;
6662 * @author Guillaume Nodet
6763 */
6864public class PlexusExtension implements BeforeEachCallback , AfterEachCallback {
69- private ExtensionContext context ;
70- private PlexusContainer container ;
7165
72- private static String basedir ;
66+ private static final ExtensionContext .Namespace PLEXUS_EXTENSION =
67+ ExtensionContext .Namespace .create ("PlexusExtension" );
7368
74- /**
75- * The base directory for the test instance
76- */
77- private String testBasedir ;
69+ private static String basedir ;
7870
7971 static {
8072 if (System .getProperty ("guice_custom_class_loading" , "" ).trim ().isEmpty ()) {
@@ -85,40 +77,30 @@ public class PlexusExtension implements BeforeEachCallback, AfterEachCallback {
8577 @ Override
8678 public void beforeEach (ExtensionContext context ) throws Exception {
8779
88- setContext (context );
89-
90- getContainer ().addComponent (getContainer (), PlexusContainer .class .getName ());
91-
92- ((DefaultPlexusContainer ) getContainer ())
80+ ((DefaultPlexusContainer ) getContainer (context ))
9381 .addPlexusInjector (
9482 Collections .emptyList (), binder -> binder .requestInjection (context .getRequiredTestInstance ()));
9583 }
9684
97- protected void setContext (ExtensionContext context ) {
98- this .context = context ;
99- }
100-
101- protected void setupContainer () {
85+ private PlexusContainer setupContainer (ExtensionContext context ) {
10286 // ----------------------------------------------------------------------------
10387 // Context Setup
10488 // ----------------------------------------------------------------------------
10589
106- DefaultContext context = new DefaultContext ();
107-
108- context . put ( "basedir" , getTestBasedir () );
90+ DefaultContext plexusContext = new DefaultContext ();
91+ plexusContext . put ( "basedir" , getTestBasedir ( context ));
92+ customizeContext ( plexusContext );
10993
110- customizeContext (context );
111-
112- boolean hasPlexusHome = context .contains ("plexus.home" );
94+ boolean hasPlexusHome = plexusContext .contains ("plexus.home" );
11395
11496 if (!hasPlexusHome ) {
115- File f = getTestFile ( "target/plexus-home" );
97+ File f = new File ( getTestBasedir ( context ), "target/plexus-home" );
11698
11799 if (!f .isDirectory ()) {
118100 f .mkdir ();
119101 }
120102
121- context .put ("plexus.home" , f .getAbsolutePath ());
103+ plexusContext .put ("plexus.home" , f .getAbsolutePath ());
122104 }
123105
124106 // ----------------------------------------------------------------------------
@@ -128,25 +110,29 @@ protected void setupContainer() {
128110 String config = getCustomConfigurationName ();
129111
130112 ContainerConfiguration containerConfiguration =
131- new DefaultContainerConfiguration ().setName ("test" ).setContext (context .getContextData ());
113+ new DefaultContainerConfiguration ().setName ("test" ).setContext (plexusContext .getContextData ());
132114
133115 if (config != null ) {
134116 containerConfiguration .setContainerConfiguration (config );
135117 } else {
136- String resource = getConfigurationName (null );
137-
118+ String resource = getConfigurationName (context );
138119 containerConfiguration .setContainerConfiguration (resource );
139120 }
140121
141122 customizeContainerConfiguration (containerConfiguration );
142- testInstanceCustomizeContainerConfiguration (containerConfiguration );
123+ testInstanceCustomizeContainerConfiguration (containerConfiguration , context );
143124
125+ PlexusContainer container ;
144126 try {
145127 container = new DefaultPlexusContainer (containerConfiguration );
128+ container .addComponent (container , PlexusContainer .class .getName ());
146129 } catch (PlexusContainerException e ) {
147130 throw new IllegalArgumentException ("Failed to create plexus container." , e );
148131 }
149- testInstanceCustomizeContainer (container );
132+ testInstanceCustomizeContainer (container , context );
133+ context .getStore (PLEXUS_EXTENSION ).put (PlexusContainer .class , container );
134+
135+ return container ;
150136 }
151137
152138 /**
@@ -160,14 +146,15 @@ protected void customizeContainerConfiguration(ContainerConfiguration containerC
160146 containerConfiguration .setClassPathScanning (PlexusConstants .SCANNING_INDEX );
161147 }
162148
163- private void testInstanceCustomizeContainerConfiguration (ContainerConfiguration containerConfiguration ) {
149+ private void testInstanceCustomizeContainerConfiguration (
150+ ContainerConfiguration containerConfiguration , ExtensionContext context ) {
164151 Object testInstance = context .getRequiredTestInstance ();
165152 if (testInstance instanceof PlexusTestConfiguration ) {
166153 ((PlexusTestConfiguration ) testInstance ).customizeConfiguration (containerConfiguration );
167154 }
168155 }
169156
170- private void testInstanceCustomizeContainer (PlexusContainer container ) {
157+ private void testInstanceCustomizeContainer (PlexusContainer container , ExtensionContext context ) {
171158 Object testInstance = context .getRequiredTestInstance ();
172159 if (testInstance instanceof PlexusTestConfiguration ) {
173160 ((PlexusTestConfiguration ) testInstance ).customizeContainer (container );
@@ -176,28 +163,29 @@ private void testInstanceCustomizeContainer(PlexusContainer container) {
176163
177164 protected void customizeContext (Context context ) {}
178165
179- protected PlexusConfiguration customizeComponentConfiguration () {
180- return null ;
181- }
182-
183166 @ Override
184167 public void afterEach (ExtensionContext context ) throws Exception {
168+ PlexusContainer container =
169+ context .getStore (PLEXUS_EXTENSION ).remove (PlexusContainer .class , PlexusContainer .class );
185170 if (container != null ) {
186171 container .dispose ();
187-
188- container = null ;
189172 }
173+ context .getStore (PLEXUS_EXTENSION ).remove ("testBasedir" , String .class );
190174 }
191175
192176 /**
193177 * The base directory for the test instance. By default, this is the same as the basedir.
194178 *
179+ * @param context the test execution context
180+ *
195181 * @return the testBasedir
196182 * @since 1.7.0
197183 */
198- protected String getTestBasedir () {
184+ protected String getTestBasedir (ExtensionContext context ) {
185+ String testBasedir = context .getStore (PLEXUS_EXTENSION ).get ("testBasedir" , String .class );
199186 if (testBasedir == null ) {
200187 testBasedir = getBasedir ();
188+ context .getStore (PLEXUS_EXTENSION ).put ("testBasedir" , testBasedir );
201189 }
202190 return testBasedir ;
203191 }
@@ -206,28 +194,22 @@ protected String getTestBasedir() {
206194 * Set the base directory for the test instance. By default, this is the same as the basedir.
207195 *
208196 * @param testBasedir the testBasedir for the test instance
197+ * @param context the test execution context
209198 * @since 1.7.0
210199 */
211- protected void setTestBasedir (String testBasedir ) {
212- this . testBasedir = testBasedir ;
200+ protected void setTestBasedir (String testBasedir , ExtensionContext context ) {
201+ context . getStore ( PLEXUS_EXTENSION ). put ( " testBasedir" , testBasedir ) ;
213202 }
214203
215- public PlexusContainer getContainer () {
204+ public PlexusContainer getContainer (ExtensionContext context ) {
205+ PlexusContainer container =
206+ context .getStore (PLEXUS_EXTENSION ).get (PlexusContainer .class , PlexusContainer .class );
216207 if (container == null ) {
217- setupContainer ();
208+ return setupContainer (context );
218209 }
219-
220210 return container ;
221211 }
222212
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-
231213 protected String getCustomConfigurationName () {
232214 return null ;
233215 }
@@ -238,10 +220,9 @@ protected String getCustomConfigurationName() {
238220 * this will produce a resource name of org/foo/FunTest.xml which would be used to
239221 * configure the Plexus container before running your test.
240222 *
241- * @param subname the subname
242223 * @return A configruation name
243224 */
244- protected String getConfigurationName (String subname ) {
225+ protected String getConfigurationName (ExtensionContext context ) {
245226 Class <?> testClass = context .getRequiredTestClass ();
246227 for (Class <?> clazz = testClass ; clazz != null ; clazz = clazz .getSuperclass ()) {
247228 String name = clazz .getName ().replace ('.' , '/' ) + ".xml" ;
@@ -252,40 +233,6 @@ protected String getConfigurationName(String subname) {
252233 return null ;
253234 }
254235
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-
289236 // ----------------------------------------------------------------------
290237 // Helper methods for sub classes
291238 // ----------------------------------------------------------------------
@@ -326,10 +273,6 @@ public static String getBasedir() {
326273 return basedir ;
327274 }
328275
329- public String getTestConfiguration () {
330- return getTestConfiguration (context .getRequiredTestClass ());
331- }
332-
333276 public static String getTestConfiguration (Class <?> clazz ) {
334277 String s = clazz .getName ().replace ('.' , '/' );
335278
0 commit comments