Skip to content

Commit 3d5a305

Browse files
committed
spring-projects#257 - Guard Redis Geo tests with a minimum required version.
Redis Geo tests are skipped if the minimum Redis version rule of version 3.2 is not met.
1 parent 9a2e95a commit 3d5a305

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

redis/example/src/test/java/example/springdata/redis/commands/GeoOperationsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
public class GeoOperationsTests {
4848

4949
// we only want to run this tests when redis is up an running
50-
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost();
50+
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost().atLeast("3.2");
5151

5252
@Autowired RedisOperations<String, String> operations;
5353
GeoOperations<String, String> geoOperations;

redis/repositories/src/test/java/example/springdata/redis/repositories/PersonRepositoryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public class PersonRepositoryTests<K, V> {
6161
* 2) Ignore tests if startup failed and no server running locally.
6262
*/
6363
public static @ClassRule RuleChain rules = RuleChain
64-
.outerRule(EmbeddedRedisServer.runningAt(6379).suppressExceptions()).around(RequiresRedisServer.onLocalhost());
64+
.outerRule(EmbeddedRedisServer.runningAt(6379).suppressExceptions())
65+
.around(RequiresRedisServer.onLocalhost().atLeast("3.2"));
6566

6667
/** {@link Charset} for String conversion **/
6768
private static final Charset CHARSET = Charset.forName("UTF-8");

redis/util/src/main/java/example/springdata/redis/test/util/RequiresRedisServer.java

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2017 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.
@@ -15,40 +15,79 @@
1515
*/
1616
package example.springdata.redis.test.util;
1717

18+
import redis.clients.jedis.Jedis;
19+
1820
import java.net.InetSocketAddress;
1921
import java.net.Socket;
2022

2123
import org.junit.AssumptionViolatedException;
2224
import org.junit.rules.ExternalResource;
25+
import org.springframework.data.redis.connection.jedis.JedisConverters;
26+
import org.springframework.data.util.Version;
27+
import org.springframework.util.Assert;
2328
import org.springframework.util.StringUtils;
2429

2530
/**
2631
* Implementation of junit rule {@link ExternalResource} to verify Redis (or at least something on the defined host and
27-
* port) is up and running.
28-
*
32+
* port) is up and running. Allows optionally to require a specific Redis version.
33+
*
2934
* @author Christoph Strobl
35+
* @author Mark Paluch
3036
*/
3137
public class RequiresRedisServer extends ExternalResource {
3238

39+
public static final Version NO_VERSION = Version.parse("0.0.0");
40+
3341
private int timeout = 30;
42+
private Version requiredVersion = NO_VERSION;
3443

3544
private final String host;
3645
private final int port;
3746

3847
private RequiresRedisServer(String host, int port) {
48+
this(host, port, NO_VERSION);
49+
}
50+
51+
private RequiresRedisServer(String host, int port, Version requiredVersion) {
3952

4053
this.host = host;
4154
this.port = port;
55+
this.requiredVersion = requiredVersion;
4256
}
4357

58+
/**
59+
* Require a Redis instance listening on {@code localhost:6379}.
60+
*
61+
* @return
62+
*/
4463
public static RequiresRedisServer onLocalhost() {
4564
return new RequiresRedisServer("localhost", 6379);
4665
}
4766

67+
/**
68+
* Require a Redis instance listening {@code host:port}.
69+
*
70+
* @param host
71+
* @param port
72+
* @return
73+
*/
4874
public static RequiresRedisServer listeningAt(String host, int port) {
4975
return new RequiresRedisServer(StringUtils.hasText(host) ? host : "127.0.0.1", port);
5076
}
5177

78+
/**
79+
* Require a specific Redis version.
80+
*
81+
* @param version must not be {@literal null} or empty.
82+
* @return
83+
*/
84+
public RequiresRedisServer atLeast(String version) {
85+
86+
Assert.hasText(version, "Version must not be empty!");
87+
88+
return new RequiresRedisServer(host, port, Version.parse(version));
89+
}
90+
5291
/*
5392
* (non-Javadoc)
5493
* @see org.junit.rules.ExternalResource#before()
@@ -61,7 +100,23 @@ protected void before() throws Throwable {
61100
socket.setSoLinger(true, 0);
62101
socket.connect(new InetSocketAddress(host, port), timeout);
63102
} catch (Exception e) {
64-
throw new AssumptionViolatedException(String.format("Seems as redis is not running at %s:%s.", host, port), e);
103+
throw new AssumptionViolatedException(String.format("Seems as Redis is not running at %s:%s.", host, port), e);
104+
}
105+
106+
if (NO_VERSION.equals(requiredVersion)) {
107+
return;
108+
}
109+
110+
try (Jedis jedis = new Jedis(host, port)) {
111+
112+
String infoServer = jedis.info("server");
113+
String redisVersion = JedisConverters.stringToProps().convert(infoServer).getProperty("redis_version");
114+
Version runningVersion = Version.parse(redisVersion);
115+
116+
if (runningVersion.isLessThan(requiredVersion)) {
117+
throw new AssumptionViolatedException(String
118+
.format("This test requires Redis version %s but you run version %s", requiredVersion, runningVersion));
119+
}
65120
}
66121
}
67122
}

0 commit comments

Comments
 (0)