mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1229 - Implement equals and hashCode on ModifyAwareMap, ModifyAwareList and ModifyAwareSet
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package io.ebeaninternal.json;
|
||||
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -16,6 +15,10 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class ModifyAwareMapTest {
|
||||
|
||||
private ModifyAwareMap<String, String> createMap() {
|
||||
@@ -34,96 +37,96 @@ public class ModifyAwareMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
public void testToString() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertEquals(map.map.toString(), map.toString());
|
||||
assertEquals(map.map.toString(), map.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsMarkedDirty() throws Exception {
|
||||
public void testIsMarkedDirty() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
map.put("A", "change");
|
||||
Assert.assertTrue(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarkAsModified() throws Exception {
|
||||
public void testMarkAsModified() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
map.markAsModified();
|
||||
Assert.assertTrue(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() throws Exception {
|
||||
public void testSize() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertEquals(5, map.size());
|
||||
assertEquals(5, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() throws Exception {
|
||||
public void testIsEmpty() {
|
||||
|
||||
Assert.assertFalse(createMap().isEmpty());
|
||||
assertFalse(createMap().isEmpty());
|
||||
Assert.assertTrue(createEmptyMap().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsKey() throws Exception {
|
||||
public void testContainsKey() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertTrue(map.containsKey("A"));
|
||||
Assert.assertFalse(map.containsKey("Z"));
|
||||
assertFalse(map.containsKey("Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsValue() throws Exception {
|
||||
public void testContainsValue() {
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertTrue(map.containsValue("one"));
|
||||
Assert.assertFalse(map.containsValue("junk"));
|
||||
assertFalse(map.containsValue("junk"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() throws Exception {
|
||||
public void testGet() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
|
||||
Assert.assertEquals("two", map.get("B"));
|
||||
assertEquals("two", map.get("B"));
|
||||
Assert.assertNull(map.get("Z"));
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPut() throws Exception {
|
||||
public void testPut() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
map.put("A", "mod");
|
||||
Assert.assertTrue(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() throws Exception {
|
||||
public void testRemove() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
map.remove("A");
|
||||
Assert.assertTrue(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutAllWithEmpty() throws Exception {
|
||||
public void testPutAllWithEmpty() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
Map<String, String> other = new HashMap<>();
|
||||
map.putAll(other);
|
||||
@@ -131,10 +134,10 @@ public class ModifyAwareMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutAll() throws Exception {
|
||||
public void testPutAll() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
Map<String, String> other = new HashMap<>();
|
||||
other.put("A", "one");
|
||||
@@ -143,49 +146,49 @@ public class ModifyAwareMapTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() throws Exception {
|
||||
public void testClear() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
map.clear();
|
||||
Assert.assertTrue(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeySet() throws Exception {
|
||||
public void testKeySet() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
Set<String> keys = map.keySet();
|
||||
Assert.assertEquals(map.size(), keys.size());
|
||||
assertEquals(map.size(), keys.size());
|
||||
Assert.assertTrue(keys.contains("A"));
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValues() throws Exception {
|
||||
public void testValues() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
Collection<String> values = map.values();
|
||||
Assert.assertEquals(map.size(), values.size());
|
||||
assertEquals(map.size(), values.size());
|
||||
Assert.assertTrue(values.contains("one"));
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntrySet() throws Exception {
|
||||
public void testEntrySet() {
|
||||
|
||||
ModifyAwareMap<String, String> map = createMap();
|
||||
Set<Map.Entry<String, String>> entries = map.entrySet();
|
||||
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
|
||||
Assert.assertEquals(map.size(), entries.size());
|
||||
Assert.assertFalse(map.isMarkedDirty());
|
||||
assertEquals(map.size(), entries.size());
|
||||
assertFalse(map.isMarkedDirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -203,7 +206,28 @@ public class ModifyAwareMapTest {
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareMap<String, String> read = (ModifyAwareMap<String, String>)ois.readObject();
|
||||
Assertions.assertThat(read).hasSize(orig.size());
|
||||
ModifyAwareMap<String, String> read = (ModifyAwareMap<String, String>) ois.readObject();
|
||||
assertThat(read).hasSize(orig.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareMap<String, String> mapA = createMap();
|
||||
ModifyAwareMap<String, String> mapB = createMap();
|
||||
|
||||
assertThat(mapA).isEqualTo(mapB);
|
||||
assertThat(mapA.hashCode()).isEqualTo(mapB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareMap<String, String> mapA = createMap();
|
||||
ModifyAwareMap<String, String> mapB = createMap();
|
||||
mapB.put("F", "Six");
|
||||
|
||||
assertThat(mapA).isNotEqualTo(mapB);
|
||||
assertThat(mapA.hashCode()).isNotEqualTo(mapB.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@ import static org.junit.Assert.assertTrue;
|
||||
public class ModifyAwareListTest {
|
||||
|
||||
private ModifyAwareList<String> createList() {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.addAll(Arrays.asList("A", "B", "C", "D", "E"));
|
||||
return new ModifyAwareList<>(list);
|
||||
return new ModifyAwareList<>(new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E")));
|
||||
}
|
||||
|
||||
private ModifyAwareList<String> createEmptyList() {
|
||||
@@ -34,27 +32,27 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() throws Exception {
|
||||
public void testSize() {
|
||||
|
||||
assertEquals(5, createList().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() throws Exception {
|
||||
public void testIsEmpty() {
|
||||
|
||||
assertFalse(createList().isEmpty());
|
||||
assertTrue(createEmptyList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() throws Exception {
|
||||
public void testContains() {
|
||||
|
||||
assertTrue(createList().contains("B"));
|
||||
assertFalse(createList().contains("Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() throws Exception {
|
||||
public void testIterator() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
Iterator<String> iterator = list.iterator();
|
||||
@@ -67,7 +65,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray() throws Exception {
|
||||
public void testToArray() {
|
||||
|
||||
Object[] objects = createList().toArray();
|
||||
assertEquals(5, objects.length);
|
||||
@@ -76,7 +74,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray1() throws Exception {
|
||||
public void testToArray1() {
|
||||
|
||||
String[] objects = createList().toArray(new String[5]);
|
||||
assertEquals(5, objects.length);
|
||||
@@ -85,7 +83,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() throws Exception {
|
||||
public void testAdd() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -95,7 +93,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() throws Exception {
|
||||
public void testRemove() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -105,7 +103,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAll() throws Exception {
|
||||
public void testContainsAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
|
||||
@@ -114,7 +112,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll() throws Exception {
|
||||
public void testAddAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -125,7 +123,7 @@ public class ModifyAwareListTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testRemoveAll() throws Exception {
|
||||
public void testRemoveAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -135,7 +133,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetainAll() throws Exception {
|
||||
public void testRetainAll() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -146,7 +144,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() throws Exception {
|
||||
public void testClear() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -158,7 +156,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() throws Exception {
|
||||
public void testGet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
|
||||
@@ -168,7 +166,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSet() throws Exception {
|
||||
public void testSet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -179,7 +177,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexOf() throws Exception {
|
||||
public void testIndexOf() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -190,7 +188,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastIndexOf() throws Exception {
|
||||
public void testLastIndexOf() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
assertFalse(list.isMarkedDirty());
|
||||
@@ -201,7 +199,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIterator() throws Exception {
|
||||
public void testListIterator() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ListIterator<String> iterator = list.listIterator();
|
||||
@@ -214,7 +212,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIterator1() throws Exception {
|
||||
public void testListIterator1() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ListIterator<String> iterator = list.listIterator(2);
|
||||
@@ -228,7 +226,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubList() throws Exception {
|
||||
public void testSubList() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
List<String> sub = list.subList(1, 3);
|
||||
@@ -242,7 +240,7 @@ public class ModifyAwareListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsSet() throws Exception {
|
||||
public void testAsSet() {
|
||||
|
||||
ModifyAwareList<String> list = createList();
|
||||
ModifyAwareSet<String> set = list.asSet();
|
||||
@@ -271,4 +269,25 @@ public class ModifyAwareListTest {
|
||||
ModifyAwareList<String> read = (ModifyAwareList<String>)ois.readObject();
|
||||
assertThat(read).contains("A", "B", "C", "D", "E");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareList<String> listA = createList();
|
||||
ModifyAwareList<String> listB = createList();
|
||||
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareList<String> listA = createList();
|
||||
ModifyAwareList<String> listB = createList();
|
||||
listB.add("F");
|
||||
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,4 +38,25 @@ public class ModifyAwareSetTest {
|
||||
ModifyAwareSet<String> read = (ModifyAwareSet<String>)ois.readObject();
|
||||
assertThat(read).contains("A", "B", "C", "D", "E");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareSet<String> setA = createSet();
|
||||
ModifyAwareSet<String> setB = createSet();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareSet<String> setA = createSet();
|
||||
ModifyAwareSet<String> setB = createSet();
|
||||
setB.add("F");
|
||||
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user