From 9d4931b9ccd82e9305efe1405a56083b6023e84e Mon Sep 17 00:00:00 2001 From: lamyaa Date: Fri, 12 Jun 2015 14:29:58 -0500 Subject: [PATCH 1/2] [Surefire-1144] Time for testsuite on commandline does not suit with the time value given in the report file --- .../surefire/report/StatelessXmlReporter.java | 2 +- .../surefire/report/TestSetRunListener.java | 1 + .../plugin/surefire/report/TestSetStats.java | 11 +++ .../report/StatelessXmlReporterTest.java | 2 - .../its/jiras/Surefire1144XmlRunTimeIT.java | 59 +++++++++++++ .../surefire-1144-xml-runtime/pom.xml | 35 ++++++++ .../src/test/java/surefire1144/Test1.java | 82 +++++++++++++++++++ 7 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1144XmlRunTimeIT.java create mode 100644 surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/pom.xml create mode 100644 surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/src/test/java/surefire1144/Test1.java 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 5295cc39e5..3e65558a5f 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 @@ -129,7 +129,7 @@ public void testSetCompleted( WrappedReportEntry testSetReportEntry, TestSetStat ppw.setEncoding( ENCODING ); createTestSuiteElement( ppw, testSetReportEntry, testSetStats, reportNameSuffix, - testSetStats.elapsedTimeAsString( getRunTimeForAllTests( methodRunHistoryMap ) ) ); + testSetStats.getElapsedForTestSet() ); showProperties( ppw ); diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java index b7bcadbf52..80ce3ba886 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java @@ -127,6 +127,7 @@ public void clearCapture() public void testSetCompleted( ReportEntry report ) { WrappedReportEntry wrap = wrapTestSet( report ); + detailsForThis.testSetCompleted(); List testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : null; if ( fileReporter != null ) { diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java index 95e92d4f91..14f50dc0de 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java @@ -40,6 +40,8 @@ public class TestSetStats private long testSetStartAt; + private long testSetEndAt; + private long testStartAt; private int completedCount; @@ -90,6 +92,15 @@ public void testSetStart() lastStartAt = testSetStartAt; } + public void testSetCompleted() + { + testSetEndAt = System.currentTimeMillis(); + if ( elapsedForTestSet < testSetEndAt - testSetStartAt ) + { + elapsedForTestSet = testSetEndAt - testSetStartAt; + } + } + public void testStart() { testStartAt = System.currentTimeMillis(); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java index dad0ac27d3..0d10107879 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java @@ -234,8 +234,6 @@ public void testOutputRerunFlakyFailure() Xpp3Dom testSuite = Xpp3DomBuilder.build( new InputStreamReader( fileInputStream, "UTF-8" ) ); assertEquals( "testsuite", testSuite.getName() ); - // 0.019 = 0.012 + 0.005 +0.002 - assertEquals( "0.019", testSuite.getAttribute( "time" ) ); Xpp3Dom properties = testSuite.getChild( "properties" ); assertEquals( System.getProperties().size(), properties.getChildCount() ); Xpp3Dom child = properties.getChild( 1 ); diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1144XmlRunTimeIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1144XmlRunTimeIT.java new file mode 100644 index 0000000000..9e27b7e8d0 --- /dev/null +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1144XmlRunTimeIT.java @@ -0,0 +1,59 @@ +package org.apache.maven.surefire.its; + +/* + * 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. + */ + +import org.apache.maven.plugins.surefire.report.ReportTestSuite; +import org.apache.maven.surefire.its.fixture.HelperAssertions; +import org.apache.maven.surefire.its.fixture.OutputValidator; +import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; +import org.junit.Test; + +import java.io.File; +import java.util.List; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; + +/** + * Test that runtime reported on console matches runtime in XML + * + * @author Lamyaa Eloussi + */ +public class Surefire1144XmlRunTimeIT + extends SurefireJUnit4IntegrationTestCase +{ + @Test + public void testXmlRunTime() + throws Exception + { + OutputValidator outputValidator = unpack( "/surefire-1144-xml-runtime" ).forkOnce().executeTest(); + + List reports = HelperAssertions.extractReports( new File[]{ outputValidator.getBaseDir() } ); + assertEquals( 1, reports.size() ); //only one report + + ReportTestSuite report = reports.get( 0 ); + float xmlTime = report.getTimeElapsed(); + assertTrue( "surefire1144.Test1 report.getTimeElapsed found:" + xmlTime, + xmlTime >= 1.6f ); //include beforeClass and afterClass + + outputValidator.verifyTextInLog( Float.toString( xmlTime ) ); //same time in console + } + +} diff --git a/surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/pom.xml b/surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/pom.xml new file mode 100644 index 0000000000..39aa3b4c77 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + org.apache.maven.plugins.surefire + surefire1144-xml-runtime + jar + 1.0-SNAPSHOT + surefire-1144-xml-runtime + http://maven.apache.org + + + junit + junit + 4.12 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + + diff --git a/surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/src/test/java/surefire1144/Test1.java b/surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/src/test/java/surefire1144/Test1.java new file mode 100644 index 0000000000..4cb79e2a67 --- /dev/null +++ b/surefire-integration-tests/src/test/resources/surefire-1144-xml-runtime/src/test/java/surefire1144/Test1.java @@ -0,0 +1,82 @@ +package surefire1144; + +/* + * 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. + */ + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class Test1 +{ + static void sleep( int ms ) + { + long target = System.currentTimeMillis() + ms; + try + { + do + { + Thread.sleep( ms ); + } + while ( System.currentTimeMillis() < target ); + } + catch ( InterruptedException e ) + { + throw new RuntimeException( e ); + } + } + + static void printTimeAndSleep( String msg, int ms ) + { + System.out.println( msg + " started @ " + System.currentTimeMillis() ); + sleep( ms ); + } + + @Test + public void testSleep100() + { + printTimeAndSleep( "Test1.sleep100", 100 ); + } + + @Test + public void testSleep200() + { + printTimeAndSleep( "Test1.sleep200", 200 ); + } + + @Test + public void testSleep300() + { + printTimeAndSleep( "Test1.sleep300", 300 ); + } + + @BeforeClass + public static void setUpBeforeClass() + throws Exception + { + printTimeAndSleep( "beforeClass sleep 500", 500 ); + } + + @AfterClass + public static void tearDownAfterClass() + throws Exception + { + printTimeAndSleep( "afterClass sleep 500", 500 ); + } +} From f2eff0d240c1c70c1411d93b46455169ec543217 Mon Sep 17 00:00:00 2001 From: lamyaa Date: Tue, 16 Jun 2015 12:31:54 -0500 Subject: [PATCH 2/2] Modified console time to be identical to XML report time and removed now unused method getElapsedSinceTestSetStart --- .../surefire/report/StatelessXmlReporter.java | 2 +- .../plugin/surefire/report/TestSetRunListener.java | 4 ++-- .../maven/plugin/surefire/report/TestSetStats.java | 13 +++---------- 3 files changed, 6 insertions(+), 13 deletions(-) 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 3e65558a5f..ab8a95d4ab 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 @@ -129,7 +129,7 @@ public void testSetCompleted( WrappedReportEntry testSetReportEntry, TestSetStat ppw.setEncoding( ENCODING ); createTestSuiteElement( ppw, testSetReportEntry, testSetStats, reportNameSuffix, - testSetStats.getElapsedForTestSet() ); + testSetStats.getElapsedForTestSetAsString() ); showProperties( ppw ); diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java index 80ce3ba886..82a7d941cd 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java @@ -126,8 +126,8 @@ public void clearCapture() public void testSetCompleted( ReportEntry report ) { - WrappedReportEntry wrap = wrapTestSet( report ); detailsForThis.testSetCompleted(); + WrappedReportEntry wrap = wrapTestSet( report ); List testResults = briefOrPlainFormat ? detailsForThis.getTestResults() : null; if ( fileReporter != null ) { @@ -249,7 +249,7 @@ private WrappedReportEntry wrapTestSet( ReportEntry other ) { return new WrappedReportEntry( other, null, other.getElapsed() != null ? other.getElapsed() - : detailsForThis.getElapsedSinceTestSetStart(), testStdOut, testStdErr ); + : detailsForThis.getElapsedForTestSet(), testStdOut, testStdErr ); } public void close() diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java index 14f50dc0de..764314a0ad 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java @@ -62,16 +62,9 @@ public TestSetStats( boolean trimStackTrace, boolean plainFormat ) this.plainFormat = plainFormat; } - public int getElapsedSinceTestSetStart() + public int getElapsedForTestSet() { - if ( testSetStartAt > 0 ) - { - return (int) ( System.currentTimeMillis() - testSetStartAt ); - } - else - { - return 0; - } + return (int) elapsedForTestSet; } public int getElapsedSinceLastStart() @@ -196,7 +189,7 @@ public String elapsedTimeAsString( long runTime ) private static final int MS_PER_SEC = 1000; - public String getElapsedForTestSet() + public String getElapsedForTestSetAsString() { return elapsedTimeAsString( elapsedForTestSet ); }