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