This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new aab3cbf252 Refactor setupContainer to validate ExtensionContext, test 
class and instance, and throw clear IllegalStateExceptions (#10901, fixes 
#10428)
aab3cbf252 is described below

commit aab3cbf2523cfcf16baf840a215d322f4564325c
Author: Arturo Bernal <aber...@apache.org>
AuthorDate: Tue Jul 15 11:34:07 2025 +0200

    Refactor setupContainer to validate ExtensionContext, test class and 
instance, and throw clear IllegalStateExceptions (#10901, fixes #10428)
    
    Fixes [MNG-8664]
---
 .../maven/api/di/testing/MavenDIExtension.java     | 28 +++++++++++++++++-----
 .../apache/maven/api/di/testing/SimpleDITest.java  | 26 ++++++++++++++++++++
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git 
a/impl/maven-testing/src/main/java/org/apache/maven/api/di/testing/MavenDIExtension.java
 
b/impl/maven-testing/src/main/java/org/apache/maven/api/di/testing/MavenDIExtension.java
index 40603619e2..12de0178d1 100644
--- 
a/impl/maven-testing/src/main/java/org/apache/maven/api/di/testing/MavenDIExtension.java
+++ 
b/impl/maven-testing/src/main/java/org/apache/maven/api/di/testing/MavenDIExtension.java
@@ -84,18 +84,34 @@ protected void setContext(ExtensionContext context) {
      * Creates and configures the DI container for test execution.
      * Performs component discovery and sets up basic bindings.
      *
-     * @throws IllegalArgumentException if container setup fails
+     * @throws IllegalStateException if the ExtensionContext is null, the 
required test class is unavailable,
+     *         the required test instance is unavailable, or if container 
setup fails
      */
-    @SuppressWarnings("unchecked")
     protected void setupContainer() {
+        if (context == null) {
+            throw new IllegalStateException("ExtensionContext must not be 
null");
+        }
+        final Class<?> testClass = context.getRequiredTestClass();
+        if (testClass == null) {
+            throw new IllegalStateException("Required test class is not 
available in ExtensionContext");
+        }
+        final Object testInstance = context.getRequiredTestInstance();
+        if (testInstance == null) {
+            throw new IllegalStateException("Required test instance is not 
available in ExtensionContext");
+        }
+
         try {
             injector = Injector.create();
             injector.bindInstance(ExtensionContext.class, context);
-            injector.discover(context.getRequiredTestClass().getClassLoader());
+            injector.discover(testClass.getClassLoader());
             injector.bindInstance(Injector.class, injector);
-            injector.bindInstance((Class) context.getRequiredTestClass(), 
context.getRequiredTestInstance());
-        } catch (Exception e) {
-            throw new IllegalArgumentException("Failed to create DI 
injector.", e);
+            injector.bindInstance(testClass.asSubclass(Object.class), (Object) 
testInstance); // Safe generics handling
+        } catch (final Exception e) {
+            throw new IllegalStateException(
+                    String.format(
+                            "Failed to set up DI injector for test class '%s': 
%s",
+                            testClass.getName(), e.getMessage()),
+                    e);
         }
     }
 
diff --git 
a/impl/maven-testing/src/test/java/org/apache/maven/api/di/testing/SimpleDITest.java
 
b/impl/maven-testing/src/test/java/org/apache/maven/api/di/testing/SimpleDITest.java
index 49fc6faec3..fbfa625a13 100644
--- 
a/impl/maven-testing/src/test/java/org/apache/maven/api/di/testing/SimpleDITest.java
+++ 
b/impl/maven-testing/src/test/java/org/apache/maven/api/di/testing/SimpleDITest.java
@@ -25,9 +25,13 @@
 import org.apache.maven.api.di.Provides;
 import org.apache.maven.api.plugin.testing.stubs.SessionMock;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtensionContext;
 
 import static org.apache.maven.api.di.testing.MavenDIExtension.getBasedir;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 @MavenDITest
 public class SimpleDITest {
@@ -47,4 +51,26 @@ void testSession() {
     Session createSession() {
         return SessionMock.getMockSession(LOCAL_REPO);
     }
+
+    @Test
+    void testSetupContainerWithNullContext() {
+        MavenDIExtension extension = new MavenDIExtension();
+        MavenDIExtension.context = null;
+        assertThrows(IllegalStateException.class, extension::setupContainer);
+    }
+
+    @Test
+    void testSetupContainerWithNullTestClass() {
+        final MavenDIExtension extension = new MavenDIExtension();
+        final ExtensionContext context = mock(ExtensionContext.class);
+        when(context.getRequiredTestClass()).thenReturn(null); // Mock null 
test class
+        when(context.getRequiredTestInstance()).thenReturn(new TestClass()); 
// Valid instance
+        MavenDIExtension.context = context;
+        assertThrows(
+                IllegalStateException.class,
+                extension::setupContainer,
+                "Should throw IllegalStateException for null test class");
+    }
+
+    static class TestClass {}
 }

Reply via email to