Skip to content

Commit

Permalink
Spike tree-based "implementation" of JUnit5 engine
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 8, 2015
1 parent 5eb4642 commit 28ffd5e
Show file tree
Hide file tree
Showing 17 changed files with 533 additions and 50 deletions.
Expand Up @@ -23,15 +23,15 @@
/**
* @since 5.0
*/
public abstract class AbstractTestDescriptor implements MutableTestDescriptor {
public abstract class AbstractTestDescriptor implements TestDescriptor {

private final String uniqueId;

private MutableTestDescriptor parent;
private TestDescriptor parent;

private TestSource source;

private final Set<MutableTestDescriptor> children = new LinkedHashSet<>();
private final Set<TestDescriptor> children = new LinkedHashSet<>();

protected AbstractTestDescriptor(String uniqueId) {
Preconditions.notBlank(uniqueId, "uniqueId must not be null or empty");
Expand All @@ -44,17 +44,17 @@ public final String getUniqueId() {
}

@Override
public Optional<MutableTestDescriptor> getParent() {
public Optional<TestDescriptor> getParent() {
return Optional.ofNullable(this.parent);
}

@Override
public final void setParent(MutableTestDescriptor parent) {
public final void setParent(TestDescriptor parent) {
this.parent = parent;
}

@Override
public void removeChild(MutableTestDescriptor child) {
public void removeChild(TestDescriptor child) {
this.children.remove(child);
child.setParent(null);
}
Expand Down Expand Up @@ -82,14 +82,14 @@ public Optional<? extends TestDescriptor> findByUniqueId(String uniqueId) {
}

@Override
public final void addChild(MutableTestDescriptor child) {
public final void addChild(TestDescriptor child) {
Preconditions.notNull(child, "child must not be null");
child.setParent(this);
this.children.add(child);
}

@Override
public final Set<MutableTestDescriptor> getChildren() {
public Set<? extends TestDescriptor> getChildren() {
return Collections.unmodifiableSet(this.children);
}

Expand Down
@@ -0,0 +1,8 @@

package org.junit.gen5.engine;

public interface Child {

Context execute(Context context) throws Throwable;

}
29 changes: 29 additions & 0 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/Context.java
@@ -0,0 +1,29 @@

package org.junit.gen5.engine;

import java.util.HashMap;
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));
}

}
Expand Up @@ -32,6 +32,11 @@ public final boolean isTest() {
return false;
}

@Override
public boolean isContainer() {
return true;
}

public TestEngine getEngine() {
return engine;
}
Expand Down

This file was deleted.

10 changes: 10 additions & 0 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/Parent.java
@@ -0,0 +1,10 @@

package org.junit.gen5.engine;

public interface Parent {

Context beforeAll(Context context);

Context afterAll(Context context);

}
Expand Up @@ -30,10 +30,14 @@ public interface TestDescriptor {

String getDisplayName();

Optional<? extends TestDescriptor> getParent();
Optional<TestDescriptor> getParent();

void setParent(TestDescriptor parent);

boolean isTest();

boolean isContainer();

default boolean isRoot() {
return getParent() == null;
}
Expand All @@ -42,6 +46,10 @@ default boolean isRoot() {

Set<? extends TestDescriptor> getChildren();

void addChild(TestDescriptor descriptor);

void removeChild(TestDescriptor descriptor);

default Set<? extends TestDescriptor> allDescendants() {
Set<TestDescriptor> all = new HashSet<>();
all.addAll(getChildren());
Expand Down
@@ -0,0 +1,47 @@

package org.junit.gen5.engine;

public abstract class TreeBasedTestEngine implements TestEngine {

@Override
public abstract TestDescriptor discoverTests(TestPlanSpecification specification);

@Override
public final void execute(ExecutionRequest request) {
try {
TestDescriptor rootTestDescriptor = request.getRootTestDescriptor();
executeAll(rootTestDescriptor, request.getEngineExecutionListener(), new Context());
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

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

}
Expand Up @@ -118,4 +118,24 @@ public String toString() {
return engineRootTestDescriptors.values().toString();
}

@Override
public void setParent(TestDescriptor parent) {
throw new UnsupportedOperationException();
}

@Override
public boolean isContainer() {
return true;
}

@Override
public void addChild(TestDescriptor descriptor) {
// TODO Auto-generated method stub
}

@Override
public void removeChild(TestDescriptor descriptor) {
// TODO Auto-generated method stub
}

}
Expand Up @@ -115,6 +115,11 @@ public String getDisplayName() {
public boolean isTest() {
return false;
}

@Override
public boolean isContainer() {
return true;
}
}

class LeafDescriptor extends AbstractTestDescriptor {
Expand All @@ -132,4 +137,9 @@ public String getDisplayName() {
public boolean isTest() {
return true;
}

@Override
public boolean isContainer() {
return false;
}
}
Expand Up @@ -31,4 +31,9 @@ public boolean isTest() {
return getChildren().isEmpty();
}

@Override
public boolean isContainer() {
return !isTest();
}

}

0 comments on commit 28ffd5e

Please sign in to comment.