#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.
This commit is contained in:
Mark Paluch
2017-01-30 09:21:37 +01:00
parent 9a2e95a784
commit 3d5a30510c
3 changed files with 62 additions and 6 deletions

View File

@@ -47,7 +47,7 @@ import org.springframework.test.context.junit4.SpringRunner;
public class GeoOperationsTests {
// we only want to run this tests when redis is up an running
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost();
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost().atLeast("3.2");
@Autowired RedisOperations<String, String> operations;
GeoOperations<String, String> geoOperations;

View File

@@ -61,7 +61,8 @@ public class PersonRepositoryTests<K, V> {
* 2) Ignore tests if startup failed and no server running locally.
*/
public static @ClassRule RuleChain rules = RuleChain
.outerRule(EmbeddedRedisServer.runningAt(6379).suppressExceptions()).around(RequiresRedisServer.onLocalhost());
.outerRule(EmbeddedRedisServer.runningAt(6379).suppressExceptions())
.around(RequiresRedisServer.onLocalhost().atLeast("3.2"));
/** {@link Charset} for String conversion **/
private static final Charset CHARSET = Charset.forName("UTF-8");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,40 +15,79 @@
*/
package example.springdata.redis.test.util;
import redis.clients.jedis.Jedis;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.junit.AssumptionViolatedException;
import org.junit.rules.ExternalResource;
import org.springframework.data.redis.connection.jedis.JedisConverters;
import org.springframework.data.util.Version;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Implementation of junit rule {@link ExternalResource} to verify Redis (or at least something on the defined host and
* port) is up and running.
*
* port) is up and running. Allows optionally to require a specific Redis version.
*
* @author Christoph Strobl
* @author Mark Paluch
*/
public class RequiresRedisServer extends ExternalResource {
public static final Version NO_VERSION = Version.parse("0.0.0");
private int timeout = 30;
private Version requiredVersion = NO_VERSION;
private final String host;
private final int port;
private RequiresRedisServer(String host, int port) {
this(host, port, NO_VERSION);
}
private RequiresRedisServer(String host, int port, Version requiredVersion) {
this.host = host;
this.port = port;
this.requiredVersion = requiredVersion;
}
/**
* Require a Redis instance listening on {@code localhost:6379}.
*
* @return
*/
public static RequiresRedisServer onLocalhost() {
return new RequiresRedisServer("localhost", 6379);
}
/**
* Require a Redis instance listening {@code host:port}.
*
* @param host
* @param port
* @return
*/
public static RequiresRedisServer listeningAt(String host, int port) {
return new RequiresRedisServer(StringUtils.hasText(host) ? host : "127.0.0.1", port);
}
/**
* Require a specific Redis version.
*
* @param version must not be {@literal null} or empty.
* @return
*/
public RequiresRedisServer atLeast(String version) {
Assert.hasText(version, "Version must not be empty!");
return new RequiresRedisServer(host, port, Version.parse(version));
}
/*
* (non-Javadoc)
* @see org.junit.rules.ExternalResource#before()
@@ -61,7 +100,23 @@ public class RequiresRedisServer extends ExternalResource {
socket.setSoLinger(true, 0);
socket.connect(new InetSocketAddress(host, port), timeout);
} catch (Exception e) {
throw new AssumptionViolatedException(String.format("Seems as redis is not running at %s:%s.", host, port), e);
throw new AssumptionViolatedException(String.format("Seems as Redis is not running at %s:%s.", host, port), e);
}
if (NO_VERSION.equals(requiredVersion)) {
return;
}
try (Jedis jedis = new Jedis(host, port)) {
String infoServer = jedis.info("server");
String redisVersion = JedisConverters.stringToProps().convert(infoServer).getProperty("redis_version");
Version runningVersion = Version.parse(redisVersion);
if (runningVersion.isLessThan(requiredVersion)) {
throw new AssumptionViolatedException(String
.format("This test requires Redis version %s but you run version %s", requiredVersion, runningVersion));
}
}
}
}