poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1806162 - in /poi/trunk/src: java/org/apache/poi/util/IOUtils.java testcases/org/apache/poi/util/TestIOUtils.java
Date Fri, 25 Aug 2017 11:51:19 GMT
Author: centic
Date: Fri Aug 25 11:51:18 2017
New Revision: 1806162

URL: http://svn.apache.org/viewvc?rev=1806162&view=rev
Log:
Verify that bug 61294 is fixed now, add some more coverage for IOUtils in general

Modified:
    poi/trunk/src/java/org/apache/poi/util/IOUtils.java
    poi/trunk/src/testcases/org/apache/poi/util/TestIOUtils.java

Modified: poi/trunk/src/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/IOUtils.java?rev=1806162&r1=1806161&r2=1806162&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/IOUtils.java Fri Aug 25 11:51:18 2017
@@ -206,7 +206,7 @@ public final class IOUtils {
      *
      * @param doc  a writeable document to write to the output stream
      * @param out  the output stream that the document is written to
-     * @throws IOException 
+     * @throws IOException thrown on errors writing to the stream
      */
     public static void write(POIDocument doc, OutputStream out) throws IOException {
         try {
@@ -225,7 +225,7 @@ public final class IOUtils {
      *
      * @param doc  a writeable document to write to the output stream
      * @param out  the output stream that the document is written to
-     * @throws IOException 
+     * @throws IOException thrown on errors writing to the stream
      */
     public static void write(Workbook doc, OutputStream out) throws IOException {
         try {
@@ -245,7 +245,7 @@ public final class IOUtils {
      *
      * @param doc  a writeable and closeable document to write to the output stream, then
close
      * @param out  the output stream that the document is written to
-     * @throws IOException 
+     * @throws IOException thrown on errors writing to the stream
      */
     public static void writeAndClose(POIDocument doc, OutputStream out) throws IOException
{
         try {
@@ -264,7 +264,7 @@ public final class IOUtils {
      *
      * @param doc  a writeable and closeable document to write to the output file, then close
      * @param out  the output file that the document is written to
-     * @throws IOException 
+     * @throws IOException thrown on errors writing to the stream
      */
     public static void writeAndClose(POIDocument doc, File out) throws IOException {
         try {
@@ -282,7 +282,7 @@ public final class IOUtils {
      * This function exists for Java 6 code.
      *
      * @param doc  a writeable document to write in-place
-     * @throws IOException 
+     * @throws IOException thrown on errors writing to the file
      */
     public static void writeAndClose(POIDocument doc) throws IOException {
         try {

Modified: poi/trunk/src/testcases/org/apache/poi/util/TestIOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/util/TestIOUtils.java?rev=1806162&r1=1806161&r2=1806162&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/util/TestIOUtils.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/util/TestIOUtils.java Fri Aug 25 11:51:18 2017
@@ -17,6 +17,7 @@
 
 package org.apache.poi.util;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
 import java.io.ByteArrayInputStream;
@@ -27,8 +28,11 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PushbackInputStream;
+import java.nio.ByteBuffer;
 import java.util.Random;
 
+import org.apache.poi.EmptyFileException;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -55,10 +59,58 @@ public final class TestIOUtils {
 
     @AfterClass
     public static void tearDown() throws IOException {
+        //noinspection ResultOfMethodCallIgnored
         TMP.delete();
     }
 
     @Test
+    public void testPeekFirst8Bytes() throws Exception {
+        assertArrayEquals("01234567".getBytes("UTF-8"),
+                IOUtils.peekFirst8Bytes(new ByteArrayInputStream("0123456789".getBytes("UTF-8"))));
+    }
+
+    @Test
+    public void testPeekFirst8BytesWithPushbackInputStream() throws Exception {
+        assertArrayEquals("01234567".getBytes("UTF-8"),
+                IOUtils.peekFirst8Bytes(new PushbackInputStream(new ByteArrayInputStream("0123456789".getBytes("UTF-8")),
8)));
+    }
+
+    @Test
+    public void testPeekFirst8BytesTooLessAvailable() throws Exception {
+        assertArrayEquals(new byte[] { 1, 2, 3, 0, 0, 0, 0, 0},
+                IOUtils.peekFirst8Bytes(new ByteArrayInputStream(new byte[] { 1, 2, 3})));
+    }
+
+    @Test(expected = EmptyFileException.class)
+    public void testPeekFirst8BytesEmpty() throws Exception {
+        IOUtils.peekFirst8Bytes(new ByteArrayInputStream(new byte[] {}));
+    }
+
+    @Test
+    public void testToByteArray() throws Exception {
+        assertArrayEquals(new byte[] { 1, 2, 3},
+                IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3})));
+    }
+
+    @Test(expected = IOException.class)
+    public void testToByteArrayToSmall() throws Exception {
+        assertArrayEquals(new byte[] { 1, 2, 3},
+                IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3}), 10));
+    }
+
+    @Test
+    public void testToByteArrayByteBuffer() throws Exception {
+        assertArrayEquals(new byte[] { 1, 2, 3},
+                IOUtils.toByteArray(ByteBuffer.wrap(new byte[]{1, 2, 3}), 10));
+    }
+
+    @Test
+    public void testToByteArrayByteBufferToSmall() throws Exception {
+        assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7},
+                IOUtils.toByteArray(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7}), 3));
+    }
+
+    @Test
     public void testSkipFully() throws IOException {
         InputStream is =  new FileInputStream(TMP);
         long skipped = IOUtils.skipFully(is, 20000L);
@@ -91,6 +143,11 @@ public final class TestIOUtils {
     }
 
     @Test
+    public void testSkipFullyBug61294() throws IOException {
+        IOUtils.skipFully(new ByteArrayInputStream(new byte[0]), 1);
+    }
+
+    @Test
     public void testZeroByte() throws IOException {
         long skipped = IOUtils.skipFully((new ByteArrayInputStream(new byte[0])), 100);
         assertEquals("zero byte", -1L, skipped);
@@ -105,7 +162,7 @@ public final class TestIOUtils {
     @Test(expected = IllegalArgumentException.class)
     public void testSkipNegative() throws IOException {
         InputStream is =  new FileInputStream(TMP);
-        long skipped = IOUtils.skipFully(is, -1);
+        IOUtils.skipFully(is, -1);
     }
 
     @Test



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message