Skip to content

Commit

Permalink
#40: Extract RunnerExecutor from JUnit4TestEngine
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks
msg systems ag (http://www.msg-systems.com) for supporting
the JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
marcphilipp committed Jan 7, 2016
1 parent 4f5d6af commit 2172817
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
Expand Up @@ -10,7 +10,7 @@


package org.junit.gen5.engine.junit4; package org.junit.gen5.engine.junit4;


import static org.junit.gen5.engine.TestExecutionResult.*; import static org.junit.gen5.engine.TestExecutionResult.successful;


import org.junit.gen5.engine.EngineAwareTestDescriptor; import org.junit.gen5.engine.EngineAwareTestDescriptor;
import org.junit.gen5.engine.EngineDescriptor; import org.junit.gen5.engine.EngineDescriptor;
Expand All @@ -21,9 +21,7 @@
import org.junit.gen5.engine.TestPlanSpecification; import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.junit4.descriptor.RunnerTestDescriptor; import org.junit.gen5.engine.junit4.descriptor.RunnerTestDescriptor;
import org.junit.gen5.engine.junit4.discovery.JUnit4TestPlanSpecificationResolver; import org.junit.gen5.engine.junit4.discovery.JUnit4TestPlanSpecificationResolver;
import org.junit.gen5.engine.junit4.execution.RunListenerAdapter; import org.junit.gen5.engine.junit4.execution.RunnerExecutor;
import org.junit.gen5.engine.junit4.execution.TestRun;
import org.junit.runner.JUnitCore;


public class JUnit4TestEngine implements TestEngine { public class JUnit4TestEngine implements TestEngine {


Expand All @@ -49,28 +47,13 @@ public void execute(ExecutionRequest request) {
} }


private void executeAllChildren(ExecutionRequest request) { private void executeAllChildren(ExecutionRequest request) {
RunnerExecutor runnerExecutor = new RunnerExecutor(request.getEngineExecutionListener());
// @formatter:off // @formatter:off
request.getRootTestDescriptor() request.getRootTestDescriptor()
.getChildren() .getChildren()
.stream() .stream()
.map(RunnerTestDescriptor.class::cast) .map(RunnerTestDescriptor.class::cast)
.forEach(runnerTestDescriptor -> executeSingleRunner(runnerTestDescriptor, request.getEngineExecutionListener())); .forEach(runnerExecutor::execute);
// @formatter:on // @formatter:on
} }

private void executeSingleRunner(RunnerTestDescriptor runnerTestDescriptor,
EngineExecutionListener engineExecutionListener) {
TestRun testRun = new TestRun(runnerTestDescriptor);
JUnitCore core = new JUnitCore();
core.addListener(new RunListenerAdapter(testRun, engineExecutionListener));
try {
core.run(runnerTestDescriptor.getRunner());
}
catch (Throwable t) {
if (testRun.isNotStarted(runnerTestDescriptor)) {
engineExecutionListener.executionStarted(runnerTestDescriptor);
}
engineExecutionListener.executionFinished(runnerTestDescriptor, failed(t));
}
}
} }
@@ -0,0 +1,48 @@
/*
* Copyright 2015-2016 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.engine.junit4.execution;

import static org.junit.gen5.engine.TestExecutionResult.failed;

import org.junit.gen5.engine.EngineExecutionListener;
import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.engine.junit4.descriptor.RunnerTestDescriptor;
import org.junit.runner.JUnitCore;

public class RunnerExecutor {

private final EngineExecutionListener engineExecutionListener;

public RunnerExecutor(EngineExecutionListener engineExecutionListener) {
this.engineExecutionListener = engineExecutionListener;
}

public void execute(RunnerTestDescriptor runnerTestDescriptor) {
TestRun testRun = new TestRun(runnerTestDescriptor);
JUnitCore core = new JUnitCore();
core.addListener(new RunListenerAdapter(testRun, engineExecutionListener));
try {
core.run(runnerTestDescriptor.getRunner());
}
catch (Throwable t) {
reportUnexpectedFailure(testRun, runnerTestDescriptor, failed(t));
}
}

private void reportUnexpectedFailure(TestRun testRun, RunnerTestDescriptor runnerTestDescriptor,
TestExecutionResult result) {
if (testRun.isNotStarted(runnerTestDescriptor)) {
engineExecutionListener.executionStarted(runnerTestDescriptor);
}
engineExecutionListener.executionFinished(runnerTestDescriptor, result);
}

}

0 comments on commit 2172817

Please sign in to comment.