Skip to content

Commit bccee3b

Browse files
committed
IO-193: Broken input and output streams
Added the proposed BrokenInputStream and BrokenOutputStream classes. Also added simple test cases for the new code. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@741531 13f79535-47bb-0310-9956-ffa450edef68
1 parent 28c59d7 commit bccee3b

6 files changed

Lines changed: 371 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.input;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
/**
23+
* Broken input stream. This stream always throws an {@link IOException} from
24+
* all the {@link InputStream} methods where the exception is declared.
25+
* <p>
26+
* This class is mostly useful for testing error handling in code that uses an
27+
* input stream.
28+
*
29+
* @since Commons IO 1.5
30+
*/
31+
public class BrokenInputStream extends InputStream {
32+
33+
/**
34+
* The exception that is thrown by all methods of this class.
35+
*/
36+
private final IOException exception;
37+
38+
/**
39+
* Creates a new stream that always throws the given exception.
40+
*
41+
* @param exception the exception to be thrown
42+
*/
43+
public BrokenInputStream(IOException exception) {
44+
this.exception = exception;
45+
}
46+
47+
/**
48+
* Creates a new stream that always throws an {@link IOException}
49+
*/
50+
public BrokenInputStream() {
51+
this(new IOException("Broken input stream"));
52+
}
53+
54+
/**
55+
* Throws the configured exception.
56+
*
57+
* @return nothing
58+
* @throws IOException always thrown
59+
*/
60+
@Override
61+
public int read() throws IOException {
62+
throw exception;
63+
}
64+
65+
/**
66+
* Throws the configured exception.
67+
*
68+
* @return nothing
69+
* @throws IOException always thrown
70+
*/
71+
@Override
72+
public int available() throws IOException {
73+
throw exception;
74+
}
75+
76+
/**
77+
* Throws the configured exception.
78+
*
79+
* @param n ignored
80+
* @return nothing
81+
* @throws IOException always thrown
82+
*/
83+
@Override
84+
public long skip(long n) throws IOException {
85+
throw exception;
86+
}
87+
88+
/**
89+
* Throws the configured exception.
90+
*
91+
* @throws IOException always thrown
92+
*/
93+
@Override
94+
public synchronized void reset() throws IOException {
95+
throw exception;
96+
}
97+
98+
/**
99+
* Throws the configured exception.
100+
*
101+
* @throws IOException always thrown
102+
*/
103+
@Override
104+
public void close() throws IOException {
105+
throw exception;
106+
}
107+
108+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
22+
/**
23+
* Broken output stream. This stream always throws an {@link IOException} from
24+
* all {@link OutputStream} methods.
25+
* <p>
26+
* This class is mostly useful for testing error handling in code that uses an
27+
* output stream.
28+
*
29+
* @since Commons IO 1.5
30+
*/
31+
public class BrokenOutputStream extends OutputStream {
32+
33+
/**
34+
* The exception that is thrown by all methods of this class.
35+
*/
36+
private final IOException exception;
37+
38+
/**
39+
* Creates a new stream that always throws the given exception.
40+
*
41+
* @param exception the exception to be thrown
42+
*/
43+
public BrokenOutputStream(IOException exception) {
44+
this.exception = exception;
45+
}
46+
47+
/**
48+
* Creates a new stream that always throws an {@link IOException}
49+
*/
50+
public BrokenOutputStream() {
51+
this(new IOException("Broken output stream"));
52+
}
53+
54+
/**
55+
* Throws the configured exception.
56+
*
57+
* @param b ignored
58+
* @throws IOException always thrown
59+
*/
60+
@Override
61+
public void write(int b) throws IOException {
62+
throw exception;
63+
}
64+
65+
/**
66+
* Throws the configured exception.
67+
*
68+
* @throws IOException always thrown
69+
*/
70+
@Override
71+
public void flush() throws IOException {
72+
throw exception;
73+
}
74+
75+
/**
76+
* Throws the configured exception.
77+
*
78+
* @throws IOException always thrown
79+
*/
80+
@Override
81+
public void close() throws IOException {
82+
throw exception;
83+
}
84+
85+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.input;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import junit.framework.TestCase;
23+
24+
/**
25+
* JUnit Test Case for {@link BrokenInputStream}.
26+
*/
27+
public class BrokenInputStreamTest extends TestCase {
28+
29+
private IOException exception;
30+
31+
private InputStream stream;
32+
33+
protected void setUp() {
34+
exception = new IOException("test exception");
35+
stream = new BrokenInputStream(exception);
36+
}
37+
38+
public void testRead() {
39+
try {
40+
stream.read();
41+
fail("Expected exception not thrown.");
42+
} catch (IOException e) {
43+
assertEquals(exception, e);
44+
}
45+
46+
try {
47+
stream.read(new byte[1]);
48+
fail("Expected exception not thrown.");
49+
} catch (IOException e) {
50+
assertEquals(exception, e);
51+
}
52+
53+
try {
54+
stream.read(new byte[1], 0, 1);
55+
fail("Expected exception not thrown.");
56+
} catch (IOException e) {
57+
assertEquals(exception, e);
58+
}
59+
}
60+
61+
public void testAvailable() {
62+
try {
63+
stream.available();
64+
fail("Expected exception not thrown.");
65+
} catch (IOException e) {
66+
assertEquals(exception, e);
67+
}
68+
}
69+
70+
public void testSkip() {
71+
try {
72+
stream.skip(1);
73+
fail("Expected exception not thrown.");
74+
} catch (IOException e) {
75+
assertEquals(exception, e);
76+
}
77+
}
78+
79+
public void testReset() {
80+
try {
81+
stream.reset();
82+
fail("Expected exception not thrown.");
83+
} catch (IOException e) {
84+
assertEquals(exception, e);
85+
}
86+
}
87+
88+
public void testClose() {
89+
try {
90+
stream.close();
91+
fail("Expected exception not thrown.");
92+
} catch (IOException e) {
93+
assertEquals(exception, e);
94+
}
95+
}
96+
97+
}

src/test/org/apache/commons/io/input/PackageTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static void main(String[] args) {
3434
public static Test suite() {
3535
TestSuite suite = new TestSuite("IO Utilities - input");
3636
suite.addTest(new TestSuite(AutoCloseInputStreamTest.class));
37+
suite.addTest(new TestSuite(BrokenInputStreamTest.class));
3738
suite.addTest(new TestSuite(CharSequenceReaderTest.class));
3839
suite.addTest(new TestSuite(ClassLoaderObjectInputStreamTest.class));
3940
suite.addTest(new TestSuite(ClosedInputStreamTest.class));
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
22+
import junit.framework.TestCase;
23+
24+
/**
25+
* JUnit Test Case for {@link BrokenOutputStream}.
26+
*/
27+
public class BrokenOutputStreamTest extends TestCase {
28+
29+
private IOException exception;
30+
31+
private OutputStream stream;
32+
33+
protected void setUp() {
34+
exception = new IOException("test exception");
35+
stream = new BrokenOutputStream(exception);
36+
}
37+
38+
public void testWrite() {
39+
try {
40+
stream.write(1);
41+
fail("Expected exception not thrown.");
42+
} catch (IOException e) {
43+
assertEquals(exception, e);
44+
}
45+
46+
try {
47+
stream.write(new byte[1]);
48+
fail("Expected exception not thrown.");
49+
} catch (IOException e) {
50+
assertEquals(exception, e);
51+
}
52+
53+
try {
54+
stream.write(new byte[1], 0, 1);
55+
fail("Expected exception not thrown.");
56+
} catch (IOException e) {
57+
assertEquals(exception, e);
58+
}
59+
}
60+
61+
public void testFlush() {
62+
try {
63+
stream.flush();
64+
fail("Expected exception not thrown.");
65+
} catch (IOException e) {
66+
assertEquals(exception, e);
67+
}
68+
}
69+
70+
public void testClose() {
71+
try {
72+
stream.close();
73+
fail("Expected exception not thrown.");
74+
} catch (IOException e) {
75+
assertEquals(exception, e);
76+
}
77+
}
78+
79+
}

src/test/org/apache/commons/io/output/PackageTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void main(String[] args) {
3333

3434
public static Test suite() {
3535
TestSuite suite = new TestSuite("IO Utilities - output");
36+
suite.addTest(new TestSuite(BrokenOutputStreamTest.class));
3637
suite.addTest(new TestSuite(ByteArrayOutputStreamTestCase.class));
3738
suite.addTest(new TestSuite(ClosedOutputStreamTest.class));
3839
suite.addTest(new TestSuite(CloseShieldOutputStreamTest.class));

0 commit comments

Comments
 (0)