1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
80 return Stream.of(
81
82 dynamicTest("IllegalArgumentException", () -> {
83 final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5);
84 final Executable testMethod = () -> Functions.stream(input)
85 .map(Integer::valueOf)
86 .filter(asIntPredicate(iae))
87 .collect(Collectors.toList());
88 final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod);
89 assertThat(thrown.getMessage(), is(equalTo("Invalid argument: " + 5)));
90 }),
91
92 dynamicTest("OutOfMemoryError", () -> {
93 final OutOfMemoryError oome = new OutOfMemoryError();
94 final Executable testMethod = () -> Functions.stream(input)
95 .map(Integer::valueOf)
96 .filter(asIntPredicate(oome))
97 .collect(Collectors.toList());
98 final OutOfMemoryError thrown = assertThrows(OutOfMemoryError.class, testMethod);
99 assertThat(thrown.getMessage(), is(nullValue()));
100 }),
101
102 dynamicTest("SAXException", () -> {
103 final SAXException se = new SAXException();
104 final Executable testMethod = () -> Functions.stream(input)
105 .map(Integer::valueOf)
106 .filter(asIntPredicate(se))
107 .collect(Collectors.toList());
108 final UndeclaredThrowableException thrown = assertThrows(UndeclaredThrowableException.class, testMethod);
109 assertAll(
110 () -> assertThat(thrown.getMessage(), is(nullValue())),
111 () -> assertThat(thrown.getCause(), is(equalTo(se)))
112 );
113 })
114 );
115 }
116
117 @TestFactory
118 public Stream<DynamicTest> simpleStreamForEachFailing() {
119 final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
120
121 return Stream.of(
122
123 dynamicTest("IllegalArgumentException", () -> {
124 final IllegalArgumentException ise = new IllegalArgumentException();
125 final Executable testMethod = () -> Functions.stream(input)
126 .forEach(asIntConsumer(ise));
127 final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod);
128 assertThat(thrown.getMessage(), is(nullValue()));
129 }),
130
131 dynamicTest("OutOfMemoryError", () -> {
132 final OutOfMemoryError oome = new OutOfMemoryError();
133 final Executable oomeTestMethod = () -> Functions.stream(input)
134 .forEach(asIntConsumer(oome));
135 final OutOfMemoryError oomeThrown = assertThrows(OutOfMemoryError.class, oomeTestMethod);
136 assertThat(oomeThrown.getMessage(), is(nullValue()));
137 }),
138
139 dynamicTest("SAXException", () -> {
140 final SAXException se = new SAXException();
141 final Executable seTestMethod = () -> Functions.stream(input)
142 .forEach(asIntConsumer(se));
143 final UndeclaredThrowableException seThrown = assertThrows(UndeclaredThrowableException.class, seTestMethod);
144 assertAll(
145 () -> assertThat(seThrown.getMessage(), is(nullValue())),
146 () -> assertThat(seThrown.getCause(), is(equalTo(se)))
147 );
148 })
149 );
150 }
151
152 @Test
153 public void testSimpleStreamFilter() {
154 final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
155 final List<Integer> output = Functions.stream(input)
156 .map(Integer::valueOf)
157 .filter(i -> (i.intValue() %2 == 0))
158 .collect(Collectors.toList());
159 assertEvenNumbers(output);
160 }
161
162 @Test
163 public void testSimpleStreamForEach() {
164 final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
165 final List<Integer> output = new ArrayList<>();
166 Functions.stream(input).forEach(s -> output.add(Integer.valueOf(s)));
167 assertEquals(6, output.size());
168 for (int i = 0; i < 6; i++) {
169 assertEquals(i+1, output.get(i).intValue());
170 }
171 }
172
173 @Test
174 public void testSimpleStreamMap() {
175 final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
176 final List<Integer> output = Functions.stream(input).map(Integer::valueOf).collect(Collectors.toList());
177 assertEquals(6, output.size());
178 for (int i = 0; i < 6; i++) {
179 assertEquals(i+1, output.get(i).intValue());
180 }
181 }
182
183 @Test
184 public void testSimpleStreamMapFailing() {
185 final List<String> input = Arrays.asList("1", "2", "3", "4 ", "5", "6");
186 final Executable testMethod = () -> Functions.stream(input).map(Integer::valueOf).collect(Collectors.toList());
187 final NumberFormatException thrown = assertThrows(NumberFormatException.class, testMethod);
188 assertEquals("For input string: \"4 \"", thrown.getMessage());
189 }
190
191 @Test
192 public void testToArray() {
193 final String[] array = Arrays.asList("2", "3", "1").stream().collect(Streams.toArray(String.class));
194 assertNotNull(array);
195 assertEquals(3, array.length);
196 assertEquals("2", array[0]);
197 assertEquals("3", array[1]);
198 assertEquals("1", array[2]);
199 }
200
201 }