maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krosenv...@apache.org
Subject svn commit: r1228960 - in /maven/surefire/trunk: surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/ surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/ surefire-integration-tests/src/test/resources...
Date Sun, 08 Jan 2012 21:24:40 GMT
Author: krosenvold
Date: Sun Jan  8 21:24:39 2012
New Revision: 1228960

URL: http://svn.apache.org/viewvc?rev=1228960&view=rev
Log:
[SUREFIRE-800] Fixed beforeclass/afterclass output when running classes=parallel

Added:
    maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ClassDemarcatingRunner.java
  (with props)
Modified:
    maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java
    maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire628ConsoleOutputBeforeAndAfterClassIT.java
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/pom.xml
    maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/src/test/java/surefire628/Test2.java
    maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConcurrentReporterManager.java
    maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
    maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java

Modified: maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java
(original)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/fixture/TestFile.java
Sun Jan  8 21:24:39 2012
@@ -121,17 +121,18 @@ public class TestFile
         return file.isFile();
     }
 
-    public void assertContainsText( String text )
+    public TestFile assertContainsText( String text )
     {
         final List<String> list = surefireVerifier.loadFile( file, false );
         for ( String line : list )
         {
             if ( line.contains( text ) )
             {
-                return;
+                return this;
             }
         }
         Assert.fail( "Did not find expected message in log" );
+        return null;
     }
 
     public URI toURI() {

Modified: maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire628ConsoleOutputBeforeAndAfterClassIT.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire628ConsoleOutputBeforeAndAfterClassIT.java?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire628ConsoleOutputBeforeAndAfterClassIT.java
(original)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire628ConsoleOutputBeforeAndAfterClassIT.java
Sun Jan  8 21:24:39 2012
@@ -21,11 +21,12 @@ package org.apache.maven.surefire.its.ji
 
 import org.apache.maven.surefire.its.fixture.OutputValidator;
 import org.apache.maven.surefire.its.fixture.SurefireIntegrationTestCase;
+import org.apache.maven.surefire.its.fixture.TestFile;
 
 /**
  * Asserts that console output always goes somewhere ;)
  *
- * @author Kristian Rosenvold
+ * @author Kristian Rosenvold       assertContainsText
  */
 public class Surefire628ConsoleOutputBeforeAndAfterClassIT
     extends SurefireIntegrationTestCase
@@ -34,14 +35,30 @@ public class Surefire628ConsoleOutputBef
     public void testJunit3ParallelBuildResultCount()
     {
         OutputValidator validator = unpack("surefire-628-consoleoutputbeforeandafterclass")
-                .failNever().executeTest();
+                .failNever().parallelMethods().executeTest();
 
         validator.verifyTextInLog("628Test1");
         validator.verifyTextInLog("Before628Test1");
         validator.verifyTextInLog("After628Test1");
         validator.verifyTextInLog("628Test2");
-        validator.verifyTextInLog("Before628Test2");
-        validator.verifyTextInLog("After628Test2");
+        validator.verifyTextInLog("BeforeClass628Test2");
+        validator.verifyTextInLog("AfterClass628Test2");
     }
 
+
+
+    public void testProperBeforeAndAfterClassCapture()
+    {
+        OutputValidator validator = unpack("surefire-628-consoleoutputbeforeandafterclass")
+                .failNever().redirectToFile( true ).parallelClasses().executeTest();
+
+        final TestFile surefireReportsFile = validator.getSurefireReportsFile( "surefire628.Test2-output.txt"
);
+
+        surefireReportsFile
+        .assertContainsText("628Test2")
+        .assertContainsText("BeforeClass628Test2")
+        .assertContainsText("AfterClass628Test2");
+    }
+
+
 }
\ No newline at end of file

Modified: maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/pom.xml
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/pom.xml?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/pom.xml
(original)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/pom.xml
Sun Jan  8 21:24:39 2012
@@ -9,7 +9,7 @@
   <url>http://maven.apache.org</url>
   <properties>
     <junit.version>4.8.1</junit.version>
-    <junit.parallel>methods</junit.parallel>
+    <parallel>methods</parallel>
   </properties>
   <dependencies>
       <dependency>
@@ -34,7 +34,6 @@
            <version>${surefire.version}</version>
            <configuration>
              <forkMode>once</forkMode>
-             <parallel>${junit.parallel}</parallel>
              <threadCount>5</threadCount>
              <includes>
                 <include>**/Test*.java</include>

Modified: maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/src/test/java/surefire628/Test2.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/src/test/java/surefire628/Test2.java?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/src/test/java/surefire628/Test2.java
(original)
+++ maven/surefire/trunk/surefire-integration-tests/src/test/resources/surefire-628-consoleoutputbeforeandafterclass/src/test/java/surefire628/Test2.java
Sun Jan  8 21:24:39 2012
@@ -19,15 +19,9 @@ package surefire628;
  */
 
 
-import org.junit.Test;
-import org.junit.BeforeClass;
 import org.junit.AfterClass;
-
-
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assume.*;
+import org.junit.BeforeClass;
+import org.junit.Test;
 public class Test2
 {
     @Test
@@ -37,12 +31,12 @@ public class Test2
 
     @BeforeClass
     public static void testWithFailingAssumption2() {
-        System.out.println( "Before628Test2 on" + Thread.currentThread().getName());
+        System.out.println( "BeforeClass628Test2 on" + Thread.currentThread().getName());
     }
 
     @AfterClass
     public static void testWithFailingAssumption3() {
-        System.out.println( "After628Test2 on" + Thread.currentThread().getName());
+        System.out.println( "AfterClass628Test2 on" + Thread.currentThread().getName());
     }
 
 }
\ No newline at end of file

Added: maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ClassDemarcatingRunner.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ClassDemarcatingRunner.java?rev=1228960&view=auto
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ClassDemarcatingRunner.java
(added)
+++ maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ClassDemarcatingRunner.java
Sun Jan  8 21:24:39 2012
@@ -0,0 +1,71 @@
+package org.apache.maven.surefire.junitcore;
+
+/*
+ * 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.runner.Description;
+import org.junit.runner.Runner;
+import org.junit.runner.notification.RunNotifier;
+
+/**
+ * Attaches the current class to the thread
+ *
+ * @author Kristian Rosenvold
+ */
+public class ClassDemarcatingRunner
+    extends Runner
+{
+    private final Runner target;
+
+    private final Class testClass;
+
+    private static final ThreadLocal<Class> currentlyRunning = new ThreadLocal<Class>();
+
+    public ClassDemarcatingRunner( Runner target, Class testClass )
+    {
+        this.target = target;
+        this.testClass = testClass;
+    }
+
+    @Override
+    public Description getDescription()
+    {
+        return target.getDescription();
+    }
+
+    @Override
+    public void run( RunNotifier notifier )
+    {
+        currentlyRunning.set( testClass );
+        try
+        {
+            target.run( notifier );
+        }
+        finally
+        {
+            currentlyRunning.remove();
+        }
+    }
+
+    public static String getCurrentTestClass()
+    {
+        Class aClass = currentlyRunning.get();
+        return aClass != null ? aClass.getName() : null;
+    }
+}

Propchange: maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ClassDemarcatingRunner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConcurrentReporterManager.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConcurrentReporterManager.java?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConcurrentReporterManager.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConcurrentReporterManager.java
Sun Jan  8 21:24:39 2012
@@ -29,7 +29,7 @@ import org.apache.maven.surefire.testset
 
 /**
  * Handles responses from concurrent junit
- *
+ * <p/>
  * Stuff to remember about JUnit threading:
  * parallel=classes; beforeClass/afterClass, constructor and all tests method run on same
thread
  * parallel=methods; beforeClass/afterClass run on main thread, constructor + each test method
run on same thread
@@ -41,7 +41,6 @@ public abstract class ConcurrentReporter
     implements RunListener, ConsoleOutputReceiver
 {
     private final Map<String, TestSet> classMethodCounts;
-    // private final ReporterConfiguration reporterConfiguration;
 
     private final ThreadLocal<RunListener> reporterManagerThreadLocal = new ThreadLocal<RunListener>();
 
@@ -152,7 +151,12 @@ public abstract class ConcurrentReporter
 
     TestSet getTestSet( ReportEntry description )
     {
-        return classMethodCounts.get( description.getSourceName() );
+        TestSet testSet = classMethodCounts.get( description.getSourceName() );
+        if ( testSet == null )
+        {
+            testSet = classMethodCounts.get( ClassDemarcatingRunner.getCurrentTestClass()
);
+        }
+        return testSet;
     }
 
     RunListener getRunListener()
@@ -191,8 +195,17 @@ public abstract class ConcurrentReporter
         }
         else
         {
-            // Not able to assocaite output with any thread. Just dump to console
-            consoleLogger.info( new String( buf, off, len ) );
+            String currentTestClassName = ClassDemarcatingRunner.getCurrentTestClass();
+            TestSet testSet = currentTestClassName != null ? classMethodCounts.get( currentTestClassName
) : null;
+            if ( testSet != null )
+            {
+                testSet.getClassLevelLogicalStream().write( stdout, buf, off, len );
+            }
+            else
+            {
+                // Not able to assocaite output with any thread. Just dump to console
+                consoleLogger.info( new String( buf, off, len ) );
+            }
         }
     }
 

Modified: maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/ConfigurableParallelComputer.java
Sun Jan  8 21:24:39 2012
@@ -140,7 +140,8 @@ public class ConfigurableParallelCompute
         throws Throwable
     {
         Runner runner = super.getRunner( builder, testClass );
-        return fMethods && !isTestSuite(  testClass )? parallelize( runner, getMethodInterceptor()
) : runner;
+        ClassDemarcatingRunner classDemarcatingRunner = new ClassDemarcatingRunner(runner,
testClass);
+        return fMethods && !isTestSuite(  testClass )? parallelize( classDemarcatingRunner,
getMethodInterceptor() ) : classDemarcatingRunner;
     }
 
     private boolean isTestSuite( Class<?> testClass){

Modified: maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java?rev=1228960&r1=1228959&r2=1228960&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestSet.java
Sun Jan  8 21:24:39 2012
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.RunListener;
 import org.apache.maven.surefire.report.SimpleReportEntry;
@@ -53,6 +54,9 @@ public class TestSet
 
     private final AtomicBoolean played = new AtomicBoolean();
 
+    private volatile LogicalStream beforeClass;
+
+    private volatile LogicalStream afterClass;
 
     public TestSet( Description testSetDescription )
     {
@@ -77,6 +81,11 @@ public class TestSet
 
             target.testSetStarting( report );
 
+            if ( beforeClass != null )
+            {
+                beforeClass.writeDetails( ( (ConsoleOutputReceiver) target ) );
+            }
+
             for ( TestMethod testMethod : testMethods )
             {
                 testMethod.replay( target );
@@ -84,6 +93,10 @@ public class TestSet
 
             report = createReportEntry( elapsed );
 
+            if ( afterClass != null )
+            {
+                afterClass.writeDetails( ( (ConsoleOutputReceiver) target ) );
+            }
             target.testSetCompleted( report );
         }
         catch ( Exception e )
@@ -149,4 +162,26 @@ public class TestSet
     {
         return testSet.get();
     }
+
+    public LogicalStream getClassLevelLogicalStream()
+    {
+        if ( numberOfCompletedChildren.get() > 0 )
+        {
+            if ( afterClass == null )
+            {
+                afterClass = new LogicalStream();
+            }
+            return afterClass;
+        }
+        else
+        {
+            if ( beforeClass == null )
+            {
+                beforeClass = new LogicalStream();
+            }
+            return beforeClass;
+        }
+    }
+
+
 }



Mime
View raw message