You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ju...@apache.org on 2009/02/06 14:29:07 UTC

svn commit: r741562 - in /commons/proper/io/trunk/src: java/org/apache/commons/io/ java/org/apache/commons/io/input/ java/org/apache/commons/io/output/ test/org/apache/commons/io/ test/org/apache/commons/io/input/ test/org/apache/commons/io/output/

Author: jukka
Date: Fri Feb  6 13:29:06 2009
New Revision: 741562

URL: http://svn.apache.org/viewvc?rev=741562&view=rev
Log:
IO-192: Tagged input and output streams

Added the proposed TaggedIOException, TaggedInputStream and TaggedOutputStream classes and related unit tests.

Added:
    commons/proper/io/trunk/src/java/org/apache/commons/io/TaggedIOException.java   (with props)
    commons/proper/io/trunk/src/java/org/apache/commons/io/input/TaggedInputStream.java   (with props)
    commons/proper/io/trunk/src/java/org/apache/commons/io/output/TaggedOutputStream.java   (with props)
    commons/proper/io/trunk/src/test/org/apache/commons/io/TaggedIOExceptionTest.java   (with props)
    commons/proper/io/trunk/src/test/org/apache/commons/io/input/TaggedInputStreamTest.java   (with props)
    commons/proper/io/trunk/src/test/org/apache/commons/io/output/TaggedOutputStreamTest.java   (with props)
Modified:
    commons/proper/io/trunk/src/test/org/apache/commons/io/PackageTestSuite.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java

Added: commons/proper/io/trunk/src/java/org/apache/commons/io/TaggedIOException.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/TaggedIOException.java?rev=741562&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/TaggedIOException.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/TaggedIOException.java Fri Feb  6 13:29:06 2009
@@ -0,0 +1,65 @@
+/*
+ * 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.commons.io;
+
+import java.io.IOException;
+
+/**
+ * An {@link IOException} wrapper that tags the wrapped exception with
+ * a given object reference. Both the tag and the wrapped original exception
+ * can be used to determine further processing when this exception is caught.
+ *
+ * @since Commons IO 1.5
+ */
+public class TaggedIOException extends IOExceptionWithCause {
+
+    /**
+     * The object reference used to tag the exception.
+     */
+    private final Object tag;
+
+    /**
+     * Creates a tagged wrapper for the given exception.
+     *
+     * @param original the exception to be tagged
+     * @param tag tag object
+     */
+    public TaggedIOException(IOException original, Object tag) {
+        super(original.getMessage(), original);
+        this.tag = tag;
+    }
+
+    /**
+     * Returns the object reference used as the tag this exception.
+     *
+     * @return tag object
+     */
+    public Object getTag() {
+        return tag;
+    }
+
+    /**
+     * Returns the wrapped exception. The only difference to the overridden
+     * {@link Throwable#getCause()} method is the narrower return type.
+     *
+     * @return wrapped exception
+     */
+    public IOException getCause() {
+        return (IOException) super.getCause();
+    }
+
+}

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/TaggedIOException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/io/trunk/src/java/org/apache/commons/io/input/TaggedInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/TaggedInputStream.java?rev=741562&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/input/TaggedInputStream.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/TaggedInputStream.java Fri Feb  6 13:29:06 2009
@@ -0,0 +1,171 @@
+/*
+ * 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.commons.io.input;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.TaggedIOException;
+
+/**
+ * An input stream decorator that tags potential exceptions so that the
+ * stream that caused the exception can easily be identified. This is
+ * done by using the {@link TaggedIOException} class to wrap all thrown
+ * {@link IOException}s. See below for an example of using this class.
+ * <pre>
+ * TaggedInputStream stream = new TaggedInputStream(...);
+ * try {
+ *     // Processing that may throw an IOException either from this stream
+ *     // or from some other IO activity like temporary files, etc.
+ *     processStream(stream);
+ * } catch (IOException e) {
+ *     if (stream.isCauseOf(e)) {
+ *         // The exception was caused by this stream.
+ *         // Use e.getCause() to get the original exception.
+ *     } else {
+ *         // The exception was caused by something else.
+ *     }
+ * }
+ * </pre>
+ * <p>
+ * Alternatively, the {@link #throwIfCauseOf(Exception)} method can be
+ * used to let higher levels of code handle the exception caused by this
+ * stream while other processing errors are being taken care of at this
+ * lower level.
+ * <pre>
+ * TaggedInputStream stream = new TaggedInputStream(...);
+ * try {
+ *     processStream(stream);
+ * } catch (IOException e) {
+ *     stream.throwIfCauseOf(e);
+ *     // ... or process the exception that was caused by something else
+ * }
+ * </pre>
+ *
+ * @see TaggedIOException
+ * @since Commons IO 1.5
+ */
+public class TaggedInputStream extends ProxyInputStream {
+
+    /**
+     * Creates a tagging decorator for the given input stream.
+     *
+     * @param proxy input stream to be decorated
+     */
+    public TaggedInputStream(InputStream proxy) {
+        super(proxy);
+    }
+
+    /**
+     * Tests if the given exception was caused by this stream.
+     *
+     * @param exception an exception
+     * @return <code>true</code> if the exception was thrown by this stream,
+     *         <code>false</code> otherwise
+     */
+    public boolean isCauseOf(IOException exception) {
+        if (exception instanceof TaggedIOException) {
+            TaggedIOException tagged = (TaggedIOException) exception;
+            return this == tagged.getTag();
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Re-throws the original exception thrown by this stream. This method
+     * first checks whether the given exception is a {@link TaggedIOException}
+     * wrapper created by this decorator, and then unwraps and throws the
+     * original wrapped exception. Returns normally if the exception was
+     * not thrown by this stream.
+     *
+     * @param exception an exception
+     * @throws IOException original exception, if any, thrown by this stream
+     */
+    public void throwIfCauseOf(Exception exception) throws IOException {
+        if (exception instanceof TaggedIOException) {
+            TaggedIOException tagged = (TaggedIOException) exception;
+            if (this == tagged.getTag()) {
+                throw tagged.getCause();
+            }
+        }
+    }
+
+    @Override
+    public int available() throws IOException {
+        try {
+            return super.available();
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public int read() throws IOException {
+        try {
+            return super.read();
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public int read(byte[] bts, int st, int end) throws IOException {
+        try {
+            return super.read(bts, st, end);
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public int read(byte[] bts) throws IOException {
+        try {
+            return super.read(bts);
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public long skip(long ln) throws IOException {
+        try {
+            return super.skip(ln);
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public void reset() throws IOException {
+        try {
+            super.reset();
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            super.close();
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+}

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/input/TaggedInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/io/trunk/src/java/org/apache/commons/io/output/TaggedOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/TaggedOutputStream.java?rev=741562&view=auto
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/output/TaggedOutputStream.java (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/output/TaggedOutputStream.java Fri Feb  6 13:29:06 2009
@@ -0,0 +1,153 @@
+/*
+ * 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.commons.io.output;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.io.TaggedIOException;
+
+/**
+ * An output stream decorator that tags potential exceptions so that the
+ * stream that caused the exception can easily be identified. This is
+ * done by using the {@link TaggedIOException} class to wrap all thrown
+ * {@link IOException}s. See below for an example of using this class.
+ * <pre>
+ * TaggedOutputStream stream = new TaggedOutputStream(...);
+ * try {
+ *     // Processing that may throw an IOException either from this stream
+ *     // or from some other IO activity like temporary files, etc.
+ *     writeToStream(stream);
+ * } catch (IOException e) {
+ *     if (stream.isCauseOf(e)) {
+ *         // The exception was caused by this stream.
+ *         // Use e.getCause() to get the original exception.
+ *     } else {
+ *         // The exception was caused by something else.
+ *     }
+ * }
+ * </pre>
+ * <p>
+ * Alternatively, the {@link #throwIfCauseOf(Exception)} method can be
+ * used to let higher levels of code handle the exception caused by this
+ * stream while other processing errors are being taken care of at this
+ * lower level.
+ * <pre>
+ * TaggedOutputStream stream = new TaggedOutputStream(...);
+ * try {
+ *     writeToStream(stream);
+ * } catch (IOException e) {
+ *     stream.throwIfCauseOf(e);
+ *     // ... or process the exception that was caused by something else
+ * }
+ * </pre>
+ *
+ * @see TaggedIOException
+ * @since Commons IO 1.5
+ */
+public class TaggedOutputStream extends ProxyOutputStream {
+
+    /**
+     * Creates a tagging decorator for the given output stream.
+     *
+     * @param proxy output stream to be decorated
+     */
+    public TaggedOutputStream(OutputStream proxy) {
+        super(proxy);
+    }
+
+    /**
+     * Tests if the given exception was caused by this stream.
+     *
+     * @param exception an exception
+     * @return <code>true</code> if the exception was thrown by this stream,
+     *         <code>false</code> otherwise
+     */
+    public boolean isCauseOf(IOException exception) {
+        if (exception instanceof TaggedIOException) {
+            TaggedIOException tagged = (TaggedIOException) exception;
+            return this == tagged.getTag();
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Re-throws the original exception thrown by this stream. This method
+     * first checks whether the given exception is a {@link TaggedIOException}
+     * wrapper created by this decorator, and then unwraps and throws the
+     * original wrapped exception. Returns normally if the exception was
+     * not thrown by this stream.
+     *
+     * @param exception an exception
+     * @throws IOException original exception, if any, thrown by this stream
+     */
+    public void throwIfCauseOf(Exception exception) throws IOException {
+        if (exception instanceof TaggedIOException) {
+            TaggedIOException tagged = (TaggedIOException) exception;
+            if (this == tagged.getTag()) {
+                throw tagged.getCause();
+            }
+        }
+    }
+
+    @Override
+    public void write(byte[] bts, int st, int end) throws IOException {
+        try {
+            super.write(bts, st, end);
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public void write(byte[] bts) throws IOException {
+        try {
+            super.write(bts);
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public void write(int idx) throws IOException {
+        try {
+            super.write(idx);
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public void flush() throws IOException {
+        try {
+            super.flush();
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            super.close();
+        } catch (IOException e) {
+            throw new TaggedIOException(e, this);
+        }
+    }
+
+}

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/output/TaggedOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/PackageTestSuite.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/PackageTestSuite.java?rev=741562&r1=741561&r2=741562&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/PackageTestSuite.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/PackageTestSuite.java Fri Feb  6 13:29:06 2009
@@ -57,6 +57,7 @@
         suite.addTest(new TestSuite(IOUtilsWriteTestCase.class));
         suite.addTest(new TestSuite(LineIteratorTestCase.class));
         suite.addTest(new TestSuite(FileUtilsWaitForTestCase.class));
+        suite.addTest(new TestSuite(TaggedIOExceptionTest.class));
         return suite;
     }
 }

Added: commons/proper/io/trunk/src/test/org/apache/commons/io/TaggedIOExceptionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/TaggedIOExceptionTest.java?rev=741562&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/TaggedIOExceptionTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/TaggedIOExceptionTest.java Fri Feb  6 13:29:06 2009
@@ -0,0 +1,37 @@
+/*
+ * 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.commons.io;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+/**
+ * JUnit Test Case for {@link TaggedIOException}.
+ */
+public class TaggedIOExceptionTest extends TestCase {
+
+    public void testTaggedIOException() {
+        Object tag = new Object();
+        IOException exception = new IOException("Test exception");
+        TaggedIOException tagged = new TaggedIOException(exception, tag);
+        assertEquals(tag, tagged.getTag());
+        assertEquals(exception, tagged.getCause());
+        assertEquals(exception.getMessage(), tagged.getMessage());
+    }
+
+}

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/TaggedIOExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java?rev=741562&r1=741561&r2=741562&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/PackageTestSuite.java Fri Feb  6 13:29:06 2009
@@ -43,6 +43,7 @@
         suite.addTest(new TestSuite(NullInputStreamTest.class));
         suite.addTest(new TestSuite(NullReaderTest.class));
         suite.addTest(new TestSuite(SwappedDataInputStreamTest.class));
+        suite.addTest(new TestSuite(TaggedInputStreamTest.class));
         suite.addTest(new TestSuite(TeeInputStreamTest.class));
         return suite;
     }

Added: commons/proper/io/trunk/src/test/org/apache/commons/io/input/TaggedInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/TaggedInputStreamTest.java?rev=741562&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/input/TaggedInputStreamTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/TaggedInputStreamTest.java Fri Feb  6 13:29:06 2009
@@ -0,0 +1,132 @@
+/*
+ * 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.commons.io.input;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.TaggedIOException;
+
+import junit.framework.TestCase;
+
+/**
+ * JUnit Test Case for {@link TaggedInputStream}.
+ */
+public class TaggedInputStreamTest extends TestCase {
+
+    public void testEmptyStream() {
+        try {
+            InputStream stream = new TaggedInputStream(new ClosedInputStream());
+            assertEquals(0, stream.available());
+            assertEquals(-1, stream.read());
+            assertEquals(-1, stream.read(new byte[1]));
+            assertEquals(-1, stream.read(new byte[1], 0, 1));
+            stream.close();
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+    }
+
+    public void testNormalStream() {
+        try {
+            InputStream stream = new TaggedInputStream(
+                    new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }));
+            assertEquals(3, stream.available());
+            assertEquals('a', stream.read());
+            byte[] buffer = new byte[1];
+            assertEquals(1, stream.read(buffer));
+            assertEquals('b', buffer[0]);
+            assertEquals(1, stream.read(buffer, 0, 1));
+            assertEquals('c', buffer[0]);
+            assertEquals(-1, stream.read());
+            stream.close();
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+    }
+
+    public void testBrokenStream() {
+        IOException exception = new IOException("test exception");
+        TaggedInputStream stream =
+            new TaggedInputStream(new BrokenInputStream(exception));
+
+        // Test the available() method
+        try {
+            stream.available();
+            fail("Expected exception not thrown.");
+        } catch (IOException e) {
+            assertTrue(stream.isCauseOf(e));
+            try {
+                stream.throwIfCauseOf(e);
+                fail("Expected exception not thrown.");
+            } catch (IOException e2) {
+                assertEquals(exception, e2);
+            }
+        }
+
+        // Test the read() method
+        try {
+            stream.read();
+            fail("Expected exception not thrown.");
+        } catch (IOException e) {
+            assertTrue(stream.isCauseOf(e));
+            try {
+                stream.throwIfCauseOf(e);
+                fail("Expected exception not thrown.");
+            } catch (IOException e2) {
+                assertEquals(exception, e2);
+            }
+        }
+
+        // Test the close() method
+        try {
+            stream.close();
+            fail("Expected exception not thrown.");
+        } catch (IOException e) {
+            assertTrue(stream.isCauseOf(e));
+            try {
+                stream.throwIfCauseOf(e);
+                fail("Expected exception not thrown.");
+            } catch (IOException e2) {
+                assertEquals(exception, e2);
+            }
+        }
+    }
+
+    public void testOtherException() {
+        IOException exception = new IOException("test exception");
+        InputStream closed = new ClosedInputStream();
+        TaggedInputStream stream = new TaggedInputStream(closed);
+
+        assertFalse(stream.isCauseOf(exception));
+        assertFalse(stream.isCauseOf(new TaggedIOException(exception, closed)));
+
+        try {
+            stream.throwIfCauseOf(exception);
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+
+        try {
+            stream.throwIfCauseOf(new TaggedIOException(exception, closed));
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+    }
+
+}

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/input/TaggedInputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java?rev=741562&r1=741561&r2=741562&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/PackageTestSuite.java Fri Feb  6 13:29:06 2009
@@ -44,6 +44,7 @@
         suite.addTest(new TestSuite(NullOutputStreamTest.class));
         suite.addTest(new TestSuite(NullWriterTest.class));
         suite.addTest(new TestSuite(StringBuilderWriterTest.class));
+        suite.addTest(new TestSuite(TaggedOutputStreamTest.class));
         suite.addTest(new TestSuite(TeeOutputStreamTest.class));
         return suite;
     }

Added: commons/proper/io/trunk/src/test/org/apache/commons/io/output/TaggedOutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/TaggedOutputStreamTest.java?rev=741562&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/output/TaggedOutputStreamTest.java (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/TaggedOutputStreamTest.java Fri Feb  6 13:29:06 2009
@@ -0,0 +1,118 @@
+/*
+ * 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.commons.io.output;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.io.TaggedIOException;
+
+import junit.framework.TestCase;
+
+/**
+ * JUnit Test Case for {@link TaggedOutputStream}.
+ */
+public class TaggedOutputStreamTest extends TestCase {
+
+    public void testNormalStream() {
+        try {
+            ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
+            OutputStream stream = new TaggedOutputStream(buffer);
+            stream.write('a');
+            stream.write(new byte[] { 'b' });
+            stream.write(new byte[] { 'c' }, 0, 1);
+            stream.flush();
+            stream.close();
+            assertEquals(3, buffer.size());
+            assertEquals('a', buffer.toByteArray()[0]);
+            assertEquals('b', buffer.toByteArray()[1]);
+            assertEquals('c', buffer.toByteArray()[2]);
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+    }
+
+    public void testBrokenStream() {
+        IOException exception = new IOException("test exception");
+        TaggedOutputStream stream =
+            new TaggedOutputStream(new BrokenOutputStream(exception));
+
+        // Test the write() method
+        try {
+            stream.write('x');
+            fail("Expected exception not thrown.");
+        } catch (IOException e) {
+            assertTrue(stream.isCauseOf(e));
+            try {
+                stream.throwIfCauseOf(e);
+                fail("Expected exception not thrown.");
+            } catch (IOException e2) {
+                assertEquals(exception, e2);
+            }
+        }
+
+        // Test the flush() method
+        try {
+            stream.flush();
+            fail("Expected exception not thrown.");
+        } catch (IOException e) {
+            assertTrue(stream.isCauseOf(e));
+            try {
+                stream.throwIfCauseOf(e);
+                fail("Expected exception not thrown.");
+            } catch (IOException e2) {
+                assertEquals(exception, e2);
+            }
+        }
+
+        // Test the close() method
+        try {
+            stream.close();
+            fail("Expected exception not thrown.");
+        } catch (IOException e) {
+            assertTrue(stream.isCauseOf(e));
+            try {
+                stream.throwIfCauseOf(e);
+                fail("Expected exception not thrown.");
+            } catch (IOException e2) {
+                assertEquals(exception, e2);
+            }
+        }
+    }
+
+    public void testOtherException() {
+        IOException exception = new IOException("test exception");
+        OutputStream closed = new ClosedOutputStream();
+        TaggedOutputStream stream = new TaggedOutputStream(closed);
+
+        assertFalse(stream.isCauseOf(exception));
+        assertFalse(stream.isCauseOf(new TaggedIOException(exception, closed)));
+
+        try {
+            stream.throwIfCauseOf(exception);
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+
+        try {
+            stream.throwIfCauseOf(new TaggedIOException(exception, closed));
+        } catch (IOException e) {
+            fail("Unexpected exception thrown");
+        }
+    }
+
+}

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/output/TaggedOutputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native