Skip to content

Commit

Permalink
Polish ReportEntry and simplify XmlReportWriter
Browse files Browse the repository at this point in the history
Issue: #293
  • Loading branch information
sbrannen committed Jun 8, 2016
1 parent a8d9a68 commit 993c93e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 35 deletions.
Expand Up @@ -18,15 +18,13 @@
import static org.junit.gen5.console.tasks.XmlReportData.isFailure; import static org.junit.gen5.console.tasks.XmlReportData.isFailure;
import static org.junit.gen5.engine.TestExecutionResult.Status.FAILED; import static org.junit.gen5.engine.TestExecutionResult.Status.FAILED;


import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import java.util.TreeSet; import java.util.TreeSet;
Expand All @@ -35,8 +33,6 @@
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter; import javax.xml.stream.XMLStreamWriter;


import org.junit.gen5.commons.util.ExceptionUtils;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.TestExecutionResult; import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.engine.reporting.ReportEntry; import org.junit.gen5.engine.reporting.ReportEntry;
import org.junit.gen5.engine.support.descriptor.JavaClassSource; import org.junit.gen5.engine.support.descriptor.JavaClassSource;
Expand Down Expand Up @@ -190,39 +186,26 @@ private void writeFailureAttributesAndContent(Throwable throwable, XMLStreamWrit
writer.writeCharacters(readStackTrace(throwable)); writer.writeCharacters(readStackTrace(throwable));
} }


private void writeReportEntriesToSystemOutElement(TestIdentifier test, XMLStreamWriter writer) private void writeReportEntriesToSystemOutElement(TestIdentifier testIdentifier, XMLStreamWriter writer)
throws XMLStreamException { throws XMLStreamException {
List<ReportEntry> entries = reportData.getReportEntries(test); List<ReportEntry> entries = reportData.getReportEntries(testIdentifier);
if (!entries.isEmpty()) { if (!entries.isEmpty()) {
writer.writeStartElement("system-out"); writer.writeStartElement("system-out");
for (int i = 0; i < entries.size(); i++) { for (int i = 0; i < entries.size(); i++) {
ReportEntry reportEntry = entries.get(i); writer.writeCharacters(buildReportEntryDescription(entries.get(i), i + 1));
StringBuilder stringBuilder = new StringBuilder();
this.appendDescriptionOfReportEntry(stringBuilder, reportEntry, "#" + (i + 1));
writer.writeCharacters(stringBuilder.toString());
} }
writer.writeEndElement(); writer.writeEndElement();
} }
} }


private void appendDescriptionOfReportEntry(Appendable appendable, ReportEntry reportEntry, String entryTitle) { private String buildReportEntryDescription(ReportEntry reportEntry, int entryNumber) {
Preconditions.notNull(appendable, "appendable must not be null"); StringBuilder builder = new StringBuilder((format("Report Entry #{0} (timestamp: {1})\n", entryNumber,
Preconditions.notNull(entryTitle, "entryTitle must not be null"); ISO_LOCAL_DATE_TIME.format(reportEntry.getTimestamp()))));


// Add left padding reportEntry.getKeyValuePairs().entrySet().forEach(
entryTitle = (entryTitle.length() > 0 ? " " + entryTitle : entryTitle); entry -> builder.append(format("\t- {0}: {1}\n", entry.getKey(), entry.getValue())));


try { return builder.toString();
appendable.append(format("Report Entry{0} (creation timestamp: {1})\n", entryTitle,
ISO_LOCAL_DATE_TIME.format(reportEntry.getCreationTimestamp())));

for (Map.Entry<String, String> entry : reportEntry.getKeyValuePairs().entrySet()) {
appendable.append(format("\t- {0}: {1}\n", entry.getKey(), entry.getValue()));
}
}
catch (IOException cannotHappen) {
ExceptionUtils.throwAsUncheckedException(cannotHappen);
}
} }


private String getTime(TestIdentifier testIdentifier, NumberFormat numberFormat) { private String getTime(TestIdentifier testIdentifier, NumberFormat numberFormat) {
Expand Down
Expand Up @@ -32,7 +32,7 @@
@API(Experimental) @API(Experimental)
public final class ReportEntry { public final class ReportEntry {


private final LocalDateTime creationTimestamp = LocalDateTime.now(); private final LocalDateTime timestamp = LocalDateTime.now();
private final Map<String, String> keyValuePairs = new LinkedHashMap<>(); private final Map<String, String> keyValuePairs = new LinkedHashMap<>();


/** /**
Expand Down Expand Up @@ -79,20 +79,20 @@ public final Map<String, String> getKeyValuePairs() {
} }


/** /**
* Get the creation timestamp of this {@code ReportEntry}. * Get the timestamp for when this {@code ReportEntry} was created.
* *
* <p>Can be used, for example, to order entries. * <p>Can be used, for example, to order entries.
* *
* @return when this entry was created; never {@code null} * @return when this entry was created; never {@code null}
*/ */
public final LocalDateTime getCreationTimestamp() { public final LocalDateTime getTimestamp() {
return this.creationTimestamp; return this.timestamp;
} }


@Override @Override
public String toString() { public String toString() {
ToStringBuilder builder = new ToStringBuilder(this); ToStringBuilder builder = new ToStringBuilder(this);
builder.append("creationTimestamp", this.creationTimestamp); builder.append("timestamp", this.timestamp);
this.keyValuePairs.forEach(builder::append); this.keyValuePairs.forEach(builder::append);
return builder.toString(); return builder.toString();
} }
Expand Down
Expand Up @@ -56,7 +56,7 @@ public void reportingEntryPublishedMessageIndented() {


assertEquals(2, split.length); assertEquals(2, split.length);
assertAll("lines in the message", () -> assertEquals("Reported: failingTest [[engine:junit]]", split[0]), assertAll("lines in the message", () -> assertEquals("Reported: failingTest [[engine:junit]]", split[0]),
() -> assertTrue(split[1].startsWith(INDENTATION + "=> Reported values: ReportEntry [creationTimestamp =")), () -> assertTrue(split[1].startsWith(INDENTATION + "=> Reported values: ReportEntry [timestamp =")),
() -> assertTrue(split[1].endsWith(", foo = 'bar']"))); () -> assertTrue(split[1].endsWith(", foo = 'bar']")));
} }


Expand Down
Expand Up @@ -73,7 +73,7 @@ void writesReportEntry() throws Exception {
//@formatter:off //@formatter:off
assertThat(content) assertThat(content)
.containsSequence( .containsSequence(
"<system-out>Report Entry #1 (creation timestamp: ", "<system-out>Report Entry #1 (timestamp: ",
"- myKey: myValue", "- myKey: myValue",
"</system-out>"); "</system-out>");
//@formatter:on //@formatter:on
Expand Down
Expand Up @@ -375,9 +375,9 @@ void writesReportEntriesToSystemOutElement(@Root Path tempDirectory, TestReporte
"<testsuite", "<testsuite",
"<testcase", "<testcase",
"<system-out>", "<system-out>",
"Report Entry #1 (creation timestamp: " + Year.now(), "Report Entry #1 (timestamp: " + Year.now(),
"- foo: bar\n", "- foo: bar\n",
"Report Entry #2 (creation timestamp: " + Year.now(), "Report Entry #2 (timestamp: " + Year.now(),
"- bar: baz\n", "- bar: baz\n",
"- qux: foo\n", "- qux: foo\n",
"</system-out>", "</system-out>",
Expand Down

0 comments on commit 993c93e

Please sign in to comment.