You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2025/07/16 05:08:50 UTC
(maven) branch maven-4.0.x updated: Refactor setupContainer to validate ExtensionContext, test class and instance, and throw clear IllegalStateExceptions (#10901, fixes #10428) (#10918)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-4.0.x by this push:
new 9f6ee205b1 Refactor setupContainer to validate ExtensionContext, test class and instance, and throw clear IllegalStateExceptions (#10901, fixes #10428) (#10918)
9f6ee205b1 is described below
commit 9f6ee205b1fb6fc0695caca2e2907bcf780e1e9a
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Wed Jul 16 07:08:44 2025 +0200
Refactor setupContainer to validate ExtensionContext, test class and instance, and throw clear IllegalStateExceptions (#10901, fixes #10428) (#10918)
Fixes [MNG-8664]
(cherry picked from commit aab3cbf2523cfcf16baf840a215d322f4564325c)
Co-authored-by: Arturo Bernal <ab...@apache.org>
---
.../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 {}
}