Skip to content

Commit

Permalink
Allow format specifiers in exception message
Browse files Browse the repository at this point in the history
Prior to this commit format specifiers in exception messages resulted
in a MissingFormatArgumentException. Now every user-defined string is
treated as an argument itself, effectively preventing a reformatting
of the passed string.

Closes #1600
  • Loading branch information
sormuras committed Sep 27, 2018
1 parent a1db201 commit 6eb5531
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
frames.push(System.currentTimeMillis());

long tests = testPlan.countTestIdentifiers(TestIdentifier::isTest);
printf(NONE, "Test plan execution started. Number of static tests: ");
printf(NONE, "%s", "Test plan execution started. Number of static tests: ");
printf(Color.TEST, "%d%n", tests);
printf(Color.CONTAINER, "%s%n", theme.root());
}
Expand All @@ -71,7 +71,7 @@ public void testPlanExecutionFinished(TestPlan testPlan) {
frames.pop();

long tests = testPlan.countTestIdentifiers(TestIdentifier::isTest);
printf(NONE, "Test plan execution finished. Number of all tests: ");
printf(NONE, "%s", "Test plan execution finished. Number of all tests: ");
printf(Color.TEST, "%d%n", tests);
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public void executionSkipped(TestIdentifier testIdentifier, String reason) {
public void dynamicTestRegistered(TestIdentifier testIdentifier) {
printVerticals(theme.entry());
printf(Color.DYNAMIC, " %s", testIdentifier.getDisplayName());
printf(NONE, " dynamically registered%n");
printf(NONE, "%s", " dynamically registered%n");
}

@Override
Expand Down Expand Up @@ -171,7 +171,7 @@ private void printDetail(Color color, String detail, String format, Object... ar
String detailFormat = "%9s";
// omit detail string if it's empty
if (!detail.isEmpty()) {
printf(NONE, String.format(detailFormat + ": ", detail));
printf(NONE, "%s", String.format(detailFormat + ": ", detail));
}
// trivial case: at least one arg is given? Let printf do the entire work
if (args.length > 0) {
Expand All @@ -180,12 +180,12 @@ private void printDetail(Color color, String detail, String format, Object... ar
}
// still here? Split format into separate lines and indent them from the second line on
String[] lines = format.split("\\R");
printf(color, lines[0]);
printf(color, "%s", lines[0]);
if (lines.length > 1) {
String delimiter = System.lineSeparator() + verticals + String.format(detailFormat + " ", "");
for (int i = 1; i < lines.length; i++) {
printf(NONE, delimiter);
printf(color, lines[i]);
printf(NONE, "%s", delimiter);
printf(color, "%s", lines[i]);
}
}
printf(NONE, "%n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2015-2018 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 v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.platform.console.tasks;

import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.platform.engine.TestExecutionResult.failed;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.platform.console.options.Theme;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.engine.test.TestDescriptorStub;
import org.junit.platform.launcher.TestIdentifier;

/**
* @since 1.4
*/
class VerboseTreeListenerTests {

private static final String EOL = System.lineSeparator();

@Test
void executionSkipped() {
StringWriter stringWriter = new StringWriter();
listener(stringWriter).executionSkipped(newTestIdentifier(), "Test" + EOL + "disabled");
String[] lines = lines(stringWriter);

assertLinesMatch(List.of( //
"+-- %c ool test", //
" tags: []", //
" uniqueId: [engine:demo-engine]", //
" parent: []", //
" reason: Test", //
" disabled", //
" status: [S] SKIPPED"), List.of(lines));
}

@Test
void reportingEntryPublished() {
StringWriter stringWriter = new StringWriter();
listener(stringWriter).reportingEntryPublished(newTestIdentifier(), ReportEntry.from("foo", "bar"));
String[] lines = lines(stringWriter);

assertLinesMatch(List.of(" reports: ReportEntry \\[timestamp = .+, foo = 'bar'\\]"), List.of(lines));
}

@Test
void executionFinishedWithFailure() {
StringWriter stringWriter = new StringWriter();
listener(stringWriter).executionFinished(newTestIdentifier(), failed(new AssertionError("Boom!")));
String[] lines = lines(stringWriter);

assertLinesMatch(List.of(" caught: java.lang.AssertionError: Boom!", //
">> STACKTRACE >>", //
" duration: \\d+ ms", //
" status: [X] FAILED"), List.of(lines));
}

@Test
void failureMessageWithFormatSpecifier() {
StringWriter stringWriter = new StringWriter();
listener(stringWriter).executionFinished(newTestIdentifier(), failed(new AssertionError("%crash")));
String[] lines = lines(stringWriter);

assertLinesMatch(List.of(" caught: java.lang.AssertionError: %crash", //
">> STACKTRACE >>", //
" duration: \\d+ ms", //
" status: [X] FAILED"), List.of(lines));
}

private VerboseTreePrintingListener listener(StringWriter stringWriter) {
return new VerboseTreePrintingListener(new PrintWriter(stringWriter), true, 16, Theme.ASCII);
}

private static TestIdentifier newTestIdentifier() {
TestDescriptorStub testDescriptor = new TestDescriptorStub(UniqueId.forEngine("demo-engine"), "%c ool test");
return TestIdentifier.from(testDescriptor);
}

private String[] lines(StringWriter stringWriter) {
return stringWriter.toString().split(EOL);
}

}

0 comments on commit 6eb5531

Please sign in to comment.