Skip to content

Commit 2773555

Browse files
Ahmed TawilaAhmed Tawila
authored andcommitted
Merge branch 'future-vavr' of https://github.com/ahmedtawila/tutorials
2 parents 3bafafd + ebc28db commit 2773555

5 files changed

Lines changed: 149 additions & 368 deletions

File tree

‎vavr/src/main/java/com/baeldung/vavr/future/Tester.java‎

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.vavr.future;
2+
3+
public class Util {
4+
5+
public static String appendData(String initial) {
6+
return initial + "Baeldung!";
7+
}
8+
9+
public static int divideByZero(int num) {
10+
return num / 0;
11+
}
12+
}

‎vavr/src/main/java/com/vavr/future/Tester.java‎

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.baeldung.vavr.future;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.ExecutionException;
9+
10+
import org.junit.Test;
11+
12+
import io.vavr.Tuple;
13+
import io.vavr.Tuple2;
14+
import io.vavr.concurrent.Future;
15+
import io.vavr.control.Option;
16+
import io.vavr.control.Try;
17+
18+
public class FutureTest {
19+
20+
@Test
21+
public void whenAppendData_thenCorrect1() {
22+
String initialValue = "Welcome to ";
23+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
24+
String result = resultFuture.get();
25+
26+
assertEquals("Welcome to Baeldung!", result);
27+
}
28+
29+
@Test
30+
public void whenAppendData_thenCorrect2() {
31+
String initialValue = "Welcome to ";
32+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
33+
resultFuture.await();
34+
Option<Try<String>> futureOption = resultFuture.getValue();
35+
Try<String> futureTry = futureOption.get();
36+
String result = futureTry.get();
37+
38+
assertEquals("Welcome to Baeldung!", result);
39+
}
40+
41+
@Test
42+
public void whenAppendData_thenSuccess() {
43+
String initialValue = "Welcome to ";
44+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
45+
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
46+
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
47+
String result = resultFuture.get();
48+
49+
assertEquals("Welcome to Baeldung!", result);
50+
}
51+
52+
@Test
53+
public void whenChainingCallbacks_thenCorrect() {
54+
String initialValue = "Welcome to ";
55+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
56+
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
57+
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
58+
String result = resultFuture.get();
59+
60+
assertEquals("Welcome to Baeldung!", result);
61+
}
62+
63+
@Test
64+
public void whenCallAwait_thenCorrect() {
65+
String initialValue = "Welcome to ";
66+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
67+
resultFuture = resultFuture.await();
68+
String result = resultFuture.get();
69+
70+
assertEquals("Welcome to Baeldung!", result);
71+
}
72+
73+
@Test
74+
public void whenDivideByZero_thenGetThrowable1() {
75+
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
76+
Future<Throwable> throwableFuture = resultFuture.failed();
77+
Throwable throwable = throwableFuture.get();
78+
79+
assertEquals("/ by zero", throwable.getMessage());
80+
}
81+
82+
@Test
83+
public void whenDivideByZero_thenGetThrowable2() {
84+
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
85+
resultFuture.await();
86+
Option<Throwable> throwableOption = resultFuture.getCause();
87+
Throwable throwable = throwableOption.get();
88+
89+
assertEquals("/ by zero", throwable.getMessage());
90+
}
91+
92+
@Test
93+
public void whenDivideByZero_thenCorrect() throws InterruptedException {
94+
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
95+
resultFuture.await();
96+
97+
assertTrue(resultFuture.isCompleted());
98+
assertFalse(resultFuture.isSuccess());
99+
assertTrue(resultFuture.isFailure());
100+
}
101+
102+
@Test
103+
public void whenAppendData_thenFutureNotEmpty() {
104+
String initialValue = "Welcome to ";
105+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
106+
resultFuture.await();
107+
108+
assertFalse(resultFuture.isEmpty());
109+
}
110+
111+
@Test
112+
public void whenCallZip_thenCorrect() {
113+
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
114+
.zip(Future.of(() -> new Integer(5)));
115+
future.await();
116+
117+
assertEquals(Tuple.of("John", new Integer(5)), future.get());
118+
}
119+
120+
@Test
121+
public void whenAppendData_thenFutureNotEmptyd() throws InterruptedException, ExecutionException {
122+
String initialValue = "Welcome to ";
123+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
124+
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
125+
126+
assertEquals("Welcome to Baeldung!", convertedFuture.get());
127+
}
128+
129+
@Test
130+
public void whenCallMap_thenCorrect() {
131+
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
132+
.map(a -> "Hello " + a);
133+
futureResult.await();
134+
135+
assertEquals("Hello from Baeldung", futureResult.get());
136+
}
137+
}

0 commit comments

Comments
 (0)