Skip to content

Commit

Permalink
Preserve eliminating nulls from instances
Browse files Browse the repository at this point in the history
A little bit unfortunate and assuming, but it is existing behavior and
must be preserved to avoid breaking stuff.
  • Loading branch information
runeflobakk committed Sep 26, 2024
1 parent b4e2b41 commit ba2f5fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/no/digipost/DiggBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.Consumer;
Expand Down Expand Up @@ -247,7 +248,9 @@ public static <T> Stream<Exception> forceOnAll(ThrowingConsumer<? super T, ? ext
* @return the Stream with exceptions, if any
*/
public static <T> Stream<Exception> forceOnAll(ThrowingConsumer<? super T, ? extends Exception> action, Stream<T> instances) {
return StreamSupport.stream(new FlatMapToExceptionSpliterator<>(action, instances.spliterator()), instances.isParallel());
return StreamSupport.stream(
new FlatMapToExceptionSpliterator<>(action, instances.filter(Objects::nonNull).spliterator()),
instances.isParallel());
}

private static final class FlatMapToExceptionSpliterator<W> implements Spliterator<Exception> {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/no/digipost/DiggBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.quicktheories.WithQuickTheories;
import org.quicktheories.core.Gen;
import org.quicktheories.dsl.TheoryBuilder;
Expand All @@ -45,6 +48,7 @@
import static no.digipost.DiggBase.friendlyName;
import static no.digipost.DiggBase.nonNull;
import static no.digipost.DiggBase.throwingAutoClose;
import static no.digipost.DiggCollectors.toSingleExceptionWithSuppressed;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -55,6 +59,7 @@
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.doNothing;
Expand All @@ -66,6 +71,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static uk.co.probablyfine.matchers.Java8Matchers.where;

@ExtendWith(MockitoExtension.class)
public class DiggBaseTest implements WithQuickTheories {

@Test
Expand Down Expand Up @@ -277,6 +283,16 @@ void worksWithParalellStreams() {
failures * 3, is(successes.get()));
}

@Test
void doesNotInvokeOperationOnNulls(@Mock AutoCloseable resource) throws Exception {
Optional<RuntimeException> exception = forceOnAll(AutoCloseable::close, resource, null, resource, null, resource)
.collect(toSingleExceptionWithSuppressed())
.map(DiggExceptions::asUnchecked);
assertAll(
() -> verify(resource, times(3)).close(),
() -> assertDoesNotThrow(() -> exception.ifPresent(e -> { throw e; })));
}

}

}
Expand Down

0 comments on commit ba2f5fe

Please sign in to comment.