Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08c5c93
commit d20d0a6
Showing
4 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
surefire-junit5/src/main/java/org/junit/gen5/surefire/AcceptAllClassesScannerFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.surefire; | ||
|
||
import org.apache.maven.surefire.util.ScannerFilter; | ||
|
||
final class AcceptAllClassesScannerFilter implements ScannerFilter { | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public boolean accept(Class testClass) { | ||
return true; | ||
} | ||
} |
70 changes: 65 additions & 5 deletions
70
surefire-junit5/src/main/java/org/junit/gen5/surefire/JUnitGen5Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,91 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.surefire; | ||
|
||
import static org.junit.gen5.engine.TestPlanSpecification.build; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
import org.apache.maven.surefire.providerapi.AbstractProvider; | ||
import org.apache.maven.surefire.providerapi.ProviderParameters; | ||
import org.apache.maven.surefire.report.ReporterException; | ||
import org.apache.maven.surefire.report.ReporterFactory; | ||
import org.apache.maven.surefire.report.RunListener; | ||
import org.apache.maven.surefire.report.SimpleReportEntry; | ||
import org.apache.maven.surefire.suite.RunResult; | ||
import org.apache.maven.surefire.testset.TestSetFailedException; | ||
import org.apache.maven.surefire.util.TestsToRun; | ||
import org.junit.gen5.engine.TestPlanSpecification; | ||
import org.junit.gen5.launcher.Launcher; | ||
|
||
public class JUnitGen5Provider extends AbstractProvider { | ||
|
||
public JUnitGen5Provider(ProviderParameters booterParameters) { | ||
// TODO Implement this. | ||
private final ProviderParameters parameters; | ||
|
||
public JUnitGen5Provider(ProviderParameters parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
@Override | ||
public Iterable<Class<?>> getSuites() { | ||
// TODO Implement this. | ||
return null; | ||
throw new UnsupportedOperationException("Not yet supported."); | ||
} | ||
|
||
@Override | ||
public RunResult invoke(Object forkTestSet) | ||
throws TestSetFailedException, ReporterException, InvocationTargetException { | ||
// TODO Implement this. | ||
throw new TestSetFailedException("Hello from JUnit5!"); | ||
if (forkTestSet != null) { | ||
// TODO Implement this. | ||
throw new UnsupportedOperationException("Not yet supported."); | ||
} | ||
|
||
TestsToRun testsToRun = scanClasspath(); | ||
return invoke(testsToRun); | ||
} | ||
|
||
private RunResult invoke(TestsToRun testsToRun) { | ||
RunResult runResult; | ||
ReporterFactory reporterFactory = parameters.getReporterFactory(); | ||
try { | ||
RunListener runListener = reporterFactory.createReporter(); | ||
|
||
Launcher launcher = new Launcher(); | ||
launcher.registerTestPlanExecutionListeners(new RunListenerAdapter(runListener)); | ||
|
||
for (Class<?> testClass : testsToRun) { | ||
invoke(testClass, launcher, runListener); | ||
} | ||
} | ||
finally { | ||
runResult = reporterFactory.close(); | ||
} | ||
return runResult; | ||
} | ||
|
||
private TestsToRun scanClasspath() { | ||
TestsToRun scannedClasses = parameters.getScanResult().applyFilter(new AcceptAllClassesScannerFilter(), | ||
parameters.getTestClassLoader()); | ||
TestsToRun orderedClasses = parameters.getRunOrderCalculator().orderTestClasses(scannedClasses); | ||
return orderedClasses; | ||
} | ||
|
||
private void invoke(Class<?> testClass, Launcher launcher, RunListener runListener) { | ||
SimpleReportEntry classEntry = new SimpleReportEntry(getClass().getName(), testClass.getName()); | ||
runListener.testSetStarting(classEntry); | ||
|
||
TestPlanSpecification specification = build(TestPlanSpecification.forClass(testClass)); | ||
launcher.execute(specification); | ||
|
||
runListener.testSetCompleted(classEntry); | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
surefire-junit5/src/main/java/org/junit/gen5/surefire/RunListenerAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.surefire; | ||
|
||
import static org.apache.maven.surefire.report.SimpleReportEntry.ignored; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.maven.surefire.report.PojoStackTraceWriter; | ||
import org.apache.maven.surefire.report.RunListener; | ||
import org.apache.maven.surefire.report.SimpleReportEntry; | ||
import org.apache.maven.surefire.report.StackTraceWriter; | ||
import org.junit.gen5.engine.JavaSource; | ||
import org.junit.gen5.engine.TestDescriptor; | ||
import org.junit.gen5.engine.TestExecutionListener; | ||
import org.junit.gen5.engine.TestSource; | ||
|
||
final class RunListenerAdapter implements TestExecutionListener { | ||
|
||
private RunListener runListener; | ||
|
||
public RunListenerAdapter(RunListener reporter) { | ||
this.runListener = reporter; | ||
} | ||
|
||
@Override | ||
public void testStarted(TestDescriptor testDescriptor) { | ||
runListener.testStarting(new SimpleReportEntry(getClassName(testDescriptor), testDescriptor.getDisplayName())); | ||
} | ||
|
||
@Override | ||
public void testSkipped(TestDescriptor testDescriptor, Throwable t) { | ||
StackTraceWriter stackTraceWriter = getStackTraceWriter(testDescriptor, t); | ||
runListener.testSkipped(ignored(getClassName(testDescriptor), testDescriptor.getDisplayName(), t.getMessage())); | ||
} | ||
|
||
@Override | ||
public void testAborted(TestDescriptor testDescriptor, Throwable t) { | ||
StackTraceWriter stackTraceWriter = getStackTraceWriter(testDescriptor, t); | ||
runListener.testAssumptionFailure( | ||
ignored(getClassName(testDescriptor), testDescriptor.getDisplayName(), t.getMessage())); | ||
} | ||
|
||
@Override | ||
public void testFailed(TestDescriptor testDescriptor, Throwable t) { | ||
StackTraceWriter stackTraceWriter = getStackTraceWriter(testDescriptor, t); | ||
runListener.testFailed(new SimpleReportEntry(getClassName(testDescriptor), testDescriptor.getDisplayName(), | ||
stackTraceWriter, null)); | ||
} | ||
|
||
@Override | ||
public void testSucceeded(TestDescriptor testDescriptor) { | ||
runListener.testSucceeded(new SimpleReportEntry(getClassName(testDescriptor), testDescriptor.getDisplayName())); | ||
} | ||
|
||
private StackTraceWriter getStackTraceWriter(TestDescriptor testDescriptor, Throwable t) { | ||
return new PojoStackTraceWriter(getClassName(testDescriptor), testDescriptor.getDisplayName(), t); | ||
} | ||
|
||
private String getClassName(TestDescriptor testDescriptor) { | ||
Optional<TestSource> testSource = testDescriptor.getSource(); | ||
if (testSource.isPresent() && testSource.get() instanceof JavaSource) { | ||
JavaSource javaSource = (JavaSource) testSource.get(); | ||
if (javaSource.getJavaClass().isPresent()) { | ||
return javaSource.getJavaClass().get().getName(); | ||
} | ||
} | ||
return testDescriptor.getUniqueId(); | ||
} | ||
} |