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
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class AutoCloseInputStreamTest {
34
35 private byte[] data;
36
37 private AutoCloseInputStream stream;
38
39 private boolean closed;
40
41 @BeforeEach
42 public void setUp() {
43 data = new byte[] { 'x', 'y', 'z' };
44 stream = new AutoCloseInputStream(new ByteArrayInputStream(data) {
45 @Override
46 public void close() {
47 closed = true;
48 }
49 });
50 closed = false;
51 }
52
53 @Test
54 public void testBuilderGet() {
55
56 assertThrows(IllegalStateException.class, () -> AutoCloseInputStream.builder().get());
57 }
58
59 @Test
60 public void testClose() throws IOException {
61 stream.close();
62 assertTrue(closed, "closed");
63 assertEquals(-1, stream.read(), "read()");
64 }
65
66 @Test
67 public void testFinalize() throws Throwable {
68 stream.finalize();
69 assertTrue(closed, "closed");
70 assertEquals(-1, stream.read(), "read()");
71 }
72
73 @Test
74 public void testRead() throws IOException {
75 for (final byte element : data) {
76 assertEquals(element, stream.read(), "read()");
77 assertFalse(closed, "closed");
78 }
79 assertEquals(-1, stream.read(), "read()");
80 assertTrue(closed, "closed");
81 }
82
83 @Test
84 public void testReadBuffer() throws IOException {
85 final byte[] b = new byte[data.length * 2];
86 int total = 0;
87 for (int n = 0; n != -1; n = stream.read(b)) {
88 assertFalse(closed, "closed");
89 for (int i = 0; i < n; i++) {
90 assertEquals(data[total + i], b[i], "read(b)");
91 }
92 total += n;
93 }
94 assertEquals(data.length, total, "read(b)");
95 assertTrue(closed, "closed");
96 assertEquals(-1, stream.read(b), "read(b)");
97 }
98
99 @Test
100 public void testReadBufferOffsetLength() throws IOException {
101 final byte[] b = new byte[data.length * 2];
102 int total = 0;
103 for (int n = 0; n != -1; n = stream.read(b, total, b.length - total)) {
104 assertFalse(closed, "closed");
105 total += n;
106 }
107 assertEquals(data.length, total, "read(b, off, len)");
108 for (int i = 0; i < data.length; i++) {
109 assertEquals(data[i], b[i], "read(b, off, len)");
110 }
111 assertTrue(closed, "closed");
112 assertEquals(-1, stream.read(b, 0, b.length), "read(b, off, len)");
113 }
114
115 private void testResetBeforeEnd(final AutoCloseInputStream inputStream) throws IOException {
116 inputStream.mark(1);
117 assertEquals('1', inputStream.read());
118 inputStream.reset();
119 assertEquals('1', inputStream.read());
120 assertEquals('2', inputStream.read());
121 inputStream.reset();
122 assertEquals('1', inputStream.read());
123 assertEquals('2', inputStream.read());
124 assertEquals('3', inputStream.read());
125 inputStream.reset();
126 assertEquals('1', inputStream.read());
127 assertEquals('2', inputStream.read());
128 assertEquals('3', inputStream.read());
129 assertEquals('4', inputStream.read());
130 inputStream.reset();
131 assertEquals('1', inputStream.read());
132 }
133
134 @Test
135 public void testResetBeforeEndCtor() throws IOException {
136 try (final AutoCloseInputStream inputStream = new AutoCloseInputStream(new ByteArrayInputStream("1234".getBytes()))) {
137 testResetBeforeEnd(inputStream);
138 }
139 }
140
141 @Test
142 public void testResetBeforeEndSetByteArray() throws IOException {
143 try (final AutoCloseInputStream inputStream = AutoCloseInputStream.builder().setByteArray("1234".getBytes()).get()) {
144 testResetBeforeEnd(inputStream);
145 }
146 }
147
148 @Test
149 public void testResetBeforeEndSetCharSequence() throws IOException {
150 try (final AutoCloseInputStream inputStream = AutoCloseInputStream.builder().setCharSequence("1234").get()) {
151 testResetBeforeEnd(inputStream);
152 }
153 }
154
155 @Test
156 public void testResetBeforeEndSetInputStream() throws IOException {
157 try (final AutoCloseInputStream inputStream = AutoCloseInputStream.builder().setInputStream(new ByteArrayInputStream("1234".getBytes())).get()) {
158 testResetBeforeEnd(inputStream);
159 }
160 }
161
162 }