|
| 1 | +package com.vavr.future; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import io.vavr.concurrent.Future; |
| 10 | + |
| 11 | +public class Tester { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void whenAppendData_thenCorrect() { |
| 15 | + String initialValue = "Welcome to "; |
| 16 | + Future<String> resultFuture = Future.of(() -> appendData(initialValue)); |
| 17 | + String result = resultFuture.get(); |
| 18 | + |
| 19 | + assertEquals("Welcome to Baeldung!", result); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void whenAppendData_thenSuccess() { |
| 24 | + String initialValue = "Welcome to "; |
| 25 | + Future<String> resultFuture = Future.of(() -> appendData(initialValue)) |
| 26 | + .onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult)) |
| 27 | + .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)); |
| 28 | + String result = resultFuture.get(); |
| 29 | + |
| 30 | + assertEquals("Welcome to Baeldung!", result); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenChainingCallbacks_thenCorrect() { |
| 35 | + String initialValue = "Welcome to "; |
| 36 | + Future<String> resultFuture = Future.of(() -> appendData(initialValue)) |
| 37 | + .andThen(finalResult -> System.out.println("Completed - 1: " + finalResult)) |
| 38 | + .andThen(finalResult -> System.out.println("Completed - 2: " + finalResult)); |
| 39 | + String result = resultFuture.get(); |
| 40 | + |
| 41 | + assertEquals("Welcome to Baeldung!", result); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void whenCallAwait_thenCorrect() { |
| 46 | + String initialValue = "Welcome to "; |
| 47 | + Future<String> resultFuture = Future.of(() -> appendData(initialValue)); |
| 48 | + resultFuture = resultFuture.await(); |
| 49 | + String result = resultFuture.get(); |
| 50 | + |
| 51 | + assertEquals("Welcome to Baeldung!", result); |
| 52 | + } |
| 53 | + |
| 54 | + public String appendData(String initial) { |
| 55 | + return initial + "Baeldung!"; |
| 56 | + } |
| 57 | +} |
0 commit comments