Idiomatic refactor (#2161)

This commit is contained in:
Grzegorz Piwowarek 2017-06-26 16:37:03 +02:00 committed by GitHub
parent 590cb07df6
commit 04b9bd0382

View File

@ -1,22 +1,22 @@
package com.baeldung.vavr.exception.handling; package com.baeldung.vavr.exception.handling;
import java.io.IOException; import io.vavr.API;
import java.util.Arrays; import io.vavr.CheckedFunction1;
import java.util.List; import io.vavr.Value;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import io.vavr.API; import java.io.IOException;
import io.vavr.CheckedFunction1; import java.util.Arrays;
import java.util.List;
public class VavrExceptionHandlingUnitTest { public class VavrExceptionHandlingUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(VavrExceptionHandlingUnitTest.class); private static final Logger LOG = LoggerFactory.getLogger(VavrExceptionHandlingUnitTest.class);
private List<Integer> integers; private List<Integer> integers;
private List<Integer> validIntegersOnly; private List<Integer> validIntegersOnly;
@ -28,50 +28,53 @@ public class VavrExceptionHandlingUnitTest {
@Test @Test
public void exceptionCausingMethod_UsingCheckedFunction_ThenSuccess() { public void exceptionCausingMethod_UsingCheckedFunction_ThenSuccess() {
CheckedFunction1<Integer, Integer> fn = i -> readFromFile(i); CheckedFunction1<Integer, Integer> fn = i -> readFromFile(i);
validIntegersOnly.stream().map(fn.unchecked()).forEach(i -> LOG.debug("{}", i)); validIntegersOnly.stream().map(fn.unchecked()).forEach(i -> LOG.debug("{}", i));
} }
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void exceptionCausingMethod_UsingCheckedFunction_ThenFailure() { public void exceptionCausingMethod_UsingCheckedFunction_ThenFailure() {
CheckedFunction1<Integer, Integer> fn = i -> readFromFile(i); CheckedFunction1<Integer, Integer> fn = i -> readFromFile(i);
integers.stream().map(fn.unchecked()).forEach(i -> LOG.debug("{}", i)); integers.stream().map(fn.unchecked()).forEach(i -> LOG.debug("{}", i));
} }
@Test @Test
public void exceptionCausingMethod_UsingAPI_ThenSuccess() { public void exceptionCausingMethod_UsingAPI_ThenSuccess() {
validIntegersOnly.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i)); validIntegersOnly.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i));
} }
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void exceptionCausingMethod_UsingAPI_ThenFailure() { public void exceptionCausingMethod_UsingAPI_ThenFailure() {
integers.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i)); integers.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i));
} }
@Test @Test
public void exceptionCausingMethod_UsingLift_ThenSuccess() { public void exceptionCausingMethod_UsingLift_ThenSuccess() {
validIntegersOnly.stream().map(CheckedFunction1.lift(i -> readFromFile(i))) validIntegersOnly.stream().map(CheckedFunction1.lift(i -> readFromFile(i)))
.map(i -> i.getOrElse(-1)) .map(i -> i.getOrElse(-1))
.forEach(i -> {Assert.assertNotSame(-1, i);LOG.debug("{}", i);}); .forEach(i -> {
Assert.assertNotSame(-1, i);
LOG.debug("{}", i);
});
} }
@Test @Test
public void exceptionCausingMethod_UsingLift_ThenFailure() { public void exceptionCausingMethod_UsingLift_ThenFailure() {
integers.stream().map(CheckedFunction1.lift(i -> readFromFile(i))) integers.stream()
.map(i -> i.getOrElse(-1)) .map(CheckedFunction1.lift(i -> readFromFile(i)))
.forEach(i -> LOG.debug("{}", i)); .map(i -> i.getOrElse(-1))
.forEach(i -> LOG.debug("{}", i));
} }
@Test @Test
public void exceptionCausingMethod_UsingTry_ThenSuccess() { public void exceptionCausingMethod_UsingTry_ThenSuccess() {
integers.stream()
integers.stream().map(CheckedFunction1.liftTry(i -> readFromFile(i))) .map(CheckedFunction1.liftTry(VavrExceptionHandlingUnitTest::readFromFile))
.map(i -> i.onFailure(e -> Assert.assertTrue(e instanceof IOException))) .flatMap(Value::toJavaStream)
.map(i -> i.onSuccess(e -> LOG.debug("{}", e))) .forEach(i -> LOG.debug("{}", i));
.forEach(i -> {});
} }
private static Integer readFromFile(Integer i) throws IOException { private static Integer readFromFile(Integer i) throws IOException {
@ -80,5 +83,5 @@ public class VavrExceptionHandlingUnitTest {
} }
return i * i; return i * i;
} }
} }