Skip to content

Commit

Permalink
TestEngine-specific context using type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 8, 2015
1 parent 28ffd5e commit 57b3122
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 50 deletions.
13 changes: 11 additions & 2 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/Child.java
@@ -1,8 +1,17 @@
/*
* 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.engine; package org.junit.gen5.engine;


public interface Child { public interface Child<C extends Context> {


Context execute(Context context) throws Throwable; C execute(C context) throws Throwable;


} }
35 changes: 10 additions & 25 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/Context.java
@@ -1,29 +1,14 @@
/*
* 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.engine; package org.junit.gen5.engine;


import java.util.HashMap; public interface Context {
import java.util.Map;

public class Context {

private final Map<String, Object> map;

public Context() {
this(new HashMap<>());
}

private Context(Map<String, Object> map) {
this.map = map;
}

public Context with(String key, Object value) {
Map<String, Object> newMap = new HashMap<>(map);
newMap.put(key, value);
return new Context(newMap);
}

public <T> T get(String key, Class<T> clazz) {
return clazz.cast(map.get(key));
}

} }
15 changes: 12 additions & 3 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/Parent.java
@@ -1,10 +1,19 @@
/*
* 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.engine; package org.junit.gen5.engine;


public interface Parent { public interface Parent<C extends Context> {


Context beforeAll(Context context); C beforeAll(C context);


Context afterAll(Context context); C afterAll(C context);


} }
@@ -1,7 +1,16 @@
/*
* 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.engine; package org.junit.gen5.engine;


public abstract class TreeBasedTestEngine implements TestEngine { public abstract class TreeBasedTestEngine<C extends Context> implements TestEngine {


@Override @Override
public abstract TestDescriptor discoverTests(TestPlanSpecification specification); public abstract TestDescriptor discoverTests(TestPlanSpecification specification);
Expand All @@ -10,37 +19,38 @@ public abstract class TreeBasedTestEngine implements TestEngine {
public final void execute(ExecutionRequest request) { public final void execute(ExecutionRequest request) {
try { try {
TestDescriptor rootTestDescriptor = request.getRootTestDescriptor(); TestDescriptor rootTestDescriptor = request.getRootTestDescriptor();
executeAll(rootTestDescriptor, request.getEngineExecutionListener(), new Context()); executeAll(rootTestDescriptor, request.getEngineExecutionListener(), createContext());
} }
catch (Exception e) { catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }


private <T> void executeAll(TestDescriptor parentDescriptor, EngineExecutionListener listener, protected abstract C createContext();
Context parentContext) throws Exception {
Context context = parentContext; private <T> void executeAll(TestDescriptor parentDescriptor, EngineExecutionListener listener, C parentContext)
throws Exception {
C context = parentContext;
if (parentDescriptor instanceof Parent) { if (parentDescriptor instanceof Parent) {
context = ((Parent) parentDescriptor).beforeAll(context); context = ((Parent<C>) parentDescriptor).beforeAll(context);
} }
for (TestDescriptor childDescriptor : parentDescriptor.getChildren()) { for (TestDescriptor childDescriptor : parentDescriptor.getChildren()) {
if (childDescriptor instanceof Child) { if (childDescriptor instanceof Child) {
Child child = (Child) childDescriptor; Child<C> child = (Child<C>) childDescriptor;
try { try {
listener.testStarted(childDescriptor); listener.testStarted(childDescriptor);
context = child.execute(context); C childContext = child.execute(context);
listener.testSucceeded(childDescriptor); listener.testSucceeded(childDescriptor);
} }
catch (Throwable t) { catch (Throwable t) {
listener.testFailed(childDescriptor, t); listener.testFailed(childDescriptor, t);
context = context.with("Throwable", t);
} }
} }
executeAll(childDescriptor, listener, context); executeAll(childDescriptor, listener, context);
} }
if (parentDescriptor instanceof Parent) { if (parentDescriptor instanceof Parent) {
context = ((Parent) parentDescriptor).afterAll(context); context = ((Parent<C>) parentDescriptor).afterAll(context);
} }
} }


Expand Down
@@ -1,12 +1,20 @@
/*
* 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.engine.junit5; package org.junit.gen5.engine.junit5;


import org.junit.gen5.commons.util.ReflectionUtils; import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.AbstractTestDescriptor; import org.junit.gen5.engine.AbstractTestDescriptor;
import org.junit.gen5.engine.Context;
import org.junit.gen5.engine.Parent; import org.junit.gen5.engine.Parent;


public class JUnit5ClassDescriptor extends AbstractTestDescriptor implements Parent { public class JUnit5ClassDescriptor extends AbstractTestDescriptor implements Parent<JUnit5Context> {


private final Class<?> testClass; private final Class<?> testClass;


Expand All @@ -31,12 +39,12 @@ public boolean isContainer() {
} }


@Override @Override
public Context beforeAll(Context context) { public JUnit5Context beforeAll(JUnit5Context context) {
return context.with("TestInstanceProvider", testInstanceProvider()); return context.withTestInstanceProvider(testInstanceProvider());
} }


@Override @Override
public Context afterAll(Context context) { public JUnit5Context afterAll(JUnit5Context context) {
return context; return context;
} }


Expand Down
@@ -0,0 +1,48 @@
/*
* 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.engine.junit5;

import java.util.HashMap;
import java.util.Map;

import org.junit.gen5.engine.Context;

public class JUnit5Context implements Context {

private final Map<String, Object> map;

public JUnit5Context() {
this(new HashMap<>());
}

private JUnit5Context(Map<String, Object> map) {
this.map = map;
}

public JUnit5Context withTestInstanceProvider(TestInstanceProvider testInstanceProvider) {
return with(TestInstanceProvider.class.getName(), testInstanceProvider);
}

public TestInstanceProvider getTestInstanceProvider() {
return get(TestInstanceProvider.class.getName(), TestInstanceProvider.class);
}

private JUnit5Context with(String key, Object value) {
Map<String, Object> newMap = new HashMap<>(map);
newMap.put(key, value);
return new JUnit5Context(newMap);
}

private <T> T get(String key, Class<T> clazz) {
return clazz.cast(map.get(key));
}

}
@@ -1,3 +1,12 @@
/*
* 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.engine.junit5; package org.junit.gen5.engine.junit5;


Expand Down
@@ -1,3 +1,12 @@
/*
* 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.engine.junit5; package org.junit.gen5.engine.junit5;


Expand All @@ -13,9 +22,8 @@
import org.junit.gen5.commons.util.ReflectionUtils.MethodSortOrder; import org.junit.gen5.commons.util.ReflectionUtils.MethodSortOrder;
import org.junit.gen5.engine.AbstractTestDescriptor; import org.junit.gen5.engine.AbstractTestDescriptor;
import org.junit.gen5.engine.Child; import org.junit.gen5.engine.Child;
import org.junit.gen5.engine.Context;


public class JUnit5MethodDescriptor extends AbstractTestDescriptor implements Child { public class JUnit5MethodDescriptor extends AbstractTestDescriptor implements Child<JUnit5Context> {


private final Class<?> testClass; private final Class<?> testClass;
private final Method method; private final Method method;
Expand All @@ -42,8 +50,8 @@ public boolean isContainer() {
} }


@Override @Override
public Context execute(Context context) throws Throwable { public JUnit5Context execute(JUnit5Context context) throws Throwable {
TestInstanceProvider provider = context.get("TestInstanceProvider", TestInstanceProvider.class); TestInstanceProvider provider = context.getTestInstanceProvider();
Object testInstance = provider.getTestInstance(); Object testInstance = provider.getTestInstance();
for (Method method : findAnnotatedMethods(testClass, BeforeEach.class, MethodSortOrder.HierarchyDown)) { for (Method method : findAnnotatedMethods(testClass, BeforeEach.class, MethodSortOrder.HierarchyDown)) {
ReflectionUtils.invokeMethod(method, testInstance); ReflectionUtils.invokeMethod(method, testInstance);
Expand Down
Expand Up @@ -21,7 +21,7 @@
import org.junit.gen5.engine.TestPlanSpecificationElementVisitor; import org.junit.gen5.engine.TestPlanSpecificationElementVisitor;
import org.junit.gen5.engine.TreeBasedTestEngine; import org.junit.gen5.engine.TreeBasedTestEngine;


public class JUnit5TestEngine extends TreeBasedTestEngine { public class JUnit5TestEngine extends TreeBasedTestEngine<JUnit5Context> {


private static final String ENGINE_ID = "junit5"; private static final String ENGINE_ID = "junit5";


Expand Down Expand Up @@ -51,4 +51,9 @@ public void visitClass(Class<?> testClass) {
}); });
return engineDescriptor; return engineDescriptor;
} }

@Override
protected JUnit5Context createContext() {
return new JUnit5Context();
}
} }
@@ -1,3 +1,12 @@
/*
* 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.engine.junit5; package org.junit.gen5.engine.junit5;


Expand Down

0 comments on commit 57b3122

Please sign in to comment.