|
| 1 | +/* |
| 2 | + * Copyright 2022-2025 Talsma ICT |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package nl.talsmasoftware.misc.utils; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Collector; |
| 27 | +import java.util.stream.IntStream; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import static java.util.stream.Collectors.toCollection; |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 33 | + |
| 34 | +class StreamUtilsTest { |
| 35 | + @Test |
| 36 | + @SuppressWarnings({ |
| 37 | + "java:S1874" // We use java8, where canAccess is not yet available to replace isAccessible. |
| 38 | + }) |
| 39 | + void streamUtils_utility_class_has_unsupported_constructor() throws ReflectiveOperationException { |
| 40 | + Constructor<StreamUtils> constructor = StreamUtils.class.getDeclaredConstructor(); |
| 41 | + assertThat(constructor.isAccessible()).isFalse(); |
| 42 | + constructor.setAccessible(true); |
| 43 | + assertThatThrownBy(constructor::newInstance) |
| 44 | + .isInstanceOf(InvocationTargetException.class) |
| 45 | + .cause() |
| 46 | + .isInstanceOf(UnsupportedOperationException.class) |
| 47 | + .hasMessage("This is a utility class and cannot be instantiated."); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void streamNullable_null_returns_empty_stream() { |
| 52 | + // when |
| 53 | + Stream<?> result = StreamUtils.streamNullable(null); |
| 54 | + |
| 55 | + // then |
| 56 | + assertThat(result).isNotNull().isEmpty(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void streamNullable_empty_returns_empty_stream() { |
| 61 | + // when |
| 62 | + Stream<?> result = StreamUtils.streamNullable(Collections.emptyList()); |
| 63 | + |
| 64 | + // then |
| 65 | + assertThat(result).isNotNull().isEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void streamNullable_empty_Iterable_returns_empty_stream() { |
| 70 | + // given |
| 71 | + Iterable<?> emptyIterable = Collections::emptyIterator; |
| 72 | + |
| 73 | + // when |
| 74 | + Stream<?> result = StreamUtils.streamNullable(emptyIterable); |
| 75 | + |
| 76 | + // then |
| 77 | + assertThat(result).isNotNull().isEmpty(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void streamNullable_returns_stream_from_collection() { |
| 82 | + // when |
| 83 | + Stream<Integer> result = StreamUtils.streamNullable(Arrays.asList(1, null, 2, null, 3)); |
| 84 | + |
| 85 | + // then |
| 86 | + assertThat(result).isNotNull().isNotEmpty().hasSize(3).containsExactly(1, 2, 3); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void streamNullable_returns_stream_from_iterable() { |
| 91 | + // given |
| 92 | + Iterable<Integer> iterable = Arrays.asList(1, null, 2, null, 3)::iterator; |
| 93 | + |
| 94 | + // when |
| 95 | + Stream<Integer> result = StreamUtils.streamNullable(iterable); |
| 96 | + |
| 97 | + // then |
| 98 | + assertThat(result).isNotNull().isNotEmpty().hasSize(3).containsExactly(1, 2, 3); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void last_count_must_be_positive() { |
| 103 | + assertThatThrownBy(() -> StreamUtils.last(0)) |
| 104 | + .isInstanceOf(IllegalArgumentException.class) |
| 105 | + .hasMessage("Maximum size must be a positive number."); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void last_2_of_10() { |
| 110 | + // given |
| 111 | + Stream<String> stream = IntStream.range(1, 11).mapToObj(Integer::toString); |
| 112 | + |
| 113 | + // when |
| 114 | + List<String> result = stream.collect(StreamUtils.last(2)); |
| 115 | + |
| 116 | + // then |
| 117 | + assertThat(result).hasSize(2).containsExactly("9", "10"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void last_10_of_2() { |
| 122 | + // given |
| 123 | + Stream<String> stream = Stream.of("1", "2"); |
| 124 | + |
| 125 | + // when |
| 126 | + List<String> result = stream.collect(StreamUtils.last(10)); |
| 127 | + |
| 128 | + // then |
| 129 | + assertThat(result).hasSize(2).containsExactly("1", "2"); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void last_10_of_10() { |
| 134 | + // given |
| 135 | + Stream<String> stream = IntStream.range(1, 11).mapToObj(Integer::toString); |
| 136 | + |
| 137 | + // when |
| 138 | + List<String> result = stream.collect(StreamUtils.last(10)); |
| 139 | + |
| 140 | + // then |
| 141 | + assertThat(result).hasSize(10).containsExactly("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void last_combiner_acc2_is_full() { |
| 146 | + // given |
| 147 | + Collector<String, ArrayList<String>, List<String>> collector = StreamUtils.last(10); |
| 148 | + ArrayList<String> acc1 = IntStream.range(1, 11).mapToObj(Integer::toString).collect(toCollection(ArrayList::new)); |
| 149 | + ArrayList<String> acc2 = IntStream.range(11, 21).mapToObj(Integer::toString).collect(toCollection(ArrayList::new)); |
| 150 | + |
| 151 | + // when |
| 152 | + List<String> result = collector.combiner().apply(acc1, acc2); |
| 153 | + |
| 154 | + // then |
| 155 | + assertThat(result) |
| 156 | + .hasSize(10) |
| 157 | + .containsExactly("11", "12", "13", "14", "15", "16", "17", "18", "19", "20") |
| 158 | + .isSameAs(acc2); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void last_combiner_acc2_has_some_space() { |
| 163 | + // given |
| 164 | + Collector<String, ArrayList<String>, List<String>> collector = StreamUtils.last(10); |
| 165 | + ArrayList<String> acc1 = IntStream.range(1, 11).mapToObj(Integer::toString).collect(toCollection(ArrayList::new)); |
| 166 | + ArrayList<String> acc2 = IntStream.range(11, 16).mapToObj(Integer::toString).collect(toCollection(ArrayList::new)); |
| 167 | + |
| 168 | + // when |
| 169 | + List<String> result = collector.combiner().apply(acc1, acc2); |
| 170 | + |
| 171 | + // then |
| 172 | + assertThat(result) |
| 173 | + .hasSize(10) |
| 174 | + .containsExactly("6", "7", "8", "9", "10", "11", "12", "13", "14", "15") |
| 175 | + .isSameAs(acc2); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + void last_combiner_acc1_and_acc2_both_fit() { |
| 180 | + // given |
| 181 | + Collector<String, ArrayList<String>, List<String>> collector = StreamUtils.last(10); |
| 182 | + ArrayList<String> acc1 = IntStream.range(1, 5).mapToObj(Integer::toString).collect(toCollection(ArrayList::new)); |
| 183 | + ArrayList<String> acc2 = IntStream.range(5, 11).mapToObj(Integer::toString).collect(toCollection(ArrayList::new)); |
| 184 | + |
| 185 | + // when |
| 186 | + List<String> result = collector.combiner().apply(acc1, acc2); |
| 187 | + |
| 188 | + // then |
| 189 | + assertThat(result) |
| 190 | + .hasSize(10) |
| 191 | + .containsExactly("1", "2", "3", "4", "5", "6", "7", "8", "9", "10") |
| 192 | + .isSameAs(acc2); |
| 193 | + } |
| 194 | +} |
0 commit comments