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.assertEquals;
21  
22  import java.io.IOException;
23  import java.math.BigInteger;
24  import java.util.concurrent.atomic.AtomicReference;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link IOTriFunction}.
30   */
31  public class IOTriFunctionTest {
32  
33      /**
34       * Tests {@link IOTriFunction#apply(Object, Object, Object)}.
35       *
36       * @throws IOException thrown on test failure
37       */
38      @Test
39      public void testAccept() throws IOException {
40          final AtomicReference<Character> ref1 = new AtomicReference<>();
41          final AtomicReference<Short> ref2 = new AtomicReference<>();
42          final AtomicReference<String> ref3 = new AtomicReference<>();
43          final IOTriFunction<AtomicReference<Character>, AtomicReference<Short>, AtomicReference<String>, String> tri = (t, u, v) -> {
44              ref1.set(Character.valueOf('a'));
45              ref2.set(Short.valueOf((short) 1));
46              ref3.set("z");
47              return "ABC";
48          };
49          assertEquals("ABC", tri.apply(ref1, ref2, ref3));
50          assertEquals(Character.valueOf('a'), ref1.get());
51          assertEquals(Short.valueOf((short) 1), ref2.get());
52          assertEquals("z", ref3.get());
53      }
54  
55      /**
56       * Tests {@link IOTriFunction#andThen(IOFunction)}.
57       *
58       * @throws IOException thrown on test failure
59       */
60      @Test
61      public void testAndThenIOFunction() throws IOException {
62          final AtomicReference<Character> ref1 = new AtomicReference<>();
63          final AtomicReference<Short> ref2 = new AtomicReference<>();
64          final AtomicReference<String> ref3 = new AtomicReference<>();
65          final IOTriFunction<AtomicReference<Character>, AtomicReference<Short>, AtomicReference<String>, String> tri = (t, u, v) -> {
66              ref1.set(Character.valueOf('a'));
67              ref2.set(Short.valueOf((short) 1));
68              ref3.set("z");
69              return "9";
70          };
71          final IOFunction<String, BigInteger> after = t -> {
72              ref1.set(Character.valueOf('b'));
73              ref2.set(Short.valueOf((short) 2));
74              ref3.set("zz");
75              return BigInteger.valueOf(Long.parseLong(t)).add(BigInteger.ONE);
76          };
77          assertEquals(BigInteger.TEN, tri.andThen(after).apply(ref1, ref2, ref3));
78          assertEquals(Character.valueOf('b'), ref1.get());
79          assertEquals(Short.valueOf((short) 2), ref2.get());
80          assertEquals("zz", ref3.get());
81      }
82  
83  }