11package com .baeldung .vavr .exception .handling ;
22
3- import java .io .IOException ;
4- import java .util .Arrays ;
5- import java .util .List ;
6-
3+ import io .vavr .API ;
4+ import io .vavr .CheckedFunction1 ;
5+ import io .vavr .Value ;
76import org .junit .Assert ;
87import org .junit .Before ;
98import org .junit .Test ;
109import org .slf4j .Logger ;
1110import org .slf4j .LoggerFactory ;
1211
13- import io .vavr .API ;
14- import io .vavr .CheckedFunction1 ;
12+ import java .io .IOException ;
13+ import java .util .Arrays ;
14+ import java .util .List ;
1515
1616public class VavrExceptionHandlingUnitTest {
1717
1818 private static final Logger LOG = LoggerFactory .getLogger (VavrExceptionHandlingUnitTest .class );
19-
19+
2020 private List <Integer > integers ;
2121 private List <Integer > validIntegersOnly ;
2222
@@ -28,50 +28,53 @@ public void init() {
2828
2929 @ Test
3030 public void exceptionCausingMethod_UsingCheckedFunction_ThenSuccess () {
31- CheckedFunction1 <Integer , Integer > fn = i -> readFromFile (i );
32-
33- validIntegersOnly .stream ().map (fn .unchecked ()).forEach (i -> LOG .debug ("{}" , i ));
31+ CheckedFunction1 <Integer , Integer > fn = i -> readFromFile (i );
32+
33+ validIntegersOnly .stream ().map (fn .unchecked ()).forEach (i -> LOG .debug ("{}" , i ));
3434 }
35-
35+
3636 @ Test (expected = IOException .class )
3737 public void exceptionCausingMethod_UsingCheckedFunction_ThenFailure () {
38- CheckedFunction1 <Integer , Integer > fn = i -> readFromFile (i );
39-
40- integers .stream ().map (fn .unchecked ()).forEach (i -> LOG .debug ("{}" , i ));
38+ CheckedFunction1 <Integer , Integer > fn = i -> readFromFile (i );
39+
40+ integers .stream ().map (fn .unchecked ()).forEach (i -> LOG .debug ("{}" , i ));
4141 }
42-
42+
4343 @ Test
4444 public void exceptionCausingMethod_UsingAPI_ThenSuccess () {
45- validIntegersOnly .stream ().map (API .unchecked (i -> readFromFile (i ))).forEach (i -> LOG .debug ("{}" , i ));
45+ validIntegersOnly .stream ().map (API .unchecked (i -> readFromFile (i ))).forEach (i -> LOG .debug ("{}" , i ));
4646 }
4747
4848 @ Test (expected = IOException .class )
4949 public void exceptionCausingMethod_UsingAPI_ThenFailure () {
50- integers .stream ().map (API .unchecked (i -> readFromFile (i ))).forEach (i -> LOG .debug ("{}" , i ));
50+ integers .stream ().map (API .unchecked (i -> readFromFile (i ))).forEach (i -> LOG .debug ("{}" , i ));
5151 }
5252
5353 @ Test
5454 public void exceptionCausingMethod_UsingLift_ThenSuccess () {
55- validIntegersOnly .stream ().map (CheckedFunction1 .lift (i -> readFromFile (i )))
56- .map (i -> i .getOrElse (-1 ))
57- .forEach (i -> {Assert .assertNotSame (-1 , i );LOG .debug ("{}" , i );});
55+ validIntegersOnly .stream ().map (CheckedFunction1 .lift (i -> readFromFile (i )))
56+ .map (i -> i .getOrElse (-1 ))
57+ .forEach (i -> {
58+ Assert .assertNotSame (-1 , i );
59+ LOG .debug ("{}" , i );
60+ });
5861 }
59-
62+
6063 @ Test
6164 public void exceptionCausingMethod_UsingLift_ThenFailure () {
62- integers .stream ().map (CheckedFunction1 .lift (i -> readFromFile (i )))
63- .map (i -> i .getOrElse (-1 ))
64- .forEach (i -> LOG .debug ("{}" , i ));
65-
65+ integers .stream ()
66+ .map (CheckedFunction1 .lift (i -> readFromFile (i )))
67+ .map (i -> i .getOrElse (-1 ))
68+ .forEach (i -> LOG .debug ("{}" , i ));
69+
6670 }
67-
71+
6872 @ Test
6973 public void exceptionCausingMethod_UsingTry_ThenSuccess () {
70-
71- integers .stream ().map (CheckedFunction1 .liftTry (i -> readFromFile (i )))
72- .map (i -> i .onFailure (e -> Assert .assertTrue (e instanceof IOException )))
73- .map (i -> i .onSuccess (e -> LOG .debug ("{}" , e )))
74- .forEach (i -> {});
74+ integers .stream ()
75+ .map (CheckedFunction1 .liftTry (VavrExceptionHandlingUnitTest ::readFromFile ))
76+ .flatMap (Value ::toJavaStream )
77+ .forEach (i -> LOG .debug ("{}" , i ));
7578 }
7679
7780 private static Integer readFromFile (Integer i ) throws IOException {
@@ -80,5 +83,5 @@ private static Integer readFromFile(Integer i) throws IOException {
8083 }
8184 return i * i ;
8285 }
83-
86+
8487}
0 commit comments