1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input;
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.InputStream;
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 BrokenInputStreamTest {
34
35 private static BrokenInputStream createBrokenInputStream(final Throwable exception) {
36 if (exception instanceof IOException) {
37 return new BrokenInputStream((IOException) exception);
38 }
39 return new BrokenInputStream(exception);
40 }
41
42 @ParameterizedTest
43 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
44 public void testAvailable(final Class<Exception> clazz) throws Exception {
45 final Throwable exception = clazz.newInstance();
46 @SuppressWarnings("resource")
47 final BrokenInputStream stream = createBrokenInputStream(exception);
48 assertEquals(exception, assertThrows(clazz, () -> stream.available()));
49 }
50
51 @ParameterizedTest
52 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
53 public void testClose(final Class<Exception> clazz) throws Exception {
54 final Throwable exception = clazz.newInstance();
55 @SuppressWarnings("resource")
56 final BrokenInputStream stream = createBrokenInputStream(exception);
57 assertEquals(exception, assertThrows(clazz, () -> stream.close()));
58 }
59
60 @Test
61 public void testInstance() {
62 assertNotNull(BrokenInputStream.INSTANCE);
63 }
64
65 @Test
66 public void testIO469() throws Throwable {
67
68
69
70
71
72 final InputStream in = new BrokenInputStream();
73 Throwable localThrowable2 = null;
74 try {
75 try {
76 in.read();
77 } catch (Throwable localThrowable1) {
78 localThrowable2 = localThrowable1;
79 throw localThrowable1;
80 } finally {
81 try {
82 in.close();
83 } catch (Throwable x2) {
84 localThrowable2.addSuppressed(x2);
85 }
86 }
87 } catch (IOException expected) {
88 final Throwable[] suppressed = expected.getSuppressed();
89 assertEquals(1, suppressed.length);
90 }
91 }
92
93 @ParameterizedTest
94 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
95 public void testRead(final Class<Exception> clazz) throws Exception {
96 final Throwable exception = clazz.newInstance();
97 @SuppressWarnings("resource")
98 final BrokenInputStream stream = createBrokenInputStream(exception);
99 assertEquals(exception, assertThrows(clazz, () -> stream.read()));
100 assertEquals(exception, assertThrows(clazz, () -> stream.read(new byte[1])));
101 assertEquals(exception, assertThrows(clazz, () -> stream.read(new byte[1], 0, 1)));
102 }
103
104 @ParameterizedTest
105 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
106 public void testReset(final Class<Exception> clazz) throws Exception {
107 final Throwable exception = clazz.newInstance();
108 @SuppressWarnings("resource")
109 final BrokenInputStream stream = createBrokenInputStream(exception);
110 assertEquals(exception, assertThrows(clazz, () -> stream.reset()));
111 }
112
113 @ParameterizedTest
114 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
115 public void testSkip(final Class<Exception> clazz) throws Exception {
116 final Throwable exception = clazz.newInstance();
117 @SuppressWarnings("resource")
118 final BrokenInputStream stream = createBrokenInputStream(exception);
119 assertEquals(exception, assertThrows(clazz, () -> stream.skip(1)));
120 }
121
122 @Test
123 public void testTryWithResources() {
124 final IOException thrown = assertThrows(IOException.class, () -> {
125 try (InputStream newStream = new BrokenInputStream()) {
126 newStream.read();
127 }
128 });
129 assertEquals("Broken input stream", thrown.getMessage());
130
131 final Throwable[] suppressed = thrown.getSuppressed();
132 assertEquals(1, suppressed.length);
133 assertEquals(IOException.class, suppressed[0].getClass());
134 assertEquals("Broken input stream", suppressed[0].getMessage());
135 }
136
137 }