import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
-import java.util.regex.Pattern;
/**
* Compiles, installs, runs and reports tests.
}
private void printResult(TestRun testRun) {
- ExpectedResult expected = testRun.getExpectedResult();
- boolean patternSuccess;
-
- if (expected.getPattern() != null) {
- Pattern pattern = Pattern.compile(expected.getPattern(),
- Pattern.MULTILINE | Pattern.DOTALL);
- patternSuccess = pattern.matcher(Strings.join(testRun.getOutputLines(), "\n")).matches();
- } else {
- patternSuccess = true;
- }
-
- if (expected.getResult() == testRun.getResult() && patternSuccess) {
+ if (testRun.isExpectedResult()) {
logger.info("OK " + testRun.getQualifiedName() + " (" + testRun.getResult() + ")");
return;
}
logger.info(" \"" + description + "\"");
}
- if (expected.getResult() != Result.SUCCESS
- && expected.getResult() != testRun.getResult()) {
- logger.info(" Expected result: " + expected.getResult());
- }
-
- if (!patternSuccess) {
- logger.info(" Expected output to match \"" + expected.getPattern() + "\"");
- }
-
- for (String output : testRun.getOutputLines()) {
- logger.info(" " + output);
- }
+ logger.info(" " + testRun.getFailureMessage().replace("\n", "\n "));
}
}
/** Matches lines in the file containing a key and value pair. */
private static final Pattern KEY_VALUE_PAIR_PATTERN = Pattern.compile("(\\w+)\\s+(.+)");
- /**
- * The expectation of a general successful test run.
- */
- static final ExpectedResult SUCCESS = new ExpectedResult(Result.SUCCESS, ".*");
-
- /**
- * The test's expected result, such as {@code EXEC_FAILED}. This property is
- * required.
- */
+ /** The pattern to use when no expected output is specified */
+ private static final Pattern MATCH_ALL_PATTERN
+ = Pattern.compile(".*", Pattern.MULTILINE | Pattern.DOTALL);
+
+ /** The expectation of a general successful test run. */
+ static final ExpectedResult SUCCESS = new ExpectedResult(Result.SUCCESS, null);
+
+ /** The test's expected result, such as {@code EXEC_FAILED}. */
private final Result result;
- /**
- * A regular expression that is the expected output will match. This field
- * is optional.
- */
- private final String pattern;
+ /** The pattern the expected output will match. */
+ private final Pattern pattern;
private ExpectedResult(Result result, String pattern) {
if (result == null) {
throw new IllegalArgumentException();
}
- if (pattern != null) {
- Pattern.compile(pattern); // verify that the pattern is well-formed
- }
this.result = result;
- this.pattern = pattern;
+ this.pattern = pattern != null
+ ? Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL)
+ : MATCH_ALL_PATTERN;
}
public Result getResult() {
return result;
}
- public String getPattern() {
+ public Pattern getPattern() {
return pattern;
}
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
+import java.util.regex.Pattern;
/**
* A test run and its outcome. This class tracks the complete lifecycle of a
return outputLines;
}
- public ExpectedResult getExpectedResult() {
- return expectedResult;
- }
-
public Class<? extends TestRunner> getTestRunner() {
return testRunner;
}
+
+ /**
+ * Returns true if the outcome of this run matches what was expected.
+ */
+ public boolean isExpectedResult() {
+ return result == expectedResult.getResult() && matchesExpectedPattern();
+ }
+
+ /**
+ * Returns true if the test's output matches the expected output.
+ */
+ private boolean matchesExpectedPattern() {
+ return expectedResult.getPattern()
+ .matcher(Strings.join(outputLines, "\n"))
+ .matches();
+ }
+
+ /**
+ * Returns the failure message for this failed test run. This message is
+ * intended to help to diagnose why the test result didn't match what was
+ * expected.
+ */
+ public String getFailureMessage() {
+ StringBuilder builder = new StringBuilder();
+
+ if (expectedResult.getResult() != Result.SUCCESS
+ && expectedResult.getResult() != result) {
+ builder.append("Expected result: ")
+ .append(expectedResult.getResult())
+ .append("\n");
+ }
+
+ if (!matchesExpectedPattern()) {
+ builder.append("Expected output to match \"")
+ .append(expectedResult.getPattern().pattern())
+ .append("\"\n");
+ }
+
+ for (String output : outputLines) {
+ builder.append(output).append("\n");
+ }
+
+ return builder.toString();
+ }
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
-import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
*/
public class XmlReportPrinter {
- /**
- * Test results of these types are omitted from the report, as if the tests
- * never existed. Equivalent to the core test runner's @BrokenTest.
- */
- private static final EnumSet<Result> IGNORED_RESULTS
- = EnumSet.of(Result.COMPILE_FAILED);
-
- private static final EnumSet<Result> FAILED_RESULTS
- = EnumSet.of(Result.EXEC_FAILED);
- private static final EnumSet<Result> ERROR_RESULTS
- = EnumSet.complementOf(EnumSet.of(Result.EXEC_FAILED, Result.SUCCESS));
-
private static final String TESTSUITE = "testsuite";
private static final String TESTCASE = "testcase";
private static final String ERROR = "error";
private Map<String, Suite> testsToSuites(Collection<TestRun> testRuns) {
Map<String, Suite> result = new LinkedHashMap<String, Suite>();
for (TestRun testRun : testRuns) {
- if (IGNORED_RESULTS.contains(testRun.getResult())) {
+ if (testRun.getResult() == Result.UNSUPPORTED) {
continue;
}
}
suite.tests.add(testRun);
- if (FAILED_RESULTS.contains(testRun.getResult())) {
- suite.failuresCount++;
- } else if (ERROR_RESULTS.contains(testRun.getResult())) {
- suite.errorsCount++;
+
+ if (!testRun.isExpectedResult()) {
+ if (testRun.getResult() == Result.EXEC_FAILED) {
+ suite.failuresCount++;
+ } else {
+ suite.errorsCount++;
+ }
}
}
return result;
serializer.attribute(ns, ATTR_CLASSNAME, testRun.getSuiteName());
serializer.attribute(ns, ATTR_TIME, "0");
- String result = ERROR_RESULTS.contains(testRun.getResult()) ? ERROR
- : FAILED_RESULTS.contains(testRun.getResult()) ? FAILURE
- : null;
-
- if (result != null) {
+ if (!testRun.isExpectedResult()) {
+ String result = testRun.getResult() == Result.EXEC_FAILED ? FAILURE : ERROR;
serializer.startTag(ns, result);
String title = testRun.getDescription();
if (title != null && title.length() > 0) {