|
| 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