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.Reader;
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 BrokenReaderTest {
34
35 private static BrokenReader createBrokenReader(final Throwable exception) {
36 if (exception instanceof IOException) {
37 return new BrokenReader((IOException) exception);
38 }
39 return new BrokenReader(exception);
40 }
41
42 @ParameterizedTest
43 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
44 public void testClose(final Class<Exception> clazz) throws Exception {
45 final Throwable exception = clazz.newInstance();
46 @SuppressWarnings("resource")
47 final BrokenReader brokenReader = createBrokenReader(exception);
48 assertEquals(exception, assertThrows(clazz, () -> brokenReader.close()));
49 }
50
51 @Test
52 public void testInstance() {
53 assertNotNull(BrokenReader.INSTANCE);
54 }
55
56 @ParameterizedTest
57 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
58 public void testMark(final Class<Throwable> clazz) throws Exception {
59 final Throwable exception = clazz.newInstance();
60 @SuppressWarnings("resource")
61 final BrokenReader brokenReader = createBrokenReader(exception);
62 assertEquals(exception, assertThrows(clazz, () -> brokenReader.mark(1)));
63 }
64
65 @ParameterizedTest
66 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
67 public void testRead(final Class<Throwable> clazz) throws Exception {
68 final Throwable exception = clazz.newInstance();
69 @SuppressWarnings("resource")
70 final BrokenReader brokenReader = createBrokenReader(exception);
71 assertEquals(exception, assertThrows(clazz, () -> brokenReader.read()));
72 }
73
74 @ParameterizedTest
75 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
76 public void testReadCharArray(final Class<Throwable> clazz) throws Exception {
77 final Throwable exception = clazz.newInstance();
78 @SuppressWarnings("resource")
79 final BrokenReader brokenReader = createBrokenReader(exception);
80 assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(new char[1])));
81 }
82
83 @ParameterizedTest
84 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
85 public void testReadCharArrayIndexed(final Class<Throwable> clazz) throws Exception {
86 final Throwable exception = clazz.newInstance();
87 @SuppressWarnings("resource")
88 final BrokenReader brokenReader = createBrokenReader(exception);
89 assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(new char[1], 0, 1)));
90 }
91
92 @ParameterizedTest
93 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
94 public void testReady(final Class<Throwable> clazz) throws Exception {
95 final Throwable exception = clazz.newInstance();
96 @SuppressWarnings("resource")
97 final BrokenReader brokenReader = createBrokenReader(exception);
98 assertEquals(exception, assertThrows(clazz, () -> brokenReader.ready()));
99 }
100
101 @ParameterizedTest
102 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
103 public void testReset(final Class<Throwable> clazz) throws Exception {
104 final Throwable exception = clazz.newInstance();
105 @SuppressWarnings("resource")
106 final BrokenReader brokenReader = createBrokenReader(exception);
107 assertEquals(exception, assertThrows(clazz, () -> brokenReader.reset()));
108 }
109
110 @ParameterizedTest
111 @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
112 public void testSkip(final Class<Throwable> clazz) throws Exception {
113 final Throwable exception = clazz.newInstance();
114 @SuppressWarnings("resource")
115 final BrokenReader brokenReader = createBrokenReader(exception);
116 assertEquals(exception, assertThrows(clazz, () -> brokenReader.skip(1)));
117 }
118
119 @Test
120 public void testTryWithResources() {
121 final IOException thrown = assertThrows(IOException.class, () -> {
122 try (Reader newReader = new BrokenReader()) {
123 newReader.read();
124 }
125 });
126 assertEquals("Broken reader", thrown.getMessage());
127
128 final Throwable[] suppressed = thrown.getSuppressed();
129 assertEquals(1, suppressed.length);
130 assertEquals(IOException.class, suppressed[0].getClass());
131 assertEquals("Broken reader", suppressed[0].getMessage());
132 }
133
134 }