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 java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.UUID;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.io.TaggedIOException;
27
28
29
30
31 public class TaggedInputStreamTest extends TestCase {
32
33 public void testEmptyStream() {
34 try {
35 final InputStream stream = new TaggedInputStream(new ClosedInputStream());
36 assertEquals(0, stream.available());
37 assertEquals(-1, stream.read());
38 assertEquals(-1, stream.read(new byte[1]));
39 assertEquals(-1, stream.read(new byte[1], 0, 1));
40 stream.close();
41 } catch (final IOException e) {
42 fail("Unexpected exception thrown");
43 }
44 }
45
46 public void testNormalStream() {
47 try {
48 final InputStream stream = new TaggedInputStream(
49 new ByteArrayInputStream(new byte[] { 'a', 'b', 'c' }));
50 assertEquals(3, stream.available());
51 assertEquals('a', stream.read());
52 final byte[] buffer = new byte[1];
53 assertEquals(1, stream.read(buffer));
54 assertEquals('b', buffer[0]);
55 assertEquals(1, stream.read(buffer, 0, 1));
56 assertEquals('c', buffer[0]);
57 assertEquals(-1, stream.read());
58 stream.close();
59 } catch (final IOException e) {
60 fail("Unexpected exception thrown");
61 }
62 }
63
64 public void testBrokenStream() {
65 final IOException exception = new IOException("test exception");
66 final TaggedInputStream stream =
67 new TaggedInputStream(new BrokenInputStream(exception));
68
69
70 try {
71 stream.available();
72 fail("Expected exception not thrown.");
73 } catch (final IOException e) {
74 assertTrue(stream.isCauseOf(e));
75 try {
76 stream.throwIfCauseOf(e);
77 fail("Expected exception not thrown.");
78 } catch (final IOException e2) {
79 assertEquals(exception, e2);
80 }
81 }
82
83
84 try {
85 stream.read();
86 fail("Expected exception not thrown.");
87 } catch (final IOException e) {
88 assertTrue(stream.isCauseOf(e));
89 try {
90 stream.throwIfCauseOf(e);
91 fail("Expected exception not thrown.");
92 } catch (final IOException e2) {
93 assertEquals(exception, e2);
94 }
95 }
96
97
98 try {
99 stream.close();
100 fail("Expected exception not thrown.");
101 } catch (final IOException e) {
102 assertTrue(stream.isCauseOf(e));
103 try {
104 stream.throwIfCauseOf(e);
105 fail("Expected exception not thrown.");
106 } catch (final IOException e2) {
107 assertEquals(exception, e2);
108 }
109 }
110 }
111
112 public void testOtherException() throws Exception {
113 final IOException exception = new IOException("test exception");
114 final InputStream closed = new ClosedInputStream();
115 final TaggedInputStream stream = new TaggedInputStream(closed);
116
117 assertFalse(stream.isCauseOf(exception));
118 assertFalse(stream.isCauseOf(
119 new TaggedIOException(exception, UUID.randomUUID())));
120
121 try {
122 stream.throwIfCauseOf(exception);
123 } catch (final IOException e) {
124 fail("Unexpected exception thrown");
125 }
126
127 try {
128 stream.throwIfCauseOf(
129 new TaggedIOException(exception, UUID.randomUUID()));
130 } catch (final IOException e) {
131 fail("Unexpected exception thrown");
132 }
133 stream.close();
134 }
135
136 }