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.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotEquals;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.LinkOption;
28 import java.nio.file.Path;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.apache.commons.io.file.PathUtils;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class IOBiFunctionTest {
39
40 @SuppressWarnings("unused")
41 private boolean not(final boolean value) throws IOException {
42 return !value;
43 }
44
45
46
47
48
49
50 @Test
51 public void testAndThenIOFunction() throws IOException {
52 final IOBiFunction<Path, LinkOption[], Boolean> isDirectory = Files::isDirectory;
53 final IOFunction<Boolean, Boolean> not = this::not;
54 assertTrue( isDirectory.apply(PathUtils.current(), PathUtils.EMPTY_LINK_OPTION_ARRAY));
55 final IOBiFunction<Path, LinkOption[], Boolean> andThen = isDirectory.andThen(not);
56 assertFalse(andThen.apply(PathUtils.current(), PathUtils.EMPTY_LINK_OPTION_ARRAY));
57 }
58
59
60
61
62
63
64 @Test
65 public void testApply() throws IOException {
66 final IOBiFunction<Path, LinkOption[], Boolean> isDirectory = Files::isDirectory;
67 assertTrue( isDirectory.apply(PathUtils.current(), PathUtils.EMPTY_LINK_OPTION_ARRAY));
68 }
69
70
71
72
73 @Test
74 public void testApplyThrowsException() {
75 final IOBiFunction<Path, LinkOption[], Boolean> isDirectory = (t, u) -> {
76 throw new IOException("Boom!");
77 };
78 assertThrows(IOException.class, () -> isDirectory.apply(PathUtils.current(), PathUtils.EMPTY_LINK_OPTION_ARRAY));
79 }
80
81 @Test
82 public void testAsBiFunction() {
83 final Map<String, Long> map = new HashMap<>();
84 map.put("1", 0L);
85 final IOBiFunction<String, Long, Long> f = (t, u) -> Files.size(PathUtils.current());
86 map.computeIfPresent("1", f.asBiFunction());
87 assertNotEquals(0L, map.get("1"));
88 }
89
90 }