[SUREFIRE-1091] Prevent IOExceptions in case a thread tries to write to an already...
authorAndreas Gudian <agudian@apache.org>
Tue, 26 Aug 2014 19:55:26 +0000 (21:55 +0200)
committerAndreas Gudian <agudian@apache.org>
Tue, 26 Aug 2014 20:30:02 +0000 (22:30 +0200)
maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/Utf8RecodingDeferredFileOutputStream.java
surefire-integration-tests/src/test/resources/consoleoutput-noisy/src/test/java/consoleoutput_noisy/Test1.java

index 5c7b8a3..af1682f 100644 (file)
@@ -37,6 +37,8 @@ class Utf8RecodingDeferredFileOutputStream
 {
     private DeferredFileOutputStream deferredFileOutputStream;
 
+    private boolean closed = false;
+
     private static final Charset UTF8 = Charset.forName( "UTF-8" );
 
     public Utf8RecodingDeferredFileOutputStream( String channel )
@@ -44,9 +46,14 @@ class Utf8RecodingDeferredFileOutputStream
         this.deferredFileOutputStream = new DeferredFileOutputStream( 1000000, channel, "deferred", null );
     }
 
-    public void write( byte[] buf, int off, int len )
+    public synchronized void write( byte[] buf, int off, int len )
         throws IOException
     {
+        if ( closed )
+        {
+            return;
+        }
+
         if ( !Charset.defaultCharset().equals( UTF8 ) )
         {
             CharBuffer decodedFromDefaultCharset = Charset.defaultCharset().decode( ByteBuffer.wrap( buf, off, len ) );
@@ -77,24 +84,29 @@ class Utf8RecodingDeferredFileOutputStream
         return deferredFileOutputStream.getByteCount();
     }
 
-    public void close()
+    public synchronized void close()
         throws IOException
     {
+        closed = true;
         deferredFileOutputStream.close();
     }
 
-    public void writeTo( OutputStream out )
+    public synchronized void writeTo( OutputStream out )
         throws IOException
     {
-        deferredFileOutputStream.writeTo( out );
+        if ( closed )
+        {
+            deferredFileOutputStream.writeTo( out );
+        }
     }
 
-    public void free()
+    public synchronized void free()
     {
         if ( null != deferredFileOutputStream && null != deferredFileOutputStream.getFile() )
         {
             try
             {
+                closed = true;
                 deferredFileOutputStream.close();
                 if ( !deferredFileOutputStream.getFile().delete() )
                 {
index 4b5a21f..888cd22 100644 (file)
@@ -19,38 +19,67 @@ package consoleoutput_noisy;
  * under the License.
  */
 
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
 
 public class Test1
-    extends TestCase
 {
 
     public static final int thousand = Integer.parseInt( System.getProperty( "thousand", "1000" ) );
 
+    @Test
     public void test1MillionBytes()
     {
         for ( int i = 0; i < ( 10 * thousand ); i++ )
         {
-            System.out.println(
-                "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" );
+            System.out.println( "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" );
         }
     }
 
-    public static void testHundredThousand()
+    @Test
+    public void testHundredThousand()
     {
-        for ( int i = 0; i < thousand; i++ )
-        {
-            System.out.println(
-                "AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFFGGGGGGGGGGHHHHHHHHHHIIIIIIIIIIJJJJJJJJJJ" );
-        }
+        printAlot();
     }
 
-    public static void testAnotherHundredThousand()
+    private static void printAlot()
     {
         for ( int i = 0; i < thousand; i++ )
         {
-            System.out.println(
-                "AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFFGGGGGGGGGGHHHHHHHHHHIIIIIIIIIIJJJJJJJJJJ" );
+            System.out.println( "AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFFGGGGGGGGGGHHHHHHHHHHIIIIIIIIIIJJJJJJJJJJ" );
         }
     }
+
+    @Test
+    public void testAnotherHundredThousand()
+    {
+        printAlot();
+    }
+
+    @Before
+    public void before()
+    {
+        printAlot();
+    }
+
+    @BeforeClass
+    public static void beforeClass()
+    {
+        printAlot();
+    }
+
+    @After
+    public void after()
+    {
+        printAlot();
+    }
+
+    @AfterClass
+    public static void afterClass()
+    {
+        printAlot();
+    }
 }

Copyright 2016, The Apache Software Foundation.