harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jessewil...@apache.org
Subject svn commit: r835209 - in /harmony/enhanced/classlib/trunk: modules/luni/make/ modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ support/src/test/java/org/apache/harmony/testframework/ support/src/test/java/tests/support/
Date Thu, 12 Nov 2009 03:17:56 GMT
Author: jessewilson
Date: Thu Nov 12 03:17:54 2009
New Revision: 835209

URL: http://svn.apache.org/viewvc?rev=835209&view=rev
Log:
97 new tests for Writers and OutputStreams. This is a followup to CL 834317 which added the
first batch of tests to OutputStreams. 

There are some new failures here. When the user writes to a closed writer or stream, our current
code often fails silently rather than throwing an exception like it should. The RI also has
this problem (bug #4799358; unfixed). I intend to commit a fix for these bugs tomorrow.

The 21 new failures are currently suppressed by exclude.common.

Added:
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/WriterTesterTest.java
  (with props)
    harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharSinkTester.java
      - copied, changed from r834668, harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java
    harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharWrapperTester.java
      - copied, changed from r835012, harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java
Modified:
    harmony/enhanced/classlib/trunk/modules/luni/make/exclude.common
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamTesterTest.java
    harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java
    harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java
    harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Streams.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/make/exclude.common
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/make/exclude.common?rev=835209&r1=835208&r2=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/make/exclude.common (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/make/exclude.common Thu Nov 12 03:17:54 2009
@@ -1 +1,3 @@
 org/apache/harmony/luni/tests/java/net/ExcludedProxyTest.java
+org/apache/harmony/luni/tests/java/io/OutputStreamTesterTest.java
+org/apache/harmony/luni/tests/java/io/WriterTesterTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamTesterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamTesterTest.java?rev=835209&r1=835208&r2=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamTesterTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/OutputStreamTesterTest.java
Thu Nov 12 03:17:54 2009
@@ -65,12 +65,12 @@
         suite.addTest(new FilterOutputStreamTester().createTests());
         suite.addTest(new DataOutputStreamTester().createTests());
         suite.addTest(new ObjectOutputStreamTester().createTests());
-        suite.addTest(new PrintStreamTester().setExceptionSuppressed(true).createTests());
+        suite.addTest(new PrintStreamTester().setThrowsExceptions(false).createTests());
 
         return suite;
     }
 
-    public static class FileOutputStreamSinkTester extends SinkTester {
+    private static class FileOutputStreamSinkTester extends SinkTester {
 
         private final boolean append;
         private File file;
@@ -86,7 +86,7 @@
         }
 
         public byte[] getBytes() throws IOException {
-            return Streams.bytesFromStream(new FileInputStream(file));
+            return Streams.streamToBytes(new FileInputStream(file));
         }
     }
 
@@ -185,7 +185,7 @@
         }
 
         public byte[] decode(byte[] delegateBytes) throws Exception {
-            return Streams.bytesFromStream(new ObjectInputStream(
+            return Streams.streamToBytes(new ObjectInputStream(
                     new ByteArrayInputStream(delegateBytes)));
         }
     }

Added: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/WriterTesterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/WriterTesterTest.java?rev=835209&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/WriterTesterTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/WriterTesterTest.java
Thu Nov 12 03:17:54 2009
@@ -0,0 +1,194 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.harmony.luni.tests.java.io;
+
+import junit.framework.TestSuite;
+import org.apache.harmony.testframework.CharSinkTester;
+import org.apache.harmony.testframework.CharWrapperTester;
+import tests.support.Streams;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.CharArrayWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.FilterWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PipedReader;
+import java.io.PipedWriter;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+/**
+ * Tests basic {@link Writer} behaviors for the luni implementations of the type.
+ */
+public class WriterTesterTest {
+
+    public static junit.framework.Test suite() {
+        TestSuite suite = new TestSuite();
+
+        // sink tests
+        suite.addTest(new FileWriterCharSinkTester(true).createTests());
+        suite.addTest(new FileWriterCharSinkTester(false).createTests());
+        suite.addTest(new CharArrayWriterCharSinkTester().createTests());
+        suite.addTest(new StringWriterCharSinkTester().createTests());
+        suite.addTest(new PipedWriterCharSinkTester().createTests());
+
+        // wrapper tests
+        suite.addTest(new BufferedWriterCharSinkTester(1).createTests());
+        suite.addTest(new BufferedWriterCharSinkTester(5).createTests());
+        suite.addTest(new BufferedWriterCharSinkTester(1024).createTests());
+        suite.addTest(new FilterWriterCharSinkTester().createTests());
+        suite.addTest(new PrintWriterCharSinkTester().setThrowsExceptions(false).createTests());
+        suite.addTest(new OutputStreamWriterCharSinkTester().createTests());
+
+        return suite;
+    }
+
+    private static class FileWriterCharSinkTester extends CharSinkTester {
+        private final boolean append;
+        private File file;
+
+        public FileWriterCharSinkTester(boolean append) {
+            this.append = append;
+        }
+
+        @Override public Writer create() throws Exception {
+            file = File.createTempFile("FileOutputStreamSinkTester", "tmp");
+            file.deleteOnExit();
+            return new FileWriter(file, append);
+        }
+
+        @Override public char[] getChars() throws Exception {
+            return Streams.streamToString(new FileReader(file)).toCharArray();
+        }
+    }
+
+    private static class CharArrayWriterCharSinkTester extends CharSinkTester {
+        private CharArrayWriter writer;
+
+        @Override public Writer create() throws Exception {
+            writer = new CharArrayWriter();
+            return writer;
+        }
+
+        @Override public char[] getChars() throws Exception {
+            return writer.toCharArray();
+        }
+    }
+
+    private static class PipedWriterCharSinkTester extends CharSinkTester {
+
+        private ExecutorService executor;
+        private Future<char[]> future;
+
+        public Writer create() throws IOException {
+            final PipedReader in = new PipedReader();
+            PipedWriter out = new PipedWriter(in);
+
+            executor = Executors.newSingleThreadExecutor();
+            future = executor.submit(new Callable<char[]>() {
+                final CharArrayWriter chars = new CharArrayWriter();
+                public char[] call() throws Exception {
+                    char[] buffer = new char[256];
+                    int count;
+                    while ((count = in.read(buffer)) != -1) {
+                        chars.write(buffer, 0, count);
+                    }
+                    return chars.toCharArray();
+                }
+            });
+
+            return out;
+        }
+
+        @Override public char[] getChars() throws Exception {
+            executor.shutdown();
+            return future.get();
+        }
+    }
+
+    private static class StringWriterCharSinkTester extends CharSinkTester {
+        private StringWriter writer;
+
+        @Override public Writer create() throws Exception {
+            writer = new StringWriter();
+            return writer;
+        }
+
+        @Override public char[] getChars() throws Exception {
+            return writer.toString().toCharArray();
+        }
+    }
+
+    private static class BufferedWriterCharSinkTester extends CharWrapperTester {
+        private final int bufferSize;
+
+        private BufferedWriterCharSinkTester(int bufferSize) {
+            this.bufferSize = bufferSize;
+        }
+
+        @Override public Writer create(Writer delegate) throws Exception {
+            return new BufferedWriter(delegate, bufferSize);
+        }
+
+        @Override public char[] decode(char[] delegateChars) throws Exception {
+            return delegateChars;
+        }
+    }
+
+    private static class FilterWriterCharSinkTester extends CharWrapperTester {
+        @Override public Writer create(Writer delegate) throws Exception {
+            return new FilterWriter(delegate) {};
+        }
+
+        @Override public char[] decode(char[] delegateChars) throws Exception {
+            return delegateChars;
+        }
+    }
+
+    private static class PrintWriterCharSinkTester extends CharWrapperTester {
+        @Override public Writer create(Writer delegate) throws Exception {
+            return new PrintWriter(delegate) {};
+        }
+
+        @Override public char[] decode(char[] delegateChars) throws Exception {
+            return delegateChars;
+        }
+    }
+
+    private static class OutputStreamWriterCharSinkTester extends CharSinkTester {
+        private ByteArrayOutputStream out;
+
+        @Override public Writer create() throws Exception {
+            out = new ByteArrayOutputStream();
+            return new OutputStreamWriter(out, "UTF-8");
+        }
+
+        @Override public char[] getChars() throws Exception {
+            return new String(out.toByteArray(), "UTF-8").toCharArray();
+        }
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/WriterTesterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharSinkTester.java
(from r834668, harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java)
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharSinkTester.java?p2=harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharSinkTester.java&p1=harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java&r1=834668&r2=835209&rev=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java
(original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharSinkTester.java
Thu Nov 12 03:17:54 2009
@@ -21,37 +21,54 @@
 import junit.framework.TestSuite;
 import org.junit.Assert;
 
-import java.io.OutputStream;
+import java.io.IOException;
+import java.io.Writer;
 import java.util.Random;
 
 /**
- * Tests behaviour common to all implementations of {@link OutputStream}. This
- * adapts streams that collects untransformed bytes so that they may be tested.
+ * Tests behaviour common to all implementations of {@link Writer}. This adapts
+ * writers that collects untransformed chars so that they may be tested.
  */
-public abstract class SinkTester {
+public abstract class CharSinkTester {
+
+    private boolean throwsExceptions = true;
 
     /**
-     * Creates a new output stream ready to receive an arbitrary number of
-     * bytes. Each time this method is invoked, any previously returned output
-     * streams may be discarded.
+     * Creates a new writer ready to receive an arbitrary number of chars. Each
+     * time this method is invoked, any previously returned writers may be
+     * discarded.
      */
-    public abstract OutputStream create() throws Exception;
+    public abstract Writer create() throws Exception;
 
     /**
-     * Returns the current set of bytes written to the output stream last
-     * returned by {@link #create}, and releases any resources held by that
-     * stream.
+     * Returns the current set of chars written to the writer last returned by
+     * {@link #create}, and releases any resources held by that writer.
      */
-    public abstract byte[] getBytes() throws Exception;
+    public abstract char[] getChars() throws Exception;
+
+    /**
+     * Configures whether the writer is expected to throw exceptions when an
+     * error is encountered. Classes like {@code PrintWriter} report errors via
+     * an API method instead.
+     */
+    public CharSinkTester setThrowsExceptions(boolean throwsExceptions) {
+        this.throwsExceptions = throwsExceptions;
+        return this;
+    }
 
     public final TestSuite createTests() {
         TestSuite result = new TestSuite();
         result.addTest(new SinkTestCase("sinkTestNoWriting"));
-        result.addTest(new SinkTestCase("sinkTestWriteZeroBytes"));
-        result.addTest(new SinkTestCase("sinkTestWriteByteByByte"));
+        result.addTest(new SinkTestCase("sinkTestWriteZeroChars"));
+        result.addTest(new SinkTestCase("sinkTestWriteCharByChar"));
         result.addTest(new SinkTestCase("sinkTestWriteArray"));
         result.addTest(new SinkTestCase("sinkTestWriteOffset"));
         result.addTest(new SinkTestCase("sinkTestWriteLargeArray"));
+
+        if (throwsExceptions) {
+            result.addTest(new SinkTestCase("sinkTestWriteAfterClose"));
+        }
+
         return result;
     }
 
@@ -66,106 +83,111 @@
         }
 
         public void sinkTestNoWriting() throws Exception {
-            byte[] expected = new byte[] {};
+            char[] expected = new char[] {};
 
-            OutputStream out = create();
+            Writer out = create();
             out.close();
-            Assert.assertArrayEquals(expected, getBytes());
+            Assert.assertArrayEquals(expected, getChars());
         }
 
-        public void sinkTestWriteZeroBytes() throws Exception {
-            byte[] expected = new byte[] {};
+        public void sinkTestWriteZeroChars() throws Exception {
+            char[] expected = new char[] {};
 
-            OutputStream out = create();
-            byte[] a = new byte[1024];
+            Writer out = create();
+            char[] a = new char[1024];
             out.write(a, 1000, 0);
             out.write(a, 0, 0);
-            out.write(new byte[] {});
+            out.write(new char[] {});
 
             out.close();
-            Assert.assertArrayEquals(expected, getBytes());
+            Assert.assertArrayEquals(expected, getChars());
         }
 
-        public void sinkTestWriteByteByByte() throws Exception {
-            byte[] expected = new byte[] { 5, 6, 7, 3, 4, 5, 3, 2, 1 };
+        public void sinkTestWriteCharByChar() throws Exception {
+            char[] expected = "EFGCDECBA".toCharArray();
 
-            OutputStream out = create();
-            for (byte b : expected) {
-                out.write(b);
+            Writer out = create();
+            for (char c : expected) {
+                out.write(c);
             }
 
             out.close();
-            Assert.assertArrayEquals(expected, getBytes());
+            Assert.assertArrayEquals(expected, getChars());
         }
 
         public void sinkTestWriteArray() throws Exception {
-            byte[] expected = new byte[] {
-                    5, 6,
-                    7, 3, 4, 5,
-                    3, 2, 1
-            };
-
-            OutputStream out = create();
-
-            byte[] a = new byte[] { 5, 6 };
-            out.write(a);
+            char[] expected = "EFGCDECBA".toCharArray();
 
-            byte[] b = new byte[] { 7, 3, 4, 5 };
-            out.write(b);
+            Writer out = create();
 
-            byte[] c = new byte[] { 3, 2, 1 };
-            out.write(c);
+            out.write("EF".toCharArray());
+            out.write("GCDE".toCharArray());
+            out.write("CBA".toCharArray());
 
             out.close();
-            Assert.assertArrayEquals(expected, getBytes());
+            Assert.assertArrayEquals(expected, getChars());
         }
 
         public void sinkTestWriteOffset() throws Exception {
-            byte[] expected = new byte[] {
-                    5, 6,
-                    7, 3, 4, 5,
-                    3, 2, 1
-            };
-
-            OutputStream out = create();
-
-            byte[] a = new byte[1024];
-            a[1000] = 5;
-            a[1001] = 6;
+            char[] expected = "EFGCDECBA".toCharArray();
+            Writer out = create();
+
+            char[] a = new char[1024];
+            a[1000] = 'E';
+            a[1001] = 'F';
             out.write(a, 1000, 2);
 
-            byte[] b = new byte[1024];
-            b[1020] = 7;
-            b[1021] = 3;
-            b[1022] = 4;
-            b[1023] = 5;
+            char[] b = new char[1024];
+            b[1020] = 'G';
+            b[1021] = 'C';
+            b[1022] = 'D';
+            b[1023] = 'E';
             out.write(b, 1020, 4);
 
-            byte[] c = new byte[1024];
-            c[0] = 3;
-            c[1] = 2;
-            c[2] = 1;
+            char[] c = new char[1024];
+            c[0] = 'C';
+            c[1] = 'B';
+            c[2] = 'A';
             out.write(c, 0, 3);
 
             out.close();
-            Assert.assertArrayEquals(expected, getBytes());
+            Assert.assertArrayEquals(expected, getChars());
         }
 
         public void sinkTestWriteLargeArray() throws Exception {
-            byte[] expected = new byte[(1024 * 1024) + 1]; // 1 MB + 1 byte
-            new Random().nextBytes(expected);
+            Random dice = new Random();
+            char[] expected = new char[(1024 * 1024) + 1]; // 2 MB + 1 char
+            for (int c = 0; c < expected.length; c++) {
+                expected[c] = (char) ('A' + dice.nextInt(26));
+            }
 
-            OutputStream out = create();
+            Writer out = create();
             out.write(expected);
             out.close();
 
-            Assert.assertArrayEquals(expected, getBytes());
+            Assert.assertArrayEquals(expected, getChars());
+        }
+
+        public void sinkTestWriteAfterClose() throws Exception {
+            char[] expectedChars = "EF".toCharArray();
+            Writer out = create();
+
+            out.write(expectedChars);
+            out.close();
+
+            try {
+                out.write("GCDE".toCharArray());
+                fail("expected already closed exception");
+            } catch (IOException expected) {
+            }
+
+            Assert.assertArrayEquals(expectedChars, getChars());
         }
 
         // adding a new test? Don't forget to update createTests().
 
         @Override public String getName() {
-            return SinkTester.this.toString() + ":" + super.getName();
+            return CharSinkTester.this.toString() + ":" + super.getName();
         }
     }
-}
+}
\ No newline at end of file

Copied: harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharWrapperTester.java
(from r835012, harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java)
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharWrapperTester.java?p2=harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharWrapperTester.java&p1=harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java&r1=835012&r2=835209&rev=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java
(original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/CharWrapperTester.java
Thu Nov 12 03:17:54 2009
@@ -20,50 +20,52 @@
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.Writer;
 
 /**
  * Tests behaviour common to wrapping and filtering implementations of {@link
- * OutputStream}.
+ * Writer}.
  */
-public abstract class WrapperTester {
+public abstract class CharWrapperTester {
 
-    private boolean exceptionsSuppressed;
+    private boolean throwsExceptions = true;
 
     /**
-     * Creates a new output stream that receives one stream of bytes, optionally
-     * transforms it, and emits another stream of bytes to {@code delegate}.
+     * Creates a new output stream that receives one stream of chars, optionally
+     * transforms it, and emits another stream of chars to {@code delegate}.
      */
-    public abstract OutputStream create(OutputStream delegate) throws Exception;
+    public abstract Writer create(Writer delegate) throws Exception;
 
     /**
-     * Decodes the bytes received by the delegate into their original form: the
-     * bytes originally received by this wrapper.
+     * Decodes the chars received by the delegate into their original form: the
+     * chars originally received by this wrapper.
      */
-    public abstract byte[] decode(byte[] delegateBytes) throws Exception;
+    public abstract char[] decode(char[] delegateChars) throws Exception;
 
     /**
-     * Configures whether the wrapper is expected to suppress exceptions thrown
-     * by the underlying stream. This is the case for wrappers like {@code
-     * PrintWriter}, that reports errors using an API method.
+     * Configures whether the writer is expected to throw exceptions when an
+     * error is encountered. Classes like {@code PrintWriter} report errors via
+     * an API method instead.
      */
-    public WrapperTester setExceptionSuppressed(boolean suppressExceptions) {
-        this.exceptionsSuppressed = suppressExceptions;
+    public CharWrapperTester setThrowsExceptions(boolean throwsExceptions) {
+        this.throwsExceptions = throwsExceptions;
         return this;
     }
 
     public final TestSuite createTests() {
         TestSuite result = new TestSuite();
-        result.addTest(new WrapperSinkTester().createTests());
+        result.addTest(new WrapperSinkTester()
+                .setThrowsExceptions(throwsExceptions)
+                .createTests());
 
-        if (exceptionsSuppressed) {
-            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaFlushSuppressed"));
-            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaCloseSuppressed"));
-        } else {
+        if (throwsExceptions) {
             result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaFlush"));
             result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaClose"));
+        } else {
+            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaFlushSuppressed"));
+            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaCloseSuppressed"));
         }
 
         return result;
@@ -73,20 +75,20 @@
         return getClass().getName();
     }
 
-    private class WrapperSinkTester extends SinkTester {
-        private ByteArrayOutputStream delegate;
+    private class WrapperSinkTester extends CharSinkTester {
+        private StringWriter delegate;
 
-        @Override public OutputStream create() throws Exception {
-            delegate = new ByteArrayOutputStream();
-            return WrapperTester.this.create(delegate);
+        @Override public Writer create() throws Exception {
+            delegate = new StringWriter();
+            return CharWrapperTester.this.create(delegate);
         }
 
-        @Override public byte[] getBytes() throws Exception {
-            return WrapperTester.this.decode(delegate.toByteArray());
+        @Override public char[] getChars() throws Exception {
+            return CharWrapperTester.this.decode(delegate.toString().toCharArray());
         }
 
         @Override public String toString() {
-            return WrapperTester.this.toString();
+            return CharWrapperTester.this.toString();
         }
     }
 
@@ -97,31 +99,31 @@
         }
 
         public void wrapperTestFlushThrowsViaFlushSuppressed() throws Exception {
-            FailOnFlushOutputStream delegate = new FailOnFlushOutputStream();
-            OutputStream o = create(delegate);
-            o.write(new byte[] { 8, 6, 7, 5 });
-            o.write(new byte[] { 3, 0, 9 });
+            FailOnFlushWriter delegate = new FailOnFlushWriter();
+            Writer o = create(delegate);
+            o.write("BUT");
+            o.write("TERS");
             o.flush();
             assertTrue(delegate.flushed);
         }
 
         public void wrapperTestFlushThrowsViaCloseSuppressed() throws Exception {
-            FailOnFlushOutputStream delegate = new FailOnFlushOutputStream();
-            OutputStream o = create(delegate);
-            o.write(new byte[] { 8, 6, 7, 5 });
-            o.write(new byte[] { 3, 0, 9 });
+            FailOnFlushWriter delegate = new FailOnFlushWriter();
+            Writer o = create(delegate);
+            o.write("BUT");
+            o.write("TERS");
             o.close();
             assertTrue(delegate.flushed);
         }
 
         public void wrapperTestFlushThrowsViaFlush() throws Exception {
-            FailOnFlushOutputStream delegate = new FailOnFlushOutputStream();
+            FailOnFlushWriter delegate = new FailOnFlushWriter();
 
-            OutputStream o = create(delegate);
+            Writer o = create(delegate);
             try {
                 // any of these is permitted to flush
-                o.write(new byte[] { 8, 6, 7, 5 });
-                o.write(new byte[] { 3, 0, 9 });
+                o.write("BUT");
+                o.write("TERS");
                 o.flush();
                 assertTrue(delegate.flushed);
                 fail("flush exception ignored");
@@ -131,33 +133,40 @@
         }
 
         public void wrapperTestFlushThrowsViaClose() throws Exception {
-            FailOnFlushOutputStream delegate = new FailOnFlushOutputStream();
+            FailOnFlushWriter delegate = new FailOnFlushWriter();
 
-            OutputStream o = create(delegate);
+            Writer o = create(delegate);
             try {
                 // any of these is permitted to flush
-                o.write(new byte[] { 8, 6, 7, 5 });
-                o.write(new byte[] { 3, 0, 9 });
+                o.write("BUT");
+                o.write("TERS");
                 o.close();
                 assertTrue(delegate.flushed);
                 fail("flush exception ignored");
             } catch (IOException expected) {
                 assertEquals("Flush failed" , expected.getMessage());
             }
+
+            try {
+                o.write("BARK");
+                fail("expected already closed exception");
+            } catch (IOException expected) {
+            }
         }
 
         // adding a new test? Don't forget to update createTests().
 
         @Override public String getName() {
-            return WrapperTester.this.toString() + ":" + super.getName();
+            return CharWrapperTester.this.toString() + ":" + super.getName();
         }
 
-        private class FailOnFlushOutputStream extends ByteArrayOutputStream {
+        private class FailOnFlushWriter extends Writer {
             boolean flushed = false;
 
+            @Override public void write(char[] buf, int offset, int count) throws IOException
{}
+
             @Override public void close() throws IOException {
                 flush();
-                super.close();
             }
 
             @Override public void flush() throws IOException {
@@ -165,8 +174,7 @@
                     flushed = true;
                     throw new IOException("Flush failed");
                 }
-                super.flush();
             }
         }
     }
-}
+}
\ No newline at end of file

Modified: harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java?rev=835209&r1=835208&r2=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java
(original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/SinkTester.java
Thu Nov 12 03:17:54 2009
@@ -21,6 +21,7 @@
 import junit.framework.TestSuite;
 import org.junit.Assert;
 
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Random;
 
@@ -30,6 +31,8 @@
  */
 public abstract class SinkTester {
 
+    private boolean throwsExceptions = true;
+
     /**
      * Creates a new output stream ready to receive an arbitrary number of
      * bytes. Each time this method is invoked, any previously returned output
@@ -44,6 +47,16 @@
      */
     public abstract byte[] getBytes() throws Exception;
 
+    /**
+     * Configures whether the stream is expected to throw exceptions when an
+     * error is encountered. Classes like {@code PrintStream} report errors via
+     * an API method instead.
+     */
+    public SinkTester setThrowsExceptions(boolean throwsExceptions) {
+        this.throwsExceptions = throwsExceptions;
+        return this;
+    }
+
     public final TestSuite createTests() {
         TestSuite result = new TestSuite();
         result.addTest(new SinkTestCase("sinkTestNoWriting"));
@@ -52,6 +65,11 @@
         result.addTest(new SinkTestCase("sinkTestWriteArray"));
         result.addTest(new SinkTestCase("sinkTestWriteOffset"));
         result.addTest(new SinkTestCase("sinkTestWriteLargeArray"));
+
+        if (throwsExceptions) {
+            result.addTest(new SinkTestCase("sinkTestWriteAfterClose"));
+        }
+
         return result;
     }
 
@@ -162,6 +180,22 @@
             Assert.assertArrayEquals(expected, getBytes());
         }
 
+        public void sinkTestWriteAfterClose() throws Exception {
+            byte[] expectedBytes = { 5, 6 };
+            OutputStream out = create();
+
+            out.write(expectedBytes);
+            out.close();
+
+            try {
+                out.write(new byte[] { 7, 3, 4, 5 });
+                fail("expected already closed exception");
+            } catch (IOException expected) {
+            }
+
+            Assert.assertArrayEquals(expectedBytes, getBytes());
+        }
+
         // adding a new test? Don't forget to update createTests().
 
         @Override public String getName() {

Modified: harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java?rev=835209&r1=835208&r2=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java
(original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/org/apache/harmony/testframework/WrapperTester.java
Thu Nov 12 03:17:54 2009
@@ -30,7 +30,7 @@
  */
 public abstract class WrapperTester {
 
-    private boolean exceptionsSuppressed;
+    private boolean throwsExceptions = true;
 
     /**
      * Creates a new output stream that receives one stream of bytes, optionally
@@ -45,25 +45,27 @@
     public abstract byte[] decode(byte[] delegateBytes) throws Exception;
 
     /**
-     * Configures whether the wrapper is expected to suppress exceptions thrown
-     * by the underlying stream. This is the case for wrappers like {@code
-     * PrintWriter}, that reports errors using an API method.
+     * Configures whether the stream is expected to throw exceptions when an
+     * error is encountered. Classes like {@code PrintStream} report errors via
+     * an API method instead.
      */
-    public WrapperTester setExceptionSuppressed(boolean suppressExceptions) {
-        this.exceptionsSuppressed = suppressExceptions;
+    public WrapperTester setThrowsExceptions(boolean throwsExceptions) {
+        this.throwsExceptions = throwsExceptions;
         return this;
     }
 
     public final TestSuite createTests() {
         TestSuite result = new TestSuite();
-        result.addTest(new WrapperSinkTester().createTests());
+        result.addTest(new WrapperSinkTester()
+                .setThrowsExceptions(throwsExceptions)
+                .createTests());
 
-        if (exceptionsSuppressed) {
-            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaFlushSuppressed"));
-            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaCloseSuppressed"));
-        } else {
+        if (throwsExceptions) {
             result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaFlush"));
             result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaClose"));
+        } else {
+            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaFlushSuppressed"));
+            result.addTest(new WrapperTestCase("wrapperTestFlushThrowsViaCloseSuppressed"));
         }
 
         return result;
@@ -144,6 +146,12 @@
             } catch (IOException expected) {
                 assertEquals("Flush failed" , expected.getMessage());
             }
+
+            try {
+                o.write(new byte[] { 4, 4, 5 });
+                fail("expected already closed exception");
+            } catch (IOException expected) {
+            }
         }
 
         // adding a new test? Don't forget to update createTests().

Modified: harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Streams.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Streams.java?rev=835209&r1=835208&r2=835209&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Streams.java (original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Streams.java Thu Nov
12 03:17:54 2009
@@ -20,16 +20,19 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringWriter;
 
 /**
  * Utility methods for working with byte and character streams.
  */
 public class Streams {
+    private Streams() {}
 
     /**
      * Drains the stream into a byte array and returns the result.
      */
-    public static byte[] bytesFromStream(InputStream source) throws IOException {
+    public static byte[] streamToBytes(InputStream source) throws IOException {
         byte[] buffer = new byte[1024];
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         int count;
@@ -38,4 +41,17 @@
         }
         return out.toByteArray();
     }
+
+    /**
+     * Drains the stream into a string and returns the result.
+     */
+    public static String streamToString(Reader fileReader) throws IOException {
+        char[] buffer = new char[1024];
+        StringWriter out = new StringWriter();
+        int count;
+        while ((count = fileReader.read(buffer)) != -1) {
+            out.write(buffer, 0, count);
+        }
+        return out.toString();
+    }
 }



Mime
View raw message