View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link IOBiFunction}.
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       * Tests {@link IOBiFunction#andThen(IOFunction)}.
47       *
48       * @throws IOException thrown on test failure
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       * Tests {@link IOBiFunction#apply(Object, Object)}.
61       *
62       * @throws IOException thrown on test failure
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       * Tests {@link IOBiFunction#apply(Object, Object)}.
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  }