This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch doxia-2.0.0
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 1281144cf411518a54f50607bb00e3a9dcdb8a26
Author: Michael Osipov <micha...@apache.org>
AuthorDate: Mon May 8 02:10:54 2023 +0200

    Format time where it is displayed, not serialized
---
 .../plugin/surefire/report/ReporterUtils.java      | 44 ----------------------
 .../surefire/report/StatelessXmlReporter.java      |  4 +-
 .../plugin/surefire/report/WrappedReportEntry.java |  9 +++--
 .../surefire/report/TestSuiteXmlParser.java        | 41 +++++++++-----------
 4 files changed, 26 insertions(+), 72 deletions(-)

diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java
deleted file mode 100644
index a673a18d0..000000000
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.maven.plugin.surefire.report;
-
-import java.text.NumberFormat;
-import java.util.Locale;
-
-/**
- * Utility for reporter classes.
- *
- * @author <a href="mailto:tibordig...@apache.org";>Tibor Digana (tibor17)</a>
- * @since 2.19
- */
-final class ReporterUtils {
-    private static final int MS_PER_SEC = 1000;
-
-    private ReporterUtils() {
-        throw new IllegalStateException("non instantiable constructor");
-    }
-
-    static String formatElapsedTime(double runTime) {
-        NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
-        numberFormat.setGroupingUsed(true);
-        numberFormat.setMinimumFractionDigits(0);
-        numberFormat.setMaximumFractionDigits(3);
-        return numberFormat.format(runTime / MS_PER_SEC);
-    }
-}
diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java
index 7a9a019d9..2289e64a2 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java
@@ -387,7 +387,7 @@ public class StatelessXmlReporter implements 
StatelessReportEventListener<Wrappe
             ppw.addAttribute("classname", extraEscapeAttribute(className));
         }
 
-        ppw.addAttribute("time", report.elapsedTimeAsString());
+        ppw.addAttribute("time", String.valueOf(report.getElapsed() / 
1000.0f));
     }
 
     private void createTestSuiteElement(XMLWriter ppw, WrappedReportEntry 
report, TestSetStats testSetStats)
@@ -407,7 +407,7 @@ public class StatelessXmlReporter implements 
StatelessReportEventListener<Wrappe
             ppw.addAttribute("group", report.getGroup());
         }
 
-        ppw.addAttribute("time", report.elapsedTimeAsString());
+        ppw.addAttribute("time", String.valueOf(report.getElapsed() / 
1000.0f));
 
         ppw.addAttribute("tests", 
String.valueOf(testSetStats.getCompletedCount()));
 
diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
index 97b3734c2..e1ad54bbe 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
@@ -20,7 +20,9 @@ package org.apache.maven.plugin.surefire.report;
 
 import javax.annotation.Nonnull;
 
+import java.text.MessageFormat;
 import java.util.Collections;
+import java.util.Locale;
 import java.util.Map;
 
 import org.apache.maven.surefire.api.report.ReportEntry;
@@ -29,7 +31,6 @@ import org.apache.maven.surefire.api.report.StackTraceWriter;
 import org.apache.maven.surefire.api.report.TestSetReportEntry;
 
 import static java.util.Collections.unmodifiableMap;
-import static 
org.apache.maven.plugin.surefire.report.ReporterUtils.formatElapsedTime;
 import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
 import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank;
 
@@ -140,7 +141,9 @@ public class WrappedReportEntry implements 
TestSetReportEntry {
     }
 
     public String elapsedTimeAsString() {
-        return formatElapsedTime(getElapsed());
+        MessageFormat format = new MessageFormat(
+                
"{0,choice,0#0|0.0<{0,number,0.000}|1.0#{0,number,0.0}|1000#{0,number,0}} s", 
Locale.ROOT);
+        return format.format(new Object[] {getElapsed() / 1000.0f});
     }
 
     String getReportSourceName() {
@@ -171,7 +174,7 @@ public class WrappedReportEntry implements 
TestSetReportEntry {
     }
 
     public String getElapsedTimeVerbose() {
-        return "Time elapsed: " + elapsedTimeAsString() + " s";
+        return "Time elapsed: " + elapsedTimeAsString();
     }
 
     public String getElapsedTimeSummary() {
diff --git 
a/surefire-report-parser/src/main/java/org/apache/maven/plugins/surefire/report/TestSuiteXmlParser.java
 
b/surefire-report-parser/src/main/java/org/apache/maven/plugins/surefire/report/TestSuiteXmlParser.java
index 83ccc0619..42402aa83 100644
--- 
a/surefire-report-parser/src/main/java/org/apache/maven/plugins/surefire/report/TestSuiteXmlParser.java
+++ 
b/surefire-report-parser/src/main/java/org/apache/maven/plugins/surefire/report/TestSuiteXmlParser.java
@@ -26,8 +26,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
-import java.text.NumberFormat;
-import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -40,15 +38,12 @@ import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
-import static java.util.Locale.ENGLISH;
 import static org.apache.maven.shared.utils.StringUtils.isBlank;
 
 /**
  *
  */
 public final class TestSuiteXmlParser extends DefaultHandler {
-    private final NumberFormat numberFormat = 
NumberFormat.getInstance(ENGLISH);
-
     private final ConsoleLogger consoleLogger;
 
     private ReportTestSuite defaultSuite;
@@ -111,13 +106,11 @@ public final class TestSuiteXmlParser extends 
DefaultHandler {
                     case "testsuite":
                         defaultSuite = new ReportTestSuite();
                         currentSuite = defaultSuite;
-
-                        try {
-                            Number time = 
numberFormat.parse(attributes.getValue("time"));
-
-                            defaultSuite.setTimeElapsed(time.floatValue());
-                        } catch (NullPointerException e) {
-                            consoleLogger.error("WARNING: no time attribute 
found on testsuite element");
+                        String timeAsString = attributes.getValue("time");
+                        if (timeAsString != null) {
+                            
defaultSuite.setTimeElapsed(Float.parseFloat(timeAsString));
+                        } else {
+                            consoleLogger.warning("No time attribute found on 
testsuite element");
                         }
 
                         final String name = attributes.getValue("name");
@@ -131,8 +124,6 @@ public final class TestSuiteXmlParser extends 
DefaultHandler {
                         
classesToSuitesIndex.put(defaultSuite.getFullClassName(), suites.size() - 1);
                         break;
                     case "testcase":
-                        currentElement = new StringBuilder();
-
                         testCase = new 
ReportTestCase().setName(attributes.getValue("name"));
 
                         String fullClassName = 
attributes.getValue("classname");
@@ -149,23 +140,25 @@ public final class TestSuiteXmlParser extends 
DefaultHandler {
                             }
                         }
 
-                        String timeAsString = attributes.getValue("time");
-                        Number time = isBlank(timeAsString) ? 0 : 
numberFormat.parse(timeAsString);
+                        timeAsString = attributes.getValue("time");
+                        float time = isBlank(timeAsString) ? 0.0f : 
Float.parseFloat(timeAsString);
 
                         
testCase.setFullClassName(currentSuite.getFullClassName())
                                 .setClassName(currentSuite.getName())
                                 .setFullName(currentSuite.getFullClassName() + 
"." + testCase.getName())
-                                .setTime(time.floatValue());
+                                .setTime(time);
 
                         if (currentSuite != defaultSuite) {
                             currentSuite.setTimeElapsed(testCase.getTime() + 
currentSuite.getTimeElapsed());
                         }
                         break;
                     case "failure":
+                        currentElement = new StringBuilder();
                         testCase.setFailure(attributes.getValue("message"), 
attributes.getValue("type"));
                         currentSuite.incrementNumberOfFailures();
                         break;
                     case "error":
+                        currentElement = new StringBuilder();
                         testCase.setError(attributes.getValue("message"), 
attributes.getValue("type"));
                         currentSuite.incrementNumberOfErrors();
                         break;
@@ -181,11 +174,14 @@ public final class TestSuiteXmlParser extends 
DefaultHandler {
                     case "failsafe-summary":
                         valid = false;
                         break;
+                    case "time":
+                        currentElement = new StringBuilder();
+                        break;
                     default:
                         break;
                 }
-            } catch (ParseException e) {
-                throw new SAXException(e.getMessage(), e);
+            } catch (NumberFormatException e) {
+                throw new SAXException("Failed to parse time value", e);
             }
         }
     }
@@ -206,10 +202,9 @@ public final class TestSuiteXmlParser extends 
DefaultHandler {
                 break;
             case "time":
                 try {
-                    defaultSuite.setTimeElapsed(
-                            
numberFormat.parse(currentElement.toString()).floatValue());
-                } catch (ParseException e) {
-                    throw new SAXException(e.getMessage(), e);
+                    
defaultSuite.setTimeElapsed(Float.parseFloat(currentElement.toString()));
+                } catch (NumberFormatException e) {
+                    throw new SAXException("Failed to parse time value", e);
                 }
                 break;
             default:

Reply via email to