Copilot commented on code in PR #3654:
URL: https://github.com/apache/maven-surefire/pull/3654#discussion_r4729370716
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,188 @@
package org.apache.maven.surefire.junitplatform;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
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 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();
+
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);
+ Throwable error = testExecutionResult.getThrowable().orElse(null);
+ if (status == FAILED) {
+ notifyListeners("testFailure", newFailure(description, error));
Review Comment:
When a JUnit 4 test fails both in its body and in `@After`, Vintage combines
the original failure events into `MultipleFailuresError` (see its
[default-branch
aggregation](https://github.com/junit-team/junit-framework/blob/5e2b5533db587737b06b9091f23cf09926ab55ae/junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/TestRun.java#L189-L208)).
Passing that aggregate directly here gives listeners one failure with a
wrapper exception instead of the separate callbacks and original exceptions
JUnit 4 supplies. Please unpack Vintage's aggregate into individual `Failure`
events before finishing the test once, and cover a body-plus-teardown failure.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,188 @@
package org.apache.maven.surefire.junitplatform;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
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 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();
+
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);
+ Throwable error = testExecutionResult.getThrowable().orElse(null);
+ if (status == FAILED) {
+ notifyListeners("testFailure", newFailure(description, error));
+ } else if (status == ABORTED) {
+ 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 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)
+ : suiteDescription(className);
+ }
+ 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);
+ }
+
+ 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) {
+ 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);
+ }
+ for (Object listener : listeners) {
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);
- }
- } catch (NoSuchMethodException e) {
- // ignore as the RunListener might not have implemented any of
those methods
- } catch (Exception e) {
+ method.invoke(listener, argument);
Review Comment:
When Platform tests run in parallel, these per-test callbacks can invoke the
same listener concurrently and corrupt unsynchronized listener state. JUnit 4
[serializes unmarked listeners using a shared
monitor](https://github.com/junit-team/junit4/blob/890f3c972647de378f25e7271d8fbbd9d3456b79/src/main/java/org/junit/runner/notification/RunNotifier.java#L42-L52)
(default branch at `890f3c9`). Preserve that synchronization while allowing
listeners annotated with `@RunListener.ThreadSafe` to run concurrently.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,188 @@
package org.apache.maven.surefire.junitplatform;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
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 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();
+
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);
+ Throwable error = testExecutionResult.getThrowable().orElse(null);
+ if (status == FAILED) {
+ notifyListeners("testFailure", newFailure(description, error));
+ } else if (status == ABORTED) {
+ 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 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)
+ : suiteDescription(className);
+ }
+ 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);
+ }
+
+ 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);
Review Comment:
Platform permits `failed(null)` and `aborted(null)`, so this can construct a
JUnit 4 `Failure` with no exception. Listeners calling
[`Failure.getMessage()`](https://github.com/junit-team/junit4/blob/890f3c972647de378f25e7271d8fbbd9d3456b79/src/main/java/org/junit/runner/notification/Failure.java#L84-L90)
(default branch at `890f3c9`) then throw `NullPointerException` instead of
recording the event. Supply a non-null explanatory throwable for both statuses
and add coverage for results without a cause.
##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnit4ListenersAdapter.java:
##########
@@ -19,185 +19,188 @@
package org.apache.maven.surefire.junitplatform;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
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 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();
+
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);
+ Throwable error = testExecutionResult.getThrowable().orElse(null);
+ if (status == FAILED) {
+ notifyListeners("testFailure", newFailure(description, error));
+ } else if (status == ABORTED) {
+ 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 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)
+ : suiteDescription(className);
+ }
+ 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);
+ }
+
+ 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) {
+ 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);
+ }
+ for (Object listener : listeners) {
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);
- }
- } catch (NoSuchMethodException e) {
- // ignore as the RunListener might not have implemented any of
those methods
- } catch (Exception e) {
+ method.invoke(listener, argument);
+ } catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
Review Comment:
If a listener throws from the newly forwarded `testFailure` or
`testAssumptionFailure`, this rethrow exits `executionFinished` before
`testFinished`; subsequent listeners also miss the failure event. JUnit 4
isolates listener exceptions and reports them to the remaining listeners (see
its [default-branch safe
dispatch](https://github.com/junit-team/junit4/blob/890f3c972647de378f25e7271d8fbbd9d3456b79/src/main/java/org/junit/runner/notification/RunNotifier.java#L66-L81)).
Please use equivalent safe dispatch so one failing listener cannot suppress
other listeners or the matching finish callback.
--
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]