From c9111e635f45b89c00e8b196739a8afccd69d64f Mon Sep 17 00:00:00 2001 From: Naresh Babu P B Date: Mon, 26 Jun 2017 17:29:18 +0530 Subject: [PATCH] Examples for BAEL-423: (#2103) * Examples for BAEL-423: Handling Exceptions in Lambda Expression with Vavr(Javaslang) * Modified Lifting and Try API examples and removed Validation API examples --- .../VavrExceptionHandlingUnitTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrExceptionHandlingUnitTest.java diff --git a/vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrExceptionHandlingUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrExceptionHandlingUnitTest.java new file mode 100644 index 0000000000..d03422b6f7 --- /dev/null +++ b/vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrExceptionHandlingUnitTest.java @@ -0,0 +1,84 @@ +package com.baeldung.vavr.exception.handling; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.vavr.API; +import io.vavr.CheckedFunction1; + +public class VavrExceptionHandlingUnitTest { + + private static final Logger LOG = LoggerFactory.getLogger(VavrExceptionHandlingUnitTest.class); + + private List integers; + private List validIntegersOnly; + + @Before + public void init() { + integers = Arrays.asList(3, 9, 7, 0, 10, 20); + validIntegersOnly = Arrays.asList(3, 9, 7, 5, 10, 20); + } + + @Test + public void exceptionCausingMethod_UsingCheckedFunction_ThenSuccess() { + CheckedFunction1 fn = i -> readFromFile(i); + + validIntegersOnly.stream().map(fn.unchecked()).forEach(i -> LOG.debug("{}", i)); + } + + @Test(expected = IOException.class) + public void exceptionCausingMethod_UsingCheckedFunction_ThenFailure() { + CheckedFunction1 fn = i -> readFromFile(i); + + integers.stream().map(fn.unchecked()).forEach(i -> LOG.debug("{}", i)); + } + + @Test + public void exceptionCausingMethod_UsingAPI_ThenSuccess() { + validIntegersOnly.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i)); + } + + @Test(expected = IOException.class) + public void exceptionCausingMethod_UsingAPI_ThenFailure() { + integers.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i)); + } + + @Test + public void exceptionCausingMethod_UsingLift_ThenSuccess() { + validIntegersOnly.stream().map(CheckedFunction1.lift(i -> readFromFile(i))) + .map(i -> i.getOrElse(-1)) + .forEach(i -> {Assert.assertNotSame(-1, i);LOG.debug("{}", i);}); + } + + @Test + public void exceptionCausingMethod_UsingLift_ThenFailure() { + integers.stream().map(CheckedFunction1.lift(i -> readFromFile(i))) + .map(i -> i.getOrElse(-1)) + .forEach(i -> LOG.debug("{}", i)); + + } + + @Test + public void exceptionCausingMethod_UsingTry_ThenSuccess() { + + integers.stream().map(CheckedFunction1.liftTry(i -> readFromFile(i))) + .map(i -> i.onFailure(e -> Assert.assertTrue(e instanceof IOException))) + .map(i -> i.onSuccess(e -> LOG.debug("{}", e))) + .forEach(i -> {}); + } + + private static Integer readFromFile(Integer i) throws IOException { + if (i == 0) { + throw new IOException(); // mock IOException + } + return i * i; + } + +}