From d3362da12c774be493b63c9b0584eb29e6ec0a50 Mon Sep 17 00:00:00 2001 From: Yaourt Date: Fri, 12 Nov 2010 15:42:42 +0100 Subject: [PATCH] hvals now return a Collection, not a Set neither a List. --- .../java/redis/clients/jedis/BinaryJedis.java | 5 +- .../clients/jedis/BinaryJedisCommands.java | 3 +- .../clients/jedis/BinaryShardedJedis.java | 3 +- src/main/java/redis/clients/jedis/Jedis.java | 5 +- .../redis/clients/jedis/JedisCommands.java | 3 +- .../redis/clients/jedis/ShardedJedis.java | 3 +- .../redis/clients/jedis/tests/JedisTest.java | 69 +++++++++++++++++++ .../tests/commands/HashesCommandsTest.java | 12 ++-- 8 files changed, 89 insertions(+), 14 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 410cc6d..a72e937 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -3,6 +3,7 @@ package redis.clients.jedis; import java.io.IOException; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -844,11 +845,11 @@ public class BinaryJedis implements BinaryJedisCommands { * @param key * @return All the fields values contained into a hash. */ - public Set hvals(final byte[] key) { + public Collection hvals(final byte[] key) { checkIsInMulti(); client.hvals(key); final List lresult = client.getBinaryMultiBulkReply(); - return new HashSet(lresult); + return lresult; } /** diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index 3b593b3..052c686 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -62,7 +63,7 @@ public interface BinaryJedisCommands { Set hkeys(byte[] key); - Set hvals(byte[] key); + Collection hvals(byte[] key); Map hgetAll(byte[] key); diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index f438226..948714b 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.io.IOException; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -169,7 +170,7 @@ public class BinaryShardedJedis extends Sharded implement return j.hkeys(key); } - public Set hvals(byte[] key) { + public Collection hvals(byte[] key) { Jedis j = getShard(key); return j.hvals(key); } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 3d46c18..194e3ea 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -773,11 +774,11 @@ public class Jedis extends BinaryJedis implements JedisCommands { * @param key * @return All the fields values contained into a hash. */ - public Set hvals(final String key) { + public Collection hvals(final String key) { checkIsInMulti(); client.hvals(key); final List lresult = client.getMultiBulkReply(); - return new HashSet(lresult); + return lresult; } /** diff --git a/src/main/java/redis/clients/jedis/JedisCommands.java b/src/main/java/redis/clients/jedis/JedisCommands.java index 0d7a1d4..a821519 100644 --- a/src/main/java/redis/clients/jedis/JedisCommands.java +++ b/src/main/java/redis/clients/jedis/JedisCommands.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -60,7 +61,7 @@ public interface JedisCommands { Set hkeys(String key); - Set hvals(String key); + Collection hvals(String key); Map hgetAll(String key); diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index 0ffacb4..a327a3e 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -155,7 +156,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands { return j.hkeys(key); } - public Set hvals(String key) { + public Collection hvals(String key) { Jedis j = getShard(key); return j.hvals(key); } diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index f968b0c..3dceb1c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -49,6 +49,63 @@ public class JedisTest extends JedisCommandTestBase { jedis.get("foo"); } + @SuppressWarnings("rawtypes") + public static boolean isCollectionAreEquals(Collection expected, Collection result) { + if(expected.size() != result.size()) { + return false; + } + + final Iterator expectedit = expected.iterator(); + while(expectedit.hasNext()) { + final Object exp = expectedit.next(); + final Iterator responseit = result.iterator(); + boolean found = false; + while(responseit.hasNext() && !found) { + final Object resp = responseit.next(); + if(exp instanceof byte[] && resp instanceof byte[]) { + final byte[] bexp = (byte[]) exp; + final byte[] bresp = (byte[]) resp; + if(Arrays.equals(bexp, bresp)) { + found = true; + } + } else if (exp instanceof Set && resp instanceof Set) { + final Set subexp = (Set) exp; + final Set subresp = (Set) resp; + if(isSetAreEquals(subexp, subresp)) { + found = true; + } + } else if (exp instanceof List && resp instanceof List) { + final List subexp = (List) exp; + final List subresp = (List) resp; + if(isListAreEquals(subexp, subresp)) { + found = true; + } + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; + if(isCollectionAreEquals(subexp, subresp)) { + found = true; + } + } else { + if (null != exp) { + if (exp.equals(resp)){ + found = true; + } + } else { + if(resp == null) { + found = true; + } + } + } + } + if(!found){ + fail("Result doesn't contain " + (null != exp ? exp.toString() : null)); + } + } + + return true; + } + @SuppressWarnings("rawtypes") public static boolean isSetAreEquals(Set expected, Set result) { if(expected.size() != result.size()) { @@ -80,6 +137,12 @@ public class JedisTest extends JedisCommandTestBase { if(isListAreEquals(subexp, subresp)) { found = true; } + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; + if(isCollectionAreEquals(subexp, subresp)) { + found = true; + } } else { if (null != exp) { if (exp.equals(resp)){ @@ -131,6 +194,12 @@ public class JedisTest extends JedisCommandTestBase { if(isListAreEquals(subexp, subresp)) { continue; } + } else if (exp instanceof Collection && resp instanceof Collection) { + final Collection subexp = (Collection) exp; + final Collection subresp = (Collection) resp; + if(isCollectionAreEquals(subexp, subresp)) { + continue; + } } else { if (null != exp) { if (exp.equals(resp)){ diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java index 567de4b..9467ce0 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -249,11 +249,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { hash.put("car", "bar"); jedis.hmset("foo", hash); - Set vals = jedis.hvals("foo"); - Set expected = new HashSet(); + Collection vals = jedis.hvals("foo"); + List expected = new ArrayList(); expected.add("car"); expected.add("bar"); - assertTrue(JedisTest.isSetAreEquals(expected, vals)); + assertTrue(JedisTest.isCollectionAreEquals(expected, vals)); //Binary Map bhash = new LinkedHashMap(); @@ -261,11 +261,11 @@ public class HashesCommandsTest extends JedisCommandTestBase { bhash.put(bcar, bbar); jedis.hmset(bfoo, bhash); - Set bvals = jedis.hvals(bfoo); - Set bexpected = new HashSet(); + Collection bvals = jedis.hvals(bfoo); + List bexpected = new ArrayList(); bexpected.add(bcar); bexpected.add(bbar); - assertTrue(JedisTest.isSetAreEquals(bexpected, bvals)); + assertTrue(JedisTest.isCollectionAreEquals(bexpected, bvals)); }