Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Use assertInstanceOf where relevant. #1078

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion java/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {

// junit
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.12.4'
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testImplementation('org.junit.jupiter:junit-jupiter:5.10.1')
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.12.4'
}

Expand Down
19 changes: 10 additions & 9 deletions java/client/src/test/java/glide/ExceptionHandlingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static glide.ffi.resolvers.SocketListenerResolver.getSocket;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static redis_request.RedisRequestOuterClass.RequestType.CustomCommand;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void channel_is_closed_when_failed_to_connect() {
var exception = assertThrows(ExecutionException.class, future::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by ConnectionManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
assertTrue(channelHandler.wasClosed);
}

Expand All @@ -99,7 +100,7 @@ public void channel_is_closed_when_disconnected_on_command() {
var exception = assertThrows(ExecutionException.class, future::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
// check the channel
assertTrue(channelHandler.wasClosed);
}
Expand All @@ -116,7 +117,7 @@ public void channel_is_not_closed_when_error_was_in_command_pipeline() {
var exception = assertThrows(ExecutionException.class, future::get);
// a RequestException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, exception.getCause());
// check the channel
assertFalse(channelHandler.wasClosed);
}
Expand All @@ -133,8 +134,8 @@ public void command_manager_rethrows_non_RedisException_too() {
var exception = assertThrows(ExecutionException.class, future::get);
// a IOException thrown from CallbackDispatcher::completeRequest and then wrapped
// by a RuntimeException and rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof RuntimeException);
assertTrue(exception.getCause().getCause() instanceof IOException);
assertInstanceOf(RuntimeException.class, exception.getCause());
assertInstanceOf(IOException.class, exception.getCause().getCause());
// check the channel
assertFalse(channelHandler.wasClosed);
}
Expand All @@ -152,8 +153,8 @@ public void connection_manager_rethrows_non_RedisException_too() {
var exception = assertThrows(ExecutionException.class, future::get);
// a IOException thrown from CallbackDispatcher::completeRequest and then wrapped
// by a RuntimeException and rethrown by ConnectionManager::exceptionHandler
assertTrue(exception.getCause() instanceof RuntimeException);
assertTrue(exception.getCause().getCause() instanceof IOException);
assertInstanceOf(RuntimeException.class, exception.getCause());
assertInstanceOf(IOException.class, exception.getCause().getCause());
// check the channel
assertTrue(channelHandler.wasClosed);
}
Expand All @@ -175,7 +176,7 @@ public void close_connection_on_response_with_closing_error() {
var exception = assertThrows(ExecutionException.class, future1::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
// check the channel
assertTrue(channelHandler.wasClosed);

Expand Down Expand Up @@ -243,7 +244,7 @@ public void close_connection_on_response_without_error_but_with_incorrect_callba
var exception = assertThrows(ExecutionException.class, future1::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
assertEquals(
exception.getCause().getMessage(), "Client is in an erroneous state and should close");
// check the channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -146,7 +147,7 @@ public Response.Builder redisRequest(RedisRequest request) {

var exception = assertThrows(ExecutionException.class, () -> testConnection().get(1, SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof ClosingException),
() -> assertInstanceOf(ClosingException.class, exception.getCause()),
() -> assertEquals("You shall not pass!", exception.getCause().getMessage()));
}

Expand All @@ -157,7 +158,7 @@ public void rethrow_error_on_read_when_malformed_packet_received() {

var exception = assertThrows(ExecutionException.class, () -> testConnection().get(1, SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof ClosingException),
() -> assertInstanceOf(ClosingException.class, exception.getCause()),
() ->
assertTrue(
exception
Expand All @@ -175,7 +176,7 @@ public void rethrow_error_if_UDS_channel_closed() {
var exception =
assertThrows(
ExecutionException.class, () -> client.customCommand(new String[0]).get(1, SECONDS));
assertTrue(exception.getCause() instanceof RuntimeException);
assertInstanceOf(RuntimeException.class, exception.getCause());

// Not a public class, can't import
assertEquals(
Expand Down
9 changes: 5 additions & 4 deletions java/client/src/test/java/glide/ffi/FfiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -69,7 +70,7 @@ public void redisValueToJavaValue_Okay() {
public void redisValueToJavaValue_Int(Long input) {
long ptr = FfiTest.createLeakedInt(input);
Object longValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(longValue instanceof Long);
assertInstanceOf(Long.class, longValue);
assertEquals(input, longValue);
}

Expand All @@ -87,7 +88,7 @@ public void redisValueToJavaValue_Array() {
long[] array = {1L, 2L, 3L};
long ptr = FfiTest.createLeakedLongArray(array);
Object longArrayValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(longArrayValue instanceof Object[]);
assertInstanceOf(Object[].class, longArrayValue);
Object[] result = (Object[]) longArrayValue;
assertArrayEquals(new Object[] {1L, 2L, 3L}, result);
}
Expand All @@ -98,7 +99,7 @@ public void redisValueToJavaValue_Map() {
long[] values = {1L, 2L, 3L};
long ptr = FfiTest.createLeakedMap(keys, values);
Object mapValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(mapValue instanceof HashMap);
assertInstanceOf(HashMap.class, mapValue);
HashMap<Object, Object> result = (HashMap<Object, Object>) mapValue;
assertAll(
() -> assertEquals(1L, result.get(12L)),
Expand Down Expand Up @@ -134,7 +135,7 @@ public void redisValueToJavaValue_Set() {
long[] array = {1L, 2L, 2L};
long ptr = FfiTest.createLeakedLongSet(array);
Object longSetValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(longSetValue instanceof HashSet);
assertInstanceOf(HashSet.class, longSetValue);
HashSet<Long> result = (HashSet<Long>) longSetValue;
assertAll(
() -> assertTrue(result.contains(1L)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void submitNewCommand_return_String_result() {
Object respPointer = result.get();

// verify
assertTrue(respPointer instanceof String);
assertInstanceOf(String.class, respPointer);
assertEquals(testString, respPointer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static glide.api.models.configuration.NodeAddress.DEFAULT_HOST;
import static glide.api.models.configuration.NodeAddress.DEFAULT_PORT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -209,7 +209,7 @@ public void connection_on_empty_response_throws_ClosingException() {
ExecutionException.class,
() -> connectionManager.connectToRedis(redisClientConfiguration).get());

assertTrue(executionException.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, executionException.getCause());
assertEquals("Unexpected empty data in response", executionException.getCause().getMessage());
verify(channel).close();
}
Expand All @@ -229,7 +229,7 @@ public void connection_on_resp_pointer_throws_ClosingException() {
ExecutionException.class,
() -> connectionManager.connectToRedis(redisClientConfiguration).get());

assertTrue(executionException.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, executionException.getCause());
assertEquals("Unexpected data in response", executionException.getCause().getMessage());
verify(channel).close();
}
Expand Down
2 changes: 1 addition & 1 deletion java/integTest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
testAnnotationProcessor 'org.projectlombok:lombok:1.18.30'

// junit
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
testImplementation 'org.mockito:mockito-junit-jupiter:3.12.4'
}

Expand Down
7 changes: 4 additions & 3 deletions java/integTest/src/test/java/glide/ErrorHandlingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package glide;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -31,7 +32,7 @@ public void basic_client_tries_to_connect_to_wrong_address() {
.build())
.get(10, TimeUnit.SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof ClosingException),
() -> assertInstanceOf(ClosingException.class, exception.getCause()),
() -> assertTrue(exception.getCause().getMessage().contains("Connection refused")));
}

Expand All @@ -50,7 +51,7 @@ public void basic_client_tries_wrong_command() {
ExecutionException.class,
() -> regularClient.customCommand(new String[] {"pewpew"}).get(10, TimeUnit.SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof RequestException),
() -> assertInstanceOf(RequestException.class, exception.getCause()),
() -> assertTrue(exception.getCause().getMessage().contains("unknown command")));
}
}
Expand All @@ -73,7 +74,7 @@ public void basic_client_tries_wrong_command_arguments() {
.customCommand(new String[] {"ping", "pang", "pong"})
.get(10, TimeUnit.SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof RequestException),
() -> assertInstanceOf(RequestException.class, exception.getCause()),
() ->
assertTrue(exception.getCause().getMessage().contains("wrong number of arguments")));
}
Expand Down
35 changes: 18 additions & 17 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -365,15 +366,15 @@ public void test_incr_commands_type_error(BaseClient client) {
assertEquals(OK, client.set(key1, "foo").get());

Exception incrException = assertThrows(ExecutionException.class, () -> client.incr(key1).get());
assertTrue(incrException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, incrException.getCause());

Exception incrByException =
assertThrows(ExecutionException.class, () -> client.incrBy(key1, 3).get());
assertTrue(incrByException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, incrByException.getCause());

Exception incrByFloatException =
assertThrows(ExecutionException.class, () -> client.incrByFloat(key1, 3.5).get());
assertTrue(incrByFloatException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, incrByFloatException.getCause());
}

@SneakyThrows
Expand Down Expand Up @@ -535,11 +536,11 @@ public void hincrBy_hincrByFloat_type_error(BaseClient client) {

Exception hincrByException =
assertThrows(ExecutionException.class, () -> client.hincrBy(key, field, 2).get());
assertTrue(hincrByException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, hincrByException.getCause());

Exception hincrByFloatException =
assertThrows(ExecutionException.class, () -> client.hincrByFloat(key, field, 2.5).get());
assertTrue(hincrByFloatException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, hincrByFloatException.getCause());
}

@SneakyThrows
Expand Down Expand Up @@ -567,18 +568,18 @@ public void lpush_lpop_lrange_type_error(BaseClient client) {

Exception lpushException =
assertThrows(ExecutionException.class, () -> client.lpush(key, new String[] {"foo"}).get());
assertTrue(lpushException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, lpushException.getCause());

Exception lpopException = assertThrows(ExecutionException.class, () -> client.lpop(key).get());
assertTrue(lpopException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, lpopException.getCause());

Exception lpopCountException =
assertThrows(ExecutionException.class, () -> client.lpopCount(key, 2).get());
assertTrue(lpopCountException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, lpopCountException.getCause());

Exception lrangeException =
assertThrows(ExecutionException.class, () -> client.lrange(key, 0, -1).get());
assertTrue(lrangeException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, lrangeException.getCause());
}

@SneakyThrows
Expand All @@ -600,7 +601,7 @@ public void ltrim_existing_non_existing_key_and_type_error(BaseClient client) {

Exception ltrimException =
assertThrows(ExecutionException.class, () -> client.ltrim(key, 0, 1).get());
assertTrue(ltrimException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, ltrimException.getCause());
}

@SneakyThrows
Expand All @@ -619,7 +620,7 @@ public void llen_existing_non_existing_key_and_type_error(BaseClient client) {

Exception lrangeException =
assertThrows(ExecutionException.class, () -> client.llen(key2).get());
assertTrue(lrangeException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, lrangeException.getCause());
}

@SneakyThrows
Expand All @@ -646,10 +647,10 @@ public void rpush_rpop_type_error(BaseClient client) {

Exception rpushException =
assertThrows(ExecutionException.class, () -> client.rpush(key, new String[] {"foo"}).get());
assertTrue(rpushException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, rpushException.getCause());

Exception rpopException = assertThrows(ExecutionException.class, () -> client.rpop(key).get());
assertTrue(rpopException.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, rpopException.getCause());
}

@SneakyThrows
Expand Down Expand Up @@ -685,16 +686,16 @@ public void sadd_srem_scard_smembers_key_with_non_set_value(BaseClient client) {

Exception e =
assertThrows(ExecutionException.class, () -> client.sadd(key, new String[] {"baz"}).get());
assertTrue(e.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, e.getCause());

e = assertThrows(ExecutionException.class, () -> client.srem(key, new String[] {"baz"}).get());
assertTrue(e.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, e.getCause());

e = assertThrows(ExecutionException.class, () -> client.scard(key).get());
assertTrue(e.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, e.getCause());

e = assertThrows(ExecutionException.class, () -> client.smembers(key).get());
assertTrue(e.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, e.getCause());
}

@SneakyThrows
Expand Down
Loading
Loading