Skip to content

Commit

Permalink
Remove getName() from TestIdentifier
Browse files Browse the repository at this point in the history
Issue: #153
  • Loading branch information
sbrannen committed May 24, 2016
1 parent ebe3d7b commit 07fa215
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
Expand Up @@ -34,6 +34,7 @@

import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.engine.reporting.ReportEntry;
import org.junit.gen5.engine.support.descriptor.JavaClassSource;
import org.junit.gen5.launcher.TestIdentifier;

class XmlReportWriter {
Expand Down Expand Up @@ -111,11 +112,11 @@ private void writeSystemProperties(XMLStreamWriter writer) throws XMLStreamExcep

private void writeTestcase(TestIdentifier test, NumberFormat numberFormat, XMLStreamWriter writer)
throws XMLStreamException {

writer.writeStartElement("testcase");

writer.writeAttribute("name", test.getDisplayName());
Optional<TestIdentifier> parent = reportData.getTestPlan().getParent(test);
writer.writeAttribute("classname", parent.map(TestIdentifier::getName).orElse("<unrooted>"));
writer.writeAttribute("classname", getParentClassName(test));
writer.writeAttribute("time", getTime(test, numberFormat));
writer.writeComment("Unique ID: " + test.getUniqueId());

Expand All @@ -125,6 +126,17 @@ private void writeTestcase(TestIdentifier test, NumberFormat numberFormat, XMLSt
writer.writeEndElement();
}

private String getParentClassName(TestIdentifier test) {
// @formatter:off
return reportData.getTestPlan().getParent(test)
.flatMap(TestIdentifier::getSource)
.filter(JavaClassSource.class::isInstance)
.map(JavaClassSource.class::cast)
.map(source -> source.getJavaClass().getName())
.orElse("<unrooted>");
// @formatter:on
}

private void writeSkippedOrErrorOrFailureElement(TestIdentifier test, XMLStreamWriter writer)
throws XMLStreamException {
if (reportData.wasSkipped(test)) {
Expand Down
Expand Up @@ -39,7 +39,6 @@ public final class TestIdentifier implements Serializable {

private final String uniqueId;
private final String parentId;
private final String name;
private final String displayName;
private final Optional<TestSource> source;
private final Set<TestTag> tags;
Expand All @@ -52,22 +51,20 @@ public final class TestIdentifier implements Serializable {
public static TestIdentifier from(TestDescriptor testDescriptor) {
// TODO Use Flyweight Pattern for TestIdentifier?
String uniqueId = testDescriptor.getUniqueId().toString();
String name = testDescriptor.getName();
String displayName = testDescriptor.getDisplayName();
Optional<TestSource> source = testDescriptor.getSource();
Set<TestTag> tags = testDescriptor.getTags();
boolean test = testDescriptor.isTest();
boolean container = !test || !testDescriptor.getChildren().isEmpty();
Optional<String> parentId = testDescriptor.getParent().map(
parentDescriptor -> parentDescriptor.getUniqueId().toString());
return new TestIdentifier(uniqueId, name, displayName, source, tags, test, container, parentId);
return new TestIdentifier(uniqueId, displayName, source, tags, test, container, parentId);
}

private TestIdentifier(String uniqueId, String name, String displayName, Optional<TestSource> source,
Set<TestTag> tags, boolean test, boolean container, Optional<String> parentId) {
private TestIdentifier(String uniqueId, String displayName, Optional<TestSource> source, Set<TestTag> tags,
boolean test, boolean container, Optional<String> parentId) {
this.uniqueId = uniqueId;
this.parentId = parentId.orElse(null);
this.name = name;
this.displayName = displayName;
this.source = (source != null ? source : Optional.empty());
this.tags = unmodifiableSet(new LinkedHashSet<>(tags));
Expand Down Expand Up @@ -95,16 +92,6 @@ public Optional<String> getParentId() {
return Optional.ofNullable(this.parentId);
}

/**
* Get the name of the represented test or container.
*
* <p>The <em>name</em> is a technical name for a test or container. It
* must not be parsed or processed besides being displayed to end-users.
*/
public String getName() {
return this.name;
}

/**
* Get the display name of the represented test or container.
*
Expand Down Expand Up @@ -170,7 +157,6 @@ public String toString() {
return new ToStringBuilder(this)
.append("uniqueId", this.uniqueId)
.append("parentId", this.parentId)
.append("name", this.name)
.append("displayName", this.displayName)
.append("source", this.source)
.append("tags", this.tags)
Expand Down
Expand Up @@ -55,6 +55,9 @@
import org.junit.gen5.launcher.TestPlan;
import org.opentest4j.AssertionFailedError;

/**
* @since 5.0
*/
@ExtendWith(TempDirectory.class)
class XmlReportsWritingListenerTests {

Expand All @@ -67,12 +70,13 @@ void writesFileForSingleSucceedingTest(@Root Path tempDirectory) throws Exceptio
executeTests(engine, tempDirectory);

String content = readValidXmlFile(tempDirectory.resolve("TEST-dummy.xml"));

//@formatter:off
assertThat(content)
.containsSequence(
"<testsuite name=\"dummy\" tests=\"1\" skipped=\"0\" failures=\"0\" errors=\"0\"",
"<!--Unique ID: [engine:dummy]-->",
"<testcase name=\"display&lt;&gt;Name\" classname=\"dummy\"",
"<testcase name=\"display&lt;&gt;Name\" classname=\"&lt;unrooted&gt;\"",
"<!--Unique ID: [engine:dummy]/[test:succeedingTest]-->",
"</testcase>",
"</testsuite>")
Expand Down Expand Up @@ -200,8 +204,8 @@ void measuresTimesInSeconds(@Root Path tempDirectory) throws Exception {
assertThat(content)
.containsSequence(
"<testsuite", "time=\"1.665\"",
"<testcase name=\"firstTest\" classname=\"dummy\" time=\"0.333\"",
"<testcase name=\"secondTest\" classname=\"dummy\" time=\"0.333\"");
"<testcase name=\"firstTest\" classname=\"&lt;unrooted&gt;\" time=\"0.333\"",
"<testcase name=\"secondTest\" classname=\"&lt;unrooted&gt;\" time=\"0.333\"");
//@formatter:on
}

Expand All @@ -219,7 +223,7 @@ void testWithImmeasurableTimeIsOutputCorrectly(@Root Path tempDirectory) throws
assertThat(content)
.containsSequence(
"<testsuite",
"<testcase name=\"test\" classname=\"dummy\" time=\"0\"");
"<testcase name=\"test\" classname=\"&lt;unrooted&gt;\" time=\"0\"");
//@formatter:on
}

Expand Down
Expand Up @@ -17,17 +17,20 @@
import org.junit.gen5.engine.TestDescriptorStub;
import org.junit.gen5.engine.UniqueId;

/**
* @since 5.0
*/
class TestIdentifierTests {

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

TestIdentifier testIdentifier = TestIdentifier.from(testDescriptor);

assertEquals("[aType:uniqueId]", testIdentifier.getUniqueId());
assertEquals("displayName", testIdentifier.getDisplayName());
assertEquals("name", testIdentifier.getName());
}

}

0 comments on commit 07fa215

Please sign in to comment.