1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.output;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.MethodSource;
29
30
31
32
33 public class BrokenOutputStreamTest {
34
35 private static BrokenOutputStream createBrokenOutputStream(final Throwable exception) {
36 if (exception instanceof IOException) {
37 return new BrokenOutputStream((IOException) exception);
38 }
39 return new BrokenOutputStream(exception);
40 }
41
42 @ParameterizedTest
43 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
44 public void testClose(final Class<Throwable> clazz) throws Exception {
45 final Throwable exception = clazz.newInstance();
46 @SuppressWarnings("resource")
47 final BrokenOutputStream stream = createBrokenOutputStream(exception);
48 assertEquals(exception, assertThrows(clazz, () -> stream.close()));
49 }
50
51 @ParameterizedTest
52 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
53 public void testFlush(final Class<Throwable> clazz) throws Exception {
54 final Throwable exception = clazz.newInstance();
55 @SuppressWarnings("resource")
56 final BrokenOutputStream stream = createBrokenOutputStream(exception);
57 assertEquals(exception, assertThrows(clazz, () -> stream.flush()));
58 }
59
60 @Test
61 public void testInstance() {
62 assertNotNull(BrokenOutputStream.INSTANCE);
63 }
64
65 @Test
66 public void testTryWithResources() {
67 final IOException thrown = assertThrows(IOException.class, () -> {
68 try (OutputStream newStream = new BrokenOutputStream()) {
69 newStream.write(1);
70 }
71 });
72 assertEquals("Broken output stream", thrown.getMessage());
73
74 final Throwable[] suppressed = thrown.getSuppressed();
75 assertEquals(1, suppressed.length);
76 assertEquals(IOException.class, suppressed[0].getClass());
77 assertEquals("Broken output stream", suppressed[0].getMessage());
78 }
79
80 @ParameterizedTest
81 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
82 public void testWriteByteArray(final Class<Throwable> clazz) throws Exception {
83 final Throwable exception = clazz.newInstance();
84 @SuppressWarnings("resource")
85 final BrokenOutputStream stream = createBrokenOutputStream(exception);
86 assertEquals(exception, assertThrows(clazz, () -> stream.write(new byte[1])));
87 }
88
89 @ParameterizedTest
90 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
91 public void testWriteByteArrayIndexed(final Class<Throwable> clazz) throws Exception {
92 final Throwable exception = clazz.newInstance();
93 @SuppressWarnings("resource")
94 final BrokenOutputStream stream = createBrokenOutputStream(exception);
95 assertEquals(exception, assertThrows(clazz, () -> stream.write(new byte[1], 0, 1)));
96 }
97
98 @ParameterizedTest
99 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
100 public void testWriteInt(final Class<Throwable> clazz) throws Exception {
101 final Throwable exception = clazz.newInstance();
102 @SuppressWarnings("resource")
103 final BrokenOutputStream stream = createBrokenOutputStream(exception);
104 assertEquals(exception, assertThrows(clazz, () -> stream.write(1)));
105 }
106
107 }