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.
1515 */
1616package example .springdata .redis .test .util ;
1717
18+ import redis .clients .jedis .Jedis ;
19+
1820import java .net .InetSocketAddress ;
1921import java .net .Socket ;
2022
2123import org .junit .AssumptionViolatedException ;
2224import 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 ;
2328import 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 */
3137public 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