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