Skip to content

Commit

Permalink
writeSurefireReports: if a test execution fails (= exit code != 0; e.g.
Browse files Browse the repository at this point in the history
with a Read timed out), a corresponding surefire report is created.
Before you had not recognized this failure on jenkins, because no
surefire report was produced.
  • Loading branch information
phauer committed Apr 27, 2015
1 parent 35e389b commit 1c9b221
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 178 deletions.
Expand Up @@ -34,9 +34,15 @@ public SurefireReportWriter(Log log, String mafiaResultsDir, String testResultsD
+ "</testsuite>%n";

final static String SUREFIRE_REPORT_ERROR_PART_TEMPLATE =
"<error type=\"java.lang.AssertionError\" message=\"exceptions: %s wrong: %s\"><br/>"
+ "See %s in workspace for more details.<br/>"
+ "See %s for the execution log."
"<error type=\"java.lang.AssertionError\" message=\"exceptions: %s wrong: %s\">%n"
+ "<br/>See %s in workspace for more details.<br/>"
+ "See %s for the execution log.%n"
+ "</error>";

final static String SUREFIRE_REPORT_ERROR_PART_TEMPLATE_WITH_ERROR =
"<error type=\"java.lang.AssertionError\" message=\"execution failed. exit code: %s, exception: %s\">%n"
+ "<br/>See %s in workspace for more details.<br/>"
+ "See %s for the execution log.%n"
+ "</error>";

public void serialize(List<TestResult> testResults, File surefireReportBaseDir) {
Expand Down Expand Up @@ -79,17 +85,28 @@ void serialize(TestResult testResult, File targetFile) throws TestResultExceptio
}

private String createErrorXmlPart(TestResult testResult) {
boolean noTestFailures = testResult.getExceptionCount() + testResult.getWrongTestCount() == 0;
if (noTestFailures) {
return "";
if (testResult.executedSuccessfully()) {
boolean noTestFailures = testResult.getExceptionCount() + testResult.getWrongTestCount() == 0;
if (noTestFailures) {
return "";
}
String surefireReportContent = String.format(SUREFIRE_REPORT_ERROR_PART_TEMPLATE,
testResult.getExceptionCount(),
testResult.getWrongTestCount(),
mafiaResultsDir,
testResultsDir
);
return surefireReportContent;
} else {
String surefireReportContent = String.format(SUREFIRE_REPORT_ERROR_PART_TEMPLATE_WITH_ERROR,
testResult.getExitCode(),
testResult.getExcutionLogException(),
mafiaResultsDir,
testResultsDir
);
return surefireReportContent;

}
String surefireReportContent = String.format(SUREFIRE_REPORT_ERROR_PART_TEMPLATE,
testResult.getExceptionCount(),
testResult.getWrongTestCount(),
mafiaResultsDir,
testResultsDir
);
return surefireReportContent;
}

private TestResultException fireException(File targetFile, Exception e) {
Expand Down
Expand Up @@ -6,121 +6,150 @@

public class TestResult {

private String[] path;
private int rightTestCount;
private int wrongTestCount;
private int ignoredTestCount;
private int exceptionCount;
private long runTimeInMillis;

TestResult() {}

public TestResult withPath(String path) {
this.path = StringUtils.split(path, '.');
return this;
}

public TestResult withRightTestCount(int rightTestCount) {
this.rightTestCount = rightTestCount;
return this;
}

public TestResult withWrongTestCount(int wrongTestCount) {
this.wrongTestCount = wrongTestCount;
return this;
}

public TestResult withIgnoredTestCount(int ignoredTestCount) {
this.ignoredTestCount = ignoredTestCount;
return this;
}

public TestResult withExceptionCount(int exceptionCount) {
this.exceptionCount = exceptionCount;
return this;
}

public TestResult withRunTimeInMillis(long runTimeInMillis) {
this.runTimeInMillis = runTimeInMillis;
return this;
}

public String getPath() {
return StringUtils.join(path, ".");
}

public int getRightTestCount() {
return rightTestCount;
}

public int getWrongTestCount() {
return wrongTestCount;
}

public int getIgnoredTestCount() {
return ignoredTestCount;
}

public int getExceptionCount() {
return exceptionCount;
}

public long getRunTimeInMillis() {
return runTimeInMillis;
}

public double getRunTimeInSec() {
return runTimeInMillis / 1000.0;
}

@Override
public String toString() {
return "TestResult [path=" + path + ", rightTestCount=" + rightTestCount + ", wrongTestCount="
+ wrongTestCount + ", ignoredTestCount=" + ignoredTestCount + ", exceptionCount=" + exceptionCount
+ ", runTimeInMillis=" + runTimeInMillis + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + exceptionCount;
result = prime * result + ignoredTestCount;
result = prime * result + ((path == null) ? 0 : path.hashCode());
result = prime * result + rightTestCount;
result = prime * result + (int) (runTimeInMillis ^ (runTimeInMillis >>> 32));
result = prime * result + wrongTestCount;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TestResult other = (TestResult) obj;
if (exceptionCount != other.exceptionCount) return false;
if (ignoredTestCount != other.ignoredTestCount) return false;
if (path == null) {
if (other.path != null) return false;
} else if (!Arrays.equals(path, other.path)) return false;
if (rightTestCount != other.rightTestCount) return false;
if (runTimeInMillis != other.runTimeInMillis) return false;
if (wrongTestCount != other.wrongTestCount) return false;
return true;
}

public Object getTotalTestCount() {
return rightTestCount + wrongTestCount + exceptionCount + ignoredTestCount;
}

public Object getSuiteName() {
String[] pathWithoutTest = Arrays.copyOf(path, path.length - 1);
return StringUtils.join(pathWithoutTest, ".");
}

public Object getTestName() {
return path[path.length - 1];
}
private String[] path;
private int rightTestCount;
private int wrongTestCount;
private int ignoredTestCount;
private int exceptionCount;
private long runTimeInMillis;
private int exitCode;
private String excutionLogException;

TestResult() {}

public TestResult withPath(String path) {
this.path = StringUtils.split(path, '.');
return this;
}

public TestResult withRightTestCount(int rightTestCount) {
this.rightTestCount = rightTestCount;
return this;
}

public TestResult withWrongTestCount(int wrongTestCount) {
this.wrongTestCount = wrongTestCount;
return this;
}

public TestResult withIgnoredTestCount(int ignoredTestCount) {
this.ignoredTestCount = ignoredTestCount;
return this;
}

public TestResult withExceptionCount(int exceptionCount) {
this.exceptionCount = exceptionCount;
return this;
}

public TestResult withRunTimeInMillis(long runTimeInMillis) {
this.runTimeInMillis = runTimeInMillis;
return this;
}

public TestResult withExitCode(int exitCode) {
this.exitCode = exitCode;
return this;
}

public TestResult withExcutionLogException(String excutionLogException) {
this.excutionLogException = excutionLogException;
return this;
}

public String getPath() {
return StringUtils.join(path, ".");
}

public int getRightTestCount() {
return rightTestCount;
}

public int getWrongTestCount() {
return wrongTestCount;
}

public int getIgnoredTestCount() {
return ignoredTestCount;
}

public int getExceptionCount() {
return exceptionCount;
}

public long getRunTimeInMillis() {
return runTimeInMillis;
}

public double getRunTimeInSec() {
return runTimeInMillis / 1000.0;
}

public int getExitCode() {
return exitCode;
}

public boolean executedSuccessfully() {
return exitCode == 0;
}

public String getExcutionLogException() {
return excutionLogException;
}

public Object getTotalTestCount() {
return rightTestCount + wrongTestCount + exceptionCount + ignoredTestCount;
}

public Object getSuiteName() {
String[] pathWithoutTest = Arrays.copyOf(path, path.length - 1);
return StringUtils.join(pathWithoutTest, ".");
}

public Object getTestName() {
return path[path.length - 1];
}

@Override
public String toString() {
return "TestResult [path=" + Arrays.toString(path) + ", rightTestCount=" + rightTestCount + ", wrongTestCount="
+ wrongTestCount + ", ignoredTestCount=" + ignoredTestCount + ", exceptionCount=" + exceptionCount
+ ", runTimeInMillis=" + runTimeInMillis + ", exitCode=" + exitCode + ", excutionLogException=" + excutionLogException
+ "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + exceptionCount;
result = prime * result + ((excutionLogException == null) ? 0 : excutionLogException.hashCode());
result = prime * result + exitCode;
result = prime * result + ignoredTestCount;
result = prime * result + Arrays.hashCode(path);
result = prime * result + rightTestCount;
result = prime * result + (int) (runTimeInMillis ^ (runTimeInMillis >>> 32));
result = prime * result + wrongTestCount;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TestResult other = (TestResult) obj;
if (exceptionCount != other.exceptionCount) return false;
if (excutionLogException == null) {
if (other.excutionLogException != null) return false;
} else if (!excutionLogException.equals(other.excutionLogException)) return false;
if (exitCode != other.exitCode) return false;
if (ignoredTestCount != other.ignoredTestCount) return false;
if (!Arrays.equals(path, other.path)) return false;
if (rightTestCount != other.rightTestCount) return false;
if (runTimeInMillis != other.runTimeInMillis) return false;
if (wrongTestCount != other.wrongTestCount) return false;
return true;
}

}

0 comments on commit 1c9b221

Please sign in to comment.