Skip to content

Refactor Tests to instanceof pattern variable #2971

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

Closed
wants to merge 3 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private Class<T> resolveReturnedClassFromGernericType() {
private ParameterizedType resolveReturnedClassFromGernericType(Class<?> clazz) {

Object genericSuperclass = clazz.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
if (genericSuperclass instanceof ParameterizedType parameterizedType) {
Type rawtype = parameterizedType.getRawType();
if (AbstractConnectionUnitTestBase.class.equals(rawtype)) {
return parameterizedType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public void tearDown() {
if (nativeCommands != null) {
nativeCommands.flushall();

if (nativeCommands instanceof RedisCommands) {
((RedisCommands) nativeCommands).getStatefulConnection().close();
if (nativeCommands instanceof RedisCommands redisCommands) {
redisCommands.getStatefulConnection().close();
}

if (nativeCommands instanceof RedisAdvancedClusterCommands) {
((RedisAdvancedClusterCommands) nativeCommands).getStatefulConnection().close();
if (nativeCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
redisAdvancedClusterCommands.getStatefulConnection().close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ public String toString() {
public void close() throws IOException {

try {
if (connectionProvider instanceof DisposableBean) {
((DisposableBean) connectionProvider).destroy();
if (connectionProvider instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}

if (nativeConnectionProvider instanceof DisposableBean) {
((DisposableBean) nativeConnectionProvider).destroy();
if (nativeConnectionProvider instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}

if (nativeBinaryConnectionProvider instanceof DisposableBean) {
((DisposableBean) nativeBinaryConnectionProvider).destroy();
if (nativeBinaryConnectionProvider instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}
} catch (Exception ex) {
throw new RuntimeException(ex);
Expand Down Expand Up @@ -198,21 +198,21 @@ public void tearDown() {
if (nativeCommands != null) {
flushAll();

if (nativeCommands instanceof RedisCommands) {
nativeConnectionProvider.release(((RedisCommands) nativeCommands).getStatefulConnection());
if (nativeCommands instanceof RedisCommands redisCommands) {
nativeConnectionProvider.release((redisCommands).getStatefulConnection());
}

if (nativeCommands instanceof RedisAdvancedClusterCommands) {
nativeConnectionProvider.release(((RedisAdvancedClusterCommands) nativeCommands).getStatefulConnection());
if (nativeCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
nativeConnectionProvider.release((redisAdvancedClusterCommands).getStatefulConnection());
}

if (nativeBinaryCommands instanceof RedisCommands) {
nativeBinaryConnectionProvider.release(((RedisCommands) nativeBinaryCommands).getStatefulConnection());
if (nativeBinaryCommands instanceof RedisCommands redisCommands) {
nativeBinaryConnectionProvider.release((redisCommands).getStatefulConnection());
}

if (nativeBinaryCommands instanceof RedisAdvancedClusterCommands) {
if (nativeBinaryCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
nativeBinaryConnectionProvider
.release(((RedisAdvancedClusterCommands) nativeBinaryCommands).getStatefulConnection());
.release((redisAdvancedClusterCommands).getStatefulConnection());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private void writesTypeToField(Bucket bucket, Class<?> type, @Nullable Object va
assertThat(bucket.keySet()).isEmpty();
} else {

byte[] expected = value instanceof Class ? ((Class) value).getName().getBytes() : value.toString().getBytes();
byte[] expected = value instanceof Class javaClass ? javaClass.getName().getBytes() : value.toString().getBytes();

assertThat(bucket.asMap()).containsKey(DefaultRedisTypeMapper.DEFAULT_TYPE_KEY);
assertThat(bucket.get(DefaultRedisTypeMapper.DEFAULT_TYPE_KEY)).isEqualTo(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public static void runAndAwaitChannelSubscription(RedisConnectionFactory connect

private static long numPat(RedisConnection connection) {

if (connection instanceof LettuceConnection) {
return ((Number) ((LettuceConnection) connection).execute("PUBSUB", new IntegerOutput<>(ByteArrayCodec.INSTANCE),
if (connection instanceof LettuceConnection lettuceConnection) {
return ((Number) lettuceConnection.execute("PUBSUB", new IntegerOutput<>(ByteArrayCodec.INSTANCE),
"NUMPAT".getBytes())).longValue();
}

Expand All @@ -98,8 +98,8 @@ private static long numPat(RedisConnection connection) {
private static long numSub(RedisConnection connection, String channel) {

List<?> pubsub;
if (connection instanceof LettuceConnection) {
pubsub = (List<?>) ((LettuceConnection) connection).execute("PUBSUB", new ArrayOutput<>(ByteArrayCodec.INSTANCE),
if (connection instanceof LettuceConnection lettuceConnection) {
pubsub = (List<?>) lettuceConnection.execute("PUBSUB", new ArrayOutput<>(ByteArrayCodec.INSTANCE),
"NUMSUB".getBytes(), channel.getBytes());
} else {
pubsub = (List<?>) connection.execute("PUBSUB", "NUMSUB".getBytes(), channel.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ private static boolean clusterAvailable() {

private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {

if (connectionFactory instanceof LettuceConnectionFactory) {
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory) {
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
if (connectionFactory instanceof LettuceConnectionFactory lettuceConnectionFactory) {
return lettuceConnectionFactory.isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory jedisConnectionFactory) {
return jedisConnectionFactory.isRedisClusterAware();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ void testStartListenersToNoSpecificChannelTest() {

private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {

if (connectionFactory instanceof LettuceConnectionFactory) {
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory) {
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
if (connectionFactory instanceof LettuceConnectionFactory lettuceConnectionFactory) {
return lettuceConnectionFactory.isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory jedisConnectionFactory) {
return jedisConnectionFactory.isRedisClusterAware();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class Jackson2HashMapperIntegrationTests {
public Jackson2HashMapperIntegrationTests(RedisConnectionFactory factory) throws Exception {

this.factory = factory;
if (factory instanceof InitializingBean) {
((InitializingBean) factory).afterPropertiesSet();
if (factory instanceof InitializingBean initializingBean) {
initializingBean.afterPropertiesSet();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ public void setName(String name) {
public boolean equals(@Nullable Object o) {
if (this == o)
return true;
if (!(o instanceof Person))
if (!(o instanceof Person person))
return false;

Person person = (Person) o;

if (id != null ? !id.equals(person.id) : person.id != null)
return false;
return name != null ? name.equals(person.name) : person.name == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public RedisOperations<byte[], byte[]> redisOperationsProducerQualified(RedisOpe

public void closeRedisOperations(@Disposes RedisOperations<byte[], byte[]> redisOperations) throws Exception {

if (redisOperations instanceof DisposableBean) {
((DisposableBean) redisOperations).destroy();
if (redisOperations instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,9 @@ public boolean equals(@Nullable Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof SimpleObject)) {
if (!(obj instanceof SimpleObject other)) {
return false;
}
SimpleObject other = (SimpleObject) obj;
return nullSafeEquals(this.longValue, other.longValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ private int getNumberOfPending(String stream, String group) {

RedisConnection connection = connectionFactory.getConnection();

if (connection instanceof LettuceConnection) {
if (connection instanceof LettuceConnection lettuceConnection) {

String value = ((List) ((LettuceConnection) connectionFactory.getConnection()).execute("XPENDING",
String value = ((List) lettuceConnection.execute("XPENDING",
new NestedMultiOutput<>(StringCodec.UTF8), new byte[][] { stream.getBytes(), group.getBytes() })).get(0)
.toString();
return NumberUtils.parseNumber(value, Integer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ void testPersist() throws Exception {

@SuppressWarnings({ "unchecked", "rawtypes" })
private void populateBoundKey() {
if (keyOps instanceof Collection) {
((Collection) keyOps).add("dummy");
} else if (keyOps instanceof Map) {
((Map) keyOps).put("dummy", "dummy");
} else if (keyOps instanceof RedisAtomicInteger) {
((RedisAtomicInteger) keyOps).set(42);
} else if (keyOps instanceof RedisAtomicLong) {
((RedisAtomicLong) keyOps).set(42L);
if (keyOps instanceof Collection collection) {
collection.add("dummy");
} else if (keyOps instanceof Map map) {
map.put("dummy", "dummy");
} else if (keyOps instanceof RedisAtomicInteger redisAtomicInteger) {
redisAtomicInteger.set(42);
} else if (keyOps instanceof RedisAtomicLong redisAtomicLong) {
redisAtomicLong.set(42L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public enum CollectionAwareComparator implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {

if (o1 instanceof Collection && o2 instanceof Collection) {

Collection<?> c1 = (Collection<?>) o1;
Collection<?> c2 = (Collection<?>) o2;
if (o1 instanceof Collection<?> c1 && o2 instanceof Collection<?> c2) {

if (c1.size() != c2.size()) {
return 1;
Expand Down
Loading