From 0071ebf22650e84845a8f165d90eba7dff556520 Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Mon, 16 Oct 2023 12:00:09 +0200 Subject: [PATCH] replace usage of guava with standard java #1207 Signed-off-by: Luca Burgazzoli --- jetcd-common/build.gradle | 1 + jetcd-core/build.gradle | 1 + .../main/java/io/etcd/jetcd/op/TxnImpl.java | 8 ++--- .../test/java/io/etcd/jetcd/impl/KVTest.java | 35 +++++++++++++------ .../etcd/jetcd/impl/LeaseMemoryLeakTest.java | 23 ++++++------ .../java/io/etcd/jetcd/impl/LeaseTest.java | 13 +++---- .../java/io/etcd/jetcd/impl/LockTest.java | 9 +++-- .../java/io/etcd/jetcd/impl/TestUtil.java | 10 +++--- .../etcd/jetcd/examples/ctl/CommandGet.java | 8 ++--- .../etcd/jetcd/examples/ctl/CommandPut.java | 8 ++--- .../etcd/jetcd/examples/ctl/CommandWatch.java | 11 +++--- jetcd-launcher/build.gradle | 1 + .../java/io/etcd/jetcd/launcher/Etcd.java | 3 +- jetcd-test/build.gradle | 1 + 14 files changed, 74 insertions(+), 58 deletions(-) diff --git a/jetcd-common/build.gradle b/jetcd-common/build.gradle index ec7241b21..4e16d87e3 100644 --- a/jetcd-common/build.gradle +++ b/jetcd-common/build.gradle @@ -17,6 +17,7 @@ dependencies { api libs.slf4j + api libs.guava api libs.grpcCore testImplementation libs.bundles.testing diff --git a/jetcd-core/build.gradle b/jetcd-core/build.gradle index aa05ddee0..5e8b0cb45 100644 --- a/jetcd-core/build.gradle +++ b/jetcd-core/build.gradle @@ -20,6 +20,7 @@ dependencies { api project(':jetcd-common') api libs.slf4j + api libs.guava api libs.failsafe //compileOnly libs.javaxAnnotation diff --git a/jetcd-core/src/main/java/io/etcd/jetcd/op/TxnImpl.java b/jetcd-core/src/main/java/io/etcd/jetcd/op/TxnImpl.java index 2c41328e9..3d9e12cd8 100644 --- a/jetcd-core/src/main/java/io/etcd/jetcd/op/TxnImpl.java +++ b/jetcd-core/src/main/java/io/etcd/jetcd/op/TxnImpl.java @@ -17,6 +17,7 @@ package io.etcd.jetcd.op; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @@ -27,7 +28,6 @@ import io.etcd.jetcd.kv.TxnResponse; import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableList; /** * Build an etcd transaction. @@ -60,7 +60,7 @@ private TxnImpl(Function> f, ByteSequ @Override public TxnImpl If(Cmp... cmps) { - return If(ImmutableList.copyOf(cmps)); + return If(Arrays.asList(cmps)); } TxnImpl If(List cmps) { @@ -77,7 +77,7 @@ TxnImpl If(List cmps) { @Override public TxnImpl Then(Op... ops) { - return Then(ImmutableList.copyOf(ops)); + return Then(Arrays.asList(ops)); } TxnImpl Then(List ops) { @@ -93,7 +93,7 @@ TxnImpl Then(List ops) { @Override public TxnImpl Else(Op... ops) { - return Else(ImmutableList.copyOf(ops)); + return Else(Arrays.asList(ops)); } TxnImpl Else(List ops) { diff --git a/jetcd-core/src/test/java/io/etcd/jetcd/impl/KVTest.java b/jetcd-core/src/test/java/io/etcd/jetcd/impl/KVTest.java index af0c9aea7..d7a675e76 100644 --- a/jetcd-core/src/test/java/io/etcd/jetcd/impl/KVTest.java +++ b/jetcd-core/src/test/java/io/etcd/jetcd/impl/KVTest.java @@ -20,7 +20,12 @@ import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.List; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -45,7 +50,6 @@ import io.etcd.jetcd.options.PutOption; import io.etcd.jetcd.test.EtcdClusterExtension; -import static com.google.common.base.Charsets.UTF_8; import static io.etcd.jetcd.impl.TestUtil.bytesOf; import static io.etcd.jetcd.impl.TestUtil.randomString; import static org.assertj.core.api.Assertions.assertThat; @@ -81,7 +85,8 @@ public void testByteSequence() { ByteSequence key = bytesOf(keyString); ByteSequence prefixedKey = prefix.concat(subPrefix).concat(key); assertThat(prefixedKey.startsWith(prefix)).isTrue(); - assertThat(prefixedKey.substring(prefix.size() + subPrefix.size()).toString(UTF_8)).isEqualTo(keyString); + assertThat(prefixedKey.substring(prefix.size() + subPrefix.size()).toString(StandardCharsets.UTF_8)) + .isEqualTo(keyString); assertThat(prefixedKey.substring(prefix.size(), prefix.size() + subPrefix.size())).isEqualTo(subPrefix); } @@ -108,7 +113,8 @@ public void testGet() throws Exception { CompletableFuture getFeature = kvClient.get(SAMPLE_KEY_2); GetResponse response = getFeature.get(); assertThat(response.getKvs()).hasSize(1); - assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE_2.toString(UTF_8)); + assertThat(response.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(SAMPLE_VALUE_2.toString(StandardCharsets.UTF_8)); assertThat(!response.isMore()).isTrue(); } @@ -121,7 +127,8 @@ public void testGetWithRev() throws Exception { CompletableFuture getFeature = kvClient.get(SAMPLE_KEY_3, option); GetResponse response = getFeature.get(); assertThat(response.getKvs()).hasSize(1); - assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE.toString(UTF_8)); + assertThat(response.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(SAMPLE_VALUE.toString(StandardCharsets.UTF_8)); } @Test @@ -137,8 +144,10 @@ public void testGetSortedPrefix() throws Exception { assertThat(response.getKvs()).hasSize(numPrefix); for (int i = 0; i < numPrefix; i++) { - assertThat(response.getKvs().get(i).getKey().toString(UTF_8)).isEqualTo(prefix + (numPrefix - i - 1)); - assertThat(response.getKvs().get(i).getValue().toString(UTF_8)).isEqualTo(String.valueOf(numPrefix - i - 1)); + assertThat(response.getKvs().get(i).getKey().toString(StandardCharsets.UTF_8)) + .isEqualTo(prefix + (numPrefix - i - 1)); + assertThat(response.getKvs().get(i).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(String.valueOf(numPrefix - i - 1)); } } @@ -207,7 +216,8 @@ public void testTxn() throws Exception { // get the value GetResponse getResp = kvClient.get(sampleKey).get(); assertThat(getResp.getKvs()).hasSize(1); - assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8)); + assertThat(getResp.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(putValue.toString(StandardCharsets.UTF_8)); } @Test @@ -255,7 +265,8 @@ public void testTxnForCmpOpNotEqual() throws Exception { // get the value GetResponse getResp = kvClient.get(sampleKey).get(); assertThat(getResp.getKvs()).hasSize(1); - assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8)); + assertThat(getResp.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(putValue.toString(StandardCharsets.UTF_8)); } @Test @@ -276,11 +287,13 @@ public void testNestedTxn() throws Exception { GetResponse getResp = kvClient.get(foo).get(); assertThat(getResp.getKvs()).hasSize(1); - assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(bar.toString(UTF_8)); + assertThat(getResp.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(bar.toString(StandardCharsets.UTF_8)); GetResponse getResp2 = kvClient.get(abc).get(); assertThat(getResp2.getKvs()).hasSize(1); - assertThat(getResp2.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(oneTwoThree.toString(UTF_8)); + assertThat(getResp2.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)) + .isEqualTo(oneTwoThree.toString(StandardCharsets.UTF_8)); } @Test diff --git a/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseMemoryLeakTest.java b/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseMemoryLeakTest.java index d0439d6f7..901022dc6 100644 --- a/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseMemoryLeakTest.java +++ b/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseMemoryLeakTest.java @@ -17,6 +17,7 @@ package io.etcd.jetcd.impl; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -25,16 +26,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import io.etcd.jetcd.ByteSequence; -import io.etcd.jetcd.Client; -import io.etcd.jetcd.KV; -import io.etcd.jetcd.Lease; -import io.etcd.jetcd.options.PutOption; -import io.etcd.jetcd.test.EtcdClusterExtension; -import io.restassured.RestAssured; - -import com.google.common.base.Charsets; - import org.assertj.core.data.Percentage; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -45,6 +36,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.KV; +import io.etcd.jetcd.Lease; +import io.etcd.jetcd.options.PutOption; +import io.etcd.jetcd.test.EtcdClusterExtension; +import io.restassured.RestAssured; + import static org.assertj.core.api.Assertions.assertThat; @Timeout(value = 45, unit = TimeUnit.SECONDS) @@ -63,8 +62,8 @@ public class LeaseMemoryLeakTest { private static final long TTL = 10; private static final int ITERATIONS = 5; private static final Pattern GO_ROUTINES_EXTRACT_PATTERN = Pattern.compile("go_goroutines (\\d+)"); - private static final ByteSequence KEY = ByteSequence.from("foo", Charsets.UTF_8); - private static final ByteSequence VALUE = ByteSequence.from("bar", Charsets.UTF_8); + private static final ByteSequence KEY = ByteSequence.from("foo", StandardCharsets.UTF_8); + private static final ByteSequence VALUE = ByteSequence.from("bar", StandardCharsets.UTF_8); @BeforeEach public void setUp() { diff --git a/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseTest.java b/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseTest.java index d5679e802..6f3347a70 100644 --- a/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseTest.java +++ b/jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseTest.java @@ -16,6 +16,7 @@ package io.etcd.jetcd.impl; +import java.nio.charset.StandardCharsets; import java.util.concurrent.ExecutionException; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; @@ -40,9 +41,9 @@ import io.etcd.jetcd.test.EtcdClusterExtension; import io.grpc.stub.StreamObserver; -import com.google.common.base.Charsets; - -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.awaitility.Awaitility.await; @Timeout(value = 30, unit = TimeUnit.SECONDS) @@ -57,9 +58,9 @@ public class LeaseTest { private Client client; private Lease leaseClient; - private static final ByteSequence KEY = ByteSequence.from("foo", Charsets.UTF_8); - private static final ByteSequence KEY_2 = ByteSequence.from("foo2", Charsets.UTF_8); - private static final ByteSequence VALUE = ByteSequence.from("bar", Charsets.UTF_8); + private static final ByteSequence KEY = ByteSequence.from("foo", StandardCharsets.UTF_8); + private static final ByteSequence KEY_2 = ByteSequence.from("foo2", StandardCharsets.UTF_8); + private static final ByteSequence VALUE = ByteSequence.from("bar", StandardCharsets.UTF_8); @BeforeEach public void setUp() { diff --git a/jetcd-core/src/test/java/io/etcd/jetcd/impl/LockTest.java b/jetcd-core/src/test/java/io/etcd/jetcd/impl/LockTest.java index 29e2b7c61..aeb2dd99d 100644 --- a/jetcd-core/src/test/java/io/etcd/jetcd/impl/LockTest.java +++ b/jetcd-core/src/test/java/io/etcd/jetcd/impl/LockTest.java @@ -16,6 +16,7 @@ package io.etcd.jetcd.impl; +import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -41,8 +42,6 @@ import io.etcd.jetcd.lock.LockResponse; import io.etcd.jetcd.test.EtcdClusterExtension; -import com.google.common.base.Charsets; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -57,9 +56,9 @@ public class LockTest { private static Lease leaseClient; private Set locksToRelease; - private static final ByteSequence SAMPLE_NAME = ByteSequence.from("sample_name", Charsets.UTF_8); - private static final ByteSequence namespace = ByteSequence.from("test-ns/", Charsets.UTF_8); - private static final ByteSequence namespace2 = ByteSequence.from("test-ns2/", Charsets.UTF_8); + private static final ByteSequence SAMPLE_NAME = ByteSequence.from("sample_name", StandardCharsets.UTF_8); + private static final ByteSequence namespace = ByteSequence.from("test-ns/", StandardCharsets.UTF_8); + private static final ByteSequence namespace2 = ByteSequence.from("test-ns2/", StandardCharsets.UTF_8); @BeforeAll public static void setUp() { diff --git a/jetcd-core/src/test/java/io/etcd/jetcd/impl/TestUtil.java b/jetcd-core/src/test/java/io/etcd/jetcd/impl/TestUtil.java index 4e6300058..cc928472d 100644 --- a/jetcd-core/src/test/java/io/etcd/jetcd/impl/TestUtil.java +++ b/jetcd-core/src/test/java/io/etcd/jetcd/impl/TestUtil.java @@ -19,6 +19,7 @@ import java.io.Closeable; import java.io.IOException; import java.net.ServerSocket; +import java.nio.charset.StandardCharsets; import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.Client; @@ -27,19 +28,16 @@ import io.etcd.jetcd.test.EtcdClusterExtension; import io.etcd.jetcd.watch.WatchResponse; -import com.google.common.base.Charsets; import com.google.protobuf.ByteString; -import static java.nio.charset.StandardCharsets.UTF_8; - public class TestUtil { public static ByteSequence bytesOf(final String string) { - return ByteSequence.from(string, UTF_8); + return ByteSequence.from(string, StandardCharsets.UTF_8); } public static ByteString byteStringOf(final String string) { - return ByteString.copyFrom(string.getBytes(UTF_8)); + return ByteString.copyFrom(string.getBytes(StandardCharsets.UTF_8)); } public static String randomString() { @@ -47,7 +45,7 @@ public static String randomString() { } public static ByteSequence randomByteSequence() { - return ByteSequence.from(randomString(), Charsets.UTF_8); + return ByteSequence.from(randomString(), StandardCharsets.UTF_8); } public static int findNextAvailablePort() throws IOException { diff --git a/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandGet.java b/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandGet.java index ec1cb5e4a..d4c24d762 100644 --- a/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandGet.java +++ b/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandGet.java @@ -16,6 +16,8 @@ package io.etcd.jetcd.examples.ctl; +import java.nio.charset.StandardCharsets; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,8 +28,6 @@ import picocli.CommandLine; -import static com.google.common.base.Charsets.UTF_8; - @CommandLine.Command(name = "get", description = "Gets the key") class CommandGet implements Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(CommandGet.class); @@ -45,7 +45,7 @@ class CommandGet implements Runnable { public void run() { try (Client client = Client.builder().endpoints(main.endpoints).build()) { GetResponse getResponse = client.getKVClient().get( - ByteSequence.from(key, UTF_8), + ByteSequence.from(key, StandardCharsets.UTF_8), GetOption.builder().withRevision(rev).build()).get(); if (getResponse.getKvs().isEmpty()) { @@ -54,7 +54,7 @@ public void run() { } LOGGER.info(key); - LOGGER.info(getResponse.getKvs().get(0).getValue().toString(UTF_8)); + LOGGER.info(getResponse.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8)); } catch (Exception e) { LOGGER.warn(e.getMessage()); } diff --git a/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandPut.java b/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandPut.java index 67e6c6b0e..506f731d0 100644 --- a/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandPut.java +++ b/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandPut.java @@ -16,14 +16,14 @@ package io.etcd.jetcd.examples.ctl; +import java.nio.charset.StandardCharsets; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.Client; -import com.google.common.base.Charsets; - import picocli.CommandLine; @CommandLine.Command(name = "put", description = "Puts the given key into the store") @@ -42,8 +42,8 @@ class CommandPut implements Runnable { public void run() { try (Client client = Client.builder().endpoints(main.endpoints).build()) { client.getKVClient().put( - ByteSequence.from(key, Charsets.UTF_8), - ByteSequence.from(val, Charsets.UTF_8)).get(); + ByteSequence.from(key, StandardCharsets.UTF_8), + ByteSequence.from(val, StandardCharsets.UTF_8)).get(); LOGGER.info("OK"); } catch (Exception e) { diff --git a/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandWatch.java b/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandWatch.java index c55f8db2c..e096e1bf1 100644 --- a/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandWatch.java +++ b/jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandWatch.java @@ -16,6 +16,7 @@ package io.etcd.jetcd.examples.ctl; +import java.nio.charset.StandardCharsets; import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; @@ -30,8 +31,6 @@ import io.etcd.jetcd.watch.WatchEvent; import io.etcd.jetcd.watch.WatchResponse; -import com.google.common.base.Charsets; - import picocli.CommandLine; @CommandLine.Command(name = "watch", description = "Watches events stream for a key") @@ -54,15 +53,17 @@ public void run() { CountDownLatch latch = new CountDownLatch(maxEvents != null ? maxEvents : Integer.MAX_VALUE); try (Client client = Client.builder().endpoints(main.endpoints).build()) { - ByteSequence watchKey = ByteSequence.from(key, Charsets.UTF_8); + ByteSequence watchKey = ByteSequence.from(key, StandardCharsets.UTF_8); WatchOption watchOpts = WatchOption.builder().withRevision(rev != null ? rev : 0).build(); Consumer consumer = response -> { for (WatchEvent event : response.getEvents()) { LOGGER.info("type={}, key={}, value={}", event.getEventType().toString(), - Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""), - Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(Charsets.UTF_8)).orElse("")); + Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(StandardCharsets.UTF_8)) + .orElse(""), + Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(StandardCharsets.UTF_8)) + .orElse("")); } latch.countDown(); diff --git a/jetcd-launcher/build.gradle b/jetcd-launcher/build.gradle index 67ac044b4..cae35b8d8 100644 --- a/jetcd-launcher/build.gradle +++ b/jetcd-launcher/build.gradle @@ -16,6 +16,7 @@ dependencies { api libs.slf4j + api libs.guava api libs.commonsCompress api libs.testcontainers diff --git a/jetcd-launcher/src/main/java/io/etcd/jetcd/launcher/Etcd.java b/jetcd-launcher/src/main/java/io/etcd/jetcd/launcher/Etcd.java index d4a3f158f..af32f0e44 100644 --- a/jetcd-launcher/src/main/java/io/etcd/jetcd/launcher/Etcd.java +++ b/jetcd-launcher/src/main/java/io/etcd/jetcd/launcher/Etcd.java @@ -24,7 +24,8 @@ import java.util.UUID; import org.testcontainers.containers.Network; -import org.testcontainers.shaded.com.google.common.base.Strings; + +import com.google.common.base.Strings; public final class Etcd { public static final String CONTAINER_IMAGE = "gcr.io/etcd-development/etcd:v3.5.9"; diff --git a/jetcd-test/build.gradle b/jetcd-test/build.gradle index df585c09c..86cbd15b7 100644 --- a/jetcd-test/build.gradle +++ b/jetcd-test/build.gradle @@ -19,6 +19,7 @@ dependencies { api project(':jetcd-launcher') api libs.slf4j + api libs.guava api libs.bundles.grpc api libs.bundles.grpcTest