Skip to content

Commit 550978a

Browse files
authored
Merge pull request eugenp#3192 from ahmedtawila/master
BAEL-1080 Introduction to Future in Vavr
2 parents b258e53 + c489044 commit 550978a

6 files changed

Lines changed: 204 additions & 296 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
UK
2-
US
3-
Germany
1+
UK
2+
US
3+
Germany

‎core-java/src/test/java/com/baeldung/file/FilesTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
9191
new FileInputStream(fileName)))
9292
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
9393
}
94-
}
94+
}

‎core-java/src/test/java/com/baeldung/nestedclass/NewOuter.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class InnerClass {
1414
public void run() {
1515
System.out.println("a = " + a);
1616
System.out.println("b = " + b);
17-
System.out.println("NewOuterTest.this.a = " + NewOuter.this.a);
18-
System.out.println("NewOuterTest.b = " + NewOuter.b);
19-
System.out.println("NewOuterTest.this.b = " + NewOuter.this.b);
17+
System.out.println("NewOuter.this.a = " + NewOuter.this.a);
18+
System.out.println("NewOuter.b = " + NewOuter.b);
19+
System.out.println("NewOuter.this.b = " + NewOuter.this.b);
2020
}
2121
}
2222

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
13+
public static String getSubstringMinusOne(String s) {
14+
return s.substring(-1);
15+
}
16+
17+
public static String getSubstringMinusTwo(String s) {
18+
return s.substring(-2);
19+
}
20+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.baeldung.vavr.future;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Executors;
8+
9+
import org.junit.Test;
10+
11+
import io.vavr.Tuple;
12+
import io.vavr.Tuple2;
13+
import io.vavr.concurrent.Future;
14+
import io.vavr.control.Option;
15+
import io.vavr.control.Try;
16+
17+
public class FutureTest {
18+
19+
@Test
20+
public void whenChangeExecutorService_thenCorrect() {
21+
String initialValue = "Welcome to ";
22+
Future<String> resultFuture = Future.of(
23+
Executors.newSingleThreadExecutor(),
24+
() -> Util.appendData(initialValue));
25+
String result = resultFuture.get();
26+
27+
assertThat(result).isEqualTo("Welcome to Baeldung!");
28+
}
29+
30+
@Test
31+
public void whenAppendData_thenCorrect1() {
32+
String initialValue = "Welcome to ";
33+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
34+
String result = resultFuture.get();
35+
36+
assertThat(result).isEqualTo("Welcome to Baeldung!");
37+
}
38+
39+
@Test
40+
public void whenAppendData_thenCorrect2() {
41+
String initialValue = "Welcome to ";
42+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
43+
resultFuture.await();
44+
Option<Try<String>> futureOption = resultFuture.getValue();
45+
Try<String> futureTry = futureOption.get();
46+
String result = futureTry.get();
47+
48+
assertThat(result).isEqualTo("Welcome to Baeldung!");
49+
}
50+
51+
@Test
52+
public void whenAppendData_thenSuccess() {
53+
String initialValue = "Welcome to ";
54+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
55+
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
56+
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
57+
String result = resultFuture.get();
58+
59+
assertThat(result).isEqualTo("Welcome to Baeldung!");
60+
}
61+
62+
@Test
63+
public void whenChainingCallbacks_thenCorrect() {
64+
String initialValue = "Welcome to ";
65+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
66+
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
67+
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
68+
String result = resultFuture.get();
69+
70+
assertThat(result).isEqualTo("Welcome to Baeldung!");
71+
}
72+
73+
@Test
74+
public void whenCallAwait_thenCorrect() {
75+
String initialValue = "Welcome to ";
76+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
77+
resultFuture = resultFuture.await();
78+
String result = resultFuture.get();
79+
80+
assertThat(result).isEqualTo("Welcome to Baeldung!");
81+
}
82+
83+
@Test
84+
public void whenDivideByZero_thenGetThrowable1() {
85+
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
86+
Future<Throwable> throwableFuture = resultFuture.failed();
87+
Throwable throwable = throwableFuture.get();
88+
89+
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
90+
}
91+
92+
@Test
93+
public void whenDivideByZero_thenGetThrowable2() {
94+
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
95+
resultFuture.await();
96+
Option<Throwable> throwableOption = resultFuture.getCause();
97+
Throwable throwable = throwableOption.get();
98+
99+
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
100+
}
101+
102+
@Test
103+
public void whenDivideByZero_thenCorrect() throws InterruptedException {
104+
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
105+
resultFuture.await();
106+
107+
assertThat(resultFuture.isCompleted()).isTrue();
108+
assertThat(resultFuture.isSuccess()).isFalse();
109+
assertThat(resultFuture.isFailure()).isTrue();
110+
}
111+
112+
@Test
113+
public void whenAppendData_thenFutureNotEmpty() {
114+
String initialValue = "Welcome to ";
115+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
116+
resultFuture.await();
117+
118+
assertThat(resultFuture.isEmpty()).isFalse();
119+
}
120+
121+
@Test
122+
public void whenCallZip_thenCorrect() {
123+
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
124+
.zip(Future.of(() -> new Integer(5)));
125+
future.await();
126+
127+
assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5)));
128+
}
129+
130+
@Test
131+
public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException {
132+
String initialValue = "Welcome to ";
133+
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
134+
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
135+
136+
assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!");
137+
}
138+
139+
@Test
140+
public void whenCallMap_thenCorrect() {
141+
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
142+
.map(a -> "Hello " + a);
143+
futureResult.await();
144+
145+
assertThat(futureResult.get()).isEqualTo("Hello from Baeldung");
146+
}
147+
148+
@Test
149+
public void whenFutureFails_thenGetErrorMessage() {
150+
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
151+
Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage);
152+
String errorMessage = errorMessageFuture.get();
153+
154+
assertThat(errorMessage).isEqualTo("String index out of range: -1");
155+
}
156+
157+
@Test
158+
public void whenFutureFails_thenGetAnotherFuture() {
159+
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
160+
Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage));
161+
String errorMessage = errorMessageFuture.get();
162+
163+
assertThat(errorMessage).isEqualTo("String index out of range: -1");
164+
}
165+
166+
@Test
167+
public void whenBothFuturesFail_thenGetErrorMessage() {
168+
Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello"));
169+
Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello"));
170+
Future<String> errorMessageFuture = future1.fallbackTo(future2);
171+
Future<Throwable> errorMessage = errorMessageFuture.failed();
172+
173+
assertThat(
174+
errorMessage.get().getMessage())
175+
.isEqualTo("String index out of range: -1");
176+
}
177+
}

0 commit comments

Comments
 (0)