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.
+ * <p>
+ * 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.
+ * <p>
+ * 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));