From 3d5a30510cd81a284c127936236a8e60cf2c99e6 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 30 Jan 2017 09:21:37 +0100 Subject: [PATCH] #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. --- .../redis/commands/GeoOperationsTests.java | 2 +- .../repositories/PersonRepositoryTests.java | 3 +- .../redis/test/util/RequiresRedisServer.java | 63 +++++++++++++++++-- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/redis/example/src/test/java/example/springdata/redis/commands/GeoOperationsTests.java b/redis/example/src/test/java/example/springdata/redis/commands/GeoOperationsTests.java index 7a0731ca..1ce03483 100644 --- a/redis/example/src/test/java/example/springdata/redis/commands/GeoOperationsTests.java +++ b/redis/example/src/test/java/example/springdata/redis/commands/GeoOperationsTests.java @@ -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 operations; GeoOperations geoOperations; diff --git a/redis/repositories/src/test/java/example/springdata/redis/repositories/PersonRepositoryTests.java b/redis/repositories/src/test/java/example/springdata/redis/repositories/PersonRepositoryTests.java index 015c4cf1..050577c4 100644 --- a/redis/repositories/src/test/java/example/springdata/redis/repositories/PersonRepositoryTests.java +++ b/redis/repositories/src/test/java/example/springdata/redis/repositories/PersonRepositoryTests.java @@ -61,7 +61,8 @@ public class PersonRepositoryTests { * 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"); diff --git a/redis/util/src/main/java/example/springdata/redis/test/util/RequiresRedisServer.java b/redis/util/src/main/java/example/springdata/redis/test/util/RequiresRedisServer.java index 0bc8669a..6e607eec 100644 --- a/redis/util/src/main/java/example/springdata/redis/test/util/RequiresRedisServer.java +++ b/redis/util/src/main/java/example/springdata/redis/test/util/RequiresRedisServer.java @@ -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)); + } } } }