From 8954f8e0b97292ad5d460f024a3baff3e362b901 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Fri, 22 Nov 2024 15:52:14 +0100 Subject: [PATCH] Add doctest profile and fix testable examples Co-authored-by: Tihomir Mateev --- pom.xml | 19 ++++++++++++++++++ .../redis/examples/async/StringExample.java | 20 +++++++++---------- .../examples/reactive/StringExample.java | 20 +++++++++---------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index ddfc4877b..81bd33933 100644 --- a/pom.xml +++ b/pom.xml @@ -1288,6 +1288,25 @@ + + doctests + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/examples/reactive/*Example.java + **/examples/async/*Example.java + + true + + + + + + diff --git a/src/test/java/io/redis/examples/async/StringExample.java b/src/test/java/io/redis/examples/async/StringExample.java index e5ecc62ea..9be29395b 100644 --- a/src/test/java/io/redis/examples/async/StringExample.java +++ b/src/test/java/io/redis/examples/async/StringExample.java @@ -6,14 +6,14 @@ import io.lettuce.core.api.StatefulRedisConnection; // REMOVE_START -import org.junit.Test; +import org.junit.jupiter.api.Test; // REMOVE_END import java.util.*; import java.util.concurrent.CompletableFuture; // REMOVE_START -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; // REMOVE_END public class StringExample { @@ -31,13 +31,13 @@ public void run() { CompletableFuture setAndGet = asyncCommands.set("bike:1", "Deimos").thenCompose(v -> { System.out.println(v); // OK // REMOVE_START - assertEquals("OK", v); + assertThat(v).isEqualTo("OK"); // REMOVE_END return asyncCommands.get("bike:1"); }) // REMOVE_START .thenApply(res -> { - assertEquals("Deimos", res); + assertThat(res).isEqualTo("Deimos"); return res; }) // REMOVE_END @@ -49,13 +49,13 @@ public void run() { CompletableFuture setnx = asyncCommands.setnx("bike:1", "bike").thenCompose(v -> { System.out.println(v); // false (because key already exists) // REMOVE_START - assertEquals(false, v); + assertThat(v).isFalse(); // REMOVE_END return asyncCommands.get("bike:1"); }) // REMOVE_START .thenApply(res -> { - assertEquals("Deimos", res); + assertThat(res).isEqualTo("Deimos"); return res; }) // REMOVE_END @@ -66,7 +66,7 @@ public void run() { CompletableFuture setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx()) // REMOVE_START .thenApply(res -> { - assertEquals("OK", res); + assertThat(res).isEqualTo("OK"); return res; }) // REMOVE_END @@ -89,7 +89,7 @@ public void run() { List> expected = new ArrayList<>( Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"), KeyValue.just("bike:3", "Vanth"))); - assertEquals(expected, res); + assertThat(res).isEqualTo(expected); return res; }) // REMOVE_END @@ -103,13 +103,13 @@ public void run() { .thenCompose(v -> asyncCommands.incr("total_crashes")).thenCompose(v -> { System.out.println(v); // 1 // REMOVE_START - assertEquals(1L, v.longValue()); + assertThat(v).isEqualTo(1L); // REMOVE_END return asyncCommands.incrby("total_crashes", 10); }) // REMOVE_START .thenApply(res -> { - assertEquals(11L, res.longValue()); + assertThat(res).isEqualTo(11L); return res; }) // REMOVE_END diff --git a/src/test/java/io/redis/examples/reactive/StringExample.java b/src/test/java/io/redis/examples/reactive/StringExample.java index dab609417..24d2443e4 100644 --- a/src/test/java/io/redis/examples/reactive/StringExample.java +++ b/src/test/java/io/redis/examples/reactive/StringExample.java @@ -5,14 +5,14 @@ import io.lettuce.core.api.reactive.RedisReactiveCommands; import io.lettuce.core.api.StatefulRedisConnection; // REMOVE_START -import org.junit.Test; +import org.junit.jupiter.api.Test; // REMOVE_END import reactor.core.publisher.Mono; import java.util.*; // REMOVE_START -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; // REMOVE_END public class StringExample { @@ -30,11 +30,11 @@ public void run() { Mono setAndGet = reactiveCommands.set("bike:1", "Deimos").doOnNext(v -> { System.out.println(v); // OK // REMOVE_START - assertEquals("OK", v); + assertThat(v).isEqualTo("OK"); // REMOVE_END }).flatMap(v -> reactiveCommands.get("bike:1")).doOnNext(res -> { // REMOVE_START - assertEquals("Deimos", res); + assertThat(res).isEqualTo("Deimos"); // REMOVE_END System.out.println(res); // Deimos }).then(); @@ -44,18 +44,18 @@ public void run() { Mono setnx = reactiveCommands.setnx("bike:1", "bike").doOnNext(v -> { System.out.println(v); // false (because key already exists) // REMOVE_START - assertEquals(false, v); + assertThat(v).isFalse(); // REMOVE_END }).flatMap(v -> reactiveCommands.get("bike:1")).doOnNext(res -> { // REMOVE_START - assertEquals("Deimos", res); + assertThat(res).isEqualTo("Deimos"); // REMOVE_END System.out.println(res); // Deimos (value is unchanged) }).then(); Mono setxx = reactiveCommands.set("bike:1", "bike", SetArgs.Builder.xx()).doOnNext(res -> { // REMOVE_START - assertEquals("OK", res); + assertThat(res).isEqualTo("OK"); // REMOVE_END System.out.println(res); // OK }).then(); @@ -73,7 +73,7 @@ public void run() { Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"), KeyValue.just("bike:3", "Vanth"))); // REMOVE_START - assertEquals(expected, res); + assertThat(res).isEqualTo(expected); // REMOVE_END System.out.println(res); // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, Vanth]] }).then(); @@ -84,11 +84,11 @@ public void run() { .doOnNext(v -> { System.out.println(v); // 1 // REMOVE_START - assertEquals(1L, v.longValue()); + assertThat(v).isEqualTo(1L); // REMOVE_END }).flatMap(v -> reactiveCommands.incrby("total_crashes", 10)).doOnNext(res -> { // REMOVE_START - assertEquals(11L, res.longValue()); + assertThat(res).isEqualTo(11L); // REMOVE_END System.out.println(res); // 11 }).then();