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    *      https://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.lang3.stream;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.LinkedHashMap;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.concurrent.atomic.AtomicInteger;
29  import java.util.stream.Collectors;
30  
31  import org.apache.commons.lang3.stream.Streams.FailableStream;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests {@link FailableStream}.
36   */
37  class FailableStreamTest {
38  
39      private Integer failable(final Map.Entry<String, AtomicInteger> value) throws IOException {
40          if (value == new Object()) {
41              throw new IOException();
42          }
43          return Integer.valueOf(value.getValue().incrementAndGet());
44      }
45  
46      private String failable(final String value) throws IOException {
47          if (value == new Object()) {
48              throw new IOException();
49          }
50          return value.toLowerCase(Locale.ROOT);
51      }
52  
53      @Test
54      void testFailableStreamOfArray() {
55          assertArrayEquals(new String[] {}, toArray());
56          assertArrayEquals(new String[] { "a" }, toArray("A"));
57          assertArrayEquals(new String[] { "a", "b" }, toArray("A", "B"));
58          assertArrayEquals(new String[] { "a", "b", "c" }, toArray("A", "B", "C"));
59      }
60  
61      @Test
62      void testFailableStreamOfCollection() {
63          assertArrayEquals(new String[] {}, toArray());
64          assertArrayEquals(new String[] { "a" }, toArray(Arrays.asList("A")));
65          assertArrayEquals(new String[] { "a", "b" }, toArray(Arrays.asList("A", "B")));
66          assertArrayEquals(new String[] { "a", "b", "c" }, toArray(Arrays.asList("A", "B", "C")));
67      }
68  
69      @Test
70      void testFailableStreamOfMap() {
71          final Map<String, AtomicInteger> map = new LinkedHashMap<>();
72          assertArrayEquals(new Integer[] {}, toArrayMap(map));
73          map.put("a", new AtomicInteger(1));
74          assertArrayEquals(new Integer[] { 2 }, toArrayMap(map));
75          map.put("b", new AtomicInteger(2));
76          assertArrayEquals(new Integer[] { 3, 3 }, toArrayMap(map));
77          map.put("c", new AtomicInteger(3));
78          assertArrayEquals(new Integer[] { 4, 4, 4 }, toArrayMap(map));
79      }
80  
81      private String[] toArray(final Collection<String> strings) {
82          return Streams.failableStream(strings).map(this::failable).collect(Collectors.toList()).toArray(new String[0]);
83      }
84  
85      private String[] toArray(final String string) {
86          return Streams.failableStream(string).map(this::failable).collect(Collectors.toList()).toArray(new String[0]);
87      }
88  
89      private String[] toArray(final String... strings) {
90          return Streams.failableStream(strings).map(this::failable).collect(Collectors.toList()).toArray(new String[0]);
91      }
92  
93      private Integer[] toArrayMap(final Map<String, AtomicInteger> map) {
94          return Streams.failableStream(map.entrySet()).map(this::failable).collect(Collectors.toList()).toArray(new Integer[0]);
95      }
96  }