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.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.UUID;
28
29 import org.apache.commons.io.TaggedIOException;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 class TaggedInputStreamTest {
36
37 @Test
38 void testBrokenStream() {
39 final IOException exception = new IOException("test exception");
40 final TaggedInputStream stream =
41 new TaggedInputStream(new BrokenInputStream(exception));
42
43
44 final IOException exceptionAvailable = assertThrows(IOException.class, stream::available);
45 assertTrue(stream.isCauseOf(exceptionAvailable));
46 final IOException exceptionAvailableCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionAvailable));
47 assertEquals(exception, exceptionAvailableCause);
48
49
50 final IOException exceptionRead = assertThrows(IOException.class, stream::read);
51 assertTrue(stream.isCauseOf(exceptionRead));
52 final IOException exceptionReadCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionRead));
53 assertEquals(exception, exceptionReadCause);
54
55
56 final IOException exceptionClose = assertThrows(IOException.class, stream::close);
57 assertTrue(stream.isCauseOf(exceptionClose));
58 final IOException exceptionCloseCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionClose));
59 assertEquals(exception, exceptionCloseCause);
60 }
61
62 @SuppressWarnings({ "resource" })
63 @Test
64 void testCloseHandleIOException() throws IOException {
65 ProxyInputStreamTest.testCloseHandleIOException(new TaggedInputStream(new BrokenInputStream((Throwable) new IOException())));
66 }
67
68 @Test
69 void testEmptyStream() throws IOException {
70 try (InputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
71 assertEquals(0, stream.available());
72 assertEquals(-1, stream.read());
73 assertEquals(-1, stream.read(new byte[1]));
74 assertEquals(-1, stream.read(new byte[1], 0, 1));
75 }
76 }
77
78 @Test
79 void testNormalStream() throws IOException {
80 try (InputStream stream = new TaggedInputStream(new ByteArrayInputStream(new byte[] {'a', 'b', 'c'}))) {
81 assertEquals(3, stream.available());
82 assertEquals('a', stream.read());
83 final byte[] buffer = new byte[1];
84 assertEquals(1, stream.read(buffer));
85 assertEquals('b', buffer[0]);
86 assertEquals(1, stream.read(buffer, 0, 1));
87 assertEquals('c', buffer[0]);
88 assertEquals(-1, stream.read());
89 }
90 }
91
92 @Test
93 void testOtherException() throws Exception {
94 final IOException exception = new IOException("test exception");
95 try (TaggedInputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
96
97 assertFalse(stream.isCauseOf(exception));
98 assertFalse(stream.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
99
100 stream.throwIfCauseOf(exception);
101
102 stream.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
103 }
104 }
105
106 }