Skip to content

Commit 9deb9c1

Browse files
author
Gary Gregory
committed
Modified patch from Rob Spoor.
1 parent fd54aa6 commit 9deb9c1

1 file changed

Lines changed: 17 additions & 32 deletions

File tree

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

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,36 @@
1717
package org.apache.commons.io.output;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.verify;
2023

2124
import java.io.ByteArrayOutputStream;
2225
import java.io.IOException;
26+
import java.io.OutputStream;
2327

24-
import org.junit.Assert;
28+
import org.apache.commons.io.testtools.YellOnCloseOutputStream;
2529
import org.junit.Test;
2630

27-
/**
31+
/**On
32+
* JUnit Test Case for {@link TeeOutputStream}.
2833
*/
29-
3034
public class TeeOutputStreamTest {
3135

32-
private static class ExceptionOnCloseByteArrayOutputStream extends ByteArrayOutputStream {
33-
34-
@Override
35-
public void close() throws IOException {
36-
throw new IOException();
37-
}
38-
}
39-
40-
private static class RecordCloseByteArrayOutputStream extends ByteArrayOutputStream {
41-
42-
boolean closed;
43-
44-
@Override
45-
public void close() throws IOException {
46-
super.close();
47-
closed = true;
48-
}
49-
}
50-
5136
/**
5237
* Tests that the branch {@code OutputStream} is closed when closing the main {@code OutputStream} throws an
5338
* exception on {@link TeeOutputStream#close()}.
5439
*/
5540
@Test
56-
public void testCloseBranchIOException() {
57-
final ByteArrayOutputStream badOs = new ExceptionOnCloseByteArrayOutputStream();
58-
final RecordCloseByteArrayOutputStream goodOs = new RecordCloseByteArrayOutputStream();
41+
public void testIOExceptionOnCloseBranch() throws IOException {
42+
final OutputStream badOs = new YellOnCloseOutputStream();
43+
final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
5944
final TeeOutputStream tos = new TeeOutputStream(goodOs, badOs);
6045
try {
6146
tos.close();
62-
Assert.fail("Expected " + IOException.class.getName());
47+
fail("Expected " + IOException.class.getName());
6348
} catch (final IOException e) {
64-
Assert.assertTrue(goodOs.closed);
49+
verify(goodOs).close();
6550
}
6651
}
6752

@@ -70,15 +55,15 @@ public void testCloseBranchIOException() {
7055
* exception on {@link TeeOutputStream#close()}.
7156
*/
7257
@Test
73-
public void testCloseMainIOException() {
74-
final ByteArrayOutputStream badOs = new ExceptionOnCloseByteArrayOutputStream();
75-
final RecordCloseByteArrayOutputStream goodOs = new RecordCloseByteArrayOutputStream();
58+
public void testIOExceptionOnClose() throws IOException {
59+
final OutputStream badOs = new YellOnCloseOutputStream();
60+
final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
7661
final TeeOutputStream tos = new TeeOutputStream(badOs, goodOs);
7762
try {
7863
tos.close();
79-
Assert.fail("Expected " + IOException.class.getName());
64+
fail("Expected " + IOException.class.getName());
8065
} catch (final IOException e) {
81-
Assert.assertTrue(goodOs.closed);
66+
verify(goodOs).close();
8267
}
8368
}
8469

0 commit comments

Comments
 (0)