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  package org.apache.commons.lang3;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.core.Is.is;
21  import static org.hamcrest.core.IsEqual.equalTo;
22  import static org.hamcrest.core.IsNull.nullValue;
23  import static org.junit.jupiter.api.Assertions.assertAll;
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  import static org.junit.jupiter.api.Assertions.assertNotNull;
26  import static org.junit.jupiter.api.Assertions.assertThrows;
27  import static org.junit.jupiter.api.DynamicTest.dynamicTest;
28  
29  import java.lang.reflect.UndeclaredThrowableException;
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  import java.util.List;
33  import java.util.stream.Collectors;
34  import java.util.stream.Stream;
35  
36  import org.apache.commons.lang3.Functions.FailableConsumer;
37  import org.apache.commons.lang3.Functions.FailablePredicate;
38  import org.junit.jupiter.api.DynamicTest;
39  import org.junit.jupiter.api.Test;
40  import org.junit.jupiter.api.TestFactory;
41  import org.junit.jupiter.api.function.Executable;
42  import org.xml.sax.SAXException;
43  
44  public class StreamsTest extends AbstractLangTest {
45  
46      protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(final T pThrowable) {
47          return s -> {
48              final int i = Integer.parseInt(s);
49              if (i == 4) {
50                  throw pThrowable;
51              }
52          };
53      }
54  
55      protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(final T pThrowable) {
56          return i -> {
57              if (i.intValue() == 5 && pThrowable != null) {
58                  throw pThrowable;
59              }
60              return i % 2 == 0;
61          };
62      }
63  
64      private void assertEvenNumbers(final List<Integer> output) {
65          assertEquals(3, output.size());
66          for (int i = 0; i < 3; i++) {
67              assertEquals((i + 1) * 2, output.get(i).intValue());
68          }
69      }
70  
71      @TestFactory
72      public Stream<DynamicTest> simpleStreamFilterFailing() {
73          final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
74          final List<Integer> output = Functions.stream(input)
75                  .map(Integer::valueOf)
76                  .filter(asIntPredicate(null))
77                  .collect(Collectors.toList());
78          assertEvenNumbers(output);
79          return Stream.of(
80                  dynamicTest("IllegalArgumentException", () -> {
81                      final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5);
82                      final Executable testMethod = () -> Functions.stream(input)
83                              .map(Integer::valueOf)
84                              .filter(asIntPredicate(iae))
85                              .collect(Collectors.toList());
86                      final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod);
87                      assertThat(thrown.getMessage(), is(equalTo("Invalid argument: " + 5)));
88                  }),
89                  dynamicTest("OutOfMemoryError", () -> {
90                      final OutOfMemoryError oome = new OutOfMemoryError();
91                      final Executable testMethod = () -> Functions.stream(input)
92                              .map(Integer::valueOf)
93                              .filter(asIntPredicate(oome))
94                              .collect(Collectors.toList());
95                      final OutOfMemoryError thrown = assertThrows(OutOfMemoryError.class, testMethod);
96                      assertThat(thrown.getMessage(), is(nullValue()));
97                  }),
98                  dynamicTest("SAXException", () -> {
99                      final SAXException se = new SAXException();
100                     final Executable testMethod = () -> Functions.stream(input)
101                             .map(Integer::valueOf)
102                             .filter(asIntPredicate(se))
103                             .collect(Collectors.toList());
104                     final UndeclaredThrowableException thrown = assertThrows(UndeclaredThrowableException.class, testMethod);
105                     assertAll(
106                             () -> assertThat(thrown.getMessage(), is(nullValue())),
107                             () -> assertThat(thrown.getCause(), is(equalTo(se)))
108                     );
109                 })
110         );
111     }
112 
113     @TestFactory
114     public Stream<DynamicTest> simpleStreamForEachFailing() {
115         final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
116         return Stream.of(
117                 dynamicTest("IllegalArgumentException", () -> {
118                     final IllegalArgumentException ise = new IllegalArgumentException();
119                     final Executable testMethod = () -> Functions.stream(input).forEach(asIntConsumer(ise));
120                     final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod);
121                     assertThat(thrown.getMessage(), is(nullValue()));
122                 }),
123                 dynamicTest("OutOfMemoryError", () -> {
124                     final OutOfMemoryError oome = new OutOfMemoryError();
125                     final Executable oomeTestMethod = () -> Functions.stream(input).forEach(asIntConsumer(oome));
126                     final OutOfMemoryError oomeThrown = assertThrows(OutOfMemoryError.class, oomeTestMethod);
127                     assertThat(oomeThrown.getMessage(), is(nullValue()));
128                 }),
129                 dynamicTest("SAXException", () -> {
130                     final SAXException se = new SAXException();
131                     final Executable seTestMethod = () -> Functions.stream(input).forEach(asIntConsumer(se));
132                     final UndeclaredThrowableException seThrown = assertThrows(UndeclaredThrowableException.class, seTestMethod);
133                     assertAll(
134                             () -> assertThat(seThrown.getMessage(), is(nullValue())),
135                             () -> assertThat(seThrown.getCause(), is(equalTo(se)))
136                     );
137                 })
138         );
139     }
140 
141     @Test
142     public void testSimpleStreamFilter() {
143         final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
144         final List<Integer> output = Functions.stream(input)
145                 .map(Integer::valueOf)
146                 .filter(i -> (i.intValue() % 2 == 0))
147                 .collect(Collectors.toList());
148         assertEvenNumbers(output);
149     }
150 
151     @Test
152     public void testSimpleStreamForEach() {
153         final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
154         final List<Integer> output = new ArrayList<>();
155         Functions.stream(input).forEach(s -> output.add(Integer.valueOf(s)));
156         assertEquals(6, output.size());
157         for (int i = 0; i < 6; i++) {
158             assertEquals(i + 1, output.get(i).intValue());
159         }
160     }
161 
162     @Test
163     public void testSimpleStreamMap() {
164         final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
165         final List<Integer> output = Functions.stream(input).map(Integer::valueOf).collect(Collectors.toList());
166         assertEquals(6, output.size());
167         for (int i = 0; i < 6; i++) {
168             assertEquals(i + 1, output.get(i).intValue());
169         }
170     }
171 
172     @Test
173     public void testSimpleStreamMapFailing() {
174         final List<String> input = Arrays.asList("1", "2", "3", "4 ", "5", "6");
175         final Executable testMethod = () -> Functions.stream(input).map(Integer::valueOf).collect(Collectors.toList());
176         final NumberFormatException thrown = assertThrows(NumberFormatException.class, testMethod);
177         assertEquals("For input string: \"4 \"", thrown.getMessage());
178     }
179 
180     @Test
181     public void testToArray() {
182         final String[] array = Arrays.asList("2", "3", "1").stream().collect(Streams.toArray(String.class));
183         assertNotNull(array);
184         assertEquals(3, array.length);
185         assertEquals("2", array[0]);
186         assertEquals("3", array[1]);
187         assertEquals("1", array[2]);
188     }
189 
190 }