Author: stephenc Date: Tue May 24 22:43:04 2011 New Revision: 1127325 URL: http://svn.apache.org/viewvc?rev=1127325&view=rev Log: update to commons-io 2.0, and add some more implementations and test cases
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 maven/sandbox/trunk/plexus-utils-commons-bridge/pom.xml 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=1127325&r1=1127324&r2=1127325&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 22:43:04 2011 @@ -17,7 +17,7 @@ <dependencies> <dependency> - <groupId>org.apache.commons</groupId> + <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> 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=1127325&r1=1127324&r2=1127325&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 22:43:04 2011 @@ -21,6 +21,12 @@ package org.codehaus.plexus.util; import org.apache.commons.io.IOUtils; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; + public final class IOUtil { private IOUtil() @@ -31,13 +37,22 @@ public final class IOUtil public static void copy( java.io.InputStream input, java.io.OutputStream output ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + IOUtils.copy( input, output ); } public static void copy( java.io.InputStream input, java.io.OutputStream output, int bufferSize ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + if ( bufferSize < 0 ) + { + throw new NegativeArraySizeException(); + } + input.getClass(); + if ( IOUtils.copy( input, output ) > 0 ) + { + // don't you just love recreating buggy behaviour for compatibility's sake + fakeBufferSizeHandler( bufferSize ); + } } public static void copy( java.io.Reader input, java.io.Writer output ) @@ -215,37 +230,54 @@ public final class IOUtil public static java.lang.String toString( byte[] input ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + return IOUtils.toString( input ); } public static java.lang.String toString( byte[] input, int bufferSize ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + input.getClass(); // throw NPE if null + fakeBufferSizeHandler( bufferSize ); + return IOUtils.toString( input ); } public static java.lang.String toString( byte[] input, java.lang.String encoding ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + input.getClass(); // throw NPE if null + encoding.getClass(); // throw NPE if null + return IOUtils.toString( input, encoding ); } public static java.lang.String toString( byte[] input, java.lang.String encoding, int bufferSize ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + input.getClass(); // throw NPE if null + encoding.getClass(); // throw NPE if null + try + { + Charset.forName( encoding ); // validate charset before checking buffer size. + } + catch ( UnsupportedCharsetException e ) + { + throw new UnsupportedEncodingException( e.getLocalizedMessage() ); + } + fakeBufferSizeHandler( bufferSize ); + return IOUtils.toString( input, encoding ); } public static void copy( byte[] input, java.io.OutputStream output ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + output.getClass(); // throw NPE if null + IOUtils.copy( new ByteArrayInputStream( input ), output ); } public static void copy( byte[] input, java.io.OutputStream output, int bufferSize ) throws java.io.IOException { - throw new UnsupportedOperationException( "Not implemented yet" ); + output.getClass(); // throw NPE if null + IOUtils.copy( new ByteArrayInputStream( input ), output ); } public static boolean contentEquals( java.io.InputStream input1, java.io.InputStream input2 ) @@ -261,7 +293,7 @@ public final class IOUtil public static void close( java.io.OutputStream outputStream ) { - IOUtils.closeQuietly( outputStream); + IOUtils.closeQuietly( outputStream ); } public static void close( java.io.Reader reader ) @@ -273,4 +305,35 @@ public final class IOUtil { IOUtils.closeQuietly( writer ); } + + /** + * Throws a NegativeArraySizeException if bufferSize is negative, infinite-loops if bufferSize is zero, + * otherwise no-op as Commons IO handles buffering for us. + * + * @param bufferSize the buffer size. + * @throws IOException if interrupted in infinite loop. + * @throws NegativeArraySizeException if buffer size less than zero. + */ + private static void fakeBufferSizeHandler( int bufferSize ) + throws IOException + { + if ( bufferSize < 0 ) + { + throw new NegativeArraySizeException(); + } + while ( bufferSize == 0 ) + { + try + { + Thread.sleep( 1 ); + } + catch ( InterruptedException e ) + { + IOException ex = new IOException( e.getLocalizedMessage() ); + ex.initCause( e ); + throw ex; + } + } + } + } \ 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=1127325&r1=1127324&r2=1127325&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 22:43:04 2011 @@ -33,12 +33,13 @@ import java.io.OutputStream; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.concurrent.atomic.AtomicBoolean; -import static org.apache.maven.tck.TckMatchers.*; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.apache.maven.tck.TckMatchers.isUtilityClass; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; public class IOUtilTest { @@ -215,100 +216,848 @@ public class IOUtilTest } @Test - public void toByteArrayFromString() throws Exception { + public void toByteArrayFromString() + throws Exception + { String probe = "A string \u2345\u00ef"; assertThat( IOUtil.toByteArray( probe ), is( probe.getBytes() ) ); } @Test - public void toByteArrayFromReader() throws Exception { + public void toByteArrayFromReader() + throws Exception + { String probe = "A string \u2345\u00ef"; assertThat( IOUtil.toByteArray( new StringReader( probe ) ), is( probe.getBytes() ) ); } @Test - public void toByteArrayFromInputStream() throws Exception { + public void toByteArrayFromInputStream() + throws Exception + { String probe = "A string \u2345\u00ef"; - assertThat( IOUtil.toByteArray( new ByteArrayInputStream( IOUtil.toByteArray( probe ) ) ), + assertThat( IOUtil.toByteArray( new DontCloseByteArrayInputStream( IOUtil.toByteArray( probe ) ) ), is( probe.getBytes() ) ); } - @Test(expected = NullPointerException.class) - public void toByteArrayNullString() throws Exception { + @Test( expected = NullPointerException.class ) + public void toByteArrayNullString() + throws Exception + { IOUtil.toByteArray( (String) null ); } - @Test(expected = NullPointerException.class) - public void toByteArrayNullReader() throws Exception { + @Test( expected = NullPointerException.class ) + public void toByteArrayNullReader() + throws Exception + { IOUtil.toByteArray( (Reader) null ); } - @Test(expected = NullPointerException.class) - public void toByteArrayNullInputStream() throws Exception { + @Test( expected = NullPointerException.class ) + public void toByteArrayNullInputStream() + throws Exception + { IOUtil.toByteArray( (InputStream) null ); } - @Test(expected = IOException.class) - public void contentEqualNullNull() throws Exception { + @Test( expected = IOException.class ) + public void contentEqualNullNull() + throws Exception + { IOUtil.contentEquals( null, null ); } - @Test(expected = IOException.class) - public void contentEqualNonNullNull() throws Exception { - IOUtil.contentEquals( new ByteArrayInputStream( new byte[0] ), null ); + @Test( expected = IOException.class ) + public void contentEqualNonNullNull() + throws Exception + { + IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[0] ), null ); } - @Test(expected = IOException.class) - public void contentEqualNullNonNull() throws Exception { - IOUtil.contentEquals( new ByteArrayInputStream( new byte[0] ), null ); + @Test( expected = IOException.class ) + public void contentEqualNullNonNull() + throws Exception + { + IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[0] ), null ); } @Test - public void contentEqualEmptyEmpty() throws Exception { + public void contentEqualEmptyEmpty() + throws Exception + { assertThat( - IOUtil.contentEquals( new ByteArrayInputStream( new byte[0] ), new ByteArrayInputStream( new byte[0] ) ), + IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[0] ), new DontCloseByteArrayInputStream( new byte[0] ) ), is( true ) ); } @Test - public void contentEqualNonEmptyEmpty() throws Exception { + public void contentEqualNonEmptyEmpty() + throws Exception + { assertThat( - IOUtil.contentEquals( new ByteArrayInputStream( new byte[1] ), new ByteArrayInputStream( new byte[0] ) ), + IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[1] ), new DontCloseByteArrayInputStream( new byte[0] ) ), is( false ) ); } @Test - public void contentEqualEmptyNonEmpty() throws Exception { + public void contentEqualEmptyNonEmpty() + throws Exception + { assertThat( - IOUtil.contentEquals( new ByteArrayInputStream( new byte[0] ), new ByteArrayInputStream( new byte[1] ) ), + IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[0] ), new DontCloseByteArrayInputStream( new byte[1] ) ), is( false ) ); } @Test - public void contentEqualNonEmptyNonEmpty() throws Exception { + public void contentEqualNonEmptyNonEmpty() + throws Exception + { assertThat( - IOUtil.contentEquals( new ByteArrayInputStream( new byte[1] ), new ByteArrayInputStream( new byte[1] ) ), + IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[1] ), new DontCloseByteArrayInputStream( new byte[1] ) ), is( true ) ); } @Test - public void contentEqualMostlySame() throws Exception { - assertThat( IOUtil.contentEquals( new ByteArrayInputStream( new byte[]{ 1, 2, 3, 4, 5, 6 } ), - new ByteArrayInputStream( new byte[]{ 1, 2, 3, 4, 5, 7 } ) ), is( false ) ); + public void contentEqualMostlySame() + throws Exception + { + assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[]{ 1, 2, 3, 4, 5, 6 } ), + new DontCloseByteArrayInputStream( new byte[]{ 1, 2, 3, 4, 5, 7 } ) ), is( false ) ); } @Test - public void contentEqualLargeSame() throws Exception { - assertThat( IOUtil.contentEquals( new ByteArrayInputStream( new byte[8192] ), - new ByteArrayInputStream( new byte[8192] ) ), is( true ) ); + public void contentEqualLargeSame() + throws Exception + { + assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[8192] ), + new DontCloseByteArrayInputStream( new byte[8192] ) ), is( true ) ); } @Test - public void contentEqualLargeDifferent() throws Exception { + public void contentEqualLargeDifferent() + throws Exception + { byte[] buf = new byte[8192]; buf[8191] = 1; - assertThat( IOUtil.contentEquals( new ByteArrayInputStream( new byte[8192] ), new ByteArrayInputStream( buf ) ), + assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[8192] ), new DontCloseByteArrayInputStream( buf ) ), is( false ) ); } + @Test( expected = NullPointerException.class ) + public void toStringNullByteArray() + throws Exception + { + IOUtil.toString( (byte[]) null ); + } + + @Test + public void toStringEmptyByteArray() + throws Exception + { + assertThat( IOUtil.toString( new byte[0] ), is( "" ) ); + } + + @Test + public void toStringByteArray() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes() ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayNegBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, -1 ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void toStringEmptyByteArrayNegBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], -1 ), is( "" ) ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void toStringByteArrayNegBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), -1 ), is( probe ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void toStringNullByteArrayZeroBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, 0 ); + } + + @Test( timeout = 150 ) + public void toStringEmptyByteArrayZeroBufSz() + throws Exception + { + final AtomicBoolean finished = new AtomicBoolean( false ); + Thread worker = new Thread() + { + @Override + public void run() + { + try + { + IOUtil.toString( new byte[0], 0 ); + } + catch ( IOException e ) + { + // ignore + } + finished.set( true ); + } + }; + worker.start(); + worker.join( 100 ); + worker.interrupt(); + assertThat( "We have an infinite loop", finished.get(), is( false ) ); + } + + @Test( timeout = 150 ) + public void toStringByteArrayZeroBufSz() + throws Exception + { + final AtomicBoolean finished = new AtomicBoolean( false ); + Thread worker = new Thread() + { + @Override + public void run() + { + try + { + String probe = "A string \u2345\u00ef"; + IOUtil.toString( probe.getBytes(), 0 ); + } + catch ( IOException e ) + { + // ignore + } + finished.set( true ); + } + }; + worker.start(); + worker.join( 100 ); + worker.interrupt(); + assertThat( "We have an infinite loop", finished.get(), is( false ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayPosBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, 1 ); + } + + @Test + public void toStringEmptyByteArrayPosBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], 1 ), is( "" ) ); + } + + @Test + public void toStringByteArrayPosBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), 1 ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayNullEncoding() + throws Exception + { + IOUtil.toString( (byte[]) null, null ); + } + + @Test( expected = NullPointerException.class ) + public void toStringEmptyByteArrayNullEncoding() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], null ), is( "" ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringByteArrayNullEncoding() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), null ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayJunkEncoding() + throws Exception + { + IOUtil.toString( (byte[]) null, "junk" ); + } + + @Test( expected = UnsupportedEncodingException.class ) + public void toStringEmptyByteArrayJunkEncoding() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], "junk" ), is( "" ) ); + } + + @Test( expected = UnsupportedEncodingException.class ) + public void toStringByteArrayJunkEncoding() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), "junk" ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayValidEncoding() + throws Exception + { + IOUtil.toString( (byte[]) null, "utf-16" ); + } + + @Test + public void toStringEmptyByteArrayValidEncoding() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], "utf-16" ), is( "" ) ); + } + + @Test + public void toStringByteArrayValidEncoding() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes( "utf-16" ), "utf-16" ).getBytes( "utf-8" ), + is( probe.getBytes( "utf-8" ) ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayNullEncodingNegBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, null, -1 ); + } + + @Test( expected = NullPointerException.class ) + public void toStringEmptyByteArrayNullEncodingNegBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], null, -1 ), is( "" ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringByteArrayNullEncodingNegBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), null, -1 ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayJunkEncodingNegBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, "junk", -1 ); + } + + @Test( expected = UnsupportedEncodingException.class ) + public void toStringEmptyByteArrayJunkEncodingNegBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], "junk", -1 ), is( "" ) ); + } + + @Test( expected = UnsupportedEncodingException.class ) + public void toStringByteArrayJunkEncodingNegBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), "junk", -1 ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class ) + public void toStringNullByteArrayValidEncodingNegBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, "utf-16", -1 ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void toStringEmptyByteArrayValidEncodingNegBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], "utf-16", -1 ), is( "" ) ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void toStringByteArrayValidEncodingNegBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes( "utf-16" ), "utf-16", -1 ).getBytes( "utf-8" ), + is( probe.getBytes( "utf-8" ) ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void toStringNullByteArrayNullEncodingZeroBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, null, 0 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void toStringEmptyByteArrayNullEncodingZeroBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], null, 0 ), is( "" ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void toStringByteArrayNullEncodingZeroBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), null, 0 ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void toStringNullByteArrayJunkEncodingZeroBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, "junk", 0 ); + } + + @Test( expected = UnsupportedEncodingException.class, timeout = 150 ) + public void toStringEmptyByteArrayJunkEncodingZeroBufSz() + throws Exception + { + assertThat( IOUtil.toString( new byte[0], "junk", 0 ), is( "" ) ); + } + + @Test( expected = UnsupportedEncodingException.class, timeout = 150 ) + public void toStringByteArrayJunkEncodingZeroBufSz() + throws Exception + { + String probe = "A string \u2345\u00ef"; + assertThat( IOUtil.toString( probe.getBytes(), "junk", 0 ).getBytes(), is( probe.getBytes() ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void toStringNullByteArrayValidEncodingZeroBufSz() + throws Exception + { + IOUtil.toString( (byte[]) null, "utf-16", 0 ); + } + + @Test( timeout = 150 ) + public void toStringEmptyByteArrayValidEncodingZeroBufSz() + throws Exception + { + final AtomicBoolean finished = new AtomicBoolean( false ); + Thread worker = new Thread() + { + @Override + public void run() + { + try + { + IOUtil.toString( new byte[0], "utf-16", 0 ); + } + catch ( IOException e ) + { + // ignore + } + finished.set( true ); + } + }; + worker.start(); + worker.join( 100 ); + worker.interrupt(); + assertThat( "We have an infinite loop", finished.get(), is( false ) ); + } + + @Test( timeout = 150 ) + public void toStringByteArrayValidEncodingZeroBufSz() + throws Exception + { + final AtomicBoolean finished = new AtomicBoolean( false ); + Thread worker = new Thread() + { + @Override + public void run() + { + try + { + String probe = "A string \u2345\u00ef"; + IOUtil.toString( probe.getBytes(), "utf-16", 0 ); + } + catch ( IOException e ) + { + // ignore + } + finished.set( true ); + } + }; + worker.start(); + worker.join( 100 ); + worker.interrupt(); + assertThat( "We have an infinite loop", finished.get(), is( false ) ); + } + + @Test( expected = NullPointerException.class ) + public void copyNullByteArrayNullOutputStream() + throws Exception + { + IOUtil.copy( (byte[]) null, (OutputStream) null ); + } + + @Test( expected = NullPointerException.class ) + public void copyNullByteArrayValidOutputStream() + throws Exception + { + IOUtil.copy( (byte[]) null, new DontCloseByteArrayOutputStream() ); + } + + @Test( expected = NullPointerException.class ) + public void copyEmptyByteArrayNullOutputStream() + throws Exception + { + IOUtil.copy( new byte[0], (OutputStream) null ); + } + + @Test + public void copyEmptyByteArrayValidOutputStream() + throws Exception + { + IOUtil.copy( new byte[0], new DontCloseByteArrayOutputStream()); + } + + @Test + public void copyByteArrayValidOutputStream() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( input, outputStream ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + + @Test( expected = NullPointerException.class ) + public void copyNullByteArrayNullOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( (byte[]) null, (OutputStream) null, -1 ); + } + + @Test( expected = NullPointerException.class ) + public void copyNullByteArrayValidOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( (byte[]) null, new DontCloseByteArrayOutputStream(), -1 ); + } + + @Test( expected = NullPointerException.class ) + public void copyEmptyByteArrayNullOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( new byte[0], (OutputStream) null, -1 ); + } + + @Test + public void copyEmptyByteArrayValidOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( new byte[0], new DontCloseByteArrayOutputStream(), -1 ); + } + + @Test + public void copyByteArrayValidOutputStreamNegBufSz() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( input, outputStream, -1 ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullByteArrayNullOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( (byte[]) null, (OutputStream) null, 0 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullByteArrayValidOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( (byte[]) null, new DontCloseByteArrayOutputStream(), 0 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyEmptyByteArrayNullOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( new byte[0], (OutputStream) null, 0 ); + } + + @Test( timeout = 150 ) + public void copyEmptyByteArrayValidOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( new byte[0], new DontCloseByteArrayOutputStream(), 0 ); + } + + @Test( timeout = 150 ) + public void copyByteArrayValidOutputStreamZeroBufSz() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( input, outputStream, 0 ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullByteArrayNullOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( (byte[]) null, (OutputStream) null, 1 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullByteArrayValidOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( (byte[]) null, new DontCloseByteArrayOutputStream(), 1 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyEmptyByteArrayNullOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( new byte[0], (OutputStream) null, 1 ); + } + + @Test( timeout = 150 ) + public void copyEmptyByteArrayValidOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( new byte[0], new DontCloseByteArrayOutputStream(), 1 ); + } + + @Test( timeout = 150 ) + public void copyByteArrayValidOutputStreamPosBufSz() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( input, outputStream, 1 ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + @Test( expected = NullPointerException.class ) + public void copyNullInputStreamNullOutputStream() + throws Exception + { + IOUtil.copy( (InputStream) null, (OutputStream) null ); + } + + @Test( expected = NullPointerException.class ) + public void copyNullInputStreamValidOutputStream() + throws Exception + { + IOUtil.copy( (InputStream) null, new DontCloseByteArrayOutputStream() ); + } + + @Test + public void copyEmptyInputStreamNullOutputStream() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0] ), (OutputStream) null ); + } + + @Test + public void copyEmptyInputStreamValidOutputStream() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0] ), new DontCloseByteArrayOutputStream()); + } + + @Test + public void copyInputStreamValidOutputStream() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( new DontCloseByteArrayInputStream( input ), outputStream ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void copyNullInputStreamNullOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( (InputStream) null, (OutputStream) null, -1 ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void copyNullInputStreamValidOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( (InputStream) null, new DontCloseByteArrayOutputStream(), -1 ); + } + + @Test( expected = NegativeArraySizeException.class ) + public void copyEmptyInputStreamNullOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0]), (OutputStream) null, -1 ); + } + + @Test(expected = NegativeArraySizeException.class) + public void copyEmptyInputStreamValidOutputStreamNegBufSz() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream(new byte[0]), new DontCloseByteArrayOutputStream(), -1 ); + } + + @Test(expected = NegativeArraySizeException.class) + public void copyInputStreamValidOutputStreamNegBufSz() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( new DontCloseByteArrayInputStream( input ), outputStream, -1 ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullInputStreamNullOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( (InputStream) null, (OutputStream) null, 0 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullInputStreamValidOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( (InputStream) null, new ByteArrayOutputStream(), 0 ); + } + + @Test( timeout = 150 ) + public void copyEmptyInputStreamNullOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0]), (OutputStream) null, 0 ); + } + + @Test( timeout = 150 ) + public void copyEmptyInputStreamValidOutputStreamZeroBufSz() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0]), new DontCloseByteArrayOutputStream(), 0 ); + } + + @Test( timeout = 150 ) + public void copyInputStreamValidOutputStreamZeroBufSz() + throws Exception + { + final AtomicBoolean finished = new AtomicBoolean( false ); + Thread worker = new Thread() + { + @Override + public void run() + { + try + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( new DontCloseByteArrayInputStream( input), outputStream, 0 ); + } + catch ( IOException e ) + { + // ignore + } + finished.set( true ); + } + }; + worker.start(); + worker.join( 100 ); + worker.interrupt(); + assertThat( "We have an infinite loop", finished.get(), is( false ) ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullInputStreamNullOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( (InputStream) null, (OutputStream) null, 1 ); + } + + @Test( expected = NullPointerException.class, timeout = 150 ) + public void copyNullInputStreamValidOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( (InputStream) null, new ByteArrayOutputStream(), 1 ); + } + + @Test( timeout = 150 ) + public void copyEmptyInputStreamNullOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0] ), (OutputStream) null, 1 ); + } + + @Test( timeout = 150 ) + public void copyEmptyInputStreamValidOutputStreamPosBufSz() + throws Exception + { + IOUtil.copy( new DontCloseByteArrayInputStream( new byte[0]), new DontCloseByteArrayOutputStream(), 1 ); + } + + @Test( timeout = 150 ) + public void copyInputStreamValidOutputStreamPosBufSz() + throws Exception + { + ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream(); + byte[] input = { 1, 2, 3, 4, 5, 6 }; + IOUtil.copy( new DontCloseByteArrayInputStream( input ), outputStream, 1 ); + assertThat( outputStream.toByteArray(), is( input ) ); + } + + private static class DontCloseByteArrayInputStream + extends ByteArrayInputStream + { + public DontCloseByteArrayInputStream( byte[] input ) + { + super( input ); + } + + @Override + public void close() + throws IOException + { + throw new UnsupportedOperationException( "should not be called" ); + } + } + + private static class DontCloseByteArrayOutputStream + extends ByteArrayOutputStream + { + @Override + public void close() + throws IOException + { + throw new UnsupportedOperationException( "should not be called" ); + } + } } Modified: maven/sandbox/trunk/plexus-utils-commons-bridge/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/pom.xml?rev=1127325&r1=1127324&r2=1127325&view=diff ============================================================================== --- maven/sandbox/trunk/plexus-utils-commons-bridge/pom.xml (original) +++ maven/sandbox/trunk/plexus-utils-commons-bridge/pom.xml Tue May 24 22:43:04 2011 @@ -46,9 +46,9 @@ under the License. <dependencyManagement> <dependencies> <dependency> - <groupId>org.apache.commons</groupId> + <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> - <version>1.3.2</version> + <version>2.0.1</version> </dependency> <dependency> <groupId>commons-codec</groupId>