Skip to content

Commit

Permalink
#190 Replacing many string unique IDs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Mar 15, 2016
1 parent 8ea046f commit 6fbef15
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 69 deletions.
Expand Up @@ -30,40 +30,40 @@ class XmlReportDataTests {
@Test
void resultOfTestIdentifierWithoutAnyReportedEventsIsEmpty() {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("test"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("child", "test"), "test"));
TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));

XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
Optional<TestExecutionResult> result = reportData.getResult(testPlan.getTestIdentifier("test"));
Optional<TestExecutionResult> result = reportData.getResult(testPlan.getTestIdentifier("[child:test]"));

assertThat(result).isEmpty();
}

@Test
void resultOfTestIdentifierWithoutReportedEventsIsFailureOfAncestor() {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("test"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("child", "test"), "test"));
TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));

XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
TestExecutionResult failureOfAncestor = failed(new RuntimeException("failed!"));
reportData.markFinished(testPlan.getTestIdentifier("[engine:engine]"), failureOfAncestor);

Optional<TestExecutionResult> result = reportData.getResult(testPlan.getTestIdentifier("test"));
Optional<TestExecutionResult> result = reportData.getResult(testPlan.getTestIdentifier("[child:test]"));

assertThat(result).contains(failureOfAncestor);
}

@Test
void resultOfTestIdentifierWithoutReportedEventsIsEmptyWhenAncestorWasSuccessful() {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("test"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("child", "test"), "test"));
TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));

XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
reportData.markFinished(testPlan.getTestIdentifier("[engine:engine]"), successful());

Optional<TestExecutionResult> result = reportData.getResult(testPlan.getTestIdentifier("test"));
Optional<TestExecutionResult> result = reportData.getResult(testPlan.getTestIdentifier("[child:test]"));

assertThat(result).isEmpty();
}
Expand Down
Expand Up @@ -49,11 +49,11 @@ void writesTestsuiteElementsWithoutTestcaseElementsWithoutAnyTests() throws Exce
@Test
void writesEmptySkippedElementForSkippedTestWithoutReason() throws Exception {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("skippedTest"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("test", "skippedTest"), "skippedTest"));

TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));
XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
reportData.markSkipped(testPlan.getTestIdentifier("skippedTest"), null);
reportData.markSkipped(testPlan.getTestIdentifier("[test:skippedTest]"), null);

StringWriter out = new StringWriter();
new XmlReportWriter(reportData).writeXmlReport(getOnlyElement(testPlan.getRoots()), out);
Expand All @@ -71,11 +71,11 @@ void writesEmptySkippedElementForSkippedTestWithoutReason() throws Exception {
@Test
void writesEmptyErrorElementForFailedTestWithoutCause() throws Exception {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("failedTest"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("test", "failedTest"), "failedTest"));

TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));
XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
reportData.markFinished(testPlan.getTestIdentifier("failedTest"), failed(null));
reportData.markFinished(testPlan.getTestIdentifier("[test:failedTest]"), failed(null));

StringWriter out = new StringWriter();
new XmlReportWriter(reportData).writeXmlReport(getOnlyElement(testPlan.getRoots()), out);
Expand All @@ -93,11 +93,11 @@ void writesEmptyErrorElementForFailedTestWithoutCause() throws Exception {
@Test
void omitsMessageAttributeForFailedTestWithThrowableWithoutMessage() throws Exception {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("failedTest"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("test", "failedTest"), "failedTest"));

TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));
XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());
reportData.markFinished(testPlan.getTestIdentifier("failedTest"), failed(new NullPointerException()));
reportData.markFinished(testPlan.getTestIdentifier("[test:failedTest]"), failed(new NullPointerException()));

StringWriter out = new StringWriter();
new XmlReportWriter(reportData).writeXmlReport(getOnlyElement(testPlan.getRoots()), out);
Expand Down
Expand Up @@ -345,15 +345,15 @@ void printsExceptionWhenReportCouldNotBeWritten(@Root Path tempDirectory) throws
@Test
void writesReportEntriesToSystemOutElement(@Root Path tempDirectory, TestReporter testReporter) throws Exception {
EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine");
engineDescriptor.addChild(new TestDescriptorStub("test"));
engineDescriptor.addChild(new TestDescriptorStub(UniqueId.root("child", "test"), "test"));
TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));

StringWriter out = new StringWriter();
XmlReportsWritingListener listener = new XmlReportsWritingListener(tempDirectory.toString(),
new PrintWriter(out));

listener.testPlanExecutionStarted(testPlan);
TestIdentifier testIdentifier = testPlan.getTestIdentifier("test");
TestIdentifier testIdentifier = testPlan.getTestIdentifier("[child:test]");
listener.executionStarted(testIdentifier);
listener.reportingEntryPublished(testIdentifier, ReportEntry.from("foo", "bar"));
Map<String, String> map = new LinkedHashMap<>();
Expand Down
Expand Up @@ -17,11 +17,11 @@ public class TestDescriptorStub extends AbstractTestDescriptor {
private final String name;
private final String displayName;

public TestDescriptorStub(String uniqueId) {
this(uniqueId, uniqueId, uniqueId);
public TestDescriptorStub(UniqueId uniqueId, String name) {
this(uniqueId, name, name);
}

public TestDescriptorStub(String uniqueId, String name, String displayName) {
public TestDescriptorStub(UniqueId uniqueId, String name, String displayName) {
super(uniqueId);
this.name = name;
this.displayName = displayName;
Expand Down
Expand Up @@ -18,15 +18,15 @@ public class TestDescriptorTests {

@Test
public void isRootWithoutParent() {
TestDescriptor root = new TestDescriptorStub("id");
TestDescriptor root = new TestDescriptorStub(UniqueId.root("root", "id"), "id");

assertTrue(root.isRoot());
}

@Test
public void isRootWithParent() {
TestDescriptor child = new TestDescriptorStub("child");
child.setParent(new TestDescriptorStub("root"));
TestDescriptor child = new TestDescriptorStub(UniqueId.root("child", "child"), "child");
child.setParent(new TestDescriptorStub(UniqueId.root("root", "root"), "root"));

assertFalse(child.isRoot());
}
Expand Down
Expand Up @@ -11,27 +11,18 @@
package org.junit.gen5.junit4.runner;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.gen5.api.Assertions.assertEquals;
import static org.junit.gen5.api.Assertions.assertThrows;
import static org.junit.gen5.api.Assertions.assertTrue;
import static org.junit.gen5.api.Assertions.*;
import static org.junit.gen5.api.Assumptions.assumeFalse;
import static org.junit.gen5.commons.util.CollectionUtils.getOnlyElement;
import static org.junit.gen5.engine.TestExecutionResult.successful;
import static org.junit.gen5.launcher.main.LauncherFactoryForTestingPurposesOnly.createLauncher;
import static org.junit.runner.Description.createSuiteDescription;
import static org.junit.runner.Description.createTestDescription;
import static org.junit.runner.Description.*;
import static org.junit.runner.manipulation.Filter.matchMethodDescription;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import java.util.List;

Expand All @@ -43,6 +34,7 @@
import org.junit.gen5.engine.TestDescriptorStub;
import org.junit.gen5.engine.TestEngine;
import org.junit.gen5.engine.TestTag;
import org.junit.gen5.engine.UniqueId;
import org.junit.gen5.engine.discovery.ClassFilter;
import org.junit.gen5.engine.discovery.ClassSelector;
import org.junit.gen5.engine.discovery.PackageSelector;
Expand Down Expand Up @@ -193,11 +185,11 @@ class Bar {
@Test
void convertsTestIdentifiersIntoDescriptions() throws Exception {

TestDescriptor container1 = new TestDescriptorStub("container1");
container1.addChild(new TestDescriptorStub("test1"));
TestDescriptor container2 = new TestDescriptorStub("container2");
container2.addChild(new TestDescriptorStub("test2a"));
container2.addChild(new TestDescriptorStub("test2b"));
TestDescriptor container1 = new TestDescriptorStub(UniqueId.root("root", "container1"), "container1");
container1.addChild(new TestDescriptorStub(UniqueId.root("root", "test1"), "test1"));
TestDescriptor container2 = new TestDescriptorStub(UniqueId.root("root", "container2"), "container2");
container2.addChild(new TestDescriptorStub(UniqueId.root("root", "test2a"), "test2a"));
container2.addChild(new TestDescriptorStub(UniqueId.root("root", "test2b"), "test2b"));
TestPlan testPlan = TestPlan.from(asList(container1, container2));

Launcher launcher = mock(Launcher.class);
Expand All @@ -210,16 +202,16 @@ void convertsTestIdentifiersIntoDescriptions() throws Exception {

List<Description> containerDescriptions = runnerDescription.getChildren();
assertThat(containerDescriptions).hasSize(2);
assertEquals(suiteDescription("container1"), containerDescriptions.get(0));
assertEquals(suiteDescription("container2"), containerDescriptions.get(1));
assertEquals(suiteDescription("[root:container1]"), containerDescriptions.get(0));
assertEquals(suiteDescription("[root:container2]"), containerDescriptions.get(1));

List<Description> testDescriptions = containerDescriptions.get(0).getChildren();
assertEquals(testDescription("test1"), getOnlyElement(testDescriptions));
assertEquals(testDescription("[root:test1]"), getOnlyElement(testDescriptions));

testDescriptions = containerDescriptions.get(1).getChildren();
assertThat(testDescriptions).hasSize(2);
assertEquals(testDescription("test2a"), testDescriptions.get(0));
assertEquals(testDescription("test2b"), testDescriptions.get(1));
assertEquals(testDescription("[root:test2a]"), testDescriptions.get(0));
assertEquals(testDescription("[root:test2b]"), testDescriptions.get(1));
}

}
Expand All @@ -230,46 +222,46 @@ class Filtering {
@Test
void appliesFilter() throws Exception {

TestDescriptor originalParent1 = new TestDescriptorStub("parent1");
originalParent1.addChild(new TestDescriptorStub("leaf1"));
TestDescriptor originalParent2 = new TestDescriptorStub("parent2");
originalParent2.addChild(new TestDescriptorStub("leaf2a"));
originalParent2.addChild(new TestDescriptorStub("leaf2b"));
TestDescriptor originalParent1 = new TestDescriptorStub(UniqueId.root("root", "parent1"), "parent1");
originalParent1.addChild(new TestDescriptorStub(UniqueId.root("root", "leaf1"), "leaf1"));
TestDescriptor originalParent2 = new TestDescriptorStub(UniqueId.root("root", "parent2"), "parent2");
originalParent2.addChild(new TestDescriptorStub(UniqueId.root("root", "leaf2a"), "leaf2a"));
originalParent2.addChild(new TestDescriptorStub(UniqueId.root("root", "leaf2b"), "leaf2b"));
TestPlan fullTestPlan = TestPlan.from(asList(originalParent1, originalParent2));

TestDescriptor filteredParent = new TestDescriptorStub("parent2");
filteredParent.addChild(new TestDescriptorStub("leaf2b"));
TestDescriptor filteredParent = new TestDescriptorStub(UniqueId.root("root", "parent2"), "parent2");
filteredParent.addChild(new TestDescriptorStub(UniqueId.root("root", "leaf2b"), "leaf2b"));
TestPlan filteredTestPlan = TestPlan.from(singleton(filteredParent));

Launcher launcher = mock(Launcher.class);
ArgumentCaptor<TestDiscoveryRequest> captor = ArgumentCaptor.forClass(TestDiscoveryRequest.class);
when(launcher.discover(captor.capture())).thenReturn(fullTestPlan).thenReturn(filteredTestPlan);

JUnit5 runner = new JUnit5(TestClass.class, launcher);
runner.filter(matchMethodDescription(testDescription("leaf2b")));
runner.filter(matchMethodDescription(testDescription("[root:leaf2b]")));

TestDiscoveryRequest lastDiscoveryRequest = captor.getValue();
List<UniqueIdSelector> uniqueIdSelectors = lastDiscoveryRequest.getSelectorsByType(UniqueIdSelector.class);
assertEquals("leaf2b", getOnlyElement(uniqueIdSelectors).getUniqueId());
assertEquals("[root:leaf2b]", getOnlyElement(uniqueIdSelectors).getUniqueId());

Description parentDescription = getOnlyElement(runner.getDescription().getChildren());
assertEquals(suiteDescription("parent2"), parentDescription);
assertEquals(suiteDescription("[root:parent2]"), parentDescription);

Description testDescription = getOnlyElement(parentDescription.getChildren());
assertEquals(testDescription("leaf2b"), testDescription);
assertEquals(testDescription("[root:leaf2b]"), testDescription);
}

@Test
void throwsNoTestsRemainExceptionWhenNoTestIdentifierMatchesFilter() throws Exception {
TestPlan testPlan = TestPlan.from(singleton(new TestDescriptorStub("test")));
TestPlan testPlan = TestPlan.from(singleton(new TestDescriptorStub(UniqueId.root("root", "test"), "test")));

Launcher launcher = mock(Launcher.class);
when(launcher.discover(any())).thenReturn(testPlan);

JUnit5 runner = new JUnit5(TestClass.class, launcher);

assertThrows(NoTestsRemainException.class,
() -> runner.filter(matchMethodDescription(suiteDescription("doesNotExist"))));
() -> runner.filter(matchMethodDescription(suiteDescription("[root:doesNotExist]"))));
}

}
Expand Down Expand Up @@ -312,9 +304,9 @@ void notifiesRunListenerOfTestExecution() throws Exception {

@Test
void reportsIgnoredEventsForLeafsWhenContainerIsSkipped() throws Exception {
TestDescriptor engineDescriptor = new TestDescriptorStub("engine");
TestDescriptor container = new TestDescriptorStub("container");
container.addChild(new TestDescriptorStub("leaf"));
TestDescriptor engineDescriptor = new TestDescriptorStub(UniqueId.forEngine("engine"), "engine");
TestDescriptor container = new TestDescriptorStub(UniqueId.root("root", "container"), "container");
container.addChild(new TestDescriptorStub(UniqueId.root("root", "leaf"), "leaf"));
engineDescriptor.addChild(container);

TestEngine engine = mock(TestEngine.class);
Expand All @@ -334,7 +326,7 @@ void reportsIgnoredEventsForLeafsWhenContainerIsSkipped() throws Exception {
notifier.addListener(runListener);
new JUnit5(TestClass.class, createLauncher(engine)).run(notifier);

verify(runListener).testIgnored(testDescription("leaf"));
verify(runListener).testIgnored(testDescription("[root:leaf]"));
verifyNoMoreInteractions(runListener);
}

Expand Down
Expand Up @@ -15,16 +15,18 @@
import org.junit.gen5.api.Test;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestDescriptorStub;
import org.junit.gen5.engine.UniqueId;

class TestIdentifierTests {

@Test
public void inheritsIdAndNamesFromDescriptor() {
TestDescriptor testDescriptor = new TestDescriptorStub("uniqueId", "name", "displayName");
TestDescriptor testDescriptor = new TestDescriptorStub(UniqueId.root("aType", "uniqueId"), "name",
"displayName");

TestIdentifier testIdentifier = TestIdentifier.from(testDescriptor);

assertEquals("uniqueId", testIdentifier.getUniqueId());
assertEquals("[aType:uniqueId]", testIdentifier.getUniqueId());
assertEquals("displayName", testIdentifier.getDisplayName());
assertEquals("name", testIdentifier.getName());
}
Expand Down
Expand Up @@ -10,9 +10,7 @@

package org.junit.gen5.launcher.listener;

import static org.junit.gen5.api.Assertions.assertAll;
import static org.junit.gen5.api.Assertions.assertEquals;
import static org.junit.gen5.api.Assertions.assertTrue;
import static org.junit.gen5.api.Assertions.*;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -23,6 +21,7 @@
import org.junit.gen5.engine.TestDescriptorStub;
import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.engine.TestSource;
import org.junit.gen5.engine.UniqueId;
import org.junit.gen5.engine.support.descriptor.JavaSource;
import org.junit.gen5.launcher.TestIdentifier;
import org.junit.gen5.launcher.TestPlan;
Expand Down Expand Up @@ -84,14 +83,15 @@ void reportingCorrectCounts() throws Exception {

@Test
void reportingCorrectFailures() throws Exception {
TestDescriptorStub testDescriptor = new TestDescriptorStub("2", "name", "failingTest") {
TestDescriptorStub testDescriptor = new TestDescriptorStub(UniqueId.root("root", "2"), "name", "failingTest") {
@Override
public Optional<TestSource> getSource() {
return Optional.of(new JavaSource(Object.class));
}
};
TestIdentifier failed = TestIdentifier.from(testDescriptor);
TestIdentifier aborted = TestIdentifier.from(new TestDescriptorStub("3", "name", "abortedTest"));
TestIdentifier aborted = TestIdentifier.from(
new TestDescriptorStub(UniqueId.root("root", "3"), "name", "abortedTest"));

listener.testPlanExecutionStarted(testPlan);
listener.executionStarted(failed);
Expand All @@ -113,7 +113,8 @@ public Optional<TestSource> getSource() {
}

private TestIdentifier createTestIdentifier(String uniqueId) {
TestIdentifier identifier = TestIdentifier.from(new TestDescriptorStub(uniqueId, "name", "displayName"));
TestIdentifier identifier = TestIdentifier.from(
new TestDescriptorStub(UniqueId.root("root", uniqueId), "name", "displayName"));
testPlan.add(identifier);
return identifier;
}
Expand Down

0 comments on commit 6fbef15

Please sign in to comment.