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