Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,14 @@
*/
public final class Surefire1095NpeInRunListenerIT extends SurefireJUnit4IntegrationTestCase {

/**
* Method Request.classes( String, Class[] ); exists in JUnit 4.0 - 4.4
* See JUnit4Reflector.
*/
@Test
public void testRunStartedWithJUnit40() {
public void testRunStartedWithJUnit4() {
unpack().setJUnitVersion("4.12")
.executeTest()
.verifyErrorFree(1)
.verifyTextInLog("Running JUnit 4.12")
.verifyTextInLog("testRunStarted [jiras.surefire1095.SomeTest]");
}

/**
* Method Request.classes( Class[] ); Since of JUnit 4.5
* See JUnit4Reflector.
*/
@Test
public void testRunStartedWithJUnit45() {
unpack().setJUnitVersion("4.12")
.executeTest()
.verifyErrorFree(1)
.verifyTextInLog("Running JUnit 4.12")
.verifyTextInLog("testRunStarted [jiras.surefire1095.SomeTest]");
}

@Test
public void testRunStartedWithJUnit47() {
unpack().setJUnitVersion("4.12")
.executeTest()
.verifyErrorFree(1)
.verifyTextInLog("Running JUnit 4.12")
.verifyTextInLog("testRunStarted [jiras.surefire1095.SomeTest]");
.verifyTextInLog("testRunStarted jiras.surefire1095.SomeTest")
.verifyTextInLog("testRunFinished true");
}

private SurefireLauncher unpack() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jiras.surefire1095;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;

public class Listener
Expand All @@ -16,4 +17,12 @@ public void testRunStarted( Description description )
? description.getChildren()
: description ) );
}

@Override
public void testRunFinished(Result result) throws Exception {
System.out.println("testRunFinished " +
(result != null || result.equals("null")
? result.wasSuccessful()
: result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.surefire.junitplatform;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

public class CustomTestExecutionListener implements TestExecutionListener {

private List<Object> listeners;

public CustomTestExecutionListener(List<Object> runListener) {
this.listeners = runListener;
}

@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 (Exception e) {
throw new RuntimeException(e);
}
});
}

@Override
public void testPlanExecutionFinished(TestPlan testPlan) {

String mainTestClass = extractMainClassName(testPlan);
listeners.forEach(runListener -> {
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 (Exception e) {
throw new RuntimeException(e);
}
});
}

private String extractMainClassName(TestPlan testPlan) {
for (Object identifier : testPlan.getRoots().toArray()) {
for (TestIdentifier child : testPlan.getChildren((TestIdentifier) identifier)) {
if (child.getType().isContainer()) {
if (child.getSource().isPresent() && child.getSource().get() instanceof ClassSource) {
return ((ClassSource) child.getSource().get()).getClassName();
}
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.maven.surefire.api.report.TestReportListener;
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.testset.TestSetFailedException;
import org.apache.maven.surefire.api.util.ReflectionUtils;
import org.apache.maven.surefire.api.util.ScanResult;
import org.apache.maven.surefire.api.util.TestsToRun;
import org.apache.maven.surefire.shared.utils.StringUtils;
Expand Down Expand Up @@ -220,29 +221,51 @@ private void invokeAllTests(LauncherAdapter launcher, TestsToRun testsToRun, Run
}

private void execute(LauncherAdapter launcher, TestsToRun testsToRun, RunListenerAdapter adapter) {
// parameters.getProviderProperties().get(CONFIGURATION_PARAMETERS)
// add this LegacyXmlReportGeneratingListener ?
// adapter,
// new LegacyXmlReportGeneratingListener(
// new File("target", "junit-platform").toPath(), new PrintWriter(System.out))
// };
List<TestExecutionListener> testExecutionListeners = new ArrayList<>();
testExecutionListeners.add(adapter);

TestExecutionListener[] testExecutionListeners = new TestExecutionListener[] {adapter};
TestExecutionListener customListeners = createCustomListener();
if (customListeners != null) {
testExecutionListeners.add(customListeners);
}

if (testsToRun.allowEagerReading()) {
List<DiscoverySelector> selectors = new ArrayList<>();
testsToRun.iterator().forEachRemaining(c -> selectors.add(selectClass(c.getName())));

LauncherDiscoveryRequestBuilder builder = newRequest().selectors(selectors);
launcher.execute(builder.build(), testExecutionListeners);
launcher.execute(
builder.build(),
testExecutionListeners.toArray(new TestExecutionListener[testExecutionListeners.size()]));
} else {
testsToRun.iterator().forEachRemaining(c -> {
LauncherDiscoveryRequestBuilder builder = newRequest().selectors(selectClass(c.getName()));
launcher.execute(builder.build(), testExecutionListeners);
launcher.execute(
builder.build(),
testExecutionListeners.toArray(new TestExecutionListener[testExecutionListeners.size()]));
});
}
}

private TestExecutionListener createCustomListener() {

String listeners = parameters.getProviderProperties().get("listener");
if (listeners != null) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
List<Object> runListeners = new ArrayList<>();
for (String listener : listeners.split(",")) {
try {
Class<?> runListenerClass = cl.loadClass("org.junit.runner.notification.RunListener");
runListeners.add(ReflectionUtils.instantiate(cl, listener, runListenerClass));
} catch (ClassCastException | ClassNotFoundException c) {
throw new RuntimeException(c);
}
}
return new CustomTestExecutionListener(runListeners);
}
return null;
}

private LauncherDiscoveryRequest buildLauncherDiscoveryRequestForRerunFailures(RunListenerAdapter adapter) {
LauncherDiscoveryRequestBuilder builder = newRequest();
// Iterate over recorded failures
Expand Down
Loading