From 5bf002e0fdf017c2e1b4384eadde496982fd1760 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Fri, 25 Aug 2023 10:35:47 -0700 Subject: [PATCH] BAEL-6865, taking care of review comments as part of PR-14653 --- .../baeldung/aggregateEx/CustomMapper.java | 6 +- .../AggregateExceptionHandlerUnitTest.java | 71 ++++++++++--------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/aggregateEx/CustomMapper.java b/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/aggregateEx/CustomMapper.java index 114a812fdc..47b433a132 100644 --- a/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/aggregateEx/CustomMapper.java +++ b/core-java-modules/core-java-streams-5/src/main/java/com/baeldung/aggregateEx/CustomMapper.java @@ -7,13 +7,11 @@ import java.util.function.Function; public class CustomMapper { public static Function> mapper(Function func) { return arg -> { - Result result; try { - result = new Result(func.apply(arg)); + return new Result(func.apply(arg)); } catch (Exception e) { - result = new Result(e); + return new Result(e); } - return result; }; } } diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/aggregateEx/AggregateExceptionHandlerUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/aggregateEx/AggregateExceptionHandlerUnitTest.java index 952ec376ac..dff9dafbb7 100644 --- a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/aggregateEx/AggregateExceptionHandlerUnitTest.java +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/aggregateEx/AggregateExceptionHandlerUnitTest.java @@ -2,9 +2,6 @@ package com.baeldung.aggregateEx; import com.baeldung.aggregateEx.entity.ExceptionAggregator; import com.baeldung.aggregateEx.entity.Result; - -import static org.junit.Assert.*; - import io.vavr.control.Either; import io.vavr.control.Try; import org.junit.Test; @@ -15,6 +12,8 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import static org.junit.Assert.*; + public class AggregateExceptionHandlerUnitTest { private static RuntimeException process(String str) { @@ -28,14 +27,14 @@ public class AggregateExceptionHandlerUnitTest { private static Object transform(String str) { try { - return (Integer.parseInt(str)); + return Integer.parseInt(str); } catch (NumberFormatException e) { return new RuntimeException(e); } } @Test - public void givenExtractedMethod_whenFoundNonInt_thenAggregateException() { + public void givenExtractedMethod_whenFoundNonInt_thenSuppressExIntoRuntimeEx() { String[] strings = {"1", "2", "3", "a", "b", "c"}; RuntimeException runEx = Arrays.stream(strings) .map(AggregateExceptionHandlerUnitTest::process) @@ -49,7 +48,7 @@ public class AggregateExceptionHandlerUnitTest { } @Test - public void givenTryCatchInPipeline_whenFoundNonInts_thenAggregateException() { + public void givenTryCatchInPipeline_whenFoundNonInts_thenSuppressExIntoRuntimeEx() { String[] strings = {"1", "2", "3", "a", "b", "c"}; RuntimeException runEx = Arrays.stream(strings) .map(str -> { @@ -70,40 +69,44 @@ public class AggregateExceptionHandlerUnitTest { } @Test - public void whenFoundNonInts_thenAggregateExceptionAndReturnOutput() { + public void givenExtractedMethodReturnOutputAndEx_whenFoundNonInts_thenSuppressExIntoRuntimeEx() { String[] strings = {"1", "2", "3", "a", "b", "c"}; Map resultMap = Arrays.stream(strings) .map(AggregateExceptionHandlerUnitTest::transform) .collect(Collectors.partitioningBy(o -> o instanceof RuntimeException, Collectors.toList())); + RuntimeException ex = null; - if (resultMap.containsKey(Boolean.TRUE)) { - List exs = (List) resultMap.get(Boolean.TRUE); - ex = exs.stream() - .reduce( - new RuntimeException("Errors Occurred"), (o1, o2) -> { - o1.addSuppressed(o2); - return o1; - }); - } + + assertTrue(resultMap.containsKey(Boolean.TRUE)); + + List exs = (List) resultMap.get(Boolean.TRUE); + ex = exs.stream() + .reduce( + new RuntimeException("Errors Occurred"), (o1, o2) -> { + o1.addSuppressed(o2); + return o1; + }); + assertEquals("Errors Occurred", ex.getMessage()); assertEquals(3, ex.getSuppressed().length); } @Test - public void givenWrapFunction_whenFoundNonInts_thenAggregateException() throws ExceptionAggregator { + public void givenWrapFunction_whenFoundNonInts_thenUseExAggregator() throws ExceptionAggregator { String[] strings = {"1", "2", "3", "a", "b", "c"}; Map>> resultmap = Arrays.stream(strings) .map(CustomMapper.mapper(Integer::parseInt)) .collect(Collectors.partitioningBy(r -> r.getException().isEmpty(), Collectors.toList())); - if (resultmap.containsKey(Boolean.FALSE)) { - List> resultList = resultmap.get(Boolean.FALSE); - List exceptionList = resultList.stream() - .map(opex -> opex.getException().get()) - .collect(Collectors.toList()); + assertTrue(resultmap.containsKey(Boolean.TRUE)); + + List> resultList = resultmap.get(Boolean.FALSE); + List exceptionList = resultList.stream() + .map(opex -> opex.getException().get()) + .collect(Collectors.toList()); + + assertThrows(ExceptionAggregator.class, () -> handleExceptions(exceptionList)); - assertThrows(ExceptionAggregator.class, () -> handleExceptions(exceptionList)); - } } private void handleExceptions(List exceptions) throws ExceptionAggregator { @@ -113,7 +116,7 @@ public class AggregateExceptionHandlerUnitTest { } @Test - public void givenExCollector_whenFoundNonInts_thenAggregateException() throws ExceptionAggregator { + public void givenExCollector_whenFoundNonInts_thenAggregateExInCustomCollector() throws ExceptionAggregator { String[] strings = {"1", "2", "3", "a", "b", "c"}; ExceptionCollector exCollector = Arrays.stream(strings) .collect(ExceptionCollector.of(Integer::parseInt)); @@ -123,21 +126,21 @@ public class AggregateExceptionHandlerUnitTest { } private static Either processAndReturnEither(String str) { - Either either = null; try { - either = Either.right(Integer.parseInt(str)); + return Either.right(Integer.parseInt(str)); } catch (NumberFormatException e) { - either = Either.left(new RuntimeException(e)); + return Either.left(new RuntimeException(e)); } - return either; } @Test - public void givenVavrEither_whenFoundNonInts_thenAggregateException() { + public void givenVavrEither_whenFoundNonInts_thenSuppressExIntoRuntimeEx() { List strings = List.of("1", "2", "3", "a", "b", "c"); Map>> map = strings.stream() .map(str -> processAndReturnEither(str)) - .collect(Collectors.partitioningBy((t) -> t.isLeft(), Collectors.toList())); + .collect(Collectors.partitioningBy(t -> t.isLeft(), Collectors.toList())); + + assertTrue(map.containsKey(Boolean.TRUE)); RuntimeException runEx = map.get(Boolean.TRUE) .stream().map(either -> either.getLeft()) @@ -149,11 +152,11 @@ public class AggregateExceptionHandlerUnitTest { } @Test - public void givenVavrTry_whenFoundNonInts_thenAggregateException() { + public void givenVavrTry_whenFoundNonInts_thenSuppressExIntoRuntimeEx() { List strings = List.of("1", "2", "3", "a", "b", "c"); Map>> map = strings.stream() .map(str -> Try.of(() -> Integer.parseInt(str))) - .collect(Collectors.partitioningBy((t) -> t.isFailure(), Collectors.toList())); + .collect(Collectors.partitioningBy(t -> t.isFailure(), Collectors.toList())); Throwable runEx = map.get(Boolean.TRUE).stream() .map(t -> t.getCause()) .reduce(new RuntimeException("Errors Occurred"), (o1, o2) -> { @@ -164,7 +167,7 @@ public class AggregateExceptionHandlerUnitTest { } @Test - public void givenVavrEitherAndTry_whenFoundNonInts_thenAggregateException() { + public void givenVavrEitherAndTry_whenFoundNonInts_thenSuppressExIntoRuntimeEx() { List strings = List.of("1", "2", "3", "a", "b", "c"); Map>> map = strings.stream() .map(str -> Try.of(() -> Integer.parseInt(str)).toEither())