refactoring

pull/3215/head
Nikita Koksharov 4 years ago
parent b0b30f36e9
commit d612a051cc

@ -1,29 +1,9 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Assert;
import org.assertj.core.api.Assertions;
import org.junit.Assume;
import org.junit.Test;
import org.redisson.api.RDestroyable;
import org.redisson.api.RLocalCachedMap;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.api.*;
import org.redisson.api.map.MapLoader;
import org.redisson.api.map.MapWriter;
import org.redisson.client.codec.Codec;
@ -34,6 +14,14 @@ import org.redisson.codec.CompositeCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import static org.assertj.core.api.Assertions.assertThat;
public abstract class BaseMapTest extends BaseTest {
public static class SimpleKey implements Serializable {
@ -237,9 +225,7 @@ public abstract class BaseMapTest extends BaseTest {
rmap.put("A", "1");
rmap.put("B", "2");
Iterator<Map.Entry<String, String>> iterator = rmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
for (Entry<String, String> next : rmap.entrySet()) {
assertThat(next).isIn(new AbstractMap.SimpleEntry("A", "1"), new AbstractMap.SimpleEntry("B", "2"));
}
@ -362,30 +348,30 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertEquals(3, map.size());
assertThat(map.size()).isEqualTo(3);
map.put(new SimpleKey("1"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
Assert.assertEquals(3, map.size());
assertThat(map.size()).isEqualTo(3);
map.put(new SimpleKey("1"), new SimpleValue("21"));
map.put(new SimpleKey("3"), new SimpleValue("41"));
Assert.assertEquals(3, map.size());
assertThat(map.size()).isEqualTo(3);
map.put(new SimpleKey("51"), new SimpleValue("6"));
Assert.assertEquals(4, map.size());
assertThat(map.size()).isEqualTo(4);
map.remove(new SimpleKey("3"));
Assert.assertEquals(3, map.size());
assertThat(map.size()).isEqualTo(3);
destroy(map);
}
@Test
public void testEmptyRemove() {
RMap<Integer, Integer> map = getMap("simple");
Assert.assertFalse(map.remove(1, 3));
assertThat(map.remove(1, 3)).isFalse();
map.put(4, 5);
Assert.assertTrue(map.remove(4, 5));
assertThat(map.remove(4, 5)).isTrue();
destroy(map);
}
@ -484,10 +470,10 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
assertThat(val1.getValue()).isEqualTo("44");
SimpleValue val2 = map.get(new SimpleKey("5"));
Assert.assertEquals("6", val2.getValue());
assertThat(val2.getValue()).isEqualTo("6");
destroy(map);
}
@ -497,13 +483,13 @@ public abstract class BaseMapTest extends BaseTest {
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.put(key, value);
Assert.assertEquals(value, map.putIfAbsent(key, new SimpleValue("3")));
Assert.assertEquals(value, map.get(key));
assertThat(map.putIfAbsent(key, new SimpleValue("3"))).isEqualTo(value);
assertThat(map.get(key)).isEqualTo(value);
SimpleKey key1 = new SimpleKey("2");
SimpleValue value1 = new SimpleValue("4");
Assert.assertNull(map.putIfAbsent(key1, value1));
Assert.assertEquals(value1, map.get(key1));
assertThat(map.putIfAbsent(key1, value1)).isNull();
assertThat(map.get(key1)).isEqualTo(value1);
destroy(map);
}
@ -513,12 +499,12 @@ public abstract class BaseMapTest extends BaseTest {
Assume.assumeTrue(!(map instanceof RLocalCachedMap));
SimpleObjectWithoutDefaultConstructor object = new SimpleObjectWithoutDefaultConstructor("test-val");
Assert.assertEquals("test-val", object.getTestField());
assertThat(object.getTestField()).isEqualTo("test-val");
map.put("test-key", object);
try {
map.get("test-key");
Assert.fail("Expected exception from map.get() call");
Assertions.fail("Expected exception from map.get() call");
} catch (Exception e) {
e.printStackTrace();
}
@ -548,10 +534,10 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("43"), new SimpleValue("31"));
Assert.assertFalse(res);
assertThat(res).isFalse();
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
assertThat(val1.getValue()).isEqualTo("2");
destroy(map);
}
@ -561,13 +547,13 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertTrue(res);
assertThat(res).isTrue();
boolean res1 = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertFalse(res1);
assertThat(res1).isFalse();
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
assertThat(val1.getValue()).isEqualTo("3");
destroy(map);
}
@ -577,10 +563,10 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("1"), new SimpleValue("2"));
SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertEquals("2", res.getValue());
assertThat(res.getValue()).isEqualTo("2");
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
assertThat(val1.getValue()).isEqualTo("3");
destroy(map);
}
@ -593,11 +579,11 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("5"), new SimpleValue("6"));
SimpleValue val1 = map.get(new SimpleKey("33"));
Assert.assertEquals("44", val1.getValue());
assertThat(val1.getValue()).isEqualTo("44");
map.put(new SimpleKey("33"), new SimpleValue("abc"));
SimpleValue val2 = map.get(new SimpleKey("33"));
Assert.assertEquals("abc", val2.getValue());
assertThat(val2.getValue()).isEqualTo("abc");
destroy(map);
}
@ -608,9 +594,9 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.containsValue(new SimpleValue("2")));
Assert.assertFalse(map.containsValue(new SimpleValue("441")));
Assert.assertFalse(map.containsValue(new SimpleKey("5")));
assertThat(map.containsValue(new SimpleValue("2"))).isTrue();
assertThat(map.containsValue(new SimpleValue("441"))).isFalse();
assertThat(map.containsValue(new SimpleKey("5"))).isFalse();
destroy(map);
}
@ -621,8 +607,8 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.containsKey(new SimpleKey("33")));
Assert.assertFalse(map.containsKey(new SimpleKey("34")));
assertThat(map.containsKey(new SimpleKey("33"))).isTrue();
assertThat(map.containsKey(new SimpleKey("34"))).isFalse();
destroy(map);
}
@ -632,13 +618,13 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.remove(new SimpleKey("2"), new SimpleValue("1"));
Assert.assertFalse(res);
assertThat(res).isFalse();
boolean res1 = map.remove(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertFalse(res1);
assertThat(res1).isFalse();
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
assertThat(val1.getValue()).isEqualTo("2");
destroy(map);
}
@ -648,12 +634,12 @@ public abstract class BaseMapTest extends BaseTest {
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.remove(new SimpleKey("1"), new SimpleValue("2"));
Assert.assertTrue(res);
assertThat(res).isTrue();
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertNull(val1);
assertThat(val1).isNull();
Assert.assertEquals(0, map.size());
assertThat(map.size()).isZero();
destroy(map);
}
@ -713,7 +699,7 @@ public abstract class BaseMapTest extends BaseTest {
for (Iterator<Integer> iterator = map.values().iterator(); iterator.hasNext();) {
Integer value = iterator.next();
if (!values.remove(value)) {
Assert.fail();
Assertions.fail("unable to remove value " + value);
}
}
@ -724,11 +710,11 @@ public abstract class BaseMapTest extends BaseTest {
@Test
public void testFastPut() throws Exception {
RMap<Integer, Integer> map = getMap("simple");
Assert.assertTrue(map.fastPut(1, 2));
assertThat(map.fastPut(1, 2)).isTrue();
assertThat(map.get(1)).isEqualTo(2);
Assert.assertFalse(map.fastPut(1, 3));
assertThat(map.fastPut(1, 3)).isFalse();
assertThat(map.get(1)).isEqualTo(3);
Assert.assertEquals(1, map.size());
assertThat(map.size()).isEqualTo(1);
destroy(map);
}
@ -739,8 +725,8 @@ public abstract class BaseMapTest extends BaseTest {
assertThat(map.fastReplace(1, 3)).isTrue();
assertThat(map.fastReplace(2, 0)).isFalse();
Assert.assertEquals(1, map.size());
assertThat(map.size()).isEqualTo(1);
assertThat(map.get(1)).isEqualTo(3);
destroy(map);
}
@ -820,7 +806,7 @@ public abstract class BaseMapTest extends BaseTest {
assertThat(map.readAllKeySet().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
assertThat(map.readAllKeySet()).isSubsetOf(testMap.keySet());
destroy(map);
}
@ -838,9 +824,9 @@ public abstract class BaseMapTest extends BaseTest {
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(cnt).isEqualTo(10000);
assertThat(map).isEmpty();
Assert.assertEquals(0, map.size());
assertThat(map.size()).isEqualTo(0);
destroy(map);
}
@ -862,7 +848,7 @@ public abstract class BaseMapTest extends BaseTest {
}
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(cnt).isEqualTo(10000);
assertThat(map.size()).isEqualTo(cnt - removed);
destroy(map);
}
@ -881,9 +867,9 @@ public abstract class BaseMapTest extends BaseTest {
iterator.remove();
cnt++;
}
Assert.assertEquals(10000, cnt);
assertThat(cnt).isEqualTo(10000);
assertThat(map).isEmpty();
Assert.assertEquals(0, map.size());
assertThat(map.size()).isEqualTo(0);
destroy(map);
}
@ -896,7 +882,7 @@ public abstract class BaseMapTest extends BaseTest {
assertThat(map.readAllKeySet().size()).isEqualTo(1000);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllKeySet()).containsOnlyElementsOf(testMap.keySet());
assertThat(map.readAllKeySet()).isSubsetOf(testMap.keySet());
destroy(map);
}
@ -909,7 +895,7 @@ public abstract class BaseMapTest extends BaseTest {
assertThat(map.readAllValues().size()).isEqualTo(3);
Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
assertThat(map.readAllValues()).containsOnlyElementsOf(testMap.values());
assertThat(map.readAllValues()).isSubsetOf(testMap.values());
destroy(map);
}

@ -1,31 +0,0 @@
package org.redisson;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.redisson.api.RedissonClient;
import org.redisson.codec.SerializationCodec;
import org.redisson.config.Config;
import java.io.IOException;
/**
* Created by jribble on 1/12/17.
*/
public class RedissonLocalCachedMapSerializationCodecTest extends RedissonLocalCachedMapTest {
public static Config createConfig() {
Config config = RedissonLocalCachedMapTest.createConfig();
config.setCodec(new SerializationCodec());
return config;
}
public static RedissonClient createInstance() {
Config config = createConfig();
return Redisson.create(config);
}
@Test @Override
public void testAddAndGet() throws InterruptedException {
// this method/test won't work with Java Serialization
}
}

@ -1,18 +1,18 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.redisson.api.MapOptions;
import org.redisson.api.MapOptions.WriteMode;
import org.redisson.api.RMap;
import org.redisson.client.codec.Codec;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.api.MapOptions;
import org.redisson.api.MapOptions.WriteMode;
import org.redisson.api.RMap;
import org.redisson.client.codec.Codec;
import static org.assertj.core.api.Assertions.assertThat;
public class RedissonMapTest extends BaseMapTest {
@ -55,7 +55,7 @@ public class RedissonMapTest extends BaseMapTest {
assertThat(map.entrySet().size()).isEqualTo(3);
Map<Integer, String> testMap = new HashMap<Integer, String>(map);
assertThat(map.entrySet()).containsOnlyElementsOf(testMap.entrySet());
assertThat(map.entrySet()).containsExactlyElementsOf(testMap.entrySet());
}
@Test
@ -67,7 +67,7 @@ public class RedissonMapTest extends BaseMapTest {
assertThat(map.readAllEntrySet().size()).isEqualTo(3);
Map<Integer, String> testMap = new HashMap<Integer, String>(map);
assertThat(map.readAllEntrySet()).containsOnlyElementsOf(testMap.entrySet());
assertThat(map.readAllEntrySet()).containsExactlyElementsOf(testMap.entrySet());
}
@Test
@ -88,8 +88,7 @@ public class RedissonMapTest extends BaseMapTest {
map.put(new SimpleKey("33"), new SimpleValue("44"));
map.put(new SimpleKey("5"), new SimpleValue("6"));
Assert.assertTrue(map.keySet().contains(new SimpleKey("33")));
Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
assertThat(map.keySet()).containsOnly(new SimpleKey("33"), new SimpleKey("1"), new SimpleKey("5"));
}
@Test
@ -105,7 +104,7 @@ public class RedissonMapTest extends BaseMapTest {
for (Iterator<Integer> iterator = map.keySet().iterator(); iterator.hasNext();) {
Integer value = iterator.next();
if (!keys.remove(value)) {
Assert.fail();
Assertions.fail("value can't be removed");
}
}

Loading…
Cancel
Save