From 47a7778fe42ad9e82ee8621efdfdc4b745dff08f Mon Sep 17 00:00:00 2001 From: kstyrc Date: Sun, 25 Jan 2015 22:50:01 +0100 Subject: [PATCH 1/4] OS detector based on StackOverflow collected know-how --- .../java/redis/embedded/util/OSDetector.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/main/java/redis/embedded/util/OSDetector.java diff --git a/src/main/java/redis/embedded/util/OSDetector.java b/src/main/java/redis/embedded/util/OSDetector.java new file mode 100644 index 00000000..de9e9894 --- /dev/null +++ b/src/main/java/redis/embedded/util/OSDetector.java @@ -0,0 +1,113 @@ +package redis.embedded.util; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class OSDetector { + + public static enum OS { + WINDOWS, + UNIX, + MACOSX + } + + public static enum Architecture { + x86, + x86_64 + } + + public static OS getOS() { + String osName = System.getProperty("os.name").toLowerCase(); + + if (osName.contains("win")) { + return OS.WINDOWS; + } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { + return OS.UNIX; + } else if ("Mac OS X".equalsIgnoreCase(osName)) { + return OS.MACOSX; + } else { + throw new RuntimeException("Unrecognized OS: " + osName); + } + } + + public static Architecture getArchitecture() { + OS os; + switch (os = getOS()) { + case WINDOWS: + return getWindowsArchitecture(); + case UNIX: + return getUnixArchitecture(); + case MACOSX: + return getMacOSXArchitecture(); + default: + throw new RuntimeException("Unsupported OS: " + os); + } + } + + private static Architecture getWindowsArchitecture() { + String arch = System.getenv("PROCESSOR_ARCHITECTURE"); + String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432"); + + if (arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64")) { + return Architecture.x86_64; + } else { + return Architecture.x86; + } + } + + private static Architecture getUnixArchitecture() { + BufferedReader input = null; + try { + String line; + Process proc = Runtime.getRuntime().exec("uname -m"); + input = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while ((line = input.readLine()) != null) { + if (line.length() > 0) { + if (line.contains("64")) { + return Architecture.x86_64; + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (Exception ignored) { + // ignore + } + } + + return Architecture.x86; + } + + private static Architecture getMacOSXArchitecture() { + BufferedReader input = null; + try { + String line; + Process proc = Runtime.getRuntime().exec("sysctl hw"); + input = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while ((line = input.readLine()) != null) { + if (line.length() > 0) { + if ((line.contains("cpu64bit_capable")) && (line.trim().endsWith("1"))) { + return Architecture.x86_64; + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (Exception ignored) { + // ignore + } + } + + return Architecture.x86; + } +} From e3e5fdeeb35569f4b44e9cf45499d416e961b39f Mon Sep 17 00:00:00 2001 From: kstyrc Date: Mon, 16 Feb 2015 20:53:47 +0100 Subject: [PATCH 2/4] Middle work --- src/main/java/redis/embedded/RedisServer.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/embedded/RedisServer.java b/src/main/java/redis/embedded/RedisServer.java index 87aaf725..1593ce23 100644 --- a/src/main/java/redis/embedded/RedisServer.java +++ b/src/main/java/redis/embedded/RedisServer.java @@ -3,6 +3,7 @@ import com.google.common.base.Strings; import com.google.common.io.Files; import redis.embedded.util.JarUtil; +import redis.embedded.util.OSDetector; import java.io.BufferedReader; import java.io.File; @@ -29,11 +30,14 @@ private RedisRunScriptEnum(String runScript) { } public static String getRedisRunScript() { - String osName = System.getProperty("os.name").toLowerCase(); - String osArch = System.getProperty("os.arch").toLowerCase(); + OSDetector.OS os = OSDetector.getOS(); + OSDetector.Architecture arch = OSDetector.getArchitecture(); - if (osName.contains("win")) { - if (osArch.contains("64")) { + switch (os) { + case + } + if (os == OSDetector.OS.WINDOWS) { + if (arch == OSDetector.Architecture.x86_64) { return WINDOWS_64.runScript; } else { return WINDOWS_32.runScript; From 6e2417bb85bf07c9a61c35e06c41beb3d8323414 Mon Sep 17 00:00:00 2001 From: kstyrc Date: Sun, 25 Jan 2015 22:50:01 +0100 Subject: [PATCH 3/4] OS detector based on StackOverflow collected know-how --- .../java/redis/embedded/util/OSDetector.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/main/java/redis/embedded/util/OSDetector.java diff --git a/src/main/java/redis/embedded/util/OSDetector.java b/src/main/java/redis/embedded/util/OSDetector.java new file mode 100644 index 00000000..de9e9894 --- /dev/null +++ b/src/main/java/redis/embedded/util/OSDetector.java @@ -0,0 +1,113 @@ +package redis.embedded.util; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class OSDetector { + + public static enum OS { + WINDOWS, + UNIX, + MACOSX + } + + public static enum Architecture { + x86, + x86_64 + } + + public static OS getOS() { + String osName = System.getProperty("os.name").toLowerCase(); + + if (osName.contains("win")) { + return OS.WINDOWS; + } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { + return OS.UNIX; + } else if ("Mac OS X".equalsIgnoreCase(osName)) { + return OS.MACOSX; + } else { + throw new RuntimeException("Unrecognized OS: " + osName); + } + } + + public static Architecture getArchitecture() { + OS os; + switch (os = getOS()) { + case WINDOWS: + return getWindowsArchitecture(); + case UNIX: + return getUnixArchitecture(); + case MACOSX: + return getMacOSXArchitecture(); + default: + throw new RuntimeException("Unsupported OS: " + os); + } + } + + private static Architecture getWindowsArchitecture() { + String arch = System.getenv("PROCESSOR_ARCHITECTURE"); + String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432"); + + if (arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64")) { + return Architecture.x86_64; + } else { + return Architecture.x86; + } + } + + private static Architecture getUnixArchitecture() { + BufferedReader input = null; + try { + String line; + Process proc = Runtime.getRuntime().exec("uname -m"); + input = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while ((line = input.readLine()) != null) { + if (line.length() > 0) { + if (line.contains("64")) { + return Architecture.x86_64; + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (Exception ignored) { + // ignore + } + } + + return Architecture.x86; + } + + private static Architecture getMacOSXArchitecture() { + BufferedReader input = null; + try { + String line; + Process proc = Runtime.getRuntime().exec("sysctl hw"); + input = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while ((line = input.readLine()) != null) { + if (line.length() > 0) { + if ((line.contains("cpu64bit_capable")) && (line.trim().endsWith("1"))) { + return Architecture.x86_64; + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (Exception ignored) { + // ignore + } + } + + return Architecture.x86; + } +} From 70f7b000390ad81053a991f5baa5176c656a23bf Mon Sep 17 00:00:00 2001 From: kstyrc Date: Sat, 21 Feb 2015 21:55:34 +0100 Subject: [PATCH 4/4] OS detection fixed. We can now override redis executable per os/arch --- README.md | 23 +++++-- .../redis/embedded/RedisExecProvider.java | 63 +++++++++++++++++++ .../redis/embedded/RedisRunScriptEnum.java | 33 ---------- .../redis/embedded/RedisSentinelBuilder.java | 17 ++--- src/main/java/redis/embedded/RedisServer.java | 12 +++- .../redis/embedded/RedisServerBuilder.java | 20 +++--- .../exceptions/OsDetectionException.java | 11 ++++ .../redis/embedded/util/Architecture.java | 6 ++ src/main/java/redis/embedded/util/OS.java | 7 +++ .../java/redis/embedded/util/OSDetector.java | 29 +++------ .../redis/embedded/util/OsArchitecture.java | 58 +++++++++++++++++ .../java/redis/embedded/RedisServerTest.java | 30 +++++++++ 12 files changed, 225 insertions(+), 84 deletions(-) create mode 100644 src/main/java/redis/embedded/RedisExecProvider.java delete mode 100644 src/main/java/redis/embedded/RedisRunScriptEnum.java create mode 100644 src/main/java/redis/embedded/exceptions/OsDetectionException.java create mode 100644 src/main/java/redis/embedded/util/Architecture.java create mode 100644 src/main/java/redis/embedded/util/OS.java create mode 100644 src/main/java/redis/embedded/util/OsArchitecture.java diff --git a/README.md b/README.md index 5a28e2e0..06488352 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,26 @@ redisServer.start(); redisServer.stop(); ``` -You can also provide RedisServer with your own redis executable to run: +You can also provide RedisServer with your own executable: ```java +// 1) given explicit file (os-independence broken!) RedisServer redisServer = new RedisServer("/path/to/your/redis", 6379); + +// 2) given os-independent matrix +RedisExecProvider customProvider = RedisExecProvider.defaultProvider() + .override(OS.UNIX, "/path/to/unix/redis") + .override(OS.WINDOWS, Architecture.x86, "/path/to/windows/redis") + .override(OS.Windows, Architecture.x86_64, "/path/to/windows/redis") + .override(OS.MAC_OS_X, Architecture.x86, "/path/to/macosx/redis") + .override(OS.MAC_OS_X, Architecture.x86_64, "/path/to/macosx/redis") + +RedisServer redisServer = new RedisServer(customProvider, 6379); ``` You can also use fluent API to create RedisServer: ```java RedisServer redisServer = RedisServer.builder() - .executable("/path/to/your/redis") + .redisExecProvider(customRedisProvider) .port(6379) .slaveOf("locahost", 6378) .configFile("/path/to/your/redis.conf") @@ -32,7 +43,7 @@ RedisServer redisServer = RedisServer.builder() Or even create simple redis.conf file from scratch: ```java RedisServer redisServer = RedisServer.builder() - .executable("/path/to/your/redis") + .redisExecProvider(customRedisProvider) .port(6379) .slaveOf("locahost", 6378) .setting("daemonize no") @@ -135,9 +146,9 @@ Contributors Changelog ============== -### 0.5 - * OS detection fix (not ready) - * redis binary per OS/arch pair (not ready) +### 0.5 + * OS detection fix + * redis binary per OS/arch pair ### 0.4 * Updated for Java 8 diff --git a/src/main/java/redis/embedded/RedisExecProvider.java b/src/main/java/redis/embedded/RedisExecProvider.java new file mode 100644 index 00000000..65d1e7ff --- /dev/null +++ b/src/main/java/redis/embedded/RedisExecProvider.java @@ -0,0 +1,63 @@ +package redis.embedded; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import redis.embedded.util.Architecture; +import redis.embedded.util.JarUtil; +import redis.embedded.util.OS; +import redis.embedded.util.OsArchitecture; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +public class RedisExecProvider { + + private final Map executables = Maps.newHashMap(); + + public static RedisExecProvider defaultProvider() { + return new RedisExecProvider(); + } + + private RedisExecProvider() { + initExecutables(); + } + + private void initExecutables() { + executables.put(OsArchitecture.WINDOWS_x86, "redis-server.exe"); + executables.put(OsArchitecture.WINDOWS_x86_64, "redis-server-64.exe"); + + executables.put(OsArchitecture.UNIX_x86, "redis-server"); + executables.put(OsArchitecture.UNIX_x86_64, "redis-server"); + + executables.put(OsArchitecture.MAC_OS_X_x86, "redis-server.app"); + executables.put(OsArchitecture.MAC_OS_X_x86_64, "redis-server.app"); + } + + public RedisExecProvider override(OS os, String executable) { + Preconditions.checkNotNull(executable); + for (Architecture arch : Architecture.values()) { + override(os, arch, executable); + } + return this; + } + + public RedisExecProvider override(OS os, Architecture arch, String executable) { + Preconditions.checkNotNull(executable); + executables.put(new OsArchitecture(os, arch), executable); + return this; + } + + public File get() throws IOException { + OsArchitecture osArch = OsArchitecture.detect(); + String executablePath = executables.get(osArch); + return fileExists(executablePath) ? + new File(executablePath) : + JarUtil.extractExecutableFromJar(executablePath); + + } + + private boolean fileExists(String executablePath) { + return new File(executablePath).exists(); + } +} diff --git a/src/main/java/redis/embedded/RedisRunScriptEnum.java b/src/main/java/redis/embedded/RedisRunScriptEnum.java deleted file mode 100644 index 385555b0..00000000 --- a/src/main/java/redis/embedded/RedisRunScriptEnum.java +++ /dev/null @@ -1,33 +0,0 @@ -package redis.embedded; - -enum RedisRunScriptEnum { - WINDOWS_32("redis-server.exe"), - WINDOWS_64("redis-server-64.exe"), - UNIX("redis-server"), - MACOSX("redis-server.app"); - - private final String runScript; - - private RedisRunScriptEnum(String runScript) { - this.runScript = runScript; - } - - public static String getRedisRunScript() { - String osName = System.getProperty("os.name").toLowerCase(); - String osArch = System.getProperty("os.arch").toLowerCase(); - - if (osName.contains("win")) { - if (osArch.contains("64")) { - return WINDOWS_64.runScript; - } else { - return WINDOWS_32.runScript; - } - } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { - return UNIX.runScript; - } else if ("Mac OS X".equalsIgnoreCase(osName)) { - return MACOSX.runScript; - } else { - throw new RuntimeException("Unsupported os/architecture...: " + osName + " on " + osArch); - } - } -} diff --git a/src/main/java/redis/embedded/RedisSentinelBuilder.java b/src/main/java/redis/embedded/RedisSentinelBuilder.java index d84a803a..99ce480b 100644 --- a/src/main/java/redis/embedded/RedisSentinelBuilder.java +++ b/src/main/java/redis/embedded/RedisSentinelBuilder.java @@ -3,7 +3,6 @@ import com.google.common.base.Preconditions; import com.google.common.io.Files; import redis.embedded.exceptions.RedisBuildingException; -import redis.embedded.util.JarUtil; import java.io.File; import java.io.IOException; @@ -21,6 +20,7 @@ public class RedisSentinelBuilder { private static final String PORT_LINE = "port %d"; private File executable; + private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider(); private Integer port = 26379; private int masterPort = 6379; private String masterName = "mymaster"; @@ -32,13 +32,8 @@ public class RedisSentinelBuilder { private StringBuilder redisConfigBuilder; - public RedisSentinelBuilder executable(File executable) { - this.executable = executable; - return this; - } - - public RedisSentinelBuilder executable(String executable) { - this.executable = new File(executable); + public RedisSentinelBuilder redisExecProvider(RedisExecProvider redisExecProvider) { + this.redisExecProvider = redisExecProvider; return this; } @@ -110,10 +105,8 @@ private void tryResolveConfAndExec() { if (sentinelConf == null) { resolveSentinelConf(); } - if (executable == null) { - executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript()); - } - } catch (IOException e) { + executable = redisExecProvider.get(); + } catch (Exception e) { throw new RedisBuildingException("Could not build sentinel instance", e); } } diff --git a/src/main/java/redis/embedded/RedisServer.java b/src/main/java/redis/embedded/RedisServer.java index c6d45a62..2393127d 100644 --- a/src/main/java/redis/embedded/RedisServer.java +++ b/src/main/java/redis/embedded/RedisServer.java @@ -1,7 +1,5 @@ package redis.embedded; -import redis.embedded.util.JarUtil; - import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -18,7 +16,7 @@ public RedisServer() throws IOException { public RedisServer(Integer port) throws IOException { super(port); - File executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript()); + File executable = RedisExecProvider.defaultProvider().get(); this.args = Arrays.asList( executable.getAbsolutePath(), "--port", Integer.toString(port) @@ -33,6 +31,14 @@ public RedisServer(File executable, Integer port) { ); } + public RedisServer(RedisExecProvider redisExecProvider, Integer port) throws IOException { + super(port); + this.args = Arrays.asList( + redisExecProvider.get().getAbsolutePath(), + "--port", Integer.toString(port) + ); + } + RedisServer(List args, int port) { super(port); this.args = new ArrayList<>(args); diff --git a/src/main/java/redis/embedded/RedisServerBuilder.java b/src/main/java/redis/embedded/RedisServerBuilder.java index 43ff3b76..7c6b99ea 100644 --- a/src/main/java/redis/embedded/RedisServerBuilder.java +++ b/src/main/java/redis/embedded/RedisServerBuilder.java @@ -3,7 +3,6 @@ import com.google.common.base.Strings; import com.google.common.io.Files; import redis.embedded.exceptions.RedisBuildingException; -import redis.embedded.util.JarUtil; import java.io.File; import java.io.IOException; @@ -17,22 +16,18 @@ public class RedisServerBuilder { private static final String CONF_FILENAME = "embedded-redis-server"; private File executable; + private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider(); private Integer port = 6379; private InetSocketAddress slaveOf; private String redisConf; private StringBuilder redisConfigBuilder; - public RedisServerBuilder executable(File executable) { - this.executable = executable; + public RedisServerBuilder redisExecProvider(RedisExecProvider redisExecProvider) { + this.redisExecProvider = redisExecProvider; return this; } - - public RedisServerBuilder executable(String executable) { - this.executable = new File(executable); - return this; - } - + public RedisServerBuilder port(Integer port) { this.port = port; return this; @@ -77,6 +72,7 @@ public RedisServer build() { } public void reset() { + this.executable = null; this.redisConfigBuilder = null; this.slaveOf = null; this.redisConf = null; @@ -98,8 +94,10 @@ private void resolveConfAndExec() throws IOException { redisConf = redisConfigFile.getAbsolutePath(); } - if (executable == null) { - executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript()); + try { + executable = redisExecProvider.get(); + } catch (Exception e) { + throw new RedisBuildingException("Failed to resolve executable", e); } } diff --git a/src/main/java/redis/embedded/exceptions/OsDetectionException.java b/src/main/java/redis/embedded/exceptions/OsDetectionException.java new file mode 100644 index 00000000..d69123f4 --- /dev/null +++ b/src/main/java/redis/embedded/exceptions/OsDetectionException.java @@ -0,0 +1,11 @@ +package redis.embedded.exceptions; + +public class OsDetectionException extends RuntimeException { + public OsDetectionException(String message) { + super(message); + } + + public OsDetectionException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/redis/embedded/util/Architecture.java b/src/main/java/redis/embedded/util/Architecture.java new file mode 100644 index 00000000..4f555ae4 --- /dev/null +++ b/src/main/java/redis/embedded/util/Architecture.java @@ -0,0 +1,6 @@ +package redis.embedded.util; + +public enum Architecture { + x86, + x86_64 +} diff --git a/src/main/java/redis/embedded/util/OS.java b/src/main/java/redis/embedded/util/OS.java new file mode 100644 index 00000000..8347b344 --- /dev/null +++ b/src/main/java/redis/embedded/util/OS.java @@ -0,0 +1,7 @@ +package redis.embedded.util; + +public enum OS { + WINDOWS, + UNIX, + MAC_OS_X +} diff --git a/src/main/java/redis/embedded/util/OSDetector.java b/src/main/java/redis/embedded/util/OSDetector.java index de9e9894..74698d1e 100644 --- a/src/main/java/redis/embedded/util/OSDetector.java +++ b/src/main/java/redis/embedded/util/OSDetector.java @@ -1,21 +1,12 @@ package redis.embedded.util; +import redis.embedded.exceptions.OsDetectionException; + import java.io.BufferedReader; import java.io.InputStreamReader; public class OSDetector { - public static enum OS { - WINDOWS, - UNIX, - MACOSX - } - - public static enum Architecture { - x86, - x86_64 - } - public static OS getOS() { String osName = System.getProperty("os.name").toLowerCase(); @@ -24,23 +15,23 @@ public static OS getOS() { } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { return OS.UNIX; } else if ("Mac OS X".equalsIgnoreCase(osName)) { - return OS.MACOSX; + return OS.MAC_OS_X; } else { - throw new RuntimeException("Unrecognized OS: " + osName); + throw new OsDetectionException("Unrecognized OS: " + osName); } } public static Architecture getArchitecture() { - OS os; - switch (os = getOS()) { + OS os = getOS(); + switch (os) { case WINDOWS: return getWindowsArchitecture(); case UNIX: return getUnixArchitecture(); - case MACOSX: + case MAC_OS_X: return getMacOSXArchitecture(); default: - throw new RuntimeException("Unsupported OS: " + os); + throw new OsDetectionException("Unrecognized OS: " + os); } } @@ -69,7 +60,7 @@ private static Architecture getUnixArchitecture() { } } } catch (Exception e) { - throw new RuntimeException(e); + throw new OsDetectionException(e); } finally { try { if (input != null) { @@ -97,7 +88,7 @@ private static Architecture getMacOSXArchitecture() { } } } catch (Exception e) { - throw new RuntimeException(e); + throw new OsDetectionException(e); } finally { try { if (input != null) { diff --git a/src/main/java/redis/embedded/util/OsArchitecture.java b/src/main/java/redis/embedded/util/OsArchitecture.java new file mode 100644 index 00000000..4d51db89 --- /dev/null +++ b/src/main/java/redis/embedded/util/OsArchitecture.java @@ -0,0 +1,58 @@ +package redis.embedded.util; + +import com.google.common.base.Preconditions; + +public class OsArchitecture { + + public static final OsArchitecture WINDOWS_x86 = new OsArchitecture(OS.WINDOWS, Architecture.x86); + public static final OsArchitecture WINDOWS_x86_64 = new OsArchitecture(OS.WINDOWS, Architecture.x86_64); + + public static final OsArchitecture UNIX_x86 = new OsArchitecture(OS.UNIX, Architecture.x86); + public static final OsArchitecture UNIX_x86_64 = new OsArchitecture(OS.UNIX, Architecture.x86_64); + + public static final OsArchitecture MAC_OS_X_x86 = new OsArchitecture(OS.MAC_OS_X, Architecture.x86); + public static final OsArchitecture MAC_OS_X_x86_64 = new OsArchitecture(OS.MAC_OS_X, Architecture.x86_64); + + private final OS os; + private final Architecture arch; + + public static OsArchitecture detect() { + OS os = OSDetector.getOS(); + Architecture arch = OSDetector.getArchitecture(); + return new OsArchitecture(os, arch); + } + + public OsArchitecture(OS os, Architecture arch) { + Preconditions.checkNotNull(os); + Preconditions.checkNotNull(arch); + + this.os = os; + this.arch = arch; + } + + public OS os() { + return os; + } + + public Architecture arch() { + return arch; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + OsArchitecture that = (OsArchitecture) o; + + return arch == that.arch && os == that.os; + + } + + @Override + public int hashCode() { + int result = os.hashCode(); + result = 31 * result + arch.hashCode(); + return result; + } +} diff --git a/src/test/java/redis/embedded/RedisServerTest.java b/src/test/java/redis/embedded/RedisServerTest.java index cbdf94e5..48904bfb 100644 --- a/src/test/java/redis/embedded/RedisServerTest.java +++ b/src/test/java/redis/embedded/RedisServerTest.java @@ -1,8 +1,12 @@ package redis.embedded; +import com.google.common.io.Resources; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; +import redis.embedded.exceptions.RedisBuildingException; +import redis.embedded.util.Architecture; +import redis.embedded.util.OS; import static org.junit.Assert.*; @@ -85,4 +89,30 @@ public void shouldIndicateInactiveAfterStop() throws Exception { redisServer.stop(); assertFalse(redisServer.isActive()); } + + @Test + public void shouldOverrideDefaultExecutable() throws Exception { + RedisExecProvider customProvider = RedisExecProvider.defaultProvider() + .override(OS.UNIX, Resources.getResource("redis-server").getFile()) + .override(OS.WINDOWS, Architecture.x86, Resources.getResource("redis-server.exe").getFile()) + .override(OS.WINDOWS, Architecture.x86_64, Resources.getResource("redis-server-64.exe").getFile()) + .override(OS.MAC_OS_X, Resources.getResource("redis-server").getFile()); + + redisServer = new RedisServerBuilder() + .redisExecProvider(customProvider) + .build(); + } + + @Test(expected = RedisBuildingException.class) + public void shouldFailWhenBadExecutableGiven() throws Exception { + RedisExecProvider buggyProvider = RedisExecProvider.defaultProvider() + .override(OS.UNIX, "some") + .override(OS.WINDOWS, Architecture.x86, "some") + .override(OS.WINDOWS, Architecture.x86_64, "some") + .override(OS.MAC_OS_X, "some"); + + redisServer = new RedisServerBuilder() + .redisExecProvider(buggyProvider) + .build(); + } }