Author: stephenc
Date: Tue May 24 09:04:29 2011
New Revision: 1126942

URL: http://svn.apache.org/viewvc?rev=1126942&view=rev
Log:
implemented the safely close methods

Modified:
    
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/pom.xml
    
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/IOUtil.java
    
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/IOUtilTest.java

Modified: 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/pom.xml?rev=1126942&r1=1126941&r2=1126942&view=diff
==============================================================================
--- 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/pom.xml
 (original)
+++ 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/pom.xml
 Tue May 24 09:04:29 2011
@@ -17,6 +17,10 @@
 
   <dependencies>
     <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>

Modified: 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/IOUtil.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/IOUtil.java?rev=1126942&r1=1126941&r2=1126942&view=diff
==============================================================================
--- 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/IOUtil.java
 (original)
+++ 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/IOUtil.java
 Tue May 24 09:04:29 2011
@@ -19,6 +19,8 @@ package org.codehaus.plexus.util;
  * under the License.
  */
 
+import org.apache.commons.io.IOUtils;
+
 public final class IOUtil
 {
     private static final int DEFAULT_BUFFER_SIZE = 4096;
@@ -256,21 +258,21 @@ public final class IOUtil
 
     public static void close( java.io.InputStream inputStream )
     {
-        throw new UnsupportedOperationException( "Not implemented yet" );
+        IOUtils.closeQuietly( inputStream );
     }
 
     public static void close( java.io.OutputStream outputStream )
     {
-        throw new UnsupportedOperationException( "Not implemented yet" );
+        IOUtils.closeQuietly( outputStream);
     }
 
     public static void close( java.io.Reader reader )
     {
-        throw new UnsupportedOperationException( "Not implemented yet" );
+        IOUtils.closeQuietly( reader );
     }
 
     public static void close( java.io.Writer writer )
     {
-        throw new UnsupportedOperationException( "Not implemented yet" );
+        IOUtils.closeQuietly( writer );
     }
 }
\ No newline at end of file

Modified: 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/IOUtilTest.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/IOUtilTest.java?rev=1126942&r1=1126941&r2=1126942&view=diff
==============================================================================
--- 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/IOUtilTest.java
 (original)
+++ 
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/IOUtilTest.java
 Tue May 24 09:04:29 2011
@@ -21,7 +21,24 @@ package org.codehaus.plexus.util;
 
 import org.junit.Test;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static 
org.apache.maven.tck.TckMatchers.isClassWithOnlyPrivateConstructors;
 import static org.apache.maven.tck.TckMatchers.isUtilityClass;
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 public class IOUtilTest
@@ -34,4 +51,136 @@ public class IOUtilTest
         assertThat( IOUtil.class, isUtilityClass() );
     }
 
+    @Test
+    public void closeReaderWithNull() throws Exception {
+        IOUtil.close( (Reader)null );
+    }
+
+    @Test
+    public void closeWriterWithNull() throws Exception {
+        IOUtil.close( (Writer)null );
+    }
+
+    @Test
+    public void closeInputStreamWithNull() throws Exception {
+        IOUtil.close( (InputStream)null );
+    }
+
+    @Test
+    public void closeOutputStreamWithNull() throws Exception {
+        IOUtil.close( (OutputStream) null );
+    }
+
+    @Test
+    public void closeReaderWithIOE() throws Exception {
+        IOUtil.close( new BufferedReader( new StringReader( "" ) ) {
+            @Override
+            public void close()
+                throws IOException
+            {
+                super.close();
+                throw new IOException( "don't bomb out" );
+            }
+        });
+    }
+
+    @Test
+    public void closeWriterWithIOE() throws Exception {
+        IOUtil.close( new BufferedWriter( new StringWriter(  ) ) {
+            @Override
+            public void close()
+                throws IOException
+            {
+                super.close();
+                throw new IOException( "don't bomb out" );
+            }
+        });
+    }
+
+    @Test
+    public void closeInputStreamWithIOE() throws Exception {
+        IOUtil.close( new BufferedInputStream( new ByteArrayInputStream( new 
byte[0] ) ){
+            @Override
+            public void close()
+                throws IOException
+            {
+                super.close();
+                throw new IOException( "don't bomb out" );
+            }
+        });
+    }
+
+    @Test
+    public void closeOutputStreamWithIOE() throws Exception {
+        IOUtil.close( new BufferedOutputStream( new ByteArrayOutputStream(  ) 
){
+            @Override
+            public void close()
+                throws IOException
+            {
+                super.close();
+                throw new IOException( "don't bomb out" );
+            }
+        });
+    }
+
+    @Test
+    public void closeReaderCloses() throws Exception {
+        final AtomicBoolean closed = new AtomicBoolean( false );
+        IOUtil.close( new BufferedReader( new StringReader( "" ) ) {
+            @Override
+            public void close()
+                throws IOException
+            {
+                closed.set( true );
+                super.close();
+            }
+        });
+        assertThat( closed.get(), is( true ) );
+    }
+
+    @Test
+    public void closeWriterCloses() throws Exception {
+        final AtomicBoolean closed = new AtomicBoolean( false );
+        IOUtil.close( new BufferedWriter( new StringWriter(  ) ) {
+            @Override
+            public void close()
+                throws IOException
+            {
+                closed.set( true );
+                super.close();
+            }
+        });
+        assertThat( closed.get(), is( true ) );
+    }
+
+    @Test
+    public void closeInputStreamCloses() throws Exception {
+        final AtomicBoolean closed = new AtomicBoolean( false );
+        IOUtil.close( new BufferedInputStream( new ByteArrayInputStream( new 
byte[0] ) ){
+            @Override
+            public void close()
+                throws IOException
+            {
+                closed.set( true );
+                super.close();
+            }
+        });
+        assertThat( closed.get(), is( true ) );
+    }
+
+    @Test
+    public void closeOutputStreamCloses() throws Exception {
+        final AtomicBoolean closed = new AtomicBoolean( false );
+        IOUtil.close( new BufferedOutputStream( new ByteArrayOutputStream(  ) 
){
+            @Override
+            public void close()
+                throws IOException
+            {
+                closed.set( true );
+                super.close();
+            }
+        });
+        assertThat( closed.get(), is( true ) );
+    }
+
 }


Reply via email to