|
19 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 20 | import static org.junit.jupiter.api.Assertions.assertThrows; |
21 | 21 |
|
| 22 | +import java.io.FileNotFoundException; |
22 | 23 | import java.io.IOException; |
23 | 24 | import java.io.InputStream; |
| 25 | +import java.nio.file.FileSystemNotFoundException; |
| 26 | +import java.util.stream.Stream; |
24 | 27 |
|
25 | | -import org.junit.jupiter.api.BeforeEach; |
26 | 28 | import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
27 | 31 |
|
28 | 32 | /** |
29 | 33 | * Tests {@link BrokenInputStream}. |
30 | 34 | */ |
31 | 35 | public class BrokenInputStreamTest { |
32 | 36 |
|
33 | | - private IOException exception; |
| 37 | + static final class CustomeException extends Exception { |
34 | 38 |
|
35 | | - private InputStream stream; |
| 39 | + private static final long serialVersionUID = 1L; |
36 | 40 |
|
37 | | - @BeforeEach |
38 | | - public void setUp() { |
39 | | - exception = new IOException("test exception"); |
40 | | - stream = new BrokenInputStream(exception); |
41 | 41 | } |
42 | 42 |
|
43 | | - @Test |
44 | | - public void testAvailable() { |
45 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.available())); |
| 43 | + static Stream<Class<? extends Throwable>> parameters() { |
| 44 | + // @formatter:off |
| 45 | + return Stream.of( |
| 46 | + IOException.class, |
| 47 | + FileNotFoundException.class, |
| 48 | + FileSystemNotFoundException.class, |
| 49 | + RuntimeException.class, |
| 50 | + IllegalArgumentException.class, |
| 51 | + IllegalStateException.class, |
| 52 | + Error.class, |
| 53 | + ExceptionInInitializerError.class, |
| 54 | + CustomeException.class |
| 55 | + ); |
| 56 | + // @formatter:on |
46 | 57 | } |
47 | 58 |
|
48 | | - @Test |
49 | | - public void testClose() { |
50 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.close())); |
| 59 | + private BrokenInputStream createBrokenInputStream(final Throwable exception) { |
| 60 | + if (exception instanceof IOException) { |
| 61 | + return new BrokenInputStream((IOException) exception); |
| 62 | + } |
| 63 | + return new BrokenInputStream(exception); |
51 | 64 | } |
52 | 65 |
|
53 | | - @Test |
54 | | - public void testRead() { |
55 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.read())); |
56 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.read(new byte[1]))); |
57 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.read(new byte[1], 0, 1))); |
| 66 | + @ParameterizedTest |
| 67 | + @MethodSource("parameters") |
| 68 | + public void testAvailable(final Class<Exception> clazz) throws Exception { |
| 69 | + final Throwable exception = clazz.newInstance(); |
| 70 | + @SuppressWarnings("resource") |
| 71 | + final BrokenInputStream stream = createBrokenInputStream(exception); |
| 72 | + assertEquals(exception, assertThrows(exception.getClass(), () -> stream.available())); |
58 | 73 | } |
59 | 74 |
|
60 | | - @Test |
61 | | - public void testReset() { |
62 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.reset())); |
| 75 | + @ParameterizedTest |
| 76 | + @MethodSource("parameters") |
| 77 | + public void testClose(final Class<Exception> clazz) throws Exception { |
| 78 | + final Throwable exception = clazz.newInstance(); |
| 79 | + @SuppressWarnings("resource") |
| 80 | + final BrokenInputStream stream = createBrokenInputStream(exception); |
| 81 | + assertEquals(exception, assertThrows(exception.getClass(), () -> stream.close())); |
63 | 82 | } |
64 | 83 |
|
65 | | - @Test |
66 | | - public void testSkip() { |
67 | | - assertEquals(exception, assertThrows(IOException.class, () -> stream.skip(1))); |
| 84 | + @ParameterizedTest |
| 85 | + @MethodSource("parameters") |
| 86 | + public void testRead(final Class<Exception> clazz) throws Exception { |
| 87 | + final Throwable exception = clazz.newInstance(); |
| 88 | + @SuppressWarnings("resource") |
| 89 | + final BrokenInputStream stream = createBrokenInputStream(exception); |
| 90 | + assertEquals(exception, assertThrows(exception.getClass(), () -> stream.read())); |
| 91 | + assertEquals(exception, assertThrows(exception.getClass(), () -> stream.read(new byte[1]))); |
| 92 | + assertEquals(exception, assertThrows(exception.getClass(), () -> stream.read(new byte[1], 0, 1))); |
| 93 | + } |
| 94 | + |
| 95 | + @ParameterizedTest |
| 96 | + @MethodSource("parameters") |
| 97 | + public void testReset(final Class<Exception> clazz) throws Exception { |
| 98 | + final Throwable exception = clazz.newInstance(); |
| 99 | + @SuppressWarnings("resource") |
| 100 | + final BrokenInputStream stream = createBrokenInputStream(exception); |
| 101 | + assertEquals(exception, assertThrows(exception.getClass(), () -> stream.reset())); |
| 102 | + } |
| 103 | + |
| 104 | + @ParameterizedTest |
| 105 | + @MethodSource("parameters") |
| 106 | + public void testSkip(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(exception.getClass(), () -> stream.skip(1))); |
68 | 111 | } |
69 | 112 |
|
70 | 113 | @Test |
71 | | - public void testTryWithResources() { |
| 114 | + public void testTryWithResources() throws Exception { |
72 | 115 | final IOException thrown = assertThrows(IOException.class, () -> { |
73 | 116 | try (InputStream newStream = new BrokenInputStream()) { |
74 | 117 | newStream.read(); |
|
0 commit comments