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 public class TaggedInputStreamTest {
36
37 @Test
38 public 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 @Test
63 public void testEmptyStream() throws IOException {
64 try (InputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
65 assertEquals(0, stream.available());
66 assertEquals(-1, stream.read());
67 assertEquals(-1, stream.read(new byte[1]));
68 assertEquals(-1, stream.read(new byte[1], 0, 1));
69 }
70 }
71
72 @Test
73 public void testNormalStream() throws IOException {
74 try (InputStream stream = new TaggedInputStream(new ByteArrayInputStream(new byte[] {'a', 'b', 'c'}))) {
75 assertEquals(3, stream.available());
76 assertEquals('a', stream.read());
77 final byte[] buffer = new byte[1];
78 assertEquals(1, stream.read(buffer));
79 assertEquals('b', buffer[0]);
80 assertEquals(1, stream.read(buffer, 0, 1));
81 assertEquals('c', buffer[0]);
82 assertEquals(-1, stream.read());
83 }
84 }
85
86 @Test
87 public void testOtherException() throws Exception {
88 final IOException exception = new IOException("test exception");
89 try (TaggedInputStream stream = new TaggedInputStream(ClosedInputStream.INSTANCE)) {
90
91 assertFalse(stream.isCauseOf(exception));
92 assertFalse(stream.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
93
94 stream.throwIfCauseOf(exception);
95
96 stream.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
97 }
98 }
99
100 }