Skip to content
Permalink
Browse files

Merge pull request #43 from jenkinsci/parse-time-in-suiteresult

[FIXED JENKINS-34407] Parse time correctly in SuiteResult.
  • Loading branch information...
olivergondza committed Apr 30, 2016
2 parents 2952d09 + 2c1655c commit e09cdd41a020533af124be8bd8baaba3d9a3f1df
@@ -36,8 +36,6 @@

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -94,19 +92,7 @@

private static float parseTime(Element testCase) {
String time = testCase.attributeValue("time");
if(time!=null) {
time = time.replace(",","");
try {
return Float.parseFloat(time);
} catch (NumberFormatException e) {
try {
return new DecimalFormat().parse(time).floatValue();
} catch (ParseException x) {
// hmm, don't know what this format is.
}
}
}
return 0.0f;
return new TimeToFloat(time).parse();
}

CaseResult(SuiteResult parent, Element testCase, String testClassName, boolean keepLongStdio) {
@@ -705,4 +691,5 @@ public int compare(CaseResult lhs, CaseResult rhs) {
};

private static final long serialVersionUID = 1L;

}
@@ -164,10 +164,9 @@ private SuiteResult(File xmlReport, Element suite, boolean keepLongStdio) throws
this.name = TestObject.safe(name);
this.timestamp = suite.attributeValue("timestamp");
this.id = suite.attributeValue("id");

// check for test suite time attribute
if( ( this.time = suite.attributeValue("time") ) != null ){
duration = Float.parseFloat(this.time);
duration = new TimeToFloat(this.time).parse();
}

Element ex = suite.element("error");
@@ -0,0 +1,57 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Daniel Dyer, Seiji Sogabe, Tom Huybrechts, Yahoo!, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.tasks.junit;

import java.text.DecimalFormat;
import java.text.ParseException;

/**
* Parse a given time string into float.
*
* @author mfriedenhagen
*/
class TimeToFloat {
private String time;

public TimeToFloat(String time) {
this.time = time;
}

public float parse() {
if (time != null) {
time = time.replace(",", "");
try {
return Float.parseFloat(time);
} catch (NumberFormatException e) {
try {
return new DecimalFormat().parse(time).floatValue();
} catch (ParseException x) {
// hmm, don't know what this format is.
}
}
}
return 0.0f;
}
}
@@ -327,12 +327,14 @@ public void testParseNestedTestSuites() throws Exception {
assertEquals(1, result.getCases().size());
}
}

@Test
public void testTestSuiteTimeAttribute() throws Exception {
// A report with blocks of testsuites some with and some without time attrs
List<SuiteResult> results = parseSuites(getDataFile("junit-report-testsuite-time-attrs.xml"));
assertEquals(results.get(0).getDuration(),25.0f); //testsuit time
assertEquals(results.get(1).getDuration(),22.0f); //sum of test cases time
assertEquals(results.get(2).getDuration(),40.0f); //testsuit time
assertEquals(results.get(3).getDuration(),20.0f); //sum of test cases time
assertEquals(2503.1f, results.get(0).getDuration(), 2); //testsuit time
assertEquals(22.0f, results.get(1).getDuration(),2); //sum of test cases time
assertEquals(40.0f, results.get(2).getDuration(), 2); //testsuit time
assertEquals(20.0f, results.get(3).getDuration(), 2); //sum of test cases time
}
}
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Automation Tests" tests="8" errors="0" failures="0" ignored="0">
<testsuite name="test.fs.FileSystemTests" time="25">
<testsuite name="test.fs.FileSystemTests" time="2,503.1">
<testcase name="testPrefix" classname="test.fs.FileSystemTest1" time="10"/>
<testcase name="testPrefix" classname="test.fs.FileSystemTest2" time="11"/>
</testsuite>

0 comments on commit e09cdd4

Please sign in to comment.
You can’t perform that action at this time.