Skip to content

Commit 293bf1e

Browse files
committed
Polishing redis#3425 and some tests
1 parent 50ec110 commit 293bf1e

11 files changed

+135
-157
lines changed

docs/jedis5-breaking.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Following BuilderFactory implementations have been removed:
2323
- `BYTE_ARRAY` (use `BINARY`)
2424
- `BYTE_ARRAY_LIST` (use `BINARY_LIST`)
25+
- `BINARY_MAP_FROM_PAIRS`
2526

2627
<!--- Deprecated in Jedis 4 --->
2728

src/main/java/redis/clients/jedis/BuilderFactory.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ public String toString() {
241241
}
242242
};
243243

244-
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST = new Builder<List<Map.Entry<byte[], byte[]>>>() {
244+
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST
245+
= new Builder<List<Map.Entry<byte[], byte[]>>>() {
245246
@Override
246247
@SuppressWarnings("unchecked")
247248
public List<Map.Entry<byte[], byte[]>> build(Object data) {
@@ -261,7 +262,8 @@ public String toString() {
261262
}
262263
};
263264

264-
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST_FROM_PAIRS = new Builder<List<Map.Entry<byte[], byte[]>>>() {
265+
public static final Builder<List<Map.Entry<byte[], byte[]>>> BINARY_PAIR_LIST_FROM_PAIRS
266+
= new Builder<List<Map.Entry<byte[], byte[]>>>() {
265267
@Override
266268
@SuppressWarnings("unchecked")
267269
public List<Map.Entry<byte[], byte[]>> build(Object data) {
@@ -326,7 +328,8 @@ public String toString() {
326328
@SuppressWarnings("unchecked")
327329
public Set<String> build(Object data) {
328330
if (null == data) return null;
329-
return ((List<Object>) data).stream().map(STRING::build).collect(Collectors.toCollection(LinkedHashSet::new));
331+
return ((List<Object>) data).stream().map(STRING::build)
332+
.collect(Collectors.toCollection(LinkedHashSet::new));
330333
}
331334

332335
@Override
@@ -355,15 +358,16 @@ public String toString() {
355358
}
356359
};
357360

358-
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST = new Builder<List<Map.Entry<String, String>>>() {
361+
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST
362+
= new Builder<List<Map.Entry<String, String>>>() {
359363
@Override
360364
@SuppressWarnings("unchecked")
361365
public List<Map.Entry<String, String>> build(Object data) {
362366
final List<byte[]> flatHash = (List<byte[]>) data;
363-
final List<Map.Entry<String, String>> pairList = new ArrayList<>();
367+
final List<Map.Entry<String, String>> pairList = new ArrayList<>(flatHash.size() / 2);
364368
final Iterator<byte[]> iterator = flatHash.iterator();
365369
while (iterator.hasNext()) {
366-
pairList.add(new AbstractMap.SimpleEntry<>(STRING.build(iterator.next()), STRING.build(iterator.next())));
370+
pairList.add(KeyValue.of(STRING.build(iterator.next()), STRING.build(iterator.next())));
367371
}
368372

369373
return pairList;
@@ -373,28 +377,22 @@ public List<Map.Entry<String, String>> build(Object data) {
373377
public String toString() {
374378
return "List<Map.Entry<String, String>>";
375379
}
376-
377380
};
378381

379-
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST_FROM_PAIRS = new Builder<List<Map.Entry<String, String>>>() {
382+
public static final Builder<List<Map.Entry<String, String>>> STRING_PAIR_LIST_FROM_PAIRS
383+
= new Builder<List<Map.Entry<String, String>>>() {
380384
@Override
381385
@SuppressWarnings("unchecked")
382386
public List<Map.Entry<String, String>> build(Object data) {
383-
final List<Object> list = (List<Object>) data;
384-
final List<Map.Entry<String, String>> pairList = new ArrayList<>();
385-
for (Object object : list) {
386-
final List<byte[]> flat = (List<byte[]>) object;
387-
pairList.add(new AbstractMap.SimpleEntry<>(SafeEncoder.encode(flat.get(0)), SafeEncoder.encode(flat.get(1))));
388-
}
389-
390-
return pairList;
387+
return ((List<Object>) data).stream().map(o -> (List<Object>) o)
388+
.map(l -> KeyValue.of(STRING.build(l.get(0)), STRING.build(l.get(1))))
389+
.collect(Collectors.toList());
391390
}
392391

393392
@Override
394393
public String toString() {
395394
return "List<Map.Entry<String, String>>";
396395
}
397-
398396
};
399397

400398
public static final Builder<KeyedListElement> KEYED_LIST_ELEMENT = new Builder<KeyedListElement>() {

src/test/java/redis/clients/jedis/ClusterPipeliningTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.hamcrest.Matchers;
1010
import org.junit.After;
1111
import org.junit.AfterClass;
12-
import org.junit.Assert;
1312
import org.junit.Before;
1413
import org.junit.Test;
1514

@@ -20,6 +19,7 @@
2019
import redis.clients.jedis.resps.GeoRadiusResponse;
2120
import redis.clients.jedis.resps.StreamEntry;
2221
import redis.clients.jedis.resps.Tuple;
22+
import redis.clients.jedis.util.AssertUtil;
2323
import redis.clients.jedis.util.JedisClusterTestUtil;
2424
import redis.clients.jedis.util.SafeEncoder;
2525

@@ -650,10 +650,11 @@ public void clusterPipelineHash() {
650650
Response<List<String>> r11 = p.hvals("mynewhash");
651651
Response<List<String>> r12 = p.hmget("myhash", "field1", "field2");
652652
Response<String> r13 = p.hrandfield("myotherhash");
653-
Response<List<String>> r14 = p.hrandfield("myotherhash", 2);
654-
Response<List<Map.Entry<String, String>>> r15 = p.hrandfieldWithValues("myotherhash", 2);
653+
Response<List<String>> r14 = p.hrandfield("myotherhash", 4);
654+
Response<List<String>> r15 = p.hrandfield("myotherhash", -4);
655655
Response<Long> r16 = p.hstrlen("myhash", "field1");
656-
Response<List<Map.Entry<String, String>>> r17 = p.hrandfieldWithValues("myotherhash", -2);
656+
Response<List<Map.Entry<String, String>>> r17 = p.hrandfieldWithValues("myotherhash", 4);
657+
Response<List<Map.Entry<String, String>>> r18 = p.hrandfieldWithValues("myotherhash", -4);
657658

658659
p.sync();
659660
assertEquals(Long.valueOf(1), r1.get());
@@ -668,11 +669,12 @@ public void clusterPipelineHash() {
668669
assertEquals(keys, r10.get());
669670
assertEquals(vals, r11.get());
670671
assertEquals(vals2, r12.get());
671-
assertTrue(hm.keySet().contains(r13.get()));
672+
AssertUtil.assertCollectionContains(hm.keySet(), r13.get());
672673
assertEquals(2, r14.get().size());
673-
Assert.assertTrue(r15.get().contains(new AbstractMap.SimpleEntry<>("field3", "5")));
674+
assertEquals(4, r15.get().size());
674675
assertEquals(Long.valueOf(5), r16.get());
675-
Assert.assertTrue(r17.get().contains(new AbstractMap.SimpleEntry<>("field3", "5")) || r17.get().contains(new AbstractMap.SimpleEntry<>("field2", "2")));
676+
assertEquals(2, r17.get().size());
677+
assertEquals(4, r18.get().size());
676678
}
677679

678680
@Test

src/test/java/redis/clients/jedis/commands/jedis/AllKindOfValuesCommandsTest.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
import static redis.clients.jedis.Protocol.Command.XINFO;
1313
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START;
1414
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START_BINARY;
15-
import static redis.clients.jedis.params.SetParams.setParams;
16-
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
17-
import static redis.clients.jedis.util.AssertUtil.assertCollectionContains;
1815

1916
import java.util.*;
2017
import org.hamcrest.MatcherAssert;
@@ -38,6 +35,8 @@
3835
import redis.clients.jedis.util.KeyValue;
3936
import redis.clients.jedis.util.SafeEncoder;
4037
import redis.clients.jedis.exceptions.JedisDataException;
38+
import redis.clients.jedis.params.SetParams;
39+
import redis.clients.jedis.util.AssertUtil;
4140

4241
public class AllKindOfValuesCommandsTest extends JedisCommandsTestBase {
4342
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
@@ -216,28 +215,20 @@ public void keys() {
216215
jedis.set("foobar", "bar");
217216

218217
Set<String> keys = jedis.keys("foo*");
219-
Set<String> expected = new HashSet<>();
220-
expected.add("foo");
221-
expected.add("foobar");
222-
assertEquals(expected, keys);
218+
AssertUtil.assertCollectionContains(keys, "foo");
219+
AssertUtil.assertCollectionContains(keys, "foobar");
223220

224-
expected = new HashSet<>();
225-
keys = jedis.keys("bar*");
226-
227-
assertEquals(expected, keys);
221+
assertEquals(Collections.emptySet(), jedis.keys("bar*"));
228222

229223
// Binary
230224
jedis.set(bfoo, bbar);
231225
jedis.set(bfoobar, bbar);
232226

233227
Set<byte[]> bkeys = jedis.keys(bfoostar);
234-
assertEquals(2, bkeys.size());
235-
assertCollectionContains(bkeys, bfoo);
236-
assertCollectionContains(bkeys, bfoobar);
237-
238-
bkeys = jedis.keys(bbarstar);
228+
AssertUtil.assertByteArrayCollectionContains(bkeys, bfoo);
229+
AssertUtil.assertByteArrayCollectionContains(bkeys, bfoobar);
239230

240-
assertEquals(0, bkeys.size());
231+
assertEquals(Collections.emptySet(), jedis.keys(bbarstar));
241232
}
242233

243234
@Test
@@ -880,11 +871,11 @@ public void scanType() {
880871
assertEquals(4, page1Count + page2Count);
881872

882873
binaryResult = jedis.scan(SCAN_POINTER_START_BINARY, noParams, hash);
883-
assertByteArrayListEquals(Collections.singletonList(new byte[]{98}), binaryResult.getResult());
874+
AssertUtil.assertByteArrayListEquals(Collections.singletonList(new byte[]{98}), binaryResult.getResult());
884875
binaryResult = jedis.scan(SCAN_POINTER_START_BINARY, noParams, set);
885-
assertByteArrayListEquals(Collections.singletonList(new byte[]{100}), binaryResult.getResult());
876+
AssertUtil.assertByteArrayListEquals(Collections.singletonList(new byte[]{100}), binaryResult.getResult());
886877
binaryResult = jedis.scan(SCAN_POINTER_START_BINARY, noParams, zset);
887-
assertByteArrayListEquals(Collections.singletonList(new byte[]{102}), binaryResult.getResult());
878+
AssertUtil.assertByteArrayListEquals(Collections.singletonList(new byte[]{102}), binaryResult.getResult());
888879
}
889880

890881
@Test
@@ -917,10 +908,10 @@ private ScanResult<String> scanCompletely(String cursor) {
917908

918909
@Test
919910
public void setNxExAndGet() {
920-
assertEquals("OK", jedis.set("hello", "world", setParams().nx().ex(expireSeconds)));
911+
assertEquals("OK", jedis.set("hello", "world", SetParams.setParams().nx().ex(expireSeconds)));
921912
assertEquals("world", jedis.get("hello"));
922913

923-
assertNull(jedis.set("hello", "bar", setParams().nx().ex(expireSeconds)));
914+
assertNull(jedis.set("hello", "bar", SetParams.setParams().nx().ex(expireSeconds)));
924915
assertEquals("world", jedis.get("hello"));
925916

926917
long ttl = jedis.ttl("hello");
@@ -930,10 +921,10 @@ public void setNxExAndGet() {
930921
byte[] bworld = { 0x77, 0x6F, 0x72, 0x6C, 0x64 };
931922
byte[] bhello = { 0x68, 0x65, 0x6C, 0x6C, 0x6F };
932923

933-
assertEquals("OK", jedis.set(bworld, bhello, setParams().nx().ex(expireSeconds)));
924+
assertEquals("OK", jedis.set(bworld, bhello, SetParams.setParams().nx().ex(expireSeconds)));
934925
assertArrayEquals(bhello, jedis.get(bworld));
935926

936-
assertNull(jedis.set(bworld, bbar, setParams().nx().ex(expireSeconds)));
927+
assertNull(jedis.set(bworld, bbar, SetParams.setParams().nx().ex(expireSeconds)));
937928
assertArrayEquals(bhello, jedis.get(bworld));
938929

939930
long bttl = jedis.ttl(bworld);
@@ -958,12 +949,12 @@ public void setGet() {
958949
assertEquals("OK", jedis.set("hello", "world"));
959950

960951
// GET old value
961-
assertEquals("world", jedis.setGet("hello", "jedis", setParams()));
952+
assertEquals("world", jedis.setGet("hello", "jedis", SetParams.setParams()));
962953

963954
assertEquals("jedis", jedis.get("hello"));
964955

965956
// GET null value
966-
assertNull(jedis.setGet("key", "value", setParams()));
957+
assertNull(jedis.setGet("key", "value", SetParams.setParams()));
967958
}
968959

969960
@Test

src/test/java/redis/clients/jedis/commands/jedis/ControlCommandsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ public void roleSentinel() {
155155
List<Object> role = sentinel.role();
156156
assertEquals("sentinel", role.get(0));
157157
assertTrue(role.get(1) instanceof List);
158-
assertTrue(((List) role.get(1)).contains("mymaster"));
158+
AssertUtil.assertCollectionContains((List) role.get(1), "mymaster");
159159

160160
// binary
161161
List<Object> brole = sentinel.roleBinary();
162162
assertArrayEquals("sentinel".getBytes(), (byte[]) brole.get(0));
163163
assertTrue(brole.get(1) instanceof List);
164-
AssertUtil.assertCollectionContains((List) brole.get(1), "mymaster".getBytes());
164+
AssertUtil.assertByteArrayCollectionContains((List) brole.get(1), "mymaster".getBytes());
165165
}
166166
}
167167

@@ -341,7 +341,7 @@ public Long call() throws Exception {
341341

342342
assertThat(latencyRead.get(), Matchers.lessThan(100L));
343343

344-
assertThat(latencyWrite.get(), greaterThan(100L));
344+
assertThat(latencyWrite.get(), Matchers.greaterThan(100L));
345345

346346
} finally {
347347
executorService.shutdown();

src/test/java/redis/clients/jedis/commands/jedis/HashesCommandsTest.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
55
import static org.junit.Assert.assertFalse;
6-
import static org.junit.Assert.assertNotNull;
76
import static org.junit.Assert.assertTrue;
87
import static org.junit.Assert.assertNull;
98

109
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START;
1110
import static redis.clients.jedis.params.ScanParams.SCAN_POINTER_START_BINARY;
12-
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
13-
import static redis.clients.jedis.util.AssertUtil.assertByteArraySetEquals;
14-
import static redis.clients.jedis.util.AssertUtil.assertCollectionContains;
1511

1612
import java.util.ArrayList;
1713
import java.util.Collections;
@@ -28,6 +24,7 @@
2824
import redis.clients.jedis.Response;
2925
import redis.clients.jedis.params.ScanParams;
3026
import redis.clients.jedis.resps.ScanResult;
27+
import redis.clients.jedis.util.AssertUtil;
3128
import redis.clients.jedis.util.JedisByteHashMap;
3229

3330
public class HashesCommandsTest extends JedisCommandsTestBase {
@@ -149,7 +146,7 @@ public void hmget() {
149146
bexpected.add(bbar);
150147
bexpected.add(null);
151148

152-
assertByteArrayListEquals(bexpected, bvalues);
149+
AssertUtil.assertByteArrayListEquals(bexpected, bvalues);
153150
}
154151

155152
@Test
@@ -272,7 +269,7 @@ public void hkeys() {
272269
Set<byte[]> bexpected = new LinkedHashSet<byte[]>();
273270
bexpected.add(bbar);
274271
bexpected.add(bcar);
275-
assertByteArraySetEquals(bexpected, bkeys);
272+
AssertUtil.assertByteArraySetEquals(bexpected, bkeys);
276273
}
277274

278275
@Test
@@ -284,8 +281,8 @@ public void hvals() {
284281

285282
List<String> vals = jedis.hvals("foo");
286283
assertEquals(2, vals.size());
287-
assertTrue(vals.contains("bar"));
288-
assertTrue(vals.contains("car"));
284+
AssertUtil.assertCollectionContains(vals, "bar");
285+
AssertUtil.assertCollectionContains(vals, "car");
289286

290287
// Binary
291288
Map<byte[], byte[]> bhash = new LinkedHashMap<byte[], byte[]>();
@@ -296,8 +293,8 @@ public void hvals() {
296293
List<byte[]> bvals = jedis.hvals(bfoo);
297294

298295
assertEquals(2, bvals.size());
299-
assertCollectionContains(bvals, bbar);
300-
assertCollectionContains(bvals, bcar);
296+
AssertUtil.assertByteArrayCollectionContains(bvals, bbar);
297+
AssertUtil.assertByteArrayCollectionContains(bvals, bcar);
301298
}
302299

303300
@Test
@@ -459,16 +456,16 @@ public void hrandfield() {
459456
assertEquals(2, jedis.hrandfield("foo", 2).size());
460457

461458
List<Map.Entry<String, String>> actual = jedis.hrandfieldWithValues("foo", 2);
462-
assertNotNull(actual);
463459
assertEquals(2, actual.size());
464-
Map.Entry entry = actual.get(0);
465-
assertEquals(hash.get(entry.getKey()), entry.getValue());
460+
actual.forEach(e -> assertEquals(hash.get(e.getKey()), e.getValue()));
466461

467-
actual = jedis.hrandfieldWithValues("foo", -2);
468-
assertNotNull(actual);
469-
assertEquals(2, actual.size());
470-
entry = actual.get(0);
471-
assertEquals(hash.get(entry.getKey()), entry.getValue());
462+
actual = jedis.hrandfieldWithValues("foo", 5);
463+
assertEquals(3, actual.size());
464+
actual.forEach(e -> assertEquals(hash.get(e.getKey()), e.getValue()));
465+
466+
actual = jedis.hrandfieldWithValues("foo", -5);
467+
assertEquals(5, actual.size());
468+
actual.forEach(e -> assertEquals(hash.get(e.getKey()), e.getValue()));
472469

473470
// binary
474471
assertNull(jedis.hrandfield(bfoo));
@@ -487,15 +484,15 @@ public void hrandfield() {
487484
assertEquals(2, jedis.hrandfield(bfoo, 2).size());
488485

489486
List<Map.Entry<byte[], byte[]>> bactual = jedis.hrandfieldWithValues(bfoo, 2);
490-
assertNotNull(bactual);
491487
assertEquals(2, bactual.size());
492-
Map.Entry bentry = bactual.get(0);
493-
assertArrayEquals(bhash.get(bentry.getKey()), (byte[]) bentry.getValue());
488+
bactual.forEach(e -> assertArrayEquals(bhash.get(e.getKey()), e.getValue()));
494489

495-
bactual = jedis.hrandfieldWithValues(bfoo, -2);
496-
assertNotNull(bactual);
497-
assertEquals(2, bactual.size());
498-
bentry = bactual.get(0);
499-
assertArrayEquals(bhash.get(bentry.getKey()), (byte[]) bentry.getValue());
490+
bactual = jedis.hrandfieldWithValues(bfoo, 5);
491+
assertEquals(3, bactual.size());
492+
bactual.forEach(e -> assertArrayEquals(bhash.get(e.getKey()), e.getValue()));
493+
494+
bactual = jedis.hrandfieldWithValues(bfoo, -5);
495+
assertEquals(5, bactual.size());
496+
bactual.forEach(e -> assertArrayEquals(bhash.get(e.getKey()), e.getValue()));
500497
}
501498
}

0 commit comments

Comments
 (0)