From 2afc7dc62353433c1d1b4cde2ab17b153a7b441c Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 4 Jan 2018 17:21:16 +0200 Subject: [PATCH] Refactor Vavr Future (#3349) --- .../java/com/baeldung/vavr/future/Util.java | 20 -- .../com/baeldung/vavr/future/FutureTest.java | 207 ++++++++---------- 2 files changed, 92 insertions(+), 135 deletions(-) delete mode 100644 vavr/src/main/java/com/baeldung/vavr/future/Util.java diff --git a/vavr/src/main/java/com/baeldung/vavr/future/Util.java b/vavr/src/main/java/com/baeldung/vavr/future/Util.java deleted file mode 100644 index 790ef2bf88..0000000000 --- a/vavr/src/main/java/com/baeldung/vavr/future/Util.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.vavr.future; - -public class Util { - - public static String appendData(String initial) { - return initial + "Baeldung!"; - } - - public static int divideByZero(int num) { - return num / 0; - } - - public static String getSubstringMinusOne(String s) { - return s.substring(-1); - } - - public static String getSubstringMinusTwo(String s) { - return s.substring(-2); - } -} diff --git a/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java b/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java index dd678bcad0..002919a937 100644 --- a/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java @@ -1,119 +1,102 @@ package com.baeldung.vavr.future; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; - -import org.junit.Test; - import io.vavr.Tuple; -import io.vavr.Tuple2; import io.vavr.concurrent.Future; import io.vavr.control.Option; import io.vavr.control.Try; +import org.junit.Test; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static java.util.concurrent.Executors.newSingleThreadExecutor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class FutureTest { - + private static final String error = "Failed to get underlying value."; + private static final String HELLO = "Welcome to Baeldung!"; @Test - public void whenChangeExecutorService_thenCorrect() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of( - Executors.newSingleThreadExecutor(), - () -> Util.appendData(initialValue)); - Thread.sleep(20); - String result = resultFuture.getOrElse(error); + public void whenChangeExecutorService_thenCorrect() { + String result = Future.of(newSingleThreadExecutor(), () -> HELLO) + .getOrElse(error); - assertThat(result).isEqualTo("Welcome to Baeldung!"); + assertThat(result) + .isEqualTo(HELLO); } @Test - public void whenAppendData_thenCorrect1() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)); - Thread.sleep(20); - String result = resultFuture.getOrElse(new String(error)); + public void whenAppendData_thenCorrect1() { + String result = Future.of(() -> HELLO) + .getOrElse(error); - assertThat(result).isEqualTo("Welcome to Baeldung!"); + assertThat(result) + .isEqualTo(HELLO); } @Test - public void whenAppendData_thenCorrect2() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)); - Thread.sleep(20); - resultFuture.await(); + public void whenAppendData_thenCorrect2() { + Future resultFuture = Future.of(() -> HELLO) + .await(); + Option> futureOption = resultFuture.getValue(); - Try futureTry = futureOption.get(); - String result = futureTry.getOrElse(error); + String result = futureOption.get().getOrElse(error); - assertThat(result).isEqualTo("Welcome to Baeldung!"); + assertThat(result) + .isEqualTo(HELLO); } @Test - public void whenAppendData_thenSuccess() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)) + public void whenAppendData_thenSuccess() { + String result = Future.of(() -> HELLO) .onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult)) - .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)); - Thread.sleep(20); - String result = resultFuture.getOrElse(error); + .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)) + .getOrElse(error); - assertThat(result).isEqualTo("Welcome to Baeldung!"); + assertThat(result) + .isEqualTo(HELLO); } @Test - public void whenChainingCallbacks_thenCorrect() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)) - .andThen(finalResult -> System.out.println("Completed - 1: " + finalResult)) - .andThen(finalResult -> System.out.println("Completed - 2: " + finalResult)); - Thread.sleep(20); - String result = resultFuture.getOrElse(error); - - assertThat(result).isEqualTo("Welcome to Baeldung!"); + public void whenChainingCallbacks_thenCorrect() { + Future.of(() -> HELLO) + .andThen(r -> System.out.println("Completed - 1: " + r)) + .andThen(r -> System.out.println("Completed - 2: " + r)); } @Test - public void whenCallAwait_thenCorrect() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)); - Thread.sleep(20); - resultFuture = resultFuture.await(); - String result = resultFuture.getOrElse(error); + public void whenCallAwait_thenCorrect() { + Future resultFuture = Future.of(() -> HELLO) + .await(); + String result = resultFuture.getValue().get().getOrElse(error); - assertThat(result).isEqualTo("Welcome to Baeldung!"); + assertThat(result) + .isEqualTo(HELLO); } @Test - public void whenDivideByZero_thenGetThrowable1() throws InterruptedException { - Future resultFuture = Future.of(() -> Util.divideByZero(10)); - Thread.sleep(20); - Future throwableFuture = resultFuture.failed(); - Throwable throwable = throwableFuture.getOrElse(new Throwable()); + public void whenDivideByZero_thenGetThrowable1() { + Future resultFuture = Future.of(() -> 10 / 0); - assertThat(throwable.getMessage()).isEqualTo("/ by zero"); + assertThatThrownBy(resultFuture::get) + .isInstanceOf(ArithmeticException.class); } @Test - public void whenDivideByZero_thenGetThrowable2() throws InterruptedException { - Future resultFuture = Future.of(() -> Util.divideByZero(10)); - Thread.sleep(20); - resultFuture.await(); - Option throwableOption = resultFuture.getCause(); - Throwable throwable = throwableOption.getOrElse(new Throwable()); + public void whenDivideByZero_thenGetThrowable2() { + Future resultFuture = Future.of(() -> 10 / 0) + .await(); - assertThat(throwable.getMessage()).isEqualTo("/ by zero"); + assertThat(resultFuture.getCause().get().getMessage()) + .isEqualTo("/ by zero"); } @Test - public void whenDivideByZero_thenCorrect() throws InterruptedException { - Future resultFuture = Future.of(() -> Util.divideByZero(10)); - Thread.sleep(20); - resultFuture.await(); + public void whenDivideByZero_thenCorrect() { + Future resultFuture = Future.of(() -> 10 / 0) + .await(); assertThat(resultFuture.isCompleted()).isTrue(); assertThat(resultFuture.isSuccess()).isFalse(); @@ -121,76 +104,70 @@ public class FutureTest { } @Test - public void whenAppendData_thenFutureNotEmpty() throws InterruptedException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)); - Thread.sleep(20); - resultFuture.await(); + public void whenAppendData_thenFutureNotEmpty() { + Future resultFuture = Future.of(() -> HELLO) + .await(); - assertThat(resultFuture.isEmpty()).isFalse(); + assertThat(resultFuture.isEmpty()) + .isFalse(); } @Test - public void whenCallZip_thenCorrect() throws InterruptedException { - Future> future = Future.of(() -> "John") - .zip(Future.of(() -> new Integer(5))); - Thread.sleep(20); - future.await(); + public void whenCallZip_thenCorrect() { + Future f1 = Future.of(() -> "hello1"); + Future f2 = Future.of(() -> "hello2"); - assertThat(future.getOrElse(new Tuple2(error, 0))) - .isEqualTo(Tuple.of("John", new Integer(5))); + assertThat(f1.zip(f2).get()) + .isEqualTo(Tuple.of("hello1", "hello2")); } @Test public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException { - String initialValue = "Welcome to "; - Future resultFuture = Future.of(() -> Util.appendData(initialValue)); - Thread.sleep(20); - CompletableFuture convertedFuture = resultFuture.toCompletableFuture(); + CompletableFuture convertedFuture = Future.of(() -> HELLO) + .toCompletableFuture(); - assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!"); + assertThat(convertedFuture.get()) + .isEqualTo(HELLO); } @Test - public void whenCallMap_thenCorrect() throws InterruptedException { - Future futureResult = Future.of(() -> new StringBuilder("from Baeldung")) - .map(a -> "Hello " + a); - Thread.sleep(20); - futureResult.await(); + public void whenCallMap_thenCorrect() { + Future futureResult = Future.of(() -> "from Baeldung") + .map(a -> "Hello " + a) + .await(); - assertThat(futureResult.getOrElse(error)).isEqualTo("Hello from Baeldung"); + assertThat(futureResult.get()) + .isEqualTo("Hello from Baeldung"); } @Test - public void whenFutureFails_thenGetErrorMessage() throws InterruptedException { - Future resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); - Thread.sleep(20); - Future errorMessageFuture = resultFuture.recover(Throwable::getMessage); - String errorMessage = errorMessageFuture.getOrElse(error); + public void whenFutureFails_thenGetErrorMessage() { + Future future = Future.of(() -> "Hello".substring(-1)) + .recover(x -> "fallback value"); - assertThat(errorMessage).isEqualTo("String index out of range: -1"); + assertThat(future.get()) + .isEqualTo("fallback value"); } @Test - public void whenFutureFails_thenGetAnotherFuture() throws InterruptedException { - Future resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); - Thread.sleep(20); - Future errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage)); - String errorMessage = errorMessageFuture.getOrElse(error); + public void whenFutureFails_thenGetAnotherFuture() { + Future future = Future.of(() -> "Hello".substring(-1)) + .recoverWith(x -> Future.of(() -> "fallback value")); - assertThat(errorMessage).isEqualTo("String index out of range: -1"); + assertThat(future.get()) + .isEqualTo("fallback value"); } @Test - public void whenBothFuturesFail_thenGetErrorMessage() throws InterruptedException { - Future future1 = Future.of(() -> Util.getSubstringMinusOne("Hello")); - Future future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello")); - Thread.sleep(20); - Future errorMessageFuture = future1.fallbackTo(future2); + public void whenBothFuturesFail_thenGetErrorMessage() { + Future f1 = Future.of(() -> "Hello".substring(-1)); + Future f2 = Future.of(() -> "Hello".substring(-2)); + + Future errorMessageFuture = f1.fallbackTo(f2); Future errorMessage = errorMessageFuture.failed(); - + assertThat( - errorMessage.getOrElse(new Throwable()).getMessage()) + errorMessage.get().getMessage()) .isEqualTo("String index out of range: -1"); } }