Skip to content

Commit

Permalink
Introduce TestExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 28, 2015
1 parent ffc643c commit 86930be
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 47 deletions.
Expand Up @@ -2,19 +2,15 @@


import static java.lang.String.*; import static java.lang.String.*;
import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.*;
import static org.junit.gen5.commons.util.ReflectionUtils.*;


import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;


import org.junit.gen5.api.After;
import org.junit.gen5.api.Before;
import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestEngine; import org.junit.gen5.engine.TestEngine;
import org.junit.gen5.engine.TestExecutionListener; import org.junit.gen5.engine.TestExecutionListener;
Expand Down Expand Up @@ -78,60 +74,39 @@ public boolean supports(TestDescriptor testDescriptor) {
@Override @Override
public void execute(Collection<TestDescriptor> testDescriptors, TestExecutionListener testExecutionListener) { public void execute(Collection<TestDescriptor> testDescriptors, TestExecutionListener testExecutionListener) {
for (TestDescriptor testDescriptor : testDescriptors) { for (TestDescriptor testDescriptor : testDescriptors) {
try {
testExecutionListener.testStarted(testDescriptor);


JavaTestDescriptor javaTestDescriptor = (JavaTestDescriptor) testDescriptor; Preconditions.condition(testDescriptor instanceof JavaTestDescriptor,
executeTest(javaTestDescriptor); String.format("%s supports test descriptors of type %s, not of type %s", getClass().getSimpleName(),
testExecutionListener.testSucceeded(testDescriptor); JavaTestDescriptor.class.getName(),
(testDescriptor != null ? testDescriptor.getClass().getName() : "null")));

JavaTestDescriptor javaTestDescriptor = (JavaTestDescriptor) testDescriptor;

try {
testExecutionListener.testStarted(javaTestDescriptor);
new TestExecutor(javaTestDescriptor).execute();
testExecutionListener.testSucceeded(javaTestDescriptor);
} }
catch (InvocationTargetException ex) { catch (InvocationTargetException ex) {
Throwable targetException = ex.getTargetException(); Throwable targetException = ex.getTargetException();
if (targetException instanceof TestSkippedException) { if (targetException instanceof TestSkippedException) {
testExecutionListener.testSkipped(testDescriptor, targetException); testExecutionListener.testSkipped(javaTestDescriptor, targetException);
} }
else if (targetException instanceof TestAbortedException) { else if (targetException instanceof TestAbortedException) {
testExecutionListener.testAborted(testDescriptor, targetException); testExecutionListener.testAborted(javaTestDescriptor, targetException);
} }
else { else {
testExecutionListener.testFailed(testDescriptor, targetException); testExecutionListener.testFailed(javaTestDescriptor, targetException);
} }
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) { }
throw new IllegalStateException( catch (NoSuchMethodException | InstantiationException | IllegalAccessException ex) {
String.format("Test %s is not well-formed and cannot be executed", testDescriptor.getUniqueId())); throw new IllegalStateException(String.format("Test %s is not well-formed and cannot be executed",
} catch (Exception ex) { javaTestDescriptor.getUniqueId()), ex);
testExecutionListener.testFailed(testDescriptor, ex); }
catch (Exception ex) {
testExecutionListener.testFailed(javaTestDescriptor, ex);
} }
} }
} }


protected void executeTest(JavaTestDescriptor javaTestDescriptor) throws Exception {
Class<?> testClass = javaTestDescriptor.getTestClass();

// TODO Extract test instantiation
Object testInstance = newInstance(testClass);

executeBeforeMethods(testClass, testInstance);
invokeMethod(javaTestDescriptor.getTestMethod(), testInstance);
executeAfterMethods(testClass, testInstance);
}

private void executeBeforeMethods(Class<?> testClass, Object testInstance) throws Exception {
for (Method method: findAnnotatedMethods(testClass, Before.class)) {
invokeMethod(method, testInstance);
}
}

private void executeAfterMethods(Class<?> testClass, Object testInstance) throws Exception {
for (Method method : findAnnotatedMethods(testClass, After.class)) {
invokeMethod(method, testInstance);
}
}

private List<Method> findAnnotatedMethods(Class<?> testClass, Class<? extends Annotation> annotationType) {
return Arrays.stream(testClass.getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(annotationType))
.collect(toList());
}

} }
@@ -0,0 +1,58 @@

package org.junit.gen5.engine.junit5;

import static java.util.stream.Collectors.*;
import static org.junit.gen5.commons.util.ReflectionUtils.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.junit.gen5.api.After;
import org.junit.gen5.api.Before;

/**
* @author Sam Brannen
* @author Matthias Merdes
* @since 5.0
*/
class TestExecutor {

private final JavaTestDescriptor testDescriptor;


TestExecutor(JavaTestDescriptor testDescriptor) {
this.testDescriptor = testDescriptor;
}

void execute() throws Exception {
Class<?> testClass = this.testDescriptor.getTestClass();

// TODO Extract test instantiation
Object testInstance = newInstance(testClass);

executeBeforeMethods(testClass, testInstance);
invokeMethod(this.testDescriptor.getTestMethod(), testInstance);
executeAfterMethods(testClass, testInstance);
}

private void executeBeforeMethods(Class<?> testClass, Object testInstance) throws Exception {
for (Method method : findAnnotatedMethods(testClass, Before.class)) {
invokeMethod(method, testInstance);
}
}

private void executeAfterMethods(Class<?> testClass, Object testInstance) throws Exception {
for (Method method : findAnnotatedMethods(testClass, After.class)) {
invokeMethod(method, testInstance);
}
}

private List<Method> findAnnotatedMethods(Class<?> testClass, Class<? extends Annotation> annotationType) {
return Arrays.stream(testClass.getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(annotationType))
.collect(toList());
}

}

0 comments on commit 86930be

Please sign in to comment.