From f8370cb972cc78ea712e21870eb6c28db0a2b0e7 Mon Sep 17 00:00:00 2001 From: Michael Osipov Date: Fri, 19 May 2023 19:57:27 +0200 Subject: [PATCH] [SUREFIRE-2170] Use ChoiceFormat to selective render elapsed time in WrappedReportEntry --- .../plugin/surefire/report/ReporterUtils.java | 44 ------------------- .../surefire/report/WrappedReportEntry.java | 19 ++++++-- 2 files changed, 15 insertions(+), 48 deletions(-) delete mode 100644 maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java 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 a673a18d05..0000000000 --- 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 Tibor Digana (tibor17) - * @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/WrappedReportEntry.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java index 0f099542a3..6069364157 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 @@ 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.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; @@ -37,6 +38,8 @@ * @author Kristian Rosenvold */ public class WrappedReportEntry implements TestSetReportEntry { + private static final float ONE_SECOND = 1000.0f; + private final ReportEntry original; private final ReportEntryType reportEntryType; @@ -49,6 +52,14 @@ public class WrappedReportEntry implements TestSetReportEntry { private final Map systemProperties; + /* + * Rationale: The idea is to always display four digits for visually consistent output + * Important: Keep in sync with maven-surefire-report-plugin/src/main/resources/surefire-report.properties + */ + private final MessageFormat elapsedTimeFormat = new MessageFormat( + "{0,choice,0#0|0.0<{0,number,0.000}|10#{0,number,0.00}|100#{0,number,0.0}|1000#{0,number,0}} s", + Locale.ROOT); + public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, @@ -140,7 +151,7 @@ public String getStackTrace(boolean trimStackTrace) { } public String elapsedTimeAsString() { - return getElapsed() != null ? formatElapsedTime(getElapsed()) : null; + return getElapsed() != null ? elapsedTimeFormat.format(new Object[] {getElapsed() / ONE_SECOND}) : null; } String getReportSourceName() { @@ -165,13 +176,13 @@ String getReportName() { public String getOutput(boolean trimStackTrace) { String outputLine = - getElapsedTimeSummary() + " <<< " + getReportEntryType().name() + "!"; + getElapsedTimeSummary() + " <<< " + getReportEntryType().name() + "!"; String trimmedStackTrace = getStackTrace(trimStackTrace); return trimmedStackTrace == null ? outputLine : outputLine + NL + trimmedStackTrace; } public String getElapsedTimeVerbose() { - return "Time elapsed: " + (getElapsed() != null ? elapsedTimeAsString() + " s" : "(unknown)"); + return "Time elapsed: " + (getElapsed() != null ? elapsedTimeAsString() : "(unknown)"); } public String getElapsedTimeSummary() {