1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.function;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.junit.jupiter.api.Assertions.fail;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.UncheckedIOException;
28 import java.util.concurrent.atomic.AtomicInteger;
29 import java.util.concurrent.atomic.AtomicLong;
30 import java.util.concurrent.atomic.AtomicReference;
31 import java.util.function.Supplier;
32
33 import org.apache.commons.io.input.BrokenInputStream;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36
37
38
39
40 public class UncheckTest {
41
42 private static final byte[] BYTES = { 'a', 'b' };
43 private static final String CAUSE_MESSAGE = "CauseMessage";
44 private static final String CUSTOM_MESSAGE = "Custom message";
45
46 private AtomicInteger atomicInt;
47 private AtomicLong atomicLong;
48 private AtomicReference<String> ref1;
49 private AtomicReference<String> ref2;
50 private AtomicReference<String> ref3;
51 private AtomicReference<String> ref4;
52
53 private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
54 assertEquals(CUSTOM_MESSAGE, e.getMessage());
55 final IOException cause = e.getCause();
56 assertEquals(expected.getClass(), cause.getClass());
57 assertEquals(CAUSE_MESSAGE, cause.getMessage());
58 }
59
60 @BeforeEach
61 public void initEach() {
62 ref1 = new AtomicReference<>();
63 ref2 = new AtomicReference<>();
64 ref3 = new AtomicReference<>();
65 ref4 = new AtomicReference<>();
66 atomicInt = new AtomicInteger();
67 atomicLong = new AtomicLong();
68 }
69
70 private ByteArrayInputStream newInputStream() {
71 return new ByteArrayInputStream(BYTES);
72 }
73
74
75
76
77 @Test
78 public void testAccept() {
79 final ByteArrayInputStream stream = newInputStream();
80 Uncheck.accept(n -> stream.skip(n), 1);
81 assertEquals('b', Uncheck.get(stream::read).intValue());
82 }
83
84 @Test
85 public void testAcceptIOBiConsumerOfTUTU() {
86 assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u) -> {
87 throw new IOException();
88 }, null, null));
89 assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_BI_CONSUMER, null, null));
90 Uncheck.accept((t, u) -> {
91 TestUtils.compareAndSetThrowsIO(ref1, t);
92 TestUtils.compareAndSetThrowsIO(ref2, u);
93 }, "new1", "new2");
94 assertEquals("new1", ref1.get());
95 assertEquals("new2", ref2.get());
96 }
97
98 @Test
99 public void testAcceptIOConsumerOfTT() {
100 assertThrows(UncheckedIOException.class, () -> Uncheck.accept(t -> {
101 throw new IOException();
102 }, null));
103 assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestUtils.throwingIOConsumer(), null));
104 Uncheck.accept(t -> TestUtils.compareAndSetThrowsIO(ref1, t), "new1");
105 assertEquals("new1", ref1.get());
106 }
107
108 @Test
109 public void testAcceptIOTriConsumerOfTUVTUV() {
110 assertThrows(UncheckedIOException.class, () -> Uncheck.accept((t, u, v) -> {
111 throw new IOException();
112 }, null, null, null));
113 assertThrows(UncheckedIOException.class, () -> Uncheck.accept(TestConstants.THROWING_IO_TRI_CONSUMER, null, null, null));
114 Uncheck.accept((t, u, v) -> {
115 TestUtils.compareAndSetThrowsIO(ref1, t);
116 TestUtils.compareAndSetThrowsIO(ref2, u);
117 TestUtils.compareAndSetThrowsIO(ref3, v);
118 }, "new1", "new2", "new3");
119 assertEquals("new1", ref1.get());
120 assertEquals("new2", ref2.get());
121 assertEquals("new3", ref3.get());
122 }
123
124
125
126
127 @Test
128 public void testApply1() {
129 final ByteArrayInputStream stream = newInputStream();
130 assertEquals(1, Uncheck.apply(n -> stream.skip(n), 1).intValue());
131 assertEquals('b', Uncheck.get(stream::read).intValue());
132 }
133
134
135
136
137 @Test
138 public void testApply2() {
139 final ByteArrayInputStream stream = newInputStream();
140 final byte[] buf = new byte[BYTES.length];
141 assertEquals(1, Uncheck.apply((o, l) -> stream.read(buf, o, l), 0, 1).intValue());
142 assertEquals('a', buf[0]);
143 }
144
145
146
147
148 @Test
149 public void testApply3() {
150 final ByteArrayInputStream stream = newInputStream();
151 final byte[] buf = new byte[BYTES.length];
152 assertEquals(1, Uncheck.apply((b, o, l) -> stream.read(b, o, l), buf, 0, 1).intValue());
153 assertEquals('a', buf[0]);
154 }
155
156 @Test
157 public void testApplyIOBiFunctionOfTURTU() {
158 assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u) -> {
159 throw new IOException();
160 }, null, null));
161 assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_BI_FUNCTION, null, null));
162 assertEquals("new0", Uncheck.apply((t, u) -> {
163 TestUtils.compareAndSetThrowsIO(ref1, t);
164 TestUtils.compareAndSetThrowsIO(ref2, u);
165 return "new0";
166 }, "new1", "new2"));
167 assertEquals("new1", ref1.get());
168 assertEquals("new2", ref2.get());
169 }
170
171 @Test
172 public void testApplyIOFunctionOfTRT() {
173 assertThrows(UncheckedIOException.class, () -> Uncheck.apply(t -> {
174 throw new IOException();
175 }, null));
176 assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_FUNCTION, null));
177 Uncheck.apply(t -> TestUtils.compareAndSetThrowsIO(ref1, t), "new1");
178 assertEquals("new1", ref1.get());
179 }
180
181 @Test
182 public void testApplyIOQuadFunctionOfTUVWRTUVW() {
183 assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u, v, w) -> {
184 throw new IOException();
185 }, null, null, null, null));
186 assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_QUAD_FUNCTION, null, null, null, null));
187 assertEquals("new0", Uncheck.apply((t, u, v, w) -> {
188 TestUtils.compareAndSetThrowsIO(ref1, t);
189 TestUtils.compareAndSetThrowsIO(ref2, u);
190 TestUtils.compareAndSetThrowsIO(ref3, v);
191 TestUtils.compareAndSetThrowsIO(ref4, w);
192 return "new0";
193 }, "new1", "new2", "new3", "new4"));
194 assertEquals("new1", ref1.get());
195 assertEquals("new2", ref2.get());
196 assertEquals("new3", ref3.get());
197 assertEquals("new4", ref4.get());
198 }
199
200 @Test
201 public void testApplyIOTriFunctionOfTUVRTUV() {
202 assertThrows(UncheckedIOException.class, () -> Uncheck.apply((t, u, v) -> {
203 throw new IOException();
204 }, null, null, null));
205 assertThrows(UncheckedIOException.class, () -> Uncheck.apply(TestConstants.THROWING_IO_TRI_FUNCTION, null, null, null));
206 assertEquals("new0", Uncheck.apply((t, u, v) -> {
207 TestUtils.compareAndSetThrowsIO(ref1, t);
208 TestUtils.compareAndSetThrowsIO(ref2, u);
209 TestUtils.compareAndSetThrowsIO(ref3, v);
210 return "new0";
211 }, "new1", "new2", "new3"));
212 assertEquals("new1", ref1.get());
213 assertEquals("new2", ref2.get());
214 assertEquals("new3", ref3.get());
215 }
216
217
218
219
220 @Test
221 public void testGet() {
222 assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
223 assertThrows(UncheckedIOException.class, () -> Uncheck.get(() -> {
224 throw new IOException();
225 }));
226 assertThrows(UncheckedIOException.class, () -> Uncheck.get(TestConstants.THROWING_IO_SUPPLIER));
227 assertEquals("new1", Uncheck.get(() -> TestUtils.compareAndSetThrowsIO(ref1, "new1")));
228 assertEquals("new1", ref1.get());
229 }
230
231 @Test
232 public void testGetAsInt() {
233 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
234 throw new IOException();
235 }));
236 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER));
237 assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
238 assertEquals(1, atomicInt.get());
239 }
240
241 @Test
242 public void testGetAsIntMessage() {
243
244 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
245 throw new IOException();
246 }, () -> CUSTOM_MESSAGE));
247 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER, () -> CUSTOM_MESSAGE));
248 assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1), () -> CUSTOM_MESSAGE));
249 assertEquals(1, atomicInt.get());
250
251 final IOException expected = new IOException(CAUSE_MESSAGE);
252 try {
253 Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
254 fail();
255 } catch (final UncheckedIOException e) {
256 assertUncheckedIOException(expected, e);
257 }
258 }
259
260 @Test
261 public void testGetAsLong() {
262 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
263 throw new IOException();
264 }));
265 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(TestConstants.THROWING_IO_LONG_SUPPLIER));
266 assertEquals(1L, Uncheck.getAsLong(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L)));
267 assertEquals(1L, atomicLong.get());
268 }
269
270 @Test
271 public void testGetAsLongMessage() {
272
273 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
274 throw new IOException();
275 }, () -> CUSTOM_MESSAGE));
276 assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(TestConstants.THROWING_IO_LONG_SUPPLIER, () -> CUSTOM_MESSAGE));
277 assertEquals(1L, Uncheck.getAsLong(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L), () -> CUSTOM_MESSAGE));
278 assertEquals(1L, atomicLong.get());
279
280 final IOException expected = new IOException(CAUSE_MESSAGE);
281 try {
282 Uncheck.getAsLong(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
283 fail();
284 } catch (final UncheckedIOException e) {
285 assertUncheckedIOException(expected, e);
286 }
287 }
288
289
290
291
292 @Test
293 public void testGetMessage() {
294
295 assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
296
297 final IOException expected = new IOException(CAUSE_MESSAGE);
298 try {
299 Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
300 fail();
301 } catch (final UncheckedIOException e) {
302 assertUncheckedIOException(expected, e);
303 }
304 }
305
306
307
308
309 @Test
310 public void testRun() {
311 final ByteArrayInputStream stream = newInputStream();
312 Uncheck.run(() -> stream.skip(1));
313 assertEquals('b', Uncheck.get(stream::read).intValue());
314
315 assertThrows(UncheckedIOException.class, () -> Uncheck.run(() -> {
316 throw new IOException();
317 }));
318 assertThrows(UncheckedIOException.class, () -> Uncheck.run(TestConstants.THROWING_IO_RUNNABLE));
319 Uncheck.run(() -> TestUtils.compareAndSetThrowsIO(ref1, "new1"));
320 assertEquals("new1", ref1.get());
321 }
322
323
324
325
326
327
328 @Test
329 public void testRunMessage() throws IOException {
330
331 final ByteArrayInputStream stream = newInputStream();
332 Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
333 assertEquals('b', Uncheck.get(stream::read).intValue());
334 final IOException expected = new IOException(CAUSE_MESSAGE);
335
336 try {
337 Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
338 fail();
339 } catch (final UncheckedIOException e) {
340 assertUncheckedIOException(expected, e);
341 }
342 }
343
344 @Test
345 public void testTest() {
346 assertThrows(UncheckedIOException.class, () -> Uncheck.test(t -> {
347 throw new IOException();
348 }, null));
349 assertThrows(UncheckedIOException.class, () -> Uncheck.test(TestConstants.THROWING_IO_PREDICATE, null));
350 assertTrue(Uncheck.test(t -> TestUtils.compareAndSetThrowsIO(ref1, t).equals(t), "new1"));
351 assertEquals("new1", ref1.get());
352 }
353
354 }