Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-12457] 'Age' column on 'Test Result' tab may show inco…
…rrect value when a test suite divided into multiple junit files
  • Loading branch information
kutzi committed Feb 18, 2012
1 parent bbcf286 commit 05937f5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
40 changes: 40 additions & 0 deletions core/src/main/java/hudson/tasks/junit/CaseResult.java
Expand Up @@ -39,6 +39,7 @@

/**
* One test result.
* It is uniquely identified by its className and testName.
*
* @author Kohsuke Kawaguchi
*/
Expand Down Expand Up @@ -129,6 +130,10 @@ private static float parseTime(Element testCase) {
stdout = possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-out"));
stderr = possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-err"));
}

void replaceParent(SuiteResult parent) {
this.parent = parent;
}

private static final int HALF_MAX_SIZE = 500;
static String possiblyTrimStdio(Collection<CaseResult> results, boolean keepLongStdio, String stdio) { // HUDSON-6516
Expand Down Expand Up @@ -512,6 +517,41 @@ public Status getStatus() {
this.classResult = classResult;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((className == null) ? 0 : className.hashCode());
result = prime * result
+ ((testName == null) ? 0 : testName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CaseResult other = (CaseResult) obj;
if (className == null) {
if (other.className != null)
return false;
} else if (!className.equals(other.className))
return false;
if (testName == null) {
if (other.testName != null)
return false;
} else if (!testName.equals(other.testName))
return false;
return true;
}



/**
* Constants that represent the status of this test.
*/
Expand Down
16 changes: 14 additions & 2 deletions core/src/main/java/hudson/tasks/junit/SuiteResult.java
Expand Up @@ -76,6 +76,9 @@ public final class SuiteResult implements Serializable {
* All test cases.
*/
private final List<CaseResult> cases = new ArrayList<CaseResult>();
// transient representation of the cases, which is only used while the SuiteResult is build up initially
private transient Set<CaseResult> casesSet = new HashSet<CaseResult>();

private transient hudson.tasks.junit.TestResult parent;

SuiteResult(String name, String stdout, String stderr) {
Expand Down Expand Up @@ -204,9 +207,16 @@ private SuiteResult(File xmlReport, Element suite, boolean keepLongStdio) throws
this.stderr = CaseResult.possiblyTrimStdio(cases, keepLongStdio, stderr);
}

/**
* Adds the {@link CaseResult} to this {@link SuiteResult}, if it's not already
* included.
*/
/*package*/ void addCase(CaseResult cr) {
cases.add(cr);
duration += cr.getDuration();
boolean added = casesSet.add(cr);
if (added) {
cases.add(cr);
duration += cr.getDuration();
}
}

@Exported(visibility=9)
Expand Down Expand Up @@ -313,6 +323,8 @@ void setParent(hudson.tasks.junit.TestResult parent) {
return false; // already frozen

this.parent = owner;

casesSet = null;
for (CaseResult c : cases)
c.freeze(this);
return true;
Expand Down
23 changes: 16 additions & 7 deletions core/src/main/java/hudson/tasks/junit/TestResult.java
Expand Up @@ -186,18 +186,27 @@ public void parse(long buildTime, File baseDir, String[] reportFiles) throws IOE

private void add(SuiteResult sr) {
for (SuiteResult s : suites) {
// a common problem is that people parse TEST-*.xml as well as TESTS-TestSuite.xml
// see http://www.nabble.com/Problem-with-duplicate-build-execution-td17549182.html for discussion
if(s.getName().equals(sr.getName()) && eq(s.getTimestamp(),sr.getTimestamp())
&& eq(s.getId(),sr.getId()))
return; // duplicate
// A common problem is that people parse TEST-*.xml as well as TESTS-TestSuite.xml
// see http://jenkins.361315.n4.nabble.com/Problem-with-duplicate-build-execution-td371616.html for discussion.
// On the other hand, it's also possible that a TestSuite is split between different files - e.g. in PHPUnit
// See JENKINS-12457
if(s.getName().equals(sr.getName()) && nullSafeEq(s.getId(),sr.getId())) {
for (CaseResult caseResult : sr.getCases()) {
s.addCase(caseResult);
caseResult.replaceParent(s);
}
return;
}
}
suites.add(sr);
duration += sr.getDuration();
}

private boolean eq(Object lhs, Object rhs) {
return lhs != null && rhs != null && lhs.equals(rhs);
private boolean nullSafeEq(Object lhs, Object rhs) {
if (lhs == null) {
return rhs == null;
}
return lhs.equals(rhs);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/hudson/tasks/junit/TestResultTest.java
Expand Up @@ -52,7 +52,7 @@ public void testIpsTests() throws Exception {
testResult.parse(getDataFile("eclipse-plugin-test-report.xml"));

Collection<SuiteResult> suites = testResult.getSuites();
assertEquals("Wrong number of test suites", 16, suites.size());
assertEquals("Wrong number of test suites", 15, suites.size());
int testCaseCount = 0;
for (SuiteResult suite : suites) {
testCaseCount += suite.getCases().size();
Expand Down
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="0" name="tests" package="" tests="20" time="15.02" timestamp="2011-03-21T16:51:12">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="0" name="testsuite1" package="" tests="20" time="15.02" timestamp="2011-03-21T16:51:12">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -290,7 +290,7 @@ x86_64
]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="1" name="tests" package="" tests="6" time="0.03" timestamp="2011-03-21T16:53:10">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="1" name="testsuite2" package="" tests="6" time="0.03" timestamp="2011-03-21T16:53:10">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -552,7 +552,7 @@ ProjectImportTask: execution finished
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="2" name="tests" package="" tests="240" time="14.494" timestamp="2011-03-21T16:57:12">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="2" name="testsuite3" package="" tests="240" time="14.494" timestamp="2011-03-21T16:57:12">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -1703,7 +1703,7 @@ java.lang.NullPointerException
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="3" name="tests" package="" tests="1742" time="97.882" timestamp="2011-03-21T16:51:29">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="3" name="testsuite4" package="" tests="1742" time="97.882" timestamp="2011-03-21T16:51:29">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -5550,7 +5550,7 @@ SEVERE: Message
]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="4" name="tests" package="" tests="95" time="3.381" timestamp="2011-03-21T16:53:11">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="4" name="testsuite5" package="" tests="95" time="3.381" timestamp="2011-03-21T16:53:11">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -5981,7 +5981,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="5" name="tests" package="" tests="28" time="2.048" timestamp="2011-03-21T16:53:15">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="5" name="testsuite6" package="" tests="28" time="2.048" timestamp="2011-03-21T16:53:15">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -6385,7 +6385,7 @@ ORMEXT: Should find 3 mapping
]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="6" name="tests" package="" tests="1" time="0.051" timestamp="2011-03-21T16:57:07">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="6" name="testsuite7_duplicate_test_name_different_id" package="" tests="1" time="0.051" timestamp="2011-03-21T16:57:07">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -6625,7 +6625,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="7" name="tests" package="" tests="176" time="91.008" timestamp="2011-03-21T16:55:36">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="7" name="testsuite7_duplicate_test_name_different_id" package="" tests="176" time="91.008" timestamp="2011-03-21T16:55:36">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -7216,7 +7216,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="8" name="tests" package="" tests="56" time="1.309" timestamp="2011-03-21T16:57:08">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" name="testsuite8_duplicate_test_name_without_id" package="" tests="56" time="1.309" timestamp="2011-03-21T16:57:08">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -7566,7 +7566,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="9" name="tests" package="" tests="60" time="0.049" timestamp="2011-03-21T16:53:17">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" name="testsuite8_duplicate_test_name_without_id" package="" tests="60" time="0.049" timestamp="2011-03-21T16:53:17">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -7924,7 +7924,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="10" name="tests" package="" tests="222" time="2.758" timestamp="2011-03-21T16:53:08">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="10" name="testsuite10" package="" tests="222" time="2.758" timestamp="2011-03-21T16:53:08">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -8602,7 +8602,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="11" name="tests" package="" tests="2" time="1.021" timestamp="2011-03-21T16:55:35">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="11" name="testsuite11" package="" tests="2" time="1.021" timestamp="2011-03-21T16:55:35">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -8844,7 +8844,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="12" name="tests" package="" tests="244" time="1.532" timestamp="2011-03-21T16:53:17">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="12" name="testsuite12" package="" tests="244" time="1.532" timestamp="2011-03-21T16:53:17">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -9572,7 +9572,7 @@ Test test.CalculationTest1 finished.
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="13" name="tests" package="" tests="323" time="136.381" timestamp="2011-03-21T16:53:19">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="13" name="testsuite13" package="" tests="323" time="136.381" timestamp="2011-03-21T16:53:19">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -10456,7 +10456,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="14" name="tests" package="" tests="37" time="0.036" timestamp="2011-03-21T16:53:10">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="14" name="testsuite14" package="" tests="37" time="0.036" timestamp="2011-03-21T16:53:10">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down Expand Up @@ -10768,7 +10768,7 @@ x86_64
<system-err><![CDATA[]]></system-err>

</testsuite>
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="15" name="tests" package="" tests="114" time="0.126" timestamp="2011-03-21T16:53:11">
<testsuite errors="0" failures="0" hostname="faktorzehn.org" id="15" name="testsuite15" package="" tests="114" time="0.126" timestamp="2011-03-21T16:53:11">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />

Expand Down

0 comments on commit 05937f5

Please sign in to comment.