Copilot commented on code in PR #3654:
URL: https://github.com/apache/maven-surefire/pull/3654#discussion_r4733007433
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,286 @@
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));
Review Comment:
If failure or assumption-failure notification exhausts the listeners able to
handle it, `notifyListeners` throws before `testFinished` is delivered. A
listener that received `testStarted` can then miss its cleanup callback even
though the test completed. Deliver `testFinished` in a `finally` block,
preserving both notification errors if completion also throws, and add a
regression test.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,286 @@
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]));
Review Comment:
With a single listener that throws from `testSuiteFinished`,
`notifyListeners` propagates the exception and skips `testRunFinished`,
potentially preventing run-level cleanup. Previously the run-finished callback
was delivered first. Keep the new ordering, but guarantee `testRunFinished` in
a `finally` block and preserve both exceptions if it also throws.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,286 @@
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 wraps two or more failures in an error of its own. Its stack is
empty once the platform prunes it,
+ // or starts in Vintage with pruning off. One thrown by the test itself,
like assertAll, stays one failure.
+ private static boolean madeByVintage(MultipleFailuresError error) {
+ StackTraceElement[] stack = error.getStackTrace();
+ return error.getFailures().size() > 1
+ && (stack.length == 0 ||
stack[0].getClassName().startsWith("org.junit.vintage."));
+ }
+
+ 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.
+ // With no listener left to tell, it's thrown so the platform logs it.
+ 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<Throwable> errors = 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) {
+ errors.add(e.getCause());
+ } catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
- });
+ }
+ if (!errors.isEmpty() && safeListeners.isEmpty()) {
+ RuntimeException thrown = new RuntimeException(errors.get(0));
+ errors.subList(1, errors.size()).forEach(thrown::addSuppressed);
+ throw thrown;
+ }
+ for (Throwable error : errors) {
+ notifyListeners(safeListeners, "testFailure",
newFailure(testMechanism(), error));
Review Comment:
If one listener throws in `testStarted` and the remaining listener throws
while handling the generated `testFailure`, this recursive call propagates only
the second exception. The original listener error is absent from both the cause
and suppressed exceptions, so the platform logs the failure-handler error but
loses the error that triggered it. Preserve the original `errors` as suppressed
exceptions before propagating a recursive notification failure.
--
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]