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  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import java.io.IOException;
25  import java.io.UncheckedIOException;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.util.function.BiFunction;
29  import java.util.function.BinaryOperator;
30  import java.util.stream.Stream;
31  
32  import org.apache.commons.io.file.PathUtils;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests {@link IOBinaryOperator}.
37   */
38  public class IOBinaryOperatorStreamTest {
39  
40      private static final IOBinaryOperator<Path> MIN_BY_IO_BO = IOBinaryOperator.minBy(IOComparatorTest.REAL_PATH_COMP);
41      private static final BinaryOperator<Path> MIN_BY_BO = MIN_BY_IO_BO.asBinaryOperator();
42      private static final IOBinaryOperator<Path> MAX_BY_IO_BO = IOBinaryOperator.maxBy(IOComparatorTest.REAL_PATH_COMP);
43      private static final BinaryOperator<Path> MAX_BY_BO = MAX_BY_IO_BO.asBinaryOperator();
44      private static final IOBinaryOperator<Path> REAL_PATH_IO_BO = (t, u) -> t.toRealPath();
45      private static final BinaryOperator<Path> REAL_PATH_BO = REAL_PATH_IO_BO.asBinaryOperator();
46  
47      @Test
48      public void testAsBinaryOperator() {
49          assertThrows(UncheckedIOException.class,
50              () -> Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(TestUtils.<Path>throwingIOBinaryOperator().asBinaryOperator()).get());
51          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MAX_BY_BO).get());
52          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MIN_BY_BO).get());
53      }
54  
55      /**
56       */
57      @Test
58      public void testMaxBy() {
59          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MAX_BY_BO).get());
60          // in-line lambda ok:
61          final IOBinaryOperator<Path> binIoOp = IOBinaryOperator.maxBy((t, u) -> t.toRealPath().compareTo(u));
62          final BiFunction<Path, Path, Path> asBiFunction = binIoOp.asBiFunction();
63          final BinaryOperator<Path> asBinaryOperator = binIoOp.asBinaryOperator();
64          assertEquals(TestConstants.ABS_PATH_B, asBiFunction.apply(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B));
65          assertEquals(TestConstants.ABS_PATH_B, asBinaryOperator.apply(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B));
66          //
67          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(asBinaryOperator).get());
68          assertEquals(TestConstants.ABS_PATH_B, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B).reduce(asBinaryOperator).get());
69          assertEquals(TestConstants.ABS_PATH_B, Stream.of(TestConstants.ABS_PATH_B, TestConstants.ABS_PATH_A).reduce(asBinaryOperator).get());
70      }
71  
72      @Test
73      public void testMinBy() {
74          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MIN_BY_BO).get());
75          // in-line lambda ok:
76          final IOBinaryOperator<Path> binIoOp = IOBinaryOperator.minBy((t, u) -> t.toRealPath().compareTo(u));
77          final BiFunction<Path, Path, Path> asBiFunction = binIoOp.asBiFunction();
78          final BinaryOperator<Path> asBinaryOperator = binIoOp.asBinaryOperator();
79          assertEquals(TestConstants.ABS_PATH_A, asBiFunction.apply(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B));
80          assertEquals(TestConstants.ABS_PATH_A, asBinaryOperator.apply(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B));
81          //
82          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(asBinaryOperator).get());
83          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B).reduce(asBinaryOperator).get());
84          assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_B, TestConstants.ABS_PATH_A).reduce(asBinaryOperator).get());
85      }
86  
87      @Test
88      public void testReduce() throws IOException {
89          // A silly example to pass in a IOBinaryOperator.
90          final Path current = PathUtils.current();
91          final Path expected;
92          try (Stream<Path> stream = Files.list(current)) {
93              expected = stream.reduce((t, u) -> {
94                  try {
95                      return t.toRealPath();
96                  } catch (final IOException e) {
97                      return fail(e);
98                  }
99              }).get();
100         }
101         try (Stream<Path> stream = Files.list(current)) {
102             assertEquals(expected, stream.reduce(REAL_PATH_BO).get());
103         }
104     }
105 
106 }