Skip to content

Commit c9111e6

Browse files
NareshBabuPBpivovarit
authored andcommitted
Examples for BAEL-423: (eugenp#2103)
* Examples for BAEL-423: Handling Exceptions in Lambda Expression with Vavr(Javaslang) * Modified Lifting and Try API examples and removed Validation API examples
1 parent b97651a commit c9111e6

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.vavr.exception.handling;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import io.vavr.API;
14+
import io.vavr.CheckedFunction1;
15+
16+
public class VavrExceptionHandlingUnitTest {
17+
18+
private static final Logger LOG = LoggerFactory.getLogger(VavrExceptionHandlingUnitTest.class);
19+
20+
private List<Integer> integers;
21+
private List<Integer> validIntegersOnly;
22+
23+
@Before
24+
public void init() {
25+
integers = Arrays.asList(3, 9, 7, 0, 10, 20);
26+
validIntegersOnly = Arrays.asList(3, 9, 7, 5, 10, 20);
27+
}
28+
29+
@Test
30+
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));
34+
}
35+
36+
@Test(expected = IOException.class)
37+
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));
41+
}
42+
43+
@Test
44+
public void exceptionCausingMethod_UsingAPI_ThenSuccess() {
45+
validIntegersOnly.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i));
46+
}
47+
48+
@Test(expected = IOException.class)
49+
public void exceptionCausingMethod_UsingAPI_ThenFailure() {
50+
integers.stream().map(API.unchecked(i -> readFromFile(i))).forEach(i -> LOG.debug("{}", i));
51+
}
52+
53+
@Test
54+
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);});
58+
}
59+
60+
@Test
61+
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+
66+
}
67+
68+
@Test
69+
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 -> {});
75+
}
76+
77+
private static Integer readFromFile(Integer i) throws IOException {
78+
if (i == 0) {
79+
throw new IOException(); // mock IOException
80+
}
81+
return i * i;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)