168168 */
169169public class MojoExtension extends MavenDIExtension implements ParameterResolver , BeforeEachCallback {
170170
171- /** The base directory of the plugin being tested */
172- protected static String pluginBasedir ;
171+ private static final ExtensionContext . Namespace MOJO_EXTENSION =
172+ ExtensionContext . Namespace . create ( "mojo-extension" ) ;
173173
174- /** The base directory for test resources */
175- protected static String basedir ;
174+ private static final String BASEDIR_KEY = "basedir" ;
176175
177176 /**
178177 * Gets the identifier for the current test method.
@@ -181,6 +180,8 @@ public class MojoExtension extends MavenDIExtension implements ParameterResolver
181180 * @return the test identifier
182181 */
183182 public static String getTestId () {
183+ ExtensionContext context =
184+ getContext ().orElseThrow (() -> new IllegalStateException ("ExtensionContext must not be null" ));
184185 return context .getRequiredTestClass ().getSimpleName () + "-"
185186 + context .getRequiredTestMethod ().getName ();
186187 }
@@ -193,7 +194,10 @@ public static String getTestId() {
193194 * @throws NullPointerException if neither basedir nor plugin basedir is set
194195 */
195196 public static String getBasedir () {
196- return requireNonNull (basedir != null ? basedir : MavenDIExtension .basedir );
197+ String basedir = getContext ()
198+ .map (context -> context .getStore (MOJO_EXTENSION ).get (BASEDIR_KEY , String .class ))
199+ .orElse (null );
200+ return requireNonNull (basedir != null ? basedir : MavenDIExtension .getBasedir ());
197201 }
198202
199203 /**
@@ -203,7 +207,7 @@ public static String getBasedir() {
203207 * @throws NullPointerException if plugin basedir is not set
204208 */
205209 public static String getPluginBasedir () {
206- return requireNonNull ( pluginBasedir );
210+ return MavenDIExtension . getBasedir ( );
207211 }
208212
209213 /**
@@ -227,11 +231,9 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
227231 throws ParameterResolutionException {
228232 try {
229233 Class <?> holder = parameterContext .getTarget ().orElseThrow ().getClass ();
230- PluginDescriptor descriptor = extensionContext
231- .getStore (ExtensionContext .Namespace .GLOBAL )
232- .get (PluginDescriptor .class , PluginDescriptor .class );
233- Model model =
234- extensionContext .getStore (ExtensionContext .Namespace .GLOBAL ).get (Model .class , Model .class );
234+ PluginDescriptor descriptor =
235+ extensionContext .getStore (MOJO_EXTENSION ).get (PluginDescriptor .class , PluginDescriptor .class );
236+ Model model = extensionContext .getStore (MOJO_EXTENSION ).get (Model .class , Model .class );
235237 InjectMojo parameterInjectMojo =
236238 parameterContext .getAnnotatedElement ().getAnnotation (InjectMojo .class );
237239 String goal ;
@@ -331,23 +333,22 @@ private static String getGoalFromMojoImplementationClass(Class<?> cl) throws IOE
331333 @ Override
332334 @ SuppressWarnings ("checkstyle:MethodLength" )
333335 public void beforeEach (ExtensionContext context ) throws Exception {
334- if (pluginBasedir == null ) {
335- pluginBasedir = MavenDIExtension .getBasedir ();
336- }
337- basedir = AnnotationSupport .findAnnotation (context .getElement ().orElseThrow (), Basedir .class )
336+ setContext (context );
337+
338+ String pluginBasedir = MavenDIExtension .getBasedir ();
339+
340+ String basedir = AnnotationSupport .findAnnotation (context .getElement ().orElseThrow (), Basedir .class )
338341 .map (Basedir ::value )
339342 .orElse (pluginBasedir );
340- if (basedir != null ) {
341- if (basedir .isEmpty ()) {
342- basedir = pluginBasedir + "/target/tests/"
343- + context .getRequiredTestClass ().getSimpleName () + "/"
344- + context .getRequiredTestMethod ().getName ();
345- } else {
346- basedir = basedir .replace ("${basedir}" , pluginBasedir );
347- }
343+ if (basedir .isEmpty ()) {
344+ basedir = pluginBasedir + "/target/tests/"
345+ + context .getRequiredTestClass ().getSimpleName () + "/"
346+ + context .getRequiredTestMethod ().getName ();
347+ } else {
348+ basedir = basedir .replace ("${basedir}" , pluginBasedir );
348349 }
349350
350- setContext ( context );
351+ context . getStore ( MOJO_EXTENSION ). put ( BASEDIR_KEY , basedir );
351352
352353 /*
353354 binder.install(ProviderMethodsModule.forObject(context.getRequiredTestInstance()));
@@ -438,7 +439,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
438439 }
439440 tmodel = new DefaultModelPathTranslator (new DefaultPathTranslator ())
440441 .alignToBaseDirectory (tmodel , Paths .get (getBasedir ()), null );
441- context .getStore (ExtensionContext . Namespace . GLOBAL ).put (Model .class , tmodel );
442+ context .getStore (MOJO_EXTENSION ).put (Model .class , tmodel );
442443
443444 // mojo execution
444445 // Map<Object, Object> map = getInjector().getContext().getContextData();
@@ -451,7 +452,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
451452 // new InterpolationFilterReader(reader, map, "${", "}");
452453 pluginDescriptor = new PluginDescriptorStaxReader ().read (reader );
453454 }
454- context .getStore (ExtensionContext . Namespace . GLOBAL ).put (PluginDescriptor .class , pluginDescriptor );
455+ context .getStore (MOJO_EXTENSION ).put (PluginDescriptor .class , pluginDescriptor );
455456 // for (ComponentDescriptor<?> desc : pluginDescriptor.getComponents()) {
456457 // getContainer().addComponentDescriptor(desc);
457458 // }
0 commit comments