Copilot commented on code in PR #3654:
URL: https://github.com/apache/maven-surefire/pull/3654#discussion_r4731285607


##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,279 @@
 package org.apache.maven.surefire.junitplatform;
 
 import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.List;
 
 import org.junit.platform.engine.TestExecutionResult;
+import org.junit.platform.engine.TestExecutionResult.Status;
+import org.junit.platform.engine.TestSource;
 import org.junit.platform.engine.support.descriptor.ClassSource;
+import org.junit.platform.engine.support.descriptor.MethodSource;
 import org.junit.platform.launcher.TestExecutionListener;
 import org.junit.platform.launcher.TestIdentifier;
 import org.junit.platform.launcher.TestPlan;
+import org.opentest4j.MultipleFailuresError;
+import org.opentest4j.TestAbortedException;
 
+import static java.util.Collections.singletonList;
+import static org.junit.platform.engine.TestExecutionResult.Status.ABORTED;
+import static org.junit.platform.engine.TestExecutionResult.Status.FAILED;
+import static org.junit.platform.engine.TestExecutionResult.Status.SUCCESSFUL;
+
+/**
+ * Calls JUnit 4 {@code RunListener}s the way JUnit 4 does: start and finish 
of each test, its failure,
+ * assumption failure or ignore. JUnit 4 is only on the test classpath, so it 
all goes through reflection.
+ */
 public class JUnit4ListenersAdapter implements TestExecutionListener {
 
+    private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];
+
     private final List<Object> listeners;
 
+    // the loader the listeners come from, events can arrive on other threads
+    private final ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+
+    // null before JUnit 4.12, no listener is thread safe then
+    private final Class<? extends Annotation> threadSafe = 
threadSafeAnnotation();
+
+    // like JUnit 4, listeners not marked thread safe are called one at a time
+    private final Object lock = new Object();
+
     public JUnit4ListenersAdapter(List<Object> runListener) {
         this.listeners = runListener;
     }
 
     @Override
     public void executionStarted(TestIdentifier testIdentifier) {
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createTestDescription = descriptionClass.getMethod(
-                        "createTestDescription", String.class, String.class, 
Annotation[].class);
-                if (testIdentifier.getSource().isPresent()
-                        && testIdentifier.getSource().get() instanceof 
ClassSource) {
-                    String className = ((ClassSource) 
testIdentifier.getSource().get()).getClassName();
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(className);
-                    Object invoke = createTestDescription.invoke(
-                            descriptionClass, classToRemove.getName(), 
testIdentifier.getDisplayName(), null);
-                    runListener
-                            .getClass()
-                            .getMethod("testRunStarted", descriptionClass)
-                            .invoke(runListener, (Object) null);
-                    runListener
-                            .getClass()
-                            .getMethod("testStarted", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        if (testIdentifier.isTest()) {
+            notifyListeners("testStarted", describe(testIdentifier));
+        }
     }
 
     @Override
     public void executionFinished(TestIdentifier testIdentifier, 
TestExecutionResult testExecutionResult) {
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createTestDescription = descriptionClass.getMethod(
-                        "createTestDescription", String.class, String.class, 
Annotation[].class);
-                if (testIdentifier.getSource().isPresent()
-                        && testIdentifier.getSource().get() instanceof 
ClassSource) {
-                    String className = ((ClassSource) 
testIdentifier.getSource().get()).getClassName();
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(className);
-                    Object invoke = createTestDescription.invoke(
-                            descriptionClass, classToRemove.getName(), 
testIdentifier.getDisplayName(), null);
-
-                    Class<?> resultClass =
-                            
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Result");
-
-                    runListener
-                            .getClass()
-                            .getMethod("testRunFinished", resultClass)
-                            .invoke(runListener, 
resultClass.getConstructor().newInstance());
-
-                    runListener
-                            .getClass()
-                            .getMethod("testFinished", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
+        Status status = testExecutionResult.getStatus();
+        // like in JUnit 4, a class only shows up when it fails, for example 
in @BeforeClass
+        if (!testIdentifier.isTest() && status == SUCCESSFUL) {
+            return;
+        }
+        Object description = describe(testIdentifier);
+        if (status == FAILED) {
+            for (Throwable failure : failuresOf(testIdentifier, 
testExecutionResult)) {
+                notifyListeners("testFailure", newFailure(description, 
failure));
             }
-        });
+        } else if (status == ABORTED) {
+            Throwable error = testExecutionResult
+                    .getThrowable()
+                    .orElseGet(() -> new TestAbortedException("Test aborted 
with no cause"));
+            notifyListeners("testAssumptionFailure", newFailure(description, 
error));
+        }
+        if (testIdentifier.isTest()) {
+            notifyListeners("testFinished", description);
+        }
     }
 
     @Override
     public void executionSkipped(TestIdentifier testIdentifier, String reason) 
{
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createTestDescription = descriptionClass.getMethod(
-                        "createTestDescription", String.class, String.class, 
Annotation[].class);
-                if (testIdentifier.getSource().isPresent()
-                        && testIdentifier.getSource().get() instanceof 
ClassSource) {
-                    String className = ((ClassSource) 
testIdentifier.getSource().get()).getClassName();
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(className);
-                    Object invoke = createTestDescription.invoke(
-                            descriptionClass, classToRemove.getName(), 
testIdentifier.getDisplayName(), null);
-                    runListener
-                            .getClass()
-                            .getMethod("testIgnored", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        notifyListeners("testIgnored", describe(testIdentifier));
     }
 
     @Override
     public void testPlanExecutionStarted(TestPlan testPlan) {
-
-        String mainTestClass = extractMainClassName(testPlan);
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createSuiteDescription = 
descriptionClass.getMethod("createSuiteDescription", Class.class);
-                if (mainTestClass != null) {
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(mainTestClass);
-                    Object invoke = 
createSuiteDescription.invoke(descriptionClass, classToRemove);
-                    runListener
-                            .getClass()
-                            .getMethod("testRunStarted", descriptionClass)
-                            .invoke(runListener, invoke);
-                    runListener
-                            .getClass()
-                            .getMethod("testSuiteStarted", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        Object suite = describeMainClass(testPlan);
+        if (suite != null) {
+            notifyListeners("testRunStarted", suite);
+            notifyListeners("testSuiteStarted", suite);
+        }
     }
 
     @Override
     public void testPlanExecutionFinished(TestPlan testPlan) {
+        Object suite = describeMainClass(testPlan);
+        if (suite != null) {
+            notifyListeners("testSuiteFinished", suite);
+            notifyListeners("testRunFinished", 
newInstance("org.junit.runner.Result", new Class<?>[0]));
+        }
+    }
+
+    private static List<Throwable> failuresOf(TestIdentifier testIdentifier, 
TestExecutionResult result) {
+        // listeners call Failure.getMessage(), which needs an exception
+        Throwable error = result.getThrowable().orElseGet(() -> new 
AssertionError("Test failed with no cause"));
+        // Vintage wraps all failures of a test, say in its body and @After, 
in one error. JUnit 4 reports each.
+        // Only this exact class: AssertJ soft assertions subclass it and are 
one failure, like assertAll.
+        if (error.getClass() == MultipleFailuresError.class
+                && 
testIdentifier.getUniqueId().startsWith("[engine:junit-vintage]")
+                && madeByVintage((MultipleFailuresError) error)) {
+            return ((MultipleFailuresError) error).getFailures();
+        }
+        return singletonList(error);
+    }
+
+    // Vintage only wraps two or more failures, a Jupiter assertAll thrown by 
the test is kept as it is
+    private static boolean madeByVintage(MultipleFailuresError error) {
+        StackTraceElement[] stack = error.getStackTrace();
+        return error.getFailures().size() > 1
+                && (stack.length == 0 || 
!"org.junit.jupiter.api.AssertAll".equals(stack[0].getClassName()));

Review Comment:
   A JUnit 4 test that directly throws a `MultipleFailuresError` with two 
failures also satisfies this predicate, so one failure becomes multiple 
callbacks and the aggregate message is lost. [Vintage preserves a single 
recorded 
result](https://github.com/junit-team/junit-framework/blob/5e2b5533db587737b06b9091f23cf09926ab55ae/junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/TestRun.java#L195-L201)
 (upstream default-branch evidence). Positively identify Vintage-created 
wrappers rather than excluding only `AssertAll`, and add a direct-throw 
regression test.



##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,279 @@
 package org.apache.maven.surefire.junitplatform;
 
 import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.List;
 
 import org.junit.platform.engine.TestExecutionResult;
+import org.junit.platform.engine.TestExecutionResult.Status;
+import org.junit.platform.engine.TestSource;
 import org.junit.platform.engine.support.descriptor.ClassSource;
+import org.junit.platform.engine.support.descriptor.MethodSource;
 import org.junit.platform.launcher.TestExecutionListener;
 import org.junit.platform.launcher.TestIdentifier;
 import org.junit.platform.launcher.TestPlan;
+import org.opentest4j.MultipleFailuresError;
+import org.opentest4j.TestAbortedException;
 
+import static java.util.Collections.singletonList;
+import static org.junit.platform.engine.TestExecutionResult.Status.ABORTED;
+import static org.junit.platform.engine.TestExecutionResult.Status.FAILED;
+import static org.junit.platform.engine.TestExecutionResult.Status.SUCCESSFUL;
+
+/**
+ * Calls JUnit 4 {@code RunListener}s the way JUnit 4 does: start and finish 
of each test, its failure,
+ * assumption failure or ignore. JUnit 4 is only on the test classpath, so it 
all goes through reflection.
+ */
 public class JUnit4ListenersAdapter implements TestExecutionListener {
 
+    private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];
+
     private final List<Object> listeners;
 
+    // the loader the listeners come from, events can arrive on other threads
+    private final ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+
+    // null before JUnit 4.12, no listener is thread safe then
+    private final Class<? extends Annotation> threadSafe = 
threadSafeAnnotation();
+
+    // like JUnit 4, listeners not marked thread safe are called one at a time
+    private final Object lock = new Object();
+
     public JUnit4ListenersAdapter(List<Object> runListener) {
         this.listeners = runListener;
     }
 
     @Override
     public void executionStarted(TestIdentifier testIdentifier) {
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createTestDescription = descriptionClass.getMethod(
-                        "createTestDescription", String.class, String.class, 
Annotation[].class);
-                if (testIdentifier.getSource().isPresent()
-                        && testIdentifier.getSource().get() instanceof 
ClassSource) {
-                    String className = ((ClassSource) 
testIdentifier.getSource().get()).getClassName();
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(className);
-                    Object invoke = createTestDescription.invoke(
-                            descriptionClass, classToRemove.getName(), 
testIdentifier.getDisplayName(), null);
-                    runListener
-                            .getClass()
-                            .getMethod("testRunStarted", descriptionClass)
-                            .invoke(runListener, (Object) null);
-                    runListener
-                            .getClass()
-                            .getMethod("testStarted", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        if (testIdentifier.isTest()) {
+            notifyListeners("testStarted", describe(testIdentifier));
+        }
     }
 
     @Override
     public void executionFinished(TestIdentifier testIdentifier, 
TestExecutionResult testExecutionResult) {
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createTestDescription = descriptionClass.getMethod(
-                        "createTestDescription", String.class, String.class, 
Annotation[].class);
-                if (testIdentifier.getSource().isPresent()
-                        && testIdentifier.getSource().get() instanceof 
ClassSource) {
-                    String className = ((ClassSource) 
testIdentifier.getSource().get()).getClassName();
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(className);
-                    Object invoke = createTestDescription.invoke(
-                            descriptionClass, classToRemove.getName(), 
testIdentifier.getDisplayName(), null);
-
-                    Class<?> resultClass =
-                            
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Result");
-
-                    runListener
-                            .getClass()
-                            .getMethod("testRunFinished", resultClass)
-                            .invoke(runListener, 
resultClass.getConstructor().newInstance());
-
-                    runListener
-                            .getClass()
-                            .getMethod("testFinished", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
+        Status status = testExecutionResult.getStatus();
+        // like in JUnit 4, a class only shows up when it fails, for example 
in @BeforeClass
+        if (!testIdentifier.isTest() && status == SUCCESSFUL) {
+            return;
+        }
+        Object description = describe(testIdentifier);
+        if (status == FAILED) {
+            for (Throwable failure : failuresOf(testIdentifier, 
testExecutionResult)) {
+                notifyListeners("testFailure", newFailure(description, 
failure));
             }
-        });
+        } else if (status == ABORTED) {
+            Throwable error = testExecutionResult
+                    .getThrowable()
+                    .orElseGet(() -> new TestAbortedException("Test aborted 
with no cause"));
+            notifyListeners("testAssumptionFailure", newFailure(description, 
error));
+        }
+        if (testIdentifier.isTest()) {
+            notifyListeners("testFinished", description);
+        }
     }
 
     @Override
     public void executionSkipped(TestIdentifier testIdentifier, String reason) 
{
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createTestDescription = descriptionClass.getMethod(
-                        "createTestDescription", String.class, String.class, 
Annotation[].class);
-                if (testIdentifier.getSource().isPresent()
-                        && testIdentifier.getSource().get() instanceof 
ClassSource) {
-                    String className = ((ClassSource) 
testIdentifier.getSource().get()).getClassName();
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(className);
-                    Object invoke = createTestDescription.invoke(
-                            descriptionClass, classToRemove.getName(), 
testIdentifier.getDisplayName(), null);
-                    runListener
-                            .getClass()
-                            .getMethod("testIgnored", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        notifyListeners("testIgnored", describe(testIdentifier));
     }
 
     @Override
     public void testPlanExecutionStarted(TestPlan testPlan) {
-
-        String mainTestClass = extractMainClassName(testPlan);
-        listeners.forEach(runListener -> {
-            try {
-                Class<?> descriptionClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                Method createSuiteDescription = 
descriptionClass.getMethod("createSuiteDescription", Class.class);
-                if (mainTestClass != null) {
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(mainTestClass);
-                    Object invoke = 
createSuiteDescription.invoke(descriptionClass, classToRemove);
-                    runListener
-                            .getClass()
-                            .getMethod("testRunStarted", descriptionClass)
-                            .invoke(runListener, invoke);
-                    runListener
-                            .getClass()
-                            .getMethod("testSuiteStarted", descriptionClass)
-                            .invoke(runListener, invoke);
-                }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        Object suite = describeMainClass(testPlan);
+        if (suite != null) {
+            notifyListeners("testRunStarted", suite);
+            notifyListeners("testSuiteStarted", suite);
+        }
     }
 
     @Override
     public void testPlanExecutionFinished(TestPlan testPlan) {
+        Object suite = describeMainClass(testPlan);
+        if (suite != null) {
+            notifyListeners("testSuiteFinished", suite);
+            notifyListeners("testRunFinished", 
newInstance("org.junit.runner.Result", new Class<?>[0]));
+        }
+    }
+
+    private static List<Throwable> failuresOf(TestIdentifier testIdentifier, 
TestExecutionResult result) {
+        // listeners call Failure.getMessage(), which needs an exception
+        Throwable error = result.getThrowable().orElseGet(() -> new 
AssertionError("Test failed with no cause"));
+        // Vintage wraps all failures of a test, say in its body and @After, 
in one error. JUnit 4 reports each.
+        // Only this exact class: AssertJ soft assertions subclass it and are 
one failure, like assertAll.
+        if (error.getClass() == MultipleFailuresError.class
+                && 
testIdentifier.getUniqueId().startsWith("[engine:junit-vintage]")
+                && madeByVintage((MultipleFailuresError) error)) {
+            return ((MultipleFailuresError) error).getFailures();
+        }
+        return singletonList(error);
+    }
+
+    // Vintage only wraps two or more failures, a Jupiter assertAll thrown by 
the test is kept as it is
+    private static boolean madeByVintage(MultipleFailuresError error) {
+        StackTraceElement[] stack = error.getStackTrace();
+        return error.getFailures().size() > 1
+                && (stack.length == 0 || 
!"org.junit.jupiter.api.AssertAll".equals(stack[0].getClassName()));
+    }
+
+    private Object describe(TestIdentifier testIdentifier) {
+        TestSource source = testIdentifier.getSource().orElse(null);
+        String name = testIdentifier.getLegacyReportingName();
+        if (source instanceof MethodSource) {
+            MethodSource method = (MethodSource) source;
+            return testDescription(method.getClassName(), name, 
annotationsOf(method));
+        }
+        if (source instanceof ClassSource) {
+            String className = ((ClassSource) source).getClassName();
+            // Vintage gives some tests a ClassSource, Parameterized ones for 
instance
+            return testIdentifier.isTest()
+                    ? testDescription(className, name, NO_ANNOTATIONS)
+                    : classDescription((ClassSource) source);
+        }
+        return suiteDescription(name);
+    }
 
+    private static Annotation[] annotationsOf(MethodSource source) {
+        try {
+            return source.getJavaMethod().getAnnotations();
+        } catch (RuntimeException e) {
+            // not always real code, a Spock feature name is not a Java method
+            return NO_ANNOTATIONS;
+        }
+    }
+
+    private Object describeMainClass(TestPlan testPlan) {
         String mainTestClass = extractMainClassName(testPlan);
-        listeners.forEach(runListener -> {
+        if (mainTestClass == null) {
+            return null;
+        }
+        try {
+            return createDescription("createSuiteDescription", new Class<?>[] 
{Class.class}, loadClass(mainTestClass));
+        } catch (ClassNotFoundException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private Object testDescription(String className, String name, Annotation[] 
annotations) {
+        return createDescription(
+                "createTestDescription",
+                new Class<?>[] {String.class, String.class, 
Annotation[].class},
+                className,
+                name,
+                annotations);
+    }
+
+    // like JUnit 4, a class description carries the class annotations, 
@Category for one
+    private Object classDescription(ClassSource source) {
+        try {
+            return createDescription("createSuiteDescription", new Class<?>[] 
{Class.class}, source.getJavaClass());
+        } catch (RuntimeException | LinkageError e) {
+            return suiteDescription(source.getClassName());
+        }
+    }
+
+    private Object suiteDescription(String name) {
+        return createDescription(
+                "createSuiteDescription", new Class<?>[] {String.class, 
Annotation[].class}, name, NO_ANNOTATIONS);
+    }
+
+    private Object createDescription(String factoryMethod, Class<?>[] 
parameterTypes, Object... arguments) {
+        try {
+            return loadClass("org.junit.runner.Description")
+                    .getMethod(factoryMethod, parameterTypes)
+                    .invoke(null, arguments);
+        } catch (ReflectiveOperationException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private Object newFailure(Object description, Throwable error) {
+        return newInstance(
+                "org.junit.runner.notification.Failure",
+                new Class<?>[] {description.getClass(), Throwable.class},
+                description,
+                error);
+    }
+
+    private Object newInstance(String className, Class<?>[] parameterTypes, 
Object... arguments) {
+        try {
+            return 
loadClass(className).getConstructor(parameterTypes).newInstance(arguments);
+        } catch (ReflectiveOperationException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private void notifyListeners(String methodName, Object argument) {
+        notifyListeners(listeners, methodName, argument);
+    }
+
+    // like JUnit 4, a listener that throws doesn't stop the others, they get 
its exception as a failure
+    private void notifyListeners(List<Object> targets, String methodName, 
Object argument) {
+        Method method;
+        try {
+            // the argument is a plain Description, Failure or Result, so its 
class is the parameter type
+            method = 
loadClass("org.junit.runner.notification.RunListener").getMethod(methodName, 
argument.getClass());
+        } catch (NoSuchMethodException e) {
+            // older JUnit 4, testSuiteStarted and testSuiteFinished only came 
with 4.13
+            return;
+        } catch (ClassNotFoundException e) {
+            throw new RuntimeException(e);
+        }
+        List<Object> safeListeners = new ArrayList<>();
+        List<Object> failures = new ArrayList<>();
+        for (Object listener : targets) {
             try {
-                Class<?> resultClass =
-                        
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Result");
-                Constructor<?> resultClassConstructor = 
resultClass.getConstructor();
-                if (mainTestClass != null) {
-                    
Thread.currentThread().getContextClassLoader().loadClass(mainTestClass);
-                    Object invoke = resultClassConstructor.newInstance();
-                    runListener
-                            .getClass()
-                            .getMethod("testRunFinished", resultClass)
-                            .invoke(runListener, invoke);
-
-                    Class<?> descriptionClass =
-                            
Thread.currentThread().getContextClassLoader().loadClass("org.junit.runner.Description");
-                    Method createSuiteDescription = 
descriptionClass.getMethod("createSuiteDescription", Class.class);
-                    Class<?> classToRemove =
-                            
Thread.currentThread().getContextClassLoader().loadClass(mainTestClass);
-                    Object createSuiteDescInvoke = 
createSuiteDescription.invoke(descriptionClass, classToRemove);
-                    runListener
-                            .getClass()
-                            .getMethod("testSuiteFinished", descriptionClass)
-                            .invoke(runListener, createSuiteDescInvoke);
+                call(listener, method, argument);
+                safeListeners.add(listener);
+            } catch (InvocationTargetException e) {
+                // JUnit 4 only catches exceptions, an Error still goes up
+                if (!(e.getCause() instanceof Exception)) {
+                    throw new RuntimeException(e);
                 }
-            } catch (NoSuchMethodException e) {
-                // ignore as the RunListener might not have implemented any of 
those methods
-            } catch (Exception e) {
+                failures.add(newFailure(testMechanism(), e.getCause()));
+            } catch (IllegalAccessException e) {
                 throw new RuntimeException(e);
             }
-        });
+        }
+        for (Object failure : failures) {
+            notifyListeners(safeListeners, "testFailure", failure);

Review Comment:
   If the only configured listener throws during `testStarted`, `safeListeners` 
is empty and its exception is silently discarded. Surefire's reporter is 
registered separately, so a passing test can finish successfully without any 
diagnostic about the listener failure. Forward listener exceptions to an 
independent reporting or logging sink so they remain visible even when every 
custom listener throws.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to