Skip to content

Commit 6991310

Browse files
DATAREDIS-1233 - Polishing.
Update license header, use early return for if statements and guard tests against failure when running against a single node instance.
1 parent fff1d3e commit 6991310

17 files changed

+112
-113
lines changed

src/test/java/org/springframework/data/redis/connection/jedis/JedisAclIntegrationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
2727
import org.springframework.data.redis.connection.RedisSentinelConnection;
2828
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
29+
import org.springframework.data.redis.test.condition.EnabledOnRedisAvailable;
2930
import org.springframework.data.redis.test.condition.EnabledOnRedisSentinelAvailable;
3031
import org.springframework.data.redis.test.condition.EnabledOnRedisVersion;
3132

@@ -35,6 +36,7 @@
3536
* @author Mark Paluch
3637
*/
3738
@EnabledOnRedisVersion("6.0")
39+
@EnabledOnRedisAvailable(6382)
3840
class JedisAclIntegrationTests {
3941

4042
@Test

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveCommandsTestSupport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
import java.util.ArrayList;
3030
import java.util.List;
3131

32+
import org.assertj.core.api.Assumptions;
3233
import org.junit.jupiter.api.AfterEach;
3334
import org.junit.jupiter.api.BeforeEach;
34-
3535
import org.springframework.data.redis.connection.lettuce.LettuceReactiveRedisConnection.ByteBufferCodec;
36+
import org.springframework.data.redis.test.condition.RedisDetector;
3637
import org.springframework.data.redis.test.extension.LettuceExtension;
3738
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
3839

@@ -151,11 +152,14 @@ public String toString() {
151152
public void setUp() {
152153

153154
if (nativeConnectionProvider instanceof StandaloneConnectionProvider) {
155+
154156
nativeCommands = nativeConnectionProvider.getConnection(StatefulRedisConnection.class).sync();
155157
nativeBinaryCommands = nativeBinaryConnectionProvider.getConnection(StatefulRedisConnection.class).sync();
156158
this.connection = new LettuceReactiveRedisConnection(connectionProvider);
157-
158159
} else {
160+
161+
Assumptions.assumeThat(RedisDetector.isClusterAvailable()).isTrue();
162+
159163
ClusterConnectionProvider clusterConnectionProvider = (ClusterConnectionProvider) nativeConnectionProvider;
160164
nativeCommands = nativeConnectionProvider.getConnection(StatefulRedisClusterConnection.class).sync();
161165
nativeBinaryCommands = nativeBinaryConnectionProvider.getConnection(StatefulRedisClusterConnection.class).sync();

src/test/java/org/springframework/data/redis/test/condition/EnabledIfLongRunningTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/org/springframework/data/redis/test/condition/EnabledOnCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,44 +25,42 @@
2525
import org.junit.jupiter.api.extension.ExecutionCondition;
2626
import org.junit.jupiter.api.extension.ExtensionContext;
2727
import org.junit.platform.commons.util.AnnotationUtils;
28-
2928
import org.springframework.data.redis.test.extension.LettuceExtension;
3029

3130
/**
3231
* {@link ExecutionCondition} for {@link EnabledOnCommandCondition @EnabledOnCommand}.
3332
*
3433
* @see EnabledOnCommandCondition
34+
* @author Mark Paluch
35+
* @author Christoph Strobl
3536
*/
3637
class EnabledOnCommandCondition implements ExecutionCondition {
3738

3839
private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = enabled("@EnabledOnCommand is not present");
39-
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace
40-
.create(RedisConditions.class);
40+
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(RedisConditions.class);
4141

4242
@Override
4343
@SuppressWarnings({ "rawtypes", "unchecked" })
4444
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
4545

4646
Optional<EnabledOnCommand> optional = AnnotationUtils.findAnnotation(context.getElement(), EnabledOnCommand.class);
4747

48-
if (optional.isPresent()) {
49-
50-
String command = optional.get().value();
48+
if (!optional.isPresent()) {
49+
return ENABLED_BY_DEFAULT;
50+
}
5151

52-
ExtensionContext.Store store = context.getStore(NAMESPACE);
53-
RedisConditions conditions = store.getOrComputeIfAbsent(RedisConditions.class, ignore -> {
52+
String command = optional.get().value();
5453

55-
StatefulRedisConnection connection = new LettuceExtension().resolve(context, StatefulRedisConnection.class);
56-
return RedisConditions.of(connection);
57-
}, RedisConditions.class);
54+
ExtensionContext.Store store = context.getStore(NAMESPACE);
55+
RedisConditions conditions = store.getOrComputeIfAbsent(RedisConditions.class, ignore -> {
5856

59-
boolean hasCommand = conditions.hasCommand(command);
60-
return hasCommand ? enabled("Enabled on command " + command)
61-
: disabled(
62-
"Disabled, command " + command + " not available on Redis version " + conditions.getRedisVersion());
63-
}
57+
StatefulRedisConnection connection = new LettuceExtension().resolve(context, StatefulRedisConnection.class);
58+
return RedisConditions.of(connection);
59+
}, RedisConditions.class);
6460

65-
return ENABLED_BY_DEFAULT;
61+
boolean hasCommand = conditions.hasCommand(command);
62+
return hasCommand ? enabled("Enabled on command " + command)
63+
: disabled("Disabled, command " + command + " not available on Redis version " + conditions.getRedisVersion());
6664
}
6765

6866
}

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisAvailable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisAvailableCondition.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,13 +26,13 @@
2626
import org.junit.jupiter.api.extension.ExecutionCondition;
2727
import org.junit.jupiter.api.extension.ExtensionContext;
2828
import org.junit.platform.commons.util.AnnotationUtils;
29-
3029
import org.springframework.data.redis.SettingsUtils;
3130

3231
/**
3332
* {@link ExecutionCondition} for {@link EnabledOnRedisAvailableCondition @EnabledOnRedisAvailable}.
3433
*
3534
* @author Mark Paluch
35+
* @author Christoph Strobl
3636
* @see EnabledOnRedisAvailableCondition
3737
*/
3838
class EnabledOnRedisAvailableCondition implements ExecutionCondition {
@@ -47,22 +47,21 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
4747
Optional<EnabledOnRedisAvailable> optional = AnnotationUtils.findAnnotation(context.getElement(),
4848
EnabledOnRedisAvailable.class);
4949

50-
if (optional.isPresent()) {
50+
if (!optional.isPresent()) {
51+
return ENABLED_BY_DEFAULT;
52+
}
5153

52-
EnabledOnRedisAvailable annotation = optional.get();
54+
EnabledOnRedisAvailable annotation = optional.get();
5355

54-
try (Socket socket = new Socket()) {
55-
socket.connect(new InetSocketAddress(SettingsUtils.getHost(), annotation.value()), 100);
56+
try (Socket socket = new Socket()) {
57+
socket.connect(new InetSocketAddress(SettingsUtils.getHost(), annotation.value()), 100);
5658

57-
return enabled(
58-
String.format("Connection successful to Redis at %s:%d", SettingsUtils.getHost(), annotation.value()));
59-
} catch (IOException e) {
60-
return disabled(
61-
String.format("Cannot connect to Redis at %s:%d (%s)", SettingsUtils.getHost(), annotation.value(), e));
62-
}
59+
return enabled(
60+
String.format("Connection successful to Redis at %s:%d", SettingsUtils.getHost(), annotation.value()));
61+
} catch (IOException e) {
62+
return disabled(
63+
String.format("Cannot connect to Redis at %s:%d (%s)", SettingsUtils.getHost(), annotation.value(), e));
6364
}
64-
65-
return ENABLED_BY_DEFAULT;
6665
}
6766

6867
}

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisClusterAvailable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisClusterCondition.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,13 @@
2323
import org.junit.jupiter.api.extension.ExecutionCondition;
2424
import org.junit.jupiter.api.extension.ExtensionContext;
2525
import org.junit.platform.commons.util.AnnotationUtils;
26-
2726
import org.springframework.data.redis.SettingsUtils;
2827

2928
/**
3029
* {@link ExecutionCondition} for {@link EnabledOnRedisClusterCondition @EnabledOnRedisClusterAvailable}.
3130
*
3231
* @author Mark Paluch
32+
* @author Christoph Strobl
3333
* @see EnabledOnRedisClusterCondition
3434
*/
3535
class EnabledOnRedisClusterCondition implements ExecutionCondition {
@@ -43,18 +43,17 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
4343
Optional<EnabledOnRedisClusterAvailable> optional = AnnotationUtils.findAnnotation(context.getElement(),
4444
EnabledOnRedisClusterAvailable.class);
4545

46-
if (optional.isPresent()) {
47-
48-
if (RedisDetector.isClusterAvailable()) {
49-
return enabled(String.format("Connection successful to Redis Cluster at %s:%d", SettingsUtils.getHost(),
50-
SettingsUtils.getClusterPort()));
51-
}
46+
if (!optional.isPresent()) {
47+
return ENABLED_BY_DEFAULT;
48+
}
5249

53-
return disabled(String.format("Cannot connect to Redis Cluster at %s:%d", SettingsUtils.getHost(),
50+
if (RedisDetector.isClusterAvailable()) {
51+
return enabled(String.format("Connection successful to Redis Cluster at %s:%d", SettingsUtils.getHost(),
5452
SettingsUtils.getClusterPort()));
5553
}
5654

57-
return ENABLED_BY_DEFAULT;
55+
return disabled(String.format("Cannot connect to Redis Cluster at %s:%d", SettingsUtils.getHost(),
56+
SettingsUtils.getClusterPort()));
5857
}
5958

6059
}
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,13 +28,13 @@
2828
import org.junit.platform.commons.function.Try;
2929
import org.junit.platform.commons.util.AnnotationUtils;
3030
import org.junit.platform.commons.util.ReflectionUtils;
31-
3231
import org.springframework.data.redis.connection.RedisConnectionFactory;
3332

3433
/**
3534
* {@link ExecutionCondition} for {@link EnabledOnRedisDriverCondition @EnabledOnRedisDriver}.
3635
*
3736
* @author Mark Paluch
37+
* @author Christoph Strobl
3838
* @see EnabledOnRedisDriver
3939
*/
4040
class EnabledOnRedisDriverCondition implements ExecutionCondition {
@@ -47,43 +47,42 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
4747
Optional<EnabledOnRedisDriver> optional = AnnotationUtils.findAnnotation(context.getElement(),
4848
EnabledOnRedisDriver.class);
4949

50-
if (optional.isPresent()) {
51-
52-
EnabledOnRedisDriver annotation = optional.get();
53-
Class<?> testClass = context.getRequiredTestClass();
50+
if (!optional.isPresent()) {
51+
return ENABLED_BY_DEFAULT;
52+
}
5453

55-
List<Field> annotatedFields = AnnotationUtils.findAnnotatedFields(testClass,
56-
EnabledOnRedisDriver.DriverQualifier.class,
57-
it -> RedisConnectionFactory.class.isAssignableFrom(it.getType()));
54+
EnabledOnRedisDriver annotation = optional.get();
55+
Class<?> testClass = context.getRequiredTestClass();
5856

59-
if (annotatedFields.isEmpty()) {
60-
throw new IllegalStateException(
61-
"@WithRedisDriver requires a field of type RedisConnectionFactory annotated with @DriverQualifier!");
62-
}
57+
List<Field> annotatedFields = AnnotationUtils.findAnnotatedFields(testClass,
58+
EnabledOnRedisDriver.DriverQualifier.class, it -> RedisConnectionFactory.class.isAssignableFrom(it.getType()));
6359

64-
for (Field field : annotatedFields) {
65-
Try<Object> fieldValue = ReflectionUtils.tryToReadFieldValue(field, context.getRequiredTestInstance());
60+
if (annotatedFields.isEmpty()) {
61+
throw new IllegalStateException(
62+
"@WithRedisDriver requires a field of type RedisConnectionFactory annotated with @DriverQualifier!");
63+
}
6664

67-
RedisConnectionFactory value = (RedisConnectionFactory) fieldValue
68-
.getOrThrow(e -> new IllegalStateException("Cannot read field " + field, e));
65+
for (Field field : annotatedFields) {
66+
Try<Object> fieldValue = ReflectionUtils.tryToReadFieldValue(field, context.getRequiredTestInstance());
6967

70-
boolean foundMatch = false;
71-
for (RedisDriver redisDriver : annotation.value()) {
72-
if (redisDriver.matches(value)) {
73-
foundMatch = true;
74-
}
75-
}
68+
RedisConnectionFactory value = (RedisConnectionFactory) fieldValue
69+
.getOrThrow(e -> new IllegalStateException("Cannot read field " + field, e));
7670

77-
if (!foundMatch) {
78-
return disabled(String.format("Driver %s not supported. Supported driver(s): %s",
79-
value, Arrays.toString(annotation.value())));
71+
boolean foundMatch = false;
72+
for (RedisDriver redisDriver : annotation.value()) {
73+
if (redisDriver.matches(value)) {
74+
foundMatch = true;
8075
}
8176
}
8277

83-
return enabled("Found enabled driver(s): " + Arrays.toString(annotation.value()));
78+
if (!foundMatch) {
79+
return disabled(String.format("Driver %s not supported. Supported driver(s): %s", value,
80+
Arrays.toString(annotation.value())));
81+
}
8482
}
8583

86-
return ENABLED_BY_DEFAULT;
84+
return enabled("Found enabled driver(s): " + Arrays.toString(annotation.value()));
85+
8786
}
8887

8988
}

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisSentinelAvailable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisSentinelCondition.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,13 @@
2323
import org.junit.jupiter.api.extension.ExecutionCondition;
2424
import org.junit.jupiter.api.extension.ExtensionContext;
2525
import org.junit.platform.commons.util.AnnotationUtils;
26-
2726
import org.springframework.data.redis.SettingsUtils;
2827

2928
/**
3029
* {@link ExecutionCondition} for {@link EnabledOnRedisSentinelCondition @EnabledOnRedisSentinelAvailable}.
3130
*
3231
* @author Mark Paluch
32+
* @author Christoph Strobl
3333
* @see EnabledOnRedisSentinelCondition
3434
*/
3535
class EnabledOnRedisSentinelCondition implements ExecutionCondition {
@@ -44,21 +44,20 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
4444
Optional<EnabledOnRedisSentinelAvailable> optional = AnnotationUtils.findAnnotation(context.getElement(),
4545
EnabledOnRedisSentinelAvailable.class);
4646

47-
if (optional.isPresent()) {
48-
49-
EnabledOnRedisSentinelAvailable annotation = optional.get();
47+
if (!optional.isPresent()) {
48+
return ENABLED_BY_DEFAULT;
49+
}
5050

51-
if (RedisDetector.canConnectToPort(annotation.value())) {
51+
EnabledOnRedisSentinelAvailable annotation = optional.get();
5252

53-
return enabled(String.format("Connection successful to Redis Sentinel at %s:%d", SettingsUtils.getHost(),
54-
annotation.value()));
55-
}
53+
if (RedisDetector.canConnectToPort(annotation.value())) {
5654

57-
return disabled(
58-
String.format("Cannot connect to Redis Sentinel at %s:%d", SettingsUtils.getHost(), annotation.value()));
55+
return enabled(String.format("Connection successful to Redis Sentinel at %s:%d", SettingsUtils.getHost(),
56+
annotation.value()));
5957
}
6058

61-
return ENABLED_BY_DEFAULT;
62-
}
59+
return disabled(
60+
String.format("Cannot connect to Redis Sentinel at %s:%d", SettingsUtils.getHost(), annotation.value()));
6361

62+
}
6463
}

0 commit comments

Comments
 (0)