Author: krosenvold Date: Tue Apr 19 18:48:25 2011 New Revision: 1095165 URL: http://svn.apache.org/viewvc?rev=1095165&view=rev Log: [SUREFIRE-730] Fixed failure status
With unit test Modified: maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4RunListener.java maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/junit4/MockReporter.java maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java Modified: maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4RunListener.java URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4RunListener.java?rev=1095165&r1=1095164&r2=1095165&view=diff ============================================================================== --- maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4RunListener.java (original) +++ maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4RunListener.java Tue Apr 19 18:48:25 2011 @@ -42,7 +42,7 @@ public class JUnit4RunListener * This flag is set after a failure has occurred so that a <code>testSucceeded</code> event is not fired. * This is necessary because JUnit4 always fires a <code>testRunFinished</code> event-- even if there was a failure. */ - private boolean failureFlag; + private ThreadLocal failureFlag = new InheritableThreadLocal(); /** * Constructor. @@ -76,7 +76,7 @@ public class JUnit4RunListener throws Exception { reporter.testStarting( createReportEntry( description ) ); - failureFlag = false; + failureFlag.remove( ); } /** @@ -100,13 +100,13 @@ public class JUnit4RunListener { this.reporter.testError( report ); } - failureFlag = true; + failureFlag.set( Boolean.TRUE ); } public void testAssumptionFailure( Failure failure ) { this.reporter.testAssumptionFailure( createReportEntry( failure.getDescription() ) ); - failureFlag = true; + failureFlag.set( Boolean.TRUE ); } @@ -118,7 +118,8 @@ public class JUnit4RunListener public void testFinished( Description description ) throws Exception { - if ( !failureFlag ) + Boolean failure = (Boolean) failureFlag.get(); + if ( failure == null ) { reporter.testSucceeded( createReportEntry( description ) ); } Modified: maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/junit4/MockReporter.java URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/junit4/MockReporter.java?rev=1095165&r1=1095164&r2=1095165&view=diff ============================================================================== --- maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/junit4/MockReporter.java (original) +++ maven/surefire/trunk/surefire-providers/common-junit4/src/main/java/org/apache/maven/surefire/junit4/MockReporter.java Tue Apr 19 18:48:25 2011 @@ -19,14 +19,13 @@ package org.apache.maven.surefire.junit4 * under the License. */ -import org.apache.maven.surefire.report.RunListener; -import org.apache.maven.surefire.report.ReportEntry; -import org.apache.maven.surefire.report.ReporterConfiguration; -import org.apache.maven.surefire.report.ReporterException; - import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.maven.surefire.report.ReportEntry; +import org.apache.maven.surefire.report.ReporterConfiguration; +import org.apache.maven.surefire.report.ReporterException; +import org.apache.maven.surefire.report.RunListener; /** Internal use only */ public class MockReporter @@ -143,10 +142,12 @@ public class MockReporter public void testError( ReportEntry report ) { + testError.incrementAndGet(); } public void testFailed( ReportEntry report ) { + testFailed.incrementAndGet(); } public void testAssumptionFailure( ReportEntry report ) Modified: maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java?rev=1095165&r1=1095164&r2=1095165&view=diff ============================================================================== --- maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java (original) +++ maven/surefire/trunk/surefire-providers/common-junit4/src/test/java/org/apache/maven/surefire/common/junit4/JUnit4RunListenerTest.java Tue Apr 19 18:48:25 2011 @@ -19,11 +19,15 @@ package org.apache.maven.surefire.common * under the License. */ +import java.util.concurrent.CountDownLatch; import org.apache.maven.surefire.junit4.MockReporter; +import junit.framework.Assert; import org.junit.Test; +import org.junit.runner.Description; import org.junit.runner.Request; import org.junit.runner.Runner; +import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; import org.junit.runner.notification.RunNotifier; @@ -44,6 +48,42 @@ public class JUnit4RunListenerTest junitTestRunner.run( runNotifier ); } + @Test + public void testParallelInvocations() + throws Exception + { + final MockReporter reporter = new MockReporter(); + final RunListener jUnit4TestSetReporter = new JUnit4RunListener( reporter ); + final CountDownLatch countDownLatch = new CountDownLatch( 1 ); + final Description testSomething = Description.createTestDescription( STest1.class, "testSomething" ); + final Description testSomething2 = Description.createTestDescription( STest2.class, "testSomething2" ); + + jUnit4TestSetReporter.testStarted( testSomething ); + + new Thread(new Runnable(){ + public void run() + { + try + { + jUnit4TestSetReporter.testStarted( testSomething2 ); + jUnit4TestSetReporter.testFailure( new Failure( testSomething2, new AssertionError( "Fud" ) )); + jUnit4TestSetReporter.testFinished( testSomething2 ); + countDownLatch.countDown(); + } + catch ( Exception e ) + { + throw new RuntimeException( e ); + } + } + }).start(); + + countDownLatch.await(); + jUnit4TestSetReporter.testFinished( testSomething ); + + Assert.assertEquals( "Failing tests", 1, reporter.getTestFailed() ); + Assert.assertEquals( "Succeeded tests", 1, reporter.getTestSucceeded() ); + } + public static class STest1 {