mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add tests and fixes in hashCode
This commit is contained in:
@@ -16,9 +16,7 @@ import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
public class ModifyAwareListTest {
|
||||
@@ -290,4 +288,21 @@ public class ModifyAwareListTest {
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareList<String> listA = createEmptyList();
|
||||
ArrayList<String> listB = new ArrayList<>();
|
||||
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
|
||||
listA.add("foo");
|
||||
assertThat(listA).isNotEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isNotEqualTo(listB.hashCode());
|
||||
|
||||
listB.add("foo");
|
||||
assertThat(listA).isEqualTo(listB);
|
||||
assertThat(listA.hashCode()).isEqualTo(listB.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebeaninternal.json.ModifyAwareMap;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class ModifyAwareMapTest {
|
||||
|
||||
private ModifyAwareMap<String, Integer> createMap() {
|
||||
HashMap<String, Integer> set = new HashMap<>();
|
||||
set.put("A", 1);
|
||||
set.put("B", 2);
|
||||
set.put("C", 3);
|
||||
set.put("D", 4);
|
||||
set.put("E", 5);
|
||||
return new ModifyAwareMap<>(set);
|
||||
}
|
||||
|
||||
private ModifyAwareMap<String, Integer> createEmptyMap() {
|
||||
HashMap<String, Integer> set = new HashMap<>();
|
||||
return new ModifyAwareMap<>(set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
oos.writeObject(createMap());
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ModifyAwareMap<String, Integer> read = (ModifyAwareMap<String, Integer>) ois.readObject();
|
||||
assertThat(read).containsKeys("A", "B", "C", "D", "E").containsValues(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenEqual() {
|
||||
|
||||
ModifyAwareMap<String, Integer> setA = createMap();
|
||||
ModifyAwareMap<String, Integer> setB = createMap();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWhenNotEqual() {
|
||||
|
||||
ModifyAwareMap<String, Integer> setA = createMap();
|
||||
ModifyAwareMap<String, Integer> setB = createMap();
|
||||
setB.put("F", 6);
|
||||
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareMap<String, Integer> setA = createEmptyMap();
|
||||
HashMap<String, Integer> setB = new HashMap<>();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
|
||||
setA.put("foo", 42);
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
|
||||
setB.put("foo", 42);
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class ModifyAwareSetTest {
|
||||
|
||||
@@ -21,6 +22,11 @@ public class ModifyAwareSetTest {
|
||||
return new ModifyAwareSet<>(set);
|
||||
}
|
||||
|
||||
private ModifyAwareSet<String> createEmptySet() {
|
||||
HashSet<String> set = new HashSet<>();
|
||||
return new ModifyAwareSet<>(set);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serialise() throws IOException, ClassNotFoundException {
|
||||
|
||||
@@ -59,4 +65,21 @@ public class ModifyAwareSetTest {
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
ModifyAwareSet<String> setA = createEmptySet();
|
||||
HashSet<String> setB = new HashSet<>();
|
||||
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
|
||||
setA.add("foo");
|
||||
assertThat(setA).isNotEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isNotEqualTo(setB.hashCode());
|
||||
|
||||
setB.add("foo");
|
||||
assertThat(setA).isEqualTo(setB);
|
||||
assertThat(setA.hashCode()).isEqualTo(setB.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user