Skip to content

Commit 2511aa1

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-1233 - Add JUnit 5 extensions.
1 parent 7d2e899 commit 2511aa1

File tree

184 files changed

+3753
-2651
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

184 files changed

+3753
-2651
lines changed

src/test/java/org/springframework/data/redis/ConnectionFactoryTracker.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ public abstract class ConnectionFactoryTracker {
3535
private static Set<Object> connFactories = new LinkedHashSet<>();
3636

3737
public static void add(RedisConnectionFactory factory) {
38+
39+
if (factory instanceof Managed) {
40+
throw new UnsupportedOperationException("Cannot track managed resource");
41+
}
42+
3843
connFactories.add(factory);
3944
}
4045

4146
public static void add(Object factory) {
47+
48+
if (factory instanceof Managed) {
49+
throw new UnsupportedOperationException("Cannot track managed resource");
50+
}
51+
4252
connFactories.add(factory);
4353
}
4454

@@ -57,4 +67,8 @@ public static void cleanUp() {
5767
}
5868
}
5969
}
70+
71+
public interface Managed {
72+
73+
}
6074
}

src/test/java/org/springframework/data/redis/PropertyEditorsTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import org.junit.After;
21-
import org.junit.Before;
22-
import org.junit.Test;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.context.support.GenericXmlApplicationContext;
2525
import org.springframework.data.redis.core.RedisOperations;
2626

2727
/**
2828
* @author Costin Leau
2929
*/
30-
public class PropertyEditorsTest {
30+
class PropertyEditorsTest {
3131

3232
private GenericXmlApplicationContext ctx;
3333

34-
@Before
35-
public void setUp() {
34+
@BeforeEach
35+
void setUp() {
3636
ctx = new GenericXmlApplicationContext("/org/springframework/data/redis/pe.xml");
3737
}
3838

39-
@After
40-
public void tearDown() {
39+
@AfterEach
40+
void tearDown() {
4141
if (ctx != null)
4242
ctx.destroy();
4343
}
4444

4545
@Test
46-
public void testInjection() throws Exception {
46+
void testInjection() throws Exception {
4747
RedisViewPE bean = ctx.getBean(RedisViewPE.class);
4848
RedisOperations<?, ?> ops = ctx.getBean(RedisOperations.class);
4949

src/test/java/org/springframework/data/redis/SettingsUtils.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
*/
1616
package org.springframework.data.redis;
1717

18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.HashSet;
1821
import java.util.Properties;
1922

23+
import org.springframework.data.redis.connection.RedisClusterConfiguration;
24+
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
2025
import org.springframework.data.redis.connection.RedisSocketConfiguration;
2126
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
2227

@@ -35,6 +40,8 @@ public abstract class SettingsUtils {
3540
static {
3641
DEFAULTS.put("host", "127.0.0.1");
3742
DEFAULTS.put("port", "6379");
43+
DEFAULTS.put("clusterPort", "7379");
44+
DEFAULTS.put("sentinelPort", "26379");
3845
DEFAULTS.put("socket", "work/redis-6379.sock");
3946

4047
SETTINGS = new Properties(DEFAULTS);
@@ -62,6 +69,28 @@ public static int getPort() {
6269
return Integer.valueOf(SETTINGS.getProperty("port"));
6370
}
6471

72+
/**
73+
* @return the Redis Cluster port.
74+
*/
75+
public static int getSentinelPort() {
76+
return Integer.valueOf(SETTINGS.getProperty("sentinelPort"));
77+
}
78+
79+
/**
80+
* @return the Redis Sentinel Master Id.
81+
*/
82+
public static String getSentinelMaster() {
83+
return "mymaster";
84+
}
85+
86+
/**
87+
* @return the Redis Cluster port.
88+
*/
89+
public static int getClusterPort() {
90+
return Integer.valueOf(SETTINGS.getProperty("clusterPort"));
91+
}
92+
93+
6594
/**
6695
* @return path to the unix domain socket.
6796
*/
@@ -78,6 +107,27 @@ public static RedisStandaloneConfiguration standaloneConfiguration() {
78107
return new RedisStandaloneConfiguration(getHost(), getPort());
79108
}
80109

110+
/**
111+
* Construct a new {@link RedisSentinelConfiguration} initialized with test endpoint settings.
112+
*
113+
* @return a new {@link RedisSentinelConfiguration} initialized with test endpoint settings.
114+
*/
115+
public static RedisSentinelConfiguration sentinelConfiguration() {
116+
return new RedisSentinelConfiguration(getSentinelMaster(),
117+
new HashSet<>(Arrays.asList(String.format("%s:%d", getHost(), getSentinelPort()),
118+
String.format("%s:%d", getHost(), getSentinelPort() + 1))));
119+
}
120+
121+
/**
122+
* Construct a new {@link RedisClusterConfiguration} initialized with test endpoint settings.
123+
*
124+
* @return a new {@link RedisClusterConfiguration} initialized with test endpoint settings.
125+
*/
126+
public static RedisClusterConfiguration clusterConfiguration() {
127+
return new RedisClusterConfiguration(
128+
Collections.singletonList(String.format("%s:%d", getHost(), getClusterPort())));
129+
}
130+
81131
/**
82132
* Construct a new {@link RedisSocketConfiguration} initialized with test endpoint settings.
83133
*
@@ -86,4 +136,5 @@ public static RedisStandaloneConfiguration standaloneConfiguration() {
86136
public static RedisSocketConfiguration socketConfiguration() {
87137
return new RedisSocketConfiguration(getSocket());
88138
}
139+
89140
}

src/test/java/org/springframework/data/redis/cache/CacheTestParams.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import static org.springframework.data.redis.connection.ClusterTestVariables.*;
19-
2018
import lombok.RequiredArgsConstructor;
2119
import lombok.experimental.Delegate;
2220

@@ -27,18 +25,20 @@
2725
import java.util.stream.Collectors;
2826

2927
import org.junit.runners.model.Statement;
28+
3029
import org.springframework.data.redis.SettingsUtils;
31-
import org.springframework.data.redis.connection.RedisClusterConfiguration;
32-
import org.springframework.data.redis.connection.RedisClusterNode;
3330
import org.springframework.data.redis.connection.RedisConnectionFactory;
3431
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
3532
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
33+
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
3634
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
37-
import org.springframework.data.redis.connection.lettuce.LettuceTestClientResources;
35+
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
3836
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
3937
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
4038
import org.springframework.data.redis.serializer.OxmSerializer;
4139
import org.springframework.data.redis.serializer.RedisSerializer;
40+
import org.springframework.data.redis.test.extension.RedisCluster;
41+
import org.springframework.data.redis.test.extension.RedisStanalone;
4242
import org.springframework.data.redis.test.util.RedisClusterRule;
4343
import org.springframework.oxm.xstream.XStreamMarshaller;
4444
import org.springframework.util.StringUtils;
@@ -57,32 +57,27 @@ private static Collection<RedisConnectionFactory> connectionFactories() {
5757
List<RedisConnectionFactory> factoryList = new ArrayList<>(3);
5858

5959
// Jedis Standalone
60-
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);
61-
jedisConnectionFactory.afterPropertiesSet();
60+
JedisConnectionFactory jedisConnectionFactory = JedisConnectionFactoryExtension
61+
.getConnectionFactory(RedisStanalone.class);
6262
factoryList.add(new FixDamnedJunitParameterizedNameForConnectionFactory(jedisConnectionFactory, ""));
6363

6464
// Lettuce Standalone
65-
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(config);
66-
lettuceConnectionFactory.setClientResources(LettuceTestClientResources.getSharedClientResources());
67-
lettuceConnectionFactory.afterPropertiesSet();
65+
LettuceConnectionFactory lettuceConnectionFactory = LettuceConnectionFactoryExtension
66+
.getConnectionFactory(RedisStanalone.class);
6867
factoryList.add(new FixDamnedJunitParameterizedNameForConnectionFactory(lettuceConnectionFactory, ""));
6968

7069
if (clusterAvailable()) {
7170

72-
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
73-
clusterConfiguration.addClusterNode(new RedisClusterNode(CLUSTER_HOST, MASTER_NODE_1_PORT));
7471

7572
// Jedis Cluster
76-
JedisConnectionFactory jedisClusterConnectionFactory = new JedisConnectionFactory(clusterConfiguration);
77-
jedisClusterConnectionFactory.afterPropertiesSet();
73+
JedisConnectionFactory jedisClusterConnectionFactory = JedisConnectionFactoryExtension
74+
.getConnectionFactory(RedisCluster.class);
7875
factoryList
7976
.add(new FixDamnedJunitParameterizedNameForConnectionFactory(jedisClusterConnectionFactory, "cluster"));
8077

8178
// Lettuce Cluster
82-
LettuceConnectionFactory lettuceClusterConnectionFactory = new LettuceConnectionFactory(clusterConfiguration);
83-
lettuceClusterConnectionFactory.setClientResources(LettuceTestClientResources.getSharedClientResources());
84-
lettuceClusterConnectionFactory.afterPropertiesSet();
85-
79+
LettuceConnectionFactory lettuceClusterConnectionFactory = LettuceConnectionFactoryExtension
80+
.getConnectionFactory(RedisCluster.class);
8681
factoryList
8782
.add(new FixDamnedJunitParameterizedNameForConnectionFactory(lettuceClusterConnectionFactory, "cluster"));
8883
}

src/test/java/org/springframework/data/redis/cache/DefaultRedisCacheWriterTests.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
import java.util.concurrent.atomic.AtomicReference;
2828
import java.util.function.Consumer;
2929

30-
import org.junit.AfterClass;
3130
import org.junit.Before;
3231
import org.junit.Test;
3332
import org.junit.runner.RunWith;
3433
import org.junit.runners.Parameterized;
3534
import org.junit.runners.Parameterized.Parameters;
36-
import org.springframework.data.redis.ConnectionFactoryTracker;
35+
3736
import org.springframework.data.redis.connection.RedisConnection;
3837
import org.springframework.data.redis.connection.RedisConnectionFactory;
3938
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
4039
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
40+
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
4141
import org.springframework.data.redis.core.types.Expiration;
42+
import org.springframework.data.redis.test.extension.RedisStanalone;
4243

4344
/**
4445
* Integration tests for {@link DefaultRedisCacheWriter}.
@@ -60,27 +61,18 @@ public class DefaultRedisCacheWriterTests {
6061
RedisConnectionFactory connectionFactory;
6162

6263
public DefaultRedisCacheWriterTests(RedisConnectionFactory connectionFactory) {
63-
6464
this.connectionFactory = connectionFactory;
65-
66-
ConnectionFactoryTracker.add(connectionFactory);
6765
}
6866

6967
@Parameters(name = "{index}: {0}")
7068
public static Collection<Object[]> testParams() {
7169
return CacheTestParams.justConnectionFactories();
7270
}
7371

74-
@AfterClass
75-
public static void cleanUpResources() {
76-
ConnectionFactoryTracker.cleanUp();
77-
}
78-
7972
@Before
8073
public void setUp() {
8174

82-
JedisConnectionFactory cf = new JedisConnectionFactory();
83-
cf.afterPropertiesSet();
75+
JedisConnectionFactory cf = JedisConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class);
8476

8577
connectionFactory = cf;
8678

src/test/java/org/springframework/data/redis/cache/LegacyRedisCacheTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public LegacyRedisCacheTests(RedisTemplate template, ObjectFactory<Object> keyFa
7272
this.keyFactory = keyFactory;
7373
this.valueFactory = valueFactory;
7474
this.allowCacheNullValues = allowCacheNullValues;
75-
ConnectionFactoryTracker.add(connectionFactory);
7675

7776
cache = createCache();
7877
}
@@ -98,11 +97,6 @@ public static Collection<Object[]> testParams() {
9897
return target;
9998
}
10099

101-
@AfterClass
102-
public static void cleanUp() {
103-
ConnectionFactoryTracker.cleanUp();
104-
}
105-
106100
@SuppressWarnings("unchecked")
107101
private RedisCache createCache() {
108102

src/test/java/org/springframework/data/redis/cache/RedisCacheConfigurationUnitTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18+
import org.junit.jupiter.api.Test;
19+
1820
import static org.assertj.core.api.Assertions.*;
1921

20-
import org.junit.Test;
2122
import org.springframework.beans.DirectFieldAccessor;
2223
import org.springframework.core.convert.converter.Converter;
2324
import org.springframework.instrument.classloading.ShadowingClassLoader;
@@ -28,10 +29,10 @@
2829
*
2930
* @author Mark Paluch
3031
*/
31-
public class RedisCacheConfigurationUnitTests {
32+
class RedisCacheConfigurationUnitTests {
3233

3334
@Test // DATAREDIS-763
34-
public void shouldSetClassLoader() {
35+
void shouldSetClassLoader() {
3536

3637
ShadowingClassLoader classLoader = new ShadowingClassLoader(getClass().getClassLoader());
3738

@@ -47,7 +48,7 @@ public void shouldSetClassLoader() {
4748
}
4849

4950
@Test // DATAREDIS-1032
50-
public void shouldAllowConverterRegistration() {
51+
void shouldAllowConverterRegistration() {
5152

5253
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
5354
config.configureKeyConverters(registry -> registry.addConverter(new DomainTypeConverter()));

src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ public static Collection<Object[]> testParams() {
8383
return CacheTestParams.connectionFactoriesAndSerializers();
8484
}
8585

86-
@AfterClass
87-
public static void cleanUpResources() {
88-
ConnectionFactoryTracker.cleanUp();
89-
}
90-
9186
@Before
9287
public void setUp() {
9388

src/test/java/org/springframework/data/redis/config/NamespaceTest.java renamed to src/test/java/org/springframework/data/redis/config/NamespaceIntegrationTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,19 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import org.junit.Test;
21-
import org.junit.runner.RunWith;
20+
import org.junit.jupiter.api.Test;
2221

2322
import org.springframework.beans.factory.annotation.Autowired;
2423
import org.springframework.data.redis.core.StringRedisTemplate;
2524
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
26-
import org.springframework.data.redis.test.util.RelaxedJUnit4ClassRunner;
27-
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
28-
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2926

3027
/**
3128
* @author Costin Leau
3229
* @author Mark Paluch
3330
*/
34-
@RunWith(RelaxedJUnit4ClassRunner.class)
35-
@ContextConfiguration("namespace.xml")
36-
@ProfileValueSourceConfiguration
37-
public class NamespaceTest {
31+
@SpringJUnitConfig(locations = "namespace.xml")
32+
class NamespaceIntegrationTests {
3833

3934
@Autowired private RedisMessageListenerContainer container;
4035

@@ -43,12 +38,12 @@ public class NamespaceTest {
4338
@Autowired private StubErrorHandler handler;
4439

4540
@Test
46-
public void testSanityTest() throws Exception {
41+
void testSanityTest() throws Exception {
4742
assertThat(container.isRunning()).isTrue();
4843
}
4944

5045
@Test
51-
public void testWithMessages() throws Exception {
46+
void testWithMessages() throws Exception {
5247
template.convertAndSend("x1", "[X]test");
5348
template.convertAndSend("z1", "[Z]test");
5449
}

0 commit comments

Comments
 (0)