Skip to content

Commit

Permalink
Do not use utility classes from dependencies
Browse files Browse the repository at this point in the history
We have been avoiding this previously and we need to continue keeping
out of any unnecessary dependencies in the code, especially we should
not use any internal classes and transitive dependencies of the
dependencies, because it easily breaks dependency updates.
  • Loading branch information
akudiyar committed Aug 2, 2023
1 parent 3ddf1ea commit cd1d0f1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/test/java/io/tarantool/driver/TarantoolUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.tarantool.driver;

import io.tarantool.driver.utils.Assert;
import org.testcontainers.shaded.org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -18,13 +17,14 @@ private TarantoolUtils() {
public static boolean versionGreaterOrEqualThen(String tarantoolVersion) {
Assert.notNull(tarantoolVersion, "tarantoolVersion must not be null");
String tarantoolCiVersion = java.lang.System.getenv(TARANTOOL_VERSION);
if (StringUtils.isEmpty(tarantoolCiVersion)) {
if (tarantoolCiVersion == null || tarantoolCiVersion.isEmpty()) {
return true;
}
TarantoolVersion ciVersion = new TarantoolVersion(tarantoolCiVersion);
TarantoolVersion version = new TarantoolVersion(tarantoolVersion);
return ciVersion.getMajor() >= version.getMajor() &&
ciVersion.getMinor() >= version.getMinor();

}

public static boolean versionWithUUID() {
Expand All @@ -44,7 +44,8 @@ public static class TarantoolVersion {
private Integer minor;

public TarantoolVersion(String stringVersion) {
List<String> majorMinor = StringUtils.isEmpty(stringVersion) ? Collections.emptyList() :
List<String> majorMinor = stringVersion == null || stringVersion.isEmpty() ?
Collections.emptyList() :
Arrays.stream(stringVersion.split("\\."))
.collect(Collectors.toList());
if (majorMinor.size() >= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.util.ClassUtil;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -364,6 +363,6 @@ public void test_AddressProviderReturnsNull_shouldThrowTarantoolClientException(

// then
TarantoolClientException exception = assertThrows(TarantoolClientException.class, client::getVersion);
assertFalse(ClassUtil.getRootCause(exception) instanceof NullPointerException);
assertFalse(Utils.getRootCause(exception) instanceof NullPointerException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.testcontainers.shaded.org.apache.commons.lang3.ArrayUtils;

import java.time.Instant;
import java.util.UUID;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -87,7 +85,7 @@ public void test_boxSelect_shouldReturnTupleWithInstant() throws Exception {
public void test_boxOperations_shouldWorkWithVarbinary() throws Exception {
//given
byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
List<Byte> byteList = Arrays.asList(ArrayUtils.toObject(bytes));
List<Byte> byteList = Utils.convertBytesToByteList(bytes);
client.space("space_with_varbinary")
.insert(tupleFactory.create(1, bytes)).get();

Expand All @@ -98,7 +96,7 @@ public void test_boxOperations_shouldWorkWithVarbinary() throws Exception {

//then
byte[] bytesFromTarantool = fields.getByteArray("varbinary_field");
List<Byte> byteListFromTarantool = Arrays.asList(ArrayUtils.toObject(bytesFromTarantool));
List<Byte> byteListFromTarantool = Utils.convertBytesToByteList(bytesFromTarantool);
Assertions.assertEquals(byteList, byteListFromTarantool);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.testcontainers.shaded.org.apache.commons.lang3.ArrayUtils;

import java.time.Instant;
import java.util.UUID;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -89,7 +87,7 @@ public void test_crudSelect_shouldReturnTupleWithInstant() throws Exception {
public void test_crudOperations_shouldWorkWithVarbinary() throws Exception {
//given
byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
List<Byte> byteList = Arrays.asList(ArrayUtils.toObject(bytes));
List<Byte> byteList = Utils.convertBytesToByteList(bytes);
client.space("space_with_varbinary")
.insert(tupleFactory.create(1, bytes)).get();

Expand All @@ -100,15 +98,15 @@ public void test_crudOperations_shouldWorkWithVarbinary() throws Exception {

//then
byte[] bytesFromTarantool = fields.getByteArray("varbinary_field");
List<Byte> byteListFromTarantool = Arrays.asList(ArrayUtils.toObject(bytesFromTarantool));
List<Byte> byteListFromTarantool = Utils.convertBytesToByteList(bytesFromTarantool);
Assertions.assertEquals(byteList, byteListFromTarantool);
}

@Test
public void test_crudOperations_shouldWorkWithBytesAsString() throws Exception {
//given
byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
List<Byte> byteList = Arrays.asList(ArrayUtils.toObject(bytes));
List<Byte> byteList = Utils.convertBytesToByteList(bytes);
client.space("space_with_string")
.insert(tupleFactory.create(1, bytes)).get();

Expand All @@ -119,7 +117,7 @@ public void test_crudOperations_shouldWorkWithBytesAsString() throws Exception {

//then
byte[] bytesFromTarantool = fields.getByteArray("string_field");
List<Byte> byteListFromTarantool = Arrays.asList(ArrayUtils.toObject(bytesFromTarantool));
List<Byte> byteListFromTarantool = Utils.convertBytesToByteList(bytesFromTarantool);
Assertions.assertEquals(byteList, byteListFromTarantool);
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/tarantool/driver/integration/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -103,4 +105,26 @@ private static long crc32(byte[] data) {
crc32 = Integer.reverse(crc32); // result reflect
return crc32 & 0x00000000ffffffffL; // the unsigned java problem
}

/**
* Converts a byte array to a list of bytes
*
* @param bytes byte array
*/
static List<Byte> convertBytesToByteList(byte[] bytes) {
return IntStream.range(0, bytes.length).mapToObj(i -> bytes[i]).collect(Collectors.toList());
}

/**
* Find the rootmost non-null cause of an Exception recursively
*
* @param t exception
* @return root cause of the exception or the exception itself
*/
static Throwable getRootCause(Throwable t) {
while (t.getCause() != null) {
t = t.getCause();
}
return t;
}
}

0 comments on commit cd1d0f1

Please sign in to comment.