Skip to content

Commit

Permalink
replace usage of guava with standard java #1207
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Oct 16, 2023
1 parent 9289689 commit 5f86725
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 85 deletions.
1 change: 1 addition & 0 deletions jetcd-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

dependencies {
api libs.slf4j
api libs.guava
api libs.grpcCore

testImplementation libs.bundles.testing
Expand Down
1 change: 1 addition & 0 deletions jetcd-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
api project(':jetcd-common')

api libs.slf4j
api libs.guava
api libs.failsafe

//compileOnly libs.javaxAnnotation
Expand Down
8 changes: 4 additions & 4 deletions jetcd-core/src/main/java/io/etcd/jetcd/op/TxnImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -60,7 +60,7 @@ private TxnImpl(Function<TxnRequest, CompletableFuture<TxnResponse>> f, ByteSequ

@Override
public TxnImpl If(Cmp... cmps) {
return If(ImmutableList.copyOf(cmps));
return If(Arrays.asList(cmps));
}

TxnImpl If(List<Cmp> cmps) {
Expand All @@ -77,7 +77,7 @@ TxnImpl If(List<Cmp> cmps) {

@Override
public TxnImpl Then(Op... ops) {
return Then(ImmutableList.copyOf(ops));
return Then(Arrays.asList(ops));
}

TxnImpl Then(List<Op> ops) {
Expand All @@ -93,7 +93,7 @@ TxnImpl Then(List<Op> ops) {

@Override
public TxnImpl Else(Op... ops) {
return Else(ImmutableList.copyOf(ops));
return Else(Arrays.asList(ops));
}

TxnImpl Else(List<Op> ops) {
Expand Down
44 changes: 24 additions & 20 deletions jetcd-core/src/test/java/io/etcd/jetcd/impl/KVTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.*;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
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 io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
Expand All @@ -45,7 +45,11 @@
import io.etcd.jetcd.options.PutOption;
import io.etcd.jetcd.test.EtcdClusterExtension;

import static com.google.common.base.Charsets.UTF_8;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;

import static io.etcd.jetcd.impl.TestUtil.bytesOf;
import static io.etcd.jetcd.impl.TestUtil.randomString;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -81,7 +85,7 @@ 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);
}

Expand All @@ -108,7 +112,7 @@ public void testGet() throws Exception {
CompletableFuture<GetResponse> 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();
}

Expand All @@ -121,7 +125,7 @@ public void testGetWithRev() throws Exception {
CompletableFuture<GetResponse> 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
Expand All @@ -137,8 +141,8 @@ 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));
}
}

Expand Down Expand Up @@ -207,7 +211,7 @@ 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
Expand Down Expand Up @@ -255,7 +259,7 @@ 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
Expand All @@ -276,11 +280,11 @@ 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
Expand All @@ -293,7 +297,7 @@ public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
.retryMaxDelay(30)
.retryChronoUnit(ChronoUnit.SECONDS)
.build()) {
ByteSequence key = ByteSequence.from("retry_dummy_key", StandardCharsets.UTF_8);
ByteSequence key = ByteSequence.from("retry_dummy_key", StandardCharsets.StandardCharsets.UTF_8);
int putCount = 1000;

ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
Expand All @@ -302,7 +306,7 @@ public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
executor.submit(() -> {
for (int i = 0; i < putCount; ++i) {
ByteSequence value = ByteSequence
.from(Integer.toString(i), StandardCharsets.UTF_8);
.from(Integer.toString(i), StandardCharsets.StandardCharsets.UTF_8);
customClient.getKVClient().put(key, value).join();
}
});
Expand All @@ -323,7 +327,7 @@ public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
.isEqualTo(1);

ByteSequence lastPutValue = ByteSequence
.from(Integer.toString(putCount - 1), StandardCharsets.UTF_8);
.from(Integer.toString(putCount - 1), StandardCharsets.StandardCharsets.UTF_8);
assertThat(getResponse.getKvs().get(0).getValue())
.as("The sequence of put operations should finish successfully. " +
"Last seen value should match the expected value.")
Expand All @@ -346,7 +350,7 @@ public void waitForReadySemantics() throws ExecutionException, InterruptedExcept

KV kvClient = customClient.getKVClient();

CompletableFuture<String> future = kvClient.get(ByteSequence.from("/x", StandardCharsets.UTF_8))
CompletableFuture<String> future = kvClient.get(ByteSequence.from("/x", StandardCharsets.StandardCharsets.UTF_8))
.thenApply(response -> "we got a response")
.exceptionally(throwable -> "completed exceptionally");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,15 +26,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.assertj.core.data.Percentage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KV;
Expand All @@ -42,7 +34,14 @@
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;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -62,8 +61,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() {
Expand Down
23 changes: 12 additions & 11 deletions jetcd-core/src/test/java/io/etcd/jetcd/impl/LeaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@

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;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KV;
Expand All @@ -40,9 +35,15 @@
import io.etcd.jetcd.test.EtcdClusterExtension;
import io.grpc.stub.StreamObserver;

import com.google.common.base.Charsets;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;

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)
Expand All @@ -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() {
Expand Down
25 changes: 12 additions & 13 deletions jetcd-core/src/test/java/io/etcd/jetcd/impl/LockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@

package io.etcd.jetcd.impl;

import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.Lease;
import io.etcd.jetcd.Lock;
import io.etcd.jetcd.lease.LeaseGrantResponse;
import io.etcd.jetcd.lock.LockResponse;
import io.etcd.jetcd.test.EtcdClusterExtension;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -33,16 +42,6 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.Lease;
import io.etcd.jetcd.Lock;
import io.etcd.jetcd.lease.LeaseGrantResponse;
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;

Expand All @@ -57,9 +56,9 @@ public class LockTest {
private static Lease leaseClient;
private Set<ByteSequence> 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() {
Expand Down
9 changes: 4 additions & 5 deletions jetcd-core/src/test/java/io/etcd/jetcd/impl/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,27 +28,25 @@
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() {
return java.util.UUID.randomUUID().toString();
}

public static ByteSequence randomByteSequence() {
return ByteSequence.from(randomString(), Charsets.UTF_8);
return ByteSequence.from(randomString(), StandardCharsets.UTF_8);
}

public static int findNextAvailablePort() throws IOException {
Expand Down
Loading

0 comments on commit 5f86725

Please sign in to comment.