BAEL-6865, taking care of review comments as part of PR-14653
This commit is contained in:
parent
9b1d47ff4e
commit
5bf002e0fd
|
@ -7,13 +7,11 @@ import java.util.function.Function;
|
||||||
public class CustomMapper {
|
public class CustomMapper {
|
||||||
public static <T, R> Function<T, Result<R>> mapper(Function<T, R> func) {
|
public static <T, R> Function<T, Result<R>> mapper(Function<T, R> func) {
|
||||||
return arg -> {
|
return arg -> {
|
||||||
Result<R> result;
|
|
||||||
try {
|
try {
|
||||||
result = new Result(func.apply(arg));
|
return new Result(func.apply(arg));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
result = new Result(e);
|
return new Result(e);
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,6 @@ package com.baeldung.aggregateEx;
|
||||||
|
|
||||||
import com.baeldung.aggregateEx.entity.ExceptionAggregator;
|
import com.baeldung.aggregateEx.entity.ExceptionAggregator;
|
||||||
import com.baeldung.aggregateEx.entity.Result;
|
import com.baeldung.aggregateEx.entity.Result;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import io.vavr.control.Either;
|
import io.vavr.control.Either;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -15,6 +12,8 @@ import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class AggregateExceptionHandlerUnitTest {
|
public class AggregateExceptionHandlerUnitTest {
|
||||||
|
|
||||||
private static RuntimeException process(String str) {
|
private static RuntimeException process(String str) {
|
||||||
|
@ -28,14 +27,14 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
|
|
||||||
private static Object transform(String str) {
|
private static Object transform(String str) {
|
||||||
try {
|
try {
|
||||||
return (Integer.parseInt(str));
|
return Integer.parseInt(str);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return new RuntimeException(e);
|
return new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExtractedMethod_whenFoundNonInt_thenAggregateException() {
|
public void givenExtractedMethod_whenFoundNonInt_thenSuppressExIntoRuntimeEx() {
|
||||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||||
RuntimeException runEx = Arrays.stream(strings)
|
RuntimeException runEx = Arrays.stream(strings)
|
||||||
.map(AggregateExceptionHandlerUnitTest::process)
|
.map(AggregateExceptionHandlerUnitTest::process)
|
||||||
|
@ -49,7 +48,7 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTryCatchInPipeline_whenFoundNonInts_thenAggregateException() {
|
public void givenTryCatchInPipeline_whenFoundNonInts_thenSuppressExIntoRuntimeEx() {
|
||||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||||
RuntimeException runEx = Arrays.stream(strings)
|
RuntimeException runEx = Arrays.stream(strings)
|
||||||
.map(str -> {
|
.map(str -> {
|
||||||
|
@ -70,13 +69,16 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenFoundNonInts_thenAggregateExceptionAndReturnOutput() {
|
public void givenExtractedMethodReturnOutputAndEx_whenFoundNonInts_thenSuppressExIntoRuntimeEx() {
|
||||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||||
Map resultMap = Arrays.stream(strings)
|
Map resultMap = Arrays.stream(strings)
|
||||||
.map(AggregateExceptionHandlerUnitTest::transform)
|
.map(AggregateExceptionHandlerUnitTest::transform)
|
||||||
.collect(Collectors.partitioningBy(o -> o instanceof RuntimeException, Collectors.toList()));
|
.collect(Collectors.partitioningBy(o -> o instanceof RuntimeException, Collectors.toList()));
|
||||||
|
|
||||||
RuntimeException ex = null;
|
RuntimeException ex = null;
|
||||||
if (resultMap.containsKey(Boolean.TRUE)) {
|
|
||||||
|
assertTrue(resultMap.containsKey(Boolean.TRUE));
|
||||||
|
|
||||||
List<RuntimeException> exs = (List<RuntimeException>) resultMap.get(Boolean.TRUE);
|
List<RuntimeException> exs = (List<RuntimeException>) resultMap.get(Boolean.TRUE);
|
||||||
ex = exs.stream()
|
ex = exs.stream()
|
||||||
.reduce(
|
.reduce(
|
||||||
|
@ -84,26 +86,27 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
o1.addSuppressed(o2);
|
o1.addSuppressed(o2);
|
||||||
return o1;
|
return o1;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
assertEquals("Errors Occurred", ex.getMessage());
|
assertEquals("Errors Occurred", ex.getMessage());
|
||||||
assertEquals(3, ex.getSuppressed().length);
|
assertEquals(3, ex.getSuppressed().length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenWrapFunction_whenFoundNonInts_thenAggregateException() throws ExceptionAggregator {
|
public void givenWrapFunction_whenFoundNonInts_thenUseExAggregator() throws ExceptionAggregator {
|
||||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||||
Map<Boolean, List<Result<Integer>>> resultmap = Arrays.stream(strings)
|
Map<Boolean, List<Result<Integer>>> resultmap = Arrays.stream(strings)
|
||||||
.map(CustomMapper.mapper(Integer::parseInt))
|
.map(CustomMapper.mapper(Integer::parseInt))
|
||||||
.collect(Collectors.partitioningBy(r -> r.getException().isEmpty(), Collectors.toList()));
|
.collect(Collectors.partitioningBy(r -> r.getException().isEmpty(), Collectors.toList()));
|
||||||
|
|
||||||
if (resultmap.containsKey(Boolean.FALSE)) {
|
assertTrue(resultmap.containsKey(Boolean.TRUE));
|
||||||
|
|
||||||
List<Result<Integer>> resultList = resultmap.get(Boolean.FALSE);
|
List<Result<Integer>> resultList = resultmap.get(Boolean.FALSE);
|
||||||
List<Exception> exceptionList = resultList.stream()
|
List<Exception> exceptionList = resultList.stream()
|
||||||
.map(opex -> opex.getException().get())
|
.map(opex -> opex.getException().get())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
assertThrows(ExceptionAggregator.class, () -> handleExceptions(exceptionList));
|
assertThrows(ExceptionAggregator.class, () -> handleExceptions(exceptionList));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleExceptions(List<Exception> exceptions) throws ExceptionAggregator {
|
private void handleExceptions(List<Exception> exceptions) throws ExceptionAggregator {
|
||||||
|
@ -113,7 +116,7 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExCollector_whenFoundNonInts_thenAggregateException() throws ExceptionAggregator {
|
public void givenExCollector_whenFoundNonInts_thenAggregateExInCustomCollector() throws ExceptionAggregator {
|
||||||
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
String[] strings = {"1", "2", "3", "a", "b", "c"};
|
||||||
ExceptionCollector exCollector = Arrays.stream(strings)
|
ExceptionCollector exCollector = Arrays.stream(strings)
|
||||||
.collect(ExceptionCollector.of(Integer::parseInt));
|
.collect(ExceptionCollector.of(Integer::parseInt));
|
||||||
|
@ -123,21 +126,21 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Either<RuntimeException, Integer> processAndReturnEither(String str) {
|
private static Either<RuntimeException, Integer> processAndReturnEither(String str) {
|
||||||
Either<RuntimeException, Integer> either = null;
|
|
||||||
try {
|
try {
|
||||||
either = Either.right(Integer.parseInt(str));
|
return Either.right(Integer.parseInt(str));
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
either = Either.left(new RuntimeException(e));
|
return Either.left(new RuntimeException(e));
|
||||||
}
|
}
|
||||||
return either;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVavrEither_whenFoundNonInts_thenAggregateException() {
|
public void givenVavrEither_whenFoundNonInts_thenSuppressExIntoRuntimeEx() {
|
||||||
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
||||||
Map<Boolean, List<Either<RuntimeException, Integer>>> map = strings.stream()
|
Map<Boolean, List<Either<RuntimeException, Integer>>> map = strings.stream()
|
||||||
.map(str -> processAndReturnEither(str))
|
.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)
|
RuntimeException runEx = map.get(Boolean.TRUE)
|
||||||
.stream().map(either -> either.getLeft())
|
.stream().map(either -> either.getLeft())
|
||||||
|
@ -149,11 +152,11 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVavrTry_whenFoundNonInts_thenAggregateException() {
|
public void givenVavrTry_whenFoundNonInts_thenSuppressExIntoRuntimeEx() {
|
||||||
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
||||||
Map<Boolean, List<Try<Integer>>> map = strings.stream()
|
Map<Boolean, List<Try<Integer>>> map = strings.stream()
|
||||||
.map(str -> Try.of(() -> Integer.parseInt(str)))
|
.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()
|
Throwable runEx = map.get(Boolean.TRUE).stream()
|
||||||
.map(t -> t.getCause())
|
.map(t -> t.getCause())
|
||||||
.reduce(new RuntimeException("Errors Occurred"), (o1, o2) -> {
|
.reduce(new RuntimeException("Errors Occurred"), (o1, o2) -> {
|
||||||
|
@ -164,7 +167,7 @@ public class AggregateExceptionHandlerUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenVavrEitherAndTry_whenFoundNonInts_thenAggregateException() {
|
public void givenVavrEitherAndTry_whenFoundNonInts_thenSuppressExIntoRuntimeEx() {
|
||||||
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
List<String> strings = List.of("1", "2", "3", "a", "b", "c");
|
||||||
Map<Boolean, List<Either<Throwable, Integer>>> map = strings.stream()
|
Map<Boolean, List<Either<Throwable, Integer>>> map = strings.stream()
|
||||||
.map(str -> Try.of(() -> Integer.parseInt(str)).toEither())
|
.map(str -> Try.of(() -> Integer.parseInt(str)).toEither())
|
||||||
|
|
Loading…
Reference in New Issue