Apply consistent Exception variable names to all catch blocks.

We now consistently align with the core Spring Framework's use of 'ex' as the variable name for Exceptions handled in catch blocks, and 'ignore' for all Exceptions thrown, but ignored by framework code. Both 'ex' and 'ignore' were appropriately used based on the context and nautre of the Exception handler in the catch block.

Additionally, we use the 'expected' variable name for Exception thrown in tests where the thrown Exception is the expected outcome of the test case.

Only 1 exception exists to these name conventions, and that is 'nested', which was necessarily used in ScanCursor due to the nested try-catch blocks.

Applied consistent use of String.format(..) to Exception messages requiring formatting.

Formatted catch block according to source code formatting style.

Closes #2748
Original pull request: #2749
This commit is contained in:
John Blum
2023-10-18 18:50:40 -07:00
committed by Mark Paluch
parent 68f514bdd8
commit f56989f9c1
71 changed files with 363 additions and 362 deletions

View File

@@ -49,7 +49,7 @@ public abstract class SettingsUtils {
try {
SETTINGS.load(SettingsUtils.class.getResourceAsStream("/org/springframework/data/redis/test.properties"));
} catch (Exception e) {
} catch (Exception ignore) {
throw new IllegalArgumentException("Cannot read settings");
}
}

View File

@@ -362,8 +362,8 @@ public class DefaultRedisCacheWriterTests {
try {
writer.put(CACHE_NAME, binaryCacheKey, binaryCacheValue, Duration.ZERO);
} catch (Exception cause) {
exceptionRef.set(cause);
} catch (Exception ex) {
exceptionRef.set(ex);
} finally {
afterWrite.countDown();
}

View File

@@ -207,7 +207,7 @@ public class LegacyRedisCacheTests {
Runnable putCache = () -> {
try {
cache.put(key1, value1);
} catch (IllegalMonitorStateException e) {
} catch (IllegalMonitorStateException ex) {
monitorStateException.set(true);
} finally {
latch.countDown();
@@ -304,8 +304,7 @@ public class LegacyRedisCacheTests {
try {
cache.put(key, null);
} catch (IllegalArgumentException e) {
// forget this one.
} catch (IllegalArgumentException expected) {
}
assertThat(cache.get(key).get()).isEqualTo(value);

View File

@@ -461,8 +461,8 @@ public class RedisCacheTests {
prepare.countDown();
try {
prepareForReturn.await(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
return storage.get();

View File

@@ -158,9 +158,8 @@ public abstract class AbstractConnectionIntegrationTests {
// since we use more than one db we're required to flush them all
connection.flushAll();
} catch (Exception e) {
// Connection may be closed in certain cases, like after pub/sub
// tests
} catch (Exception ignore) {
// Connection may be closed in certain cases, like after pub/sub tests
}
connection.close();
connection = null;
@@ -584,8 +583,7 @@ public abstract class AbstractConnectionIntegrationTests {
try {
connection.decr((String) null);
fail("Decrement should fail with null key");
} catch (Exception ex) {
// expected
} catch (Exception expected) {
}
}
@@ -598,8 +596,7 @@ public abstract class AbstractConnectionIntegrationTests {
try {
connection.append(key, null);
fail("Append should fail with null value");
} catch (DataAccessException ex) {
// expected
} catch (DataAccessException expected) {
}
}
@@ -611,8 +608,7 @@ public abstract class AbstractConnectionIntegrationTests {
try {
connection.hExists(key, null);
fail("hExists should fail with null key");
} catch (DataAccessException ex) {
// expected
} catch (DataAccessException expected) {
}
}
@@ -626,8 +622,7 @@ public abstract class AbstractConnectionIntegrationTests {
try {
connection.hSet(key, field, null);
fail("hSet should fail with null value");
} catch (DataAccessException ex) {
// expected
} catch (DataAccessException expected) {
}
}
@@ -665,7 +660,8 @@ public abstract class AbstractConnectionIntegrationTests {
try {
Thread.sleep(500);
} catch (InterruptedException o_O) {}
} catch (InterruptedException ignore) {
}
// open a new connection
RedisConnection connection2 = connectionFactory.getConnection();
@@ -708,7 +704,8 @@ public abstract class AbstractConnectionIntegrationTests {
try {
Thread.sleep(500);
} catch (InterruptedException o_O) {}
} catch (InterruptedException ignore) {
}
// open a new connection
RedisConnection connection2 = connectionFactory.getConnection();

View File

@@ -182,8 +182,8 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends Abstrac
try {
// we give redis some time to keep up
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
return connection.closePipeline();

View File

@@ -252,10 +252,10 @@ class ClusterCommandExecutorUnitTests {
try {
executor.executeCommandOnAllNodes(COMMAND_CALLBACK);
} catch (ClusterCommandExecutionFailureException cause) {
} catch (ClusterCommandExecutionFailureException ex) {
assertThat(cause.getSuppressed()).hasSize(1);
assertThat(cause.getSuppressed()[0]).isInstanceOf(DataAccessException.class);
assertThat(ex.getSuppressed()).hasSize(1);
assertThat(ex.getSuppressed()[0]).isInstanceOf(DataAccessException.class);
}
verify(connection1).theWheelWeavesAsTheWheelWills();

View File

@@ -123,8 +123,8 @@ public class JedisClusterConnectionTests implements ClusterConnectionTests {
for (ConnectionPool pool : nativeConnection.getClusterNodes().values()) {
try (Jedis jedis = new Jedis(pool.getResource())) {
jedis.flushAll();
} catch (Exception e) {
// ignore this one since we cannot remove data from replicas
} catch (Exception ignore) {
// ignore since we cannot remove data from replicas
}
}
}
@@ -2857,8 +2857,8 @@ public class JedisClusterConnectionTests implements ClusterConnectionTests {
try {
clusterConnection.scriptingCommands().evalSha(luaScriptBin, ReturnType.VALUE, 1, keyAndArgs);
fail("expected InvalidDataAccessApiUsageException");
} catch (InvalidDataAccessApiUsageException e) {
assertThat(e.getMessage()).contains("NOSCRIPT");
} catch (InvalidDataAccessApiUsageException ex) {
assertThat(ex.getMessage()).contains("NOSCRIPT");
}
}

View File

@@ -68,17 +68,14 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
public void tearDown() {
try {
connection.flushAll();
} catch (Exception e) {
// Jedis leaves some incomplete data in OutputStream on NPE caused
// by null key/value tests
// Attempting to flush the DB or close the connection will result in
// error on sending QUIT to Redis
} catch (Exception ignore) {
// Jedis leaves some incomplete data in OutputStream on NPE caused by null key/value tests
// Attempting to flush the DB or close the connection will result in error on sending QUIT to Redis
}
try {
connection.close();
} catch (Exception e) {
// silently close connection
} catch (Exception ignore) {
}
connection = null;
@@ -231,16 +228,16 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
RedisConnection con = connectionFactory.getConnection();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
con.publish(expectedChannel.getBytes(), expectedMessage.getBytes());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
/*
@@ -289,8 +286,8 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
RedisConnection con = connectionFactory.getConnection();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
con.publish("channel1".getBytes(), expectedMessage.getBytes());
@@ -298,8 +295,8 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
con.close();
@@ -341,8 +338,7 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
try (RedisConnection conn = factory2.getConnection()) {
conn.get(null);
} catch (Exception e) {
} catch (Exception ignore) {
} finally {
// Make sure we don't end up with broken connection
factory2.getConnection().dbSize();

View File

@@ -47,11 +47,9 @@ public class JedisConnectionPipelineIntegrationTests extends AbstractConnectionP
try {
connection.flushAll();
connection.close();
} catch (Exception e) {
// Jedis leaves some incomplete data in OutputStream on NPE caused
// by null key/value tests
// Attempting to close the connection will result in error on
// sending QUIT to Redis
} catch (Exception ignore) {
// Jedis leaves some incomplete data in OutputStream on NPE caused by null key/value tests
// Attempting to close the connection will result in error on sending QUIT to Redis
}
connection = null;
}

View File

@@ -46,11 +46,9 @@ public class JedisConnectionTransactionIntegrationTests extends AbstractConnecti
try {
connection.flushAll();
connection.close();
} catch (Exception e) {
// Jedis leaves some incomplete data in OutputStream on NPE caused
// by null key/value tests
// Attempting to close the connection will result in error on
// sending QUIT to Redis
} catch (Exception ignore) {
// Jedis leaves some incomplete data in OutputStream on NPE caused by null key/value tests
// Attempting to close the connection will result in error on sending QUIT to Redis
}
connection = null;
}

View File

@@ -65,8 +65,7 @@ class JedisConnectionUnitTests {
try {
connection.shutdown(null);
} catch (InvalidDataAccessApiUsageException e) {
// all good. Sometimes it throws an Exception.
} catch (InvalidDataAccessApiUsageException ignore) {
}
verify(jedisSpy).shutdown();

View File

@@ -100,8 +100,8 @@ public class ScanTests {
cursorMap.next();
}
cursorMap.close();
} catch (Exception e) {
exception.set(e);
} catch (Exception ex) {
exception.set(ex);
}
});
}

View File

@@ -227,8 +227,8 @@ public class JedisConnectionFactoryExtension implements ParameterResolver {
try {
mayClose = true;
destroy();
} catch (Exception e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@@ -101,7 +101,7 @@ class LettuceConnectionFactoryTests {
try {
connection.get("test3");
fail("Expected exception using natively closed conn");
} catch (RedisSystemException e) {
} catch (RedisSystemException expected) {
// expected, shared conn is closed
}
DefaultStringRedisConnection conn2 = new DefaultStringRedisConnection(factory.getConnection());
@@ -122,7 +122,7 @@ class LettuceConnectionFactoryTests {
try {
conn2.set("anotherkey", "anothervalue");
fail("Expected exception using natively closed conn");
} catch (RedisSystemException e) {
} catch (RedisSystemException expected) {
// expected, as we are re-using the natively closed conn
} finally {
conn2.close();
@@ -274,7 +274,8 @@ class LettuceConnectionFactoryTests {
try {
factory.getConnection();
fail("Expected connection failure exception");
} catch (RedisConnectionFailureException e) {}
} catch (RedisConnectionFailureException expected) {
}
}
@Test
@@ -467,9 +468,9 @@ class LettuceConnectionFactoryTests {
try {
connection.ping();
fail("Expected RedisException: Master is currently unknown");
} catch (RedisSystemException e) {
assertThat(e.getCause()).isInstanceOf(RedisException.class);
assertThat(e.getCause().getMessage()).contains("Master is currently unknown");
} catch (RedisSystemException ex) {
assertThat(ex.getCause()).isInstanceOf(RedisException.class);
assertThat(ex.getCause().getMessage()).contains("Master is currently unknown");
} finally {
connection.close();
}

View File

@@ -97,7 +97,7 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
try {
connection.exec();
fail("Expected exception resuming tx");
} catch (RedisSystemException e) {
} catch (RedisSystemException expected) {
// expected, can't resume tx after closing conn
}
}
@@ -115,7 +115,8 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
// can't do blocking ops after closing
connection.bLPop(1, "what".getBytes());
fail("Expected exception using a closed conn for dedicated ops");
} catch (RedisSystemException e) {}
} catch (RedisSystemException expected) {
}
}
@Test
@@ -134,8 +135,8 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
try {
connection.set("foo".getBytes(), "bar".getBytes());
fail("Exception should be thrown trying to use a closed connection");
} catch (RedisSystemException e) {}
finally {
} catch (RedisSystemException expected) {
} finally {
factory2.destroy();
}

View File

@@ -166,8 +166,8 @@ public abstract class LettuceReactiveCommandsTestSupport {
if (nativeBinaryConnectionProvider instanceof DisposableBean) {
((DisposableBean) nativeBinaryConnectionProvider).destroy();
}
} catch (Exception e) {
throw new RuntimeException(e);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}

View File

@@ -78,9 +78,8 @@ public class LettuceSentinelIntegrationTests extends AbstractConnectionIntegrati
// since we use more than one db we're required to flush them all
connection.flushAll();
} catch (Exception e) {
// Connection may be closed in certain cases, like after pub/sub
// tests
} catch (Exception ignore) {
// Connection may be closed in certain cases, like after pub/sub tests
}
connection.close();
}

View File

@@ -55,8 +55,8 @@ class ConnectionSplittingInterceptorUnitTests {
try {
WRITE_METHOD = ClassUtils.getMethod(RedisConnection.class, "expire", byte[].class, long.class);
READONLY_METHOD = ClassUtils.getMethod(RedisConnection.class, "keys", byte[].class);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

View File

@@ -103,7 +103,8 @@ public class DefaultSetOperationsIntegrationTests<K, V> {
try {
setOps.randomMembers(keyFactory.instance(), -1);
fail("IllegalArgumentException should be thrown");
} catch (IllegalArgumentException e) {}
} catch (IllegalArgumentException expected) {
}
}
@ParameterizedRedisTest
@@ -112,7 +113,8 @@ public class DefaultSetOperationsIntegrationTests<K, V> {
try {
setOps.distinctRandomMembers(keyFactory.instance(), -2);
fail("IllegalArgumentException should be thrown");
} catch (IllegalArgumentException e) {}
} catch (IllegalArgumentException expected) {
}
}
@SuppressWarnings("unchecked")

View File

@@ -95,14 +95,10 @@ public class ReactiveRedisTemplateIntegrationTests<K, V> {
@EnabledOnCommand("COPY")
void copy() {
ReactiveRedisClusterConnection connection = null;
try {
connection = redisTemplate.getConnectionFactory().getReactiveClusterConnection();
assumeThat(connection == null).isTrue();
} catch (InvalidDataAccessApiUsageException e) {} finally {
if (connection != null) {
connection.close();
}
try (ReactiveRedisClusterConnection connection = redisTemplate.getConnectionFactory()
.getReactiveClusterConnection()){
assumeThat(connection).isNull();
} catch (InvalidDataAccessApiUsageException ignore) {
}
K key = keyFactory.instance();
@@ -397,23 +393,17 @@ public class ReactiveRedisTemplateIntegrationTests<K, V> {
@ParameterizedRedisTest // DATAREDIS-602
void move() {
ReactiveRedisClusterConnection connection = null;
try {
connection = redisTemplate.getConnectionFactory().getReactiveClusterConnection();
assumeThat(connection == null).isTrue();
} catch (InvalidDataAccessApiUsageException e) {} finally {
if (connection != null) {
connection.close();
}
try (ReactiveRedisClusterConnection connection = redisTemplate.getConnectionFactory()
.getReactiveClusterConnection()) {
assumeThat(connection).isNull();
} catch (InvalidDataAccessApiUsageException ignore) {
}
K key = keyFactory.instance();
V value = valueFactory.instance();
redisTemplate.opsForValue().set(key, value).as(StepVerifier::create).expectNext(true).verifyComplete();
redisTemplate.move(key, 5).as(StepVerifier::create).expectNext(true).verifyComplete();
redisTemplate.hasKey(key).as(StepVerifier::create).expectNext(false).verifyComplete();
}

View File

@@ -102,8 +102,7 @@ public class RedisKeyValueAdapterTests {
try {
adapter.destroy();
} catch (Exception e) {
// ignore
} catch (Exception ignore) {
}
}

View File

@@ -351,6 +351,7 @@ public class RedisTemplateIntegrationTests<K, V> {
V value1 = valueFactory.instance();
List<Object> pipelinedResults = redisTemplate.executePipelined(new SessionCallback() {
public Object execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.opsForList().leftPush(key1, value1);
operations.opsForList().rightPop(key1);
@@ -360,9 +361,12 @@ public class RedisTemplateIntegrationTests<K, V> {
try {
// Await EXEC completion as it's executed on a dedicated connection.
Thread.sleep(100);
} catch (InterruptedException e) {}
} catch (InterruptedException ignore) {
}
operations.opsForValue().set(key1, value1);
operations.opsForValue().get(key1);
return null;
}
});
@@ -719,7 +723,8 @@ public class RedisTemplateIntegrationTests<K, V> {
th.start();
try {
th.join();
} catch (InterruptedException e) {}
} catch (InterruptedException ignore) {
}
operations.multi();
operations.opsForValue().set(key1, value3);
@@ -751,7 +756,8 @@ public class RedisTemplateIntegrationTests<K, V> {
th.start();
try {
th.join();
} catch (InterruptedException e) {}
} catch (InterruptedException ignore) {
}
operations.unwatch();
operations.multi();
@@ -788,7 +794,8 @@ public class RedisTemplateIntegrationTests<K, V> {
th.start();
try {
th.join();
} catch (InterruptedException e) {}
} catch (InterruptedException ignore) {
}
operations.multi();
operations.opsForValue().set(key1, value3);

View File

@@ -54,13 +54,14 @@ class EnabledOnRedisAvailableCondition implements ExecutionCondition {
EnabledOnRedisAvailable annotation = optional.get();
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(SettingsUtils.getHost(), annotation.value()), 100);
return enabled(
String.format("Connection successful to Redis at %s:%d", SettingsUtils.getHost(), annotation.value()));
} catch (IOException e) {
return disabled(
String.format("Cannot connect to Redis at %s:%d (%s)", SettingsUtils.getHost(), annotation.value(), e));
return enabled(String.format("Connection successful to Redis at %s:%d", SettingsUtils.getHost(),
annotation.value()));
} catch (IOException ex) {
return disabled(String.format("Cannot connect to Redis at %s:%d (%s)", SettingsUtils.getHost(),
annotation.value(), ex));
}
}

View File

@@ -57,8 +57,8 @@ class RedisConditions {
p.load(inputStream);
version = Version.parse(p.getProperty("redis_version"));
} catch (IOException e) {
throw new IllegalStateException(e);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

View File

@@ -39,7 +39,7 @@ public class RedisDetector {
cluster.getConnectionFromSlot(1).close();
return true;
} catch (Exception e) {
} catch (Exception ignore) {
return false;
}
});
@@ -54,9 +54,8 @@ public class RedisDetector {
socket.connect(new InetSocketAddress(SettingsUtils.getHost(), port), 100);
return true;
} catch (IOException e) {
} catch (IOException ignore) {
return false;
}
}
}

View File

@@ -147,8 +147,8 @@ class ParameterizedRedisTestExtension implements TestTemplateInvocationContextPr
protected static Stream<? extends Arguments> arguments(ArgumentsProvider provider, ExtensionContext context) {
try {
return provider.provideArguments(context);
} catch (Exception e) {
throw ExceptionUtils.throwAsUncheckedException(e);
} catch (Exception ex) {
throw ExceptionUtils.throwAsUncheckedException(ex);
}
}

View File

@@ -81,8 +81,8 @@ public class ConnectionVerifier<T extends RedisConnectionFactory> {
if (factory instanceof InitializingBean initializingBean) {
try {
initializingBean.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException(e);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
if (factory instanceof SmartLifecycle smartLifecycle) {
@@ -97,14 +97,14 @@ public class ConnectionVerifier<T extends RedisConnectionFactory> {
if (it instanceof DisposableBean bean) {
try {
bean.destroy();
} catch (Exception e) {
throw new DataAccessResourceFailureException("Cannot close resource", e);
} catch (Exception ex) {
throw new DataAccessResourceFailureException("Cannot close resource", ex);
}
} else if (it instanceof Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
throw new DataAccessResourceFailureException("Cannot close resource", e);
} catch (IOException ex) {
throw new DataAccessResourceFailureException("Cannot close resource", ex);
}
} else if (it instanceof SmartLifecycle smartLifecycle && smartLifecycle.isRunning()) {
smartLifecycle.stop();