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.assertNotEquals;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.IOException;
25 import java.io.UncheckedIOException;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 public class IOIntSupplierTest {
35
36 private AtomicInteger atomicInt;
37
38 private int getThrowsIO(final IOIntSupplier supplier) throws IOException {
39 return supplier.getAsInt();
40 }
41
42 private int getThrowsNone(final IOIntSupplier supplier) {
43 return supplier.asIntSupplier().getAsInt();
44 }
45
46 @BeforeEach
47 public void initEach() {
48 atomicInt = new AtomicInteger();
49 }
50
51 @Test
52 public void testAsSupplier() {
53 assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier().getAsInt());
54 assertEquals(1, getThrowsNone(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
55 assertEquals(1, atomicInt.get());
56 assertNotEquals(TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier(), TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier());
57 }
58
59 @Test
60 public void testGet() throws IOException {
61 assertThrows(IOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.getAsInt());
62 assertThrows(IOException.class, () -> {
63 throw new IOException();
64 });
65 assertEquals(1, getThrowsIO(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
66 assertEquals(1, atomicInt.get());
67 }
68
69 }