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 IOQuadFunction}.
30   */
31  public class IOQuadFunctionTest {
32  
33      /**
34       * Tests {@link IOQuadFunction#apply(Object, Object, Object, Object)}.
35       *
36       * @throws IOException thrown on test failure
37       */
38      @Test
39      public void testAccept() throws IOException {
40          final AtomicReference<Byte> ref1 = new AtomicReference<>();
41          final AtomicReference<Short> ref2 = new AtomicReference<>();
42          final AtomicReference<String> ref3 = new AtomicReference<>();
43          final AtomicReference<Long> ref4 = new AtomicReference<>();
44          final IOQuadFunction<AtomicReference<Byte>, AtomicReference<Short>, AtomicReference<String>, AtomicReference<Long>, String> quad = (t, u, v, w) -> {
45              ref1.set(Byte.valueOf("1"));
46              ref2.set(Short.valueOf((short) 1));
47              ref3.set("z");
48              ref4.set(Long.valueOf(2));
49              return "ABCD";
50          };
51          assertEquals("ABCD", quad.apply(ref1, ref2, ref3, ref4));
52          assertEquals(Byte.valueOf("1"), ref1.get());
53          assertEquals(Short.valueOf((short) 1), ref2.get());
54          assertEquals("z", ref3.get());
55          assertEquals(Long.valueOf(2), ref4.get());
56      }
57  
58      /**
59       * Tests {@link IOTriFunction#andThen(IOFunction)}.
60       *
61       * @throws IOException thrown on test failure
62       */
63      @Test
64      public void testAndThenIOFunction() throws IOException {
65          final AtomicReference<Byte> ref1 = new AtomicReference<>();
66          final AtomicReference<Short> ref2 = new AtomicReference<>();
67          final AtomicReference<String> ref3 = new AtomicReference<>();
68          final AtomicReference<Long> ref4 = new AtomicReference<>();
69          final IOQuadFunction<AtomicReference<Byte>, AtomicReference<Short>, AtomicReference<String>, AtomicReference<Long>, String> quad = (t, u, v, w) -> {
70              ref1.set(Byte.valueOf("1"));
71              ref2.set(Short.valueOf((short) 1));
72              ref3.set("z");
73              ref4.set(Long.valueOf(2));
74              return "9";
75          };
76          final IOFunction<String, BigInteger> after = t -> {
77              ref1.set(Byte.valueOf("2"));
78              ref2.set(Short.valueOf((short) 2));
79              ref3.set("zz");
80              ref4.set(Long.valueOf(3));
81              return BigInteger.valueOf(Long.parseLong(t)).add(BigInteger.ONE);
82          };
83          assertEquals(BigInteger.TEN, quad.andThen(after).apply(ref1, ref2, ref3, ref4));
84          assertEquals(Byte.valueOf("2"), ref1.get());
85          assertEquals(Short.valueOf((short) 2), ref2.get());
86          assertEquals("zz", ref3.get());
87          assertEquals(Long.valueOf(3), ref4.get());
88      }
89  
90  }