Skip to content

Commit

Permalink
replace usage of guava with standard java #1207
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Burgazzoli <lburgazzoli@gmail.com>
  • Loading branch information
lburgazzoli committed Oct 16, 2023
1 parent 2fac4da commit 939341a
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 58 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
35 changes: 24 additions & 11 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,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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -108,7 +113,8 @@ 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 +127,8 @@ 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 +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));
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
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,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;
Expand All @@ -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)
Expand All @@ -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() {
Expand Down
13 changes: 7 additions & 6 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,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;
Expand All @@ -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)
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
9 changes: 4 additions & 5 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,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;
Expand All @@ -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;

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
10 changes: 4 additions & 6 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,24 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.etcd.jetcd.examples.ctl;

import java.nio.charset.StandardCharsets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -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);
Expand All @@ -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()) {
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 939341a

Please sign in to comment.