Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 73528 invoked from network); 6 Feb 2009 11:03:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 6 Feb 2009 11:03:06 -0000 Received: (qmail 71977 invoked by uid 500); 6 Feb 2009 11:03:06 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 71681 invoked by uid 500); 6 Feb 2009 11:03:04 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 71672 invoked by uid 99); 6 Feb 2009 11:03:04 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Feb 2009 03:03:04 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Feb 2009 11:03:01 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 6F7A52388892; Fri, 6 Feb 2009 11:02:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r741531 - in /commons/proper/io/trunk/src: java/org/apache/commons/io/input/ java/org/apache/commons/io/output/ test/org/apache/commons/io/input/ test/org/apache/commons/io/output/ Date: Fri, 06 Feb 2009 11:02:38 -0000 To: commits@commons.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090206110239.6F7A52388892@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Fri Feb 6 11:02:37 2009 New Revision: 741531 URL: http://svn.apache.org/viewvc?rev=741531&view=rev Log: IO-193: Broken input and output streams Added the proposed BrokenInputStream and BrokenOutputStream classes. Also added simple test cases for the new code. Added: commons/proper/io/trunk/src/java/org/apache/commons/io/input/BrokenInputStream.java commons/proper/io/trunk/src/java/org/apache/commons/io/output/BrokenOutputStream.java commons/proper/io/trunk/src/test/org/apache/commons/io/input/BrokenInputStreamTest.java commons/proper/io/trunk/src/test/org/apache/commons/io/output/BrokenOutputStreamTest.java Modified: 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/input/BrokenInputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/input/BrokenInputStream.java?rev=741531&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/input/BrokenInputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/input/BrokenInputStream.java Fri Feb 6 11:02:37 2009 @@ -0,0 +1,108 @@ +/* + * 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; + +/** + * Broken input stream. This stream always throws an {@link IOException} from + * all the {@link InputStream} methods where the exception is declared. + *

+ * This class is mostly useful for testing error handling in code that uses an + * input stream. + * + * @since Commons IO 1.5 + */ +public class BrokenInputStream extends InputStream { + + /** + * The exception that is thrown by all methods of this class. + */ + private final IOException exception; + + /** + * Creates a new stream that always throws the given exception. + * + * @param exception the exception to be thrown + */ + public BrokenInputStream(IOException exception) { + this.exception = exception; + } + + /** + * Creates a new stream that always throws an {@link IOException} + */ + public BrokenInputStream() { + this(new IOException("Broken input stream")); + } + + /** + * Throws the configured exception. + * + * @return nothing + * @throws IOException always thrown + */ + @Override + public int read() throws IOException { + throw exception; + } + + /** + * Throws the configured exception. + * + * @return nothing + * @throws IOException always thrown + */ + @Override + public int available() throws IOException { + throw exception; + } + + /** + * Throws the configured exception. + * + * @param n ignored + * @return nothing + * @throws IOException always thrown + */ + @Override + public long skip(long n) throws IOException { + throw exception; + } + + /** + * Throws the configured exception. + * + * @throws IOException always thrown + */ + @Override + public synchronized void reset() throws IOException { + throw exception; + } + + /** + * Throws the configured exception. + * + * @throws IOException always thrown + */ + @Override + public void close() throws IOException { + throw exception; + } + +} Added: commons/proper/io/trunk/src/java/org/apache/commons/io/output/BrokenOutputStream.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/BrokenOutputStream.java?rev=741531&view=auto ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/output/BrokenOutputStream.java (added) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/output/BrokenOutputStream.java Fri Feb 6 11:02:37 2009 @@ -0,0 +1,85 @@ +/* + * 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; + +/** + * Broken output stream. This stream always throws an {@link IOException} from + * all {@link OutputStream} methods. + *

+ * This class is mostly useful for testing error handling in code that uses an + * output stream. + * + * @since Commons IO 1.5 + */ +public class BrokenOutputStream extends OutputStream { + + /** + * The exception that is thrown by all methods of this class. + */ + private final IOException exception; + + /** + * Creates a new stream that always throws the given exception. + * + * @param exception the exception to be thrown + */ + public BrokenOutputStream(IOException exception) { + this.exception = exception; + } + + /** + * Creates a new stream that always throws an {@link IOException} + */ + public BrokenOutputStream() { + this(new IOException("Broken output stream")); + } + + /** + * Throws the configured exception. + * + * @param b ignored + * @throws IOException always thrown + */ + @Override + public void write(int b) throws IOException { + throw exception; + } + + /** + * Throws the configured exception. + * + * @throws IOException always thrown + */ + @Override + public void flush() throws IOException { + throw exception; + } + + /** + * Throws the configured exception. + * + * @throws IOException always thrown + */ + @Override + public void close() throws IOException { + throw exception; + } + +} Added: commons/proper/io/trunk/src/test/org/apache/commons/io/input/BrokenInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/input/BrokenInputStreamTest.java?rev=741531&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/input/BrokenInputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/input/BrokenInputStreamTest.java Fri Feb 6 11:02:37 2009 @@ -0,0 +1,97 @@ +/* + * 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 junit.framework.TestCase; + +/** + * JUnit Test Case for {@link BrokenInputStream}. + */ +public class BrokenInputStreamTest extends TestCase { + + private IOException exception; + + private InputStream stream; + + protected void setUp() { + exception = new IOException("test exception"); + stream = new BrokenInputStream(exception); + } + + public void testRead() { + try { + stream.read(); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + + try { + stream.read(new byte[1]); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + + try { + stream.read(new byte[1], 0, 1); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + + public void testAvailable() { + try { + stream.available(); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + + public void testSkip() { + try { + stream.skip(1); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + + public void testReset() { + try { + stream.reset(); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + + public void testClose() { + try { + stream.close(); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + +} 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=741531&r1=741530&r2=741531&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 11:02:37 2009 @@ -34,6 +34,7 @@ public static Test suite() { TestSuite suite = new TestSuite("IO Utilities - input"); suite.addTest(new TestSuite(AutoCloseInputStreamTest.class)); + suite.addTest(new TestSuite(BrokenInputStreamTest.class)); suite.addTest(new TestSuite(CharSequenceReaderTest.class)); suite.addTest(new TestSuite(ClassLoaderObjectInputStreamTest.class)); suite.addTest(new TestSuite(ClosedInputStreamTest.class)); Added: commons/proper/io/trunk/src/test/org/apache/commons/io/output/BrokenOutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/BrokenOutputStreamTest.java?rev=741531&view=auto ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/output/BrokenOutputStreamTest.java (added) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/BrokenOutputStreamTest.java Fri Feb 6 11:02:37 2009 @@ -0,0 +1,79 @@ +/* + * 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 junit.framework.TestCase; + +/** + * JUnit Test Case for {@link BrokenOutputStream}. + */ +public class BrokenOutputStreamTest extends TestCase { + + private IOException exception; + + private OutputStream stream; + + protected void setUp() { + exception = new IOException("test exception"); + stream = new BrokenOutputStream(exception); + } + + public void testWrite() { + try { + stream.write(1); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + + try { + stream.write(new byte[1]); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + + try { + stream.write(new byte[1], 0, 1); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + + public void testFlush() { + try { + stream.flush(); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + + public void testClose() { + try { + stream.close(); + fail("Expected exception not thrown."); + } catch (IOException e) { + assertEquals(exception, e); + } + } + +} 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=741531&r1=741530&r2=741531&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 11:02:37 2009 @@ -33,6 +33,7 @@ public static Test suite() { TestSuite suite = new TestSuite("IO Utilities - output"); + suite.addTest(new TestSuite(BrokenOutputStreamTest.class)); suite.addTest(new TestSuite(ByteArrayOutputStreamTestCase.class)); suite.addTest(new TestSuite(ClosedOutputStreamTest.class)); suite.addTest(new TestSuite(CloseShieldOutputStreamTest.class));