Author: krosenvold
Date: Fri Jun 26 15:27:24 2015
New Revision: 1687787

URL: http://svn.apache.org/r1687787
Log:
Further improved test coverage

Modified:
    
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java
    
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
    
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java

Modified: 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java?rev=1687787&r1=1687786&r2=1687787&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java
 (original)
+++ 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedOutputStreamTest.java
 Fri Jun 26 15:27:24 2015
@@ -18,6 +18,7 @@ package org.apache.commons.io.output;
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.IOException;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.junit.Test;
@@ -30,13 +31,7 @@ public class ChunkedOutputStreamTest {
     @Test
     public void write_four_chunks() throws Exception {
         final AtomicInteger numWrites = new AtomicInteger();
-        ByteArrayOutputStream baos = new ByteArrayOutputStream() {
-            @Override
-            public void write(byte[] b, int off, int len) {
-                numWrites.incrementAndGet();
-                super.write(b, off, len);
-            }
-        };
+        ByteArrayOutputStream baos = getByteArrayOutputStream(numWrites);
         ChunkedOutputStream chunked = new ChunkedOutputStream(baos, 10);
         chunked.write("0123456789012345678901234567891".getBytes());
         assertEquals(4, numWrites.get());
@@ -46,4 +41,25 @@ public class ChunkedOutputStreamTest {
     public void negative_chunksize_not_permitted() {
         new ChunkedOutputStream(new ByteArrayOutputStream(), 0);
     }
+
+    @Test
+    public void defaultConstructor() throws IOException {
+        final AtomicInteger numWrites = new AtomicInteger();
+        ByteArrayOutputStream baos = getByteArrayOutputStream(numWrites);
+        ChunkedOutputStream chunked = new ChunkedOutputStream(baos);
+        chunked.write(new byte[1024 * 4 + 1]);
+        assertEquals(2, numWrites.get());
+    }
+
+    private ByteArrayOutputStream getByteArrayOutputStream(final AtomicInteger 
numWrites) {
+        return new ByteArrayOutputStream() {
+            @Override
+            public void write(byte[] b, int off, int len) {
+                numWrites.incrementAndGet();
+                super.write(b, off, len);
+            }
+        };
+    }
+
+
 }

Modified: 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java?rev=1687787&r1=1687786&r2=1687787&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
 (original)
+++ 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ChunkedWriterTest.java
 Fri Jun 26 15:27:24 2015
@@ -28,19 +28,34 @@ public class ChunkedWriterTest {
     @Test
     public void write_four_chunks() throws Exception {
         final AtomicInteger numWrites = new AtomicInteger();
+        OutputStreamWriter osw = getOutputStreamWriter(numWrites);
+
+        ChunkedWriter chunked = new ChunkedWriter(osw, 10);
+        chunked.write("0123456789012345678901234567891".toCharArray());
+        chunked.flush();
+        assertEquals(4, numWrites.get());
+    }
+
+    @Test
+    public void write_two_chunks_default_constructor() throws Exception {
+        final AtomicInteger numWrites = new AtomicInteger();
+        OutputStreamWriter osw = getOutputStreamWriter(numWrites);
+
+        ChunkedWriter chunked = new ChunkedWriter(osw);
+        chunked.write(new char[1024 * 4 + 1]);
+        chunked.flush();
+        assertEquals(2, numWrites.get());
+    }
+
+    private OutputStreamWriter getOutputStreamWriter(final AtomicInteger 
numWrites) {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        OutputStreamWriter osw = new OutputStreamWriter(baos) {
+        return new OutputStreamWriter(baos) {
             @Override
             public void write(char[] cbuf, int off, int len) throws 
IOException {
                 numWrites.incrementAndGet();
                 super.write(cbuf, off, len);
             }
         };
-
-        ChunkedWriter chunked = new ChunkedWriter(osw, 10);
-        chunked.write("0123456789012345678901234567891".toCharArray());
-        chunked.flush();
-        assertEquals(4, numWrites.get());
     }
 
     @Test(expected = IllegalArgumentException.class)

Modified: 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java?rev=1687787&r1=1687786&r2=1687787&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
 (original)
+++ 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java
 Fri Jun 26 15:27:24 2015
@@ -23,9 +23,9 @@ import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
 
 import junit.framework.AssertionFailedError;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -41,6 +41,7 @@ public class FileWriterWithEncodingTest
     private File file1;
     private File file2;
     private String textContent;
+    private char[] anotherTestContent = new char[]{'f', 'z', 'x'};
 
     public FileWriterWithEncodingTest(final String name) {
         super(name);
@@ -75,20 +76,39 @@ public class FileWriterWithEncodingTest
     }
 
     //-----------------------------------------------------------------------
-    public void testSameEncoding() throws Exception {
+    public void testSameEncoding_string_constructor() throws Exception {
+        succesfulRun(new FileWriterWithEncoding(file2, defaultEncoding));
+    }
+
+    public void testSameEncoding_string_string_constructor() throws Exception {
+        succesfulRun(new FileWriterWithEncoding(file2.getPath(), 
defaultEncoding));
+    }
+
+    public void testSameEncoding_Charset_constructor() throws Exception {
+        succesfulRun(new FileWriterWithEncoding(file2, 
Charset.defaultCharset()));
+    }
+
+    public void testSameEncoding_string_Charset_constructor() throws Exception 
{
+        succesfulRun(new FileWriterWithEncoding(file2.getPath(), 
Charset.defaultCharset()));
+    }
+
+    public void testSameEncoding_CharsetEncoder_constructor() throws Exception 
{
+        CharsetEncoder enc = Charset.defaultCharset().newEncoder();
+        succesfulRun(new FileWriterWithEncoding(file2, enc));
+    }
+
+    public void testSameEncoding_string_CharsetEncoder_constructor() throws 
Exception {
+        CharsetEncoder enc = Charset.defaultCharset().newEncoder();
+        succesfulRun(new FileWriterWithEncoding(file2.getPath(), enc));
+    }
+
+    private void succesfulRun(FileWriterWithEncoding fw21) throws Exception {
         FileWriter fw1 = null;
         FileWriterWithEncoding fw2 = null;
         try {
             fw1 = new FileWriter(file1);  // default encoding
-            fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
-            assertTrue(file1.exists());
-            assertTrue(file2.exists());
-
-            fw1.write(textContent);
-            fw2.write(textContent);
-
-            fw1.flush();
-            fw2.flush();
+            fw2 = fw21;
+            writeTestPayload(fw1, fw2);
             checkFile(file1, file2);
 
         } finally {
@@ -106,14 +126,7 @@ public class FileWriterWithEncodingTest
             try {
                 fw1 = new FileWriter(file1);  // default encoding
                 fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
-                assertTrue(file1.exists());
-                assertTrue(file2.exists());
-
-                fw1.write(textContent);
-                fw2.write(textContent);
-
-                fw1.flush();
-                fw2.flush();
+                writeTestPayload(fw1, fw2);
                 try {
                     checkFile(file1, file2);
                     fail();
@@ -134,14 +147,7 @@ public class FileWriterWithEncodingTest
             try {
                 fw1 = new FileWriter(file1);  // default encoding
                 fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
-                assertTrue(file1.exists());
-                assertTrue(file2.exists());
-
-                fw1.write(textContent);
-                fw2.write(textContent);
-
-                fw1.flush();
-                fw2.flush();
+                writeTestPayload(fw1, fw2);
                 try {
                     checkFile(file1, file2);
                     fail();
@@ -158,6 +164,25 @@ public class FileWriterWithEncodingTest
         }
     }
 
+    private void writeTestPayload(FileWriter fw1, FileWriterWithEncoding fw2) 
throws IOException {
+        assertTrue(file1.exists());
+        assertTrue(file2.exists());
+
+        fw1.write(textContent);
+        fw2.write(textContent);
+        fw1.write(42);
+        fw2.write(42);
+        fw1.write(anotherTestContent);
+        fw2.write(anotherTestContent);
+        fw1.write(anotherTestContent, 1, 2);
+        fw2.write(anotherTestContent, 1, 2);
+        fw1.write("CAFE", 1, 2);
+        fw2.write("CAFE", 1, 2);
+
+        fw1.flush();
+        fw2.flush();
+    }
+
     //-----------------------------------------------------------------------
     public void testConstructor_File_encoding_badEncoding() {
         Writer writer = null;
@@ -218,4 +243,12 @@ public class FileWriterWithEncodingTest
         assertFalse(file1.exists());
     }
 
+    public void testSameEncoding_null_Charset_constructor() throws Exception {
+        try {
+            succesfulRun(new FileWriterWithEncoding(file2, (Charset) null));
+            fail();
+        } catch (NullPointerException ignore) {
+
+        }
+    }
 }


Reply via email to