Skip to content

Commit

Permalink
Update tests only for BeanList BeanSet BeanMap
Browse files Browse the repository at this point in the history
Use Product entity with equals/hashCode implementation added via ebean enhancement
  • Loading branch information
rbygrave committed Feb 7, 2024
1 parent 7d36809 commit 66940c0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 64 deletions.
29 changes: 16 additions & 13 deletions ebean-test/src/test/java/io/ebean/xtest/common/BeanListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.ebean.bean.BeanCollection;
import io.ebean.common.BeanList;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Product;

import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -13,22 +14,22 @@

public class BeanListTest {

private final Object object1 = new Object();
private final Object object2 = new Object();
private final Object object3 = new Object();
private final Product object1 = new Product(1);
private final Product object2 = new Product(2);
private final Product object3 = new Product(3);

private List<Object> all() {
List<Object> all = new ArrayList<>();
all.add(object1);
all.add(object2);
all.add(object3);
all.add(new Product(1));
all.add(new Product(2));
all.add(new Product(3));
return all;
}

private List<Object> some() {
List<Object> some = new ArrayList<>();
some.add(object2);
some.add(object3);
some.add(new Product(2));
some.add(new Product(3));
return some;
}

Expand Down Expand Up @@ -163,14 +164,15 @@ public void testAddSome_given_someAlreadyIn() {
@Test
public void testRemove_given_beansInAdditions() {

List<Object> all = all();
BeanList<Object> list = new BeanList<>();
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
list.addAll(all());
list.addAll(all);
assertThat(list.modifyAdditions()).containsOnly(object1, object2, object3);

// act
list.remove(object2);
list.remove(object3);
list.remove(all.get(1));
list.remove(all.get(2));

assertThat(list.modifyAdditions()).containsOnly(object1);
assertThat(list.modifyRemovals()).isEmpty();
Expand All @@ -179,13 +181,14 @@ public void testRemove_given_beansInAdditions() {
@Test
public void testRemoveAll_given_beansInAdditions() {

List<Object> all = all();
BeanList<Object> list = new BeanList<>();
list.setModifyListening(BeanCollection.ModifyListenMode.ALL);
list.addAll(all());
list.addAll(all);
assertThat(list.modifyAdditions()).containsOnly(object1, object2, object3);

// act
list.removeAll(some());
list.removeAll(List.of(all.get(1), all.get(2)));

assertThat(list.modifyAdditions()).containsOnly(object1);
assertThat(list.modifyRemovals()).isEmpty();
Expand Down
78 changes: 40 additions & 38 deletions ebean-test/src/test/java/io/ebean/xtest/common/BeanMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.ebean.bean.BeanCollection;
import io.ebean.common.BeanMap;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.EBasic;
import org.tests.model.basic.Product;

import java.util.*;

Expand All @@ -14,24 +14,24 @@

public class BeanMapTest {

private final EBasic object1 = new EBasic("o1");
private final EBasic object2 = new EBasic("o2");
private final EBasic object3 = new EBasic("o3");
private final EBasic object4 = new EBasic("o4");
private final EBasic object5 = new EBasic("o5");
private final Product object1 = new Product(1);
private final Product object2 = new Product(2);
private final Product object3 = new Product(3);
private final Product object4 = new Product(4);
private final Product object5 = new Product(5);

private Map<String, Object> all() {
Map<String, Object> all = new LinkedHashMap<>();
all.put("1", object1);
all.put("2", object2);
all.put("3", object3);
all.put("1", new Product(1));
all.put("2", new Product(2));
all.put("3", new Product(3));
return all;
}

private Map<String, Object> some() {
Map<String, Object> all = new LinkedHashMap<>();
all.put("2", object2);
all.put("3", object3);
all.put("2", new Product(2));
all.put("3", new Product(3));
return all;
}

Expand Down Expand Up @@ -73,8 +73,8 @@ public void testAddAll_given_emptyStart() {

@Test
public void testAdd_given_someAlreadyIn() {

BeanMap<String, Object> map = new BeanMap<>(some());
Map<String, Object> some = new LinkedHashMap<>(Map.of("2", object2, "3", object3));
BeanMap<String, Object> map = new BeanMap<>(some);
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);

// act
Expand All @@ -90,11 +90,13 @@ public void testAdd_given_someAlreadyIn() {
@Test
public void testAddSome_given_someAlreadyIn() {

BeanMap<String, Object> map = new BeanMap<>(some());
Map<String, Object> some = new LinkedHashMap<>(Map.of("2", object2, "3", object3));
BeanMap<String, Object> map = new BeanMap<>(some);
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);

// act
map.putAll(all());
Map<String, Object> all = Map.of("1", object1, "2", object2, "3", object3);
map.putAll(all);

assertThat(map.modifyAdditions()).containsOnly(object1);
assertThat(map.modifyRemovals()).isEmpty();
Expand Down Expand Up @@ -179,7 +181,7 @@ public void testClear() {
@Test
public void testClear_given_someBeansInAdditions() {

BeanMap<String, EBasic> map = newModifyListeningMap();
BeanMap<String, Product> map = newModifyListeningMap();
map.put("2", object2);
map.put("3", object3);

Expand All @@ -193,7 +195,7 @@ public void testClear_given_someBeansInAdditions() {

@Test
public void keySet_add_whenModifyListening() {
BeanMap<String, EBasic> map = newModifyListeningMap();
BeanMap<String, Product> map = newModifyListeningMap();
assertThrows(UnsupportedOperationException.class, () -> map.keySet().add("3"));
}

Expand All @@ -205,7 +207,7 @@ public void keySet_add() {

@Test
public void keySet_addAll_whenModifyListening() {
BeanMap<String, EBasic> map = newModifyListeningMap();
BeanMap<String, Product> map = newModifyListeningMap();
assertThrows(UnsupportedOperationException.class, () -> map.keySet().addAll(asList("3", "4")));
}

Expand Down Expand Up @@ -317,13 +319,13 @@ public void keySet_retainAll() {

@Test
public void values_add() {
BeanMap<String, EBasic> map = new BeanMap<>();
BeanMap<String, Product> map = new BeanMap<>();
assertThrows(UnsupportedOperationException.class, () -> map.values().add(object3));
}

@Test
public void values_addAll() {
BeanMap<String, EBasic> map = new BeanMap<>();
BeanMap<String, Product> map = new BeanMap<>();
assertThrows(UnsupportedOperationException.class, () -> map.values().addAll(asList(object3, object5)));
}

Expand All @@ -337,8 +339,8 @@ public void entrySet_add() {

@Test
public void entrySet_clear() {
final BeanMap<String, EBasic> map = newModifyListeningMap();
final Set<Map.Entry<String, EBasic>> entries = map.entrySet();
final BeanMap<String, Product> map = newModifyListeningMap();
final Set<Map.Entry<String, Product>> entries = map.entrySet();
entries.clear();

assertThat(entries).isEmpty();
Expand All @@ -348,8 +350,8 @@ public void entrySet_clear() {

@Test
public void entrySet_remove() {
final BeanMap<String, EBasic> map = newModifyListeningMap5();
final Set<Map.Entry<String, EBasic>> entries = map.entrySet();
final BeanMap<String, Product> map = newModifyListeningMap5();
final Set<Map.Entry<String, Product>> entries = map.entrySet();

assertThat(map).hasSize(5);

Expand All @@ -365,8 +367,8 @@ public void entrySet_remove() {

@Test
public void entrySet_remove_whenNotEqualValue() {
final BeanMap<String, EBasic> map = newModifyListeningMap5();
final Set<Map.Entry<String, EBasic>> entries = map.entrySet();
final BeanMap<String, Product> map = newModifyListeningMap5();
final Set<Map.Entry<String, Product>> entries = map.entrySet();

assertThat(map).hasSize(5);

Expand All @@ -379,11 +381,11 @@ public void entrySet_remove_whenNotEqualValue() {

@Test
public void entrySet_iterator_remove() {
final BeanMap<String, EBasic> map = newModifyListeningMap5();
final Set<Map.Entry<String, EBasic>> entries = map.entrySet();
final Iterator<Map.Entry<String, EBasic>> iterator = entries.iterator();
final BeanMap<String, Product> map = newModifyListeningMap5();
final Set<Map.Entry<String, Product>> entries = map.entrySet();
final Iterator<Map.Entry<String, Product>> iterator = entries.iterator();
while (iterator.hasNext()) {
final Map.Entry<String, EBasic> entry = iterator.next();
final Map.Entry<String, Product> entry = iterator.next();
if (entry.getKey().equals("2") || entry.getKey().equals("5")) {
iterator.remove();
}
Expand All @@ -395,8 +397,8 @@ public void entrySet_iterator_remove() {

@Test
public void entrySet_removeAll() {
final BeanMap<String, EBasic> map = newModifyListeningMap5();
final Set<Map.Entry<String, EBasic>> entries = map.entrySet();
final BeanMap<String, Product> map = newModifyListeningMap5();
final Set<Map.Entry<String, Product>> entries = map.entrySet();

entries.removeAll(asList(new AbstractMap.SimpleEntry<>("1", object1), new AbstractMap.SimpleEntry<>("3", object4), new AbstractMap.SimpleEntry<>("4", object4)));
assertThat(map).hasSize(3);
Expand All @@ -406,24 +408,24 @@ public void entrySet_removeAll() {

@Test
public void entrySet_retainAll() {
final BeanMap<String, EBasic> map = newModifyListeningMap5();
final Set<Map.Entry<String, EBasic>> entries = map.entrySet();
final BeanMap<String, Product> map = newModifyListeningMap5();
final Set<Map.Entry<String, Product>> entries = map.entrySet();

entries.retainAll(asList(new AbstractMap.SimpleEntry<>("1", object1), new AbstractMap.SimpleEntry<>("3", object4), new AbstractMap.SimpleEntry<>("4", object4)));
assertThat(map).hasSize(2);
assertThat(entries).hasSize(2);
assertThat(map.modifyRemovals()).containsOnly(object2, object3, object5);
}

private BeanMap<String, EBasic> newModifyListeningMap() {
BeanMap<String, EBasic> map = new BeanMap<>();
private BeanMap<String, Product> newModifyListeningMap() {
BeanMap<String, Product> map = new BeanMap<>();
map.put("1", object1);
map.setModifyListening(BeanCollection.ModifyListenMode.ALL);
return map;
}

private BeanMap<String, EBasic> newModifyListeningMap5() {
BeanMap<String, EBasic> map = new BeanMap<>();
private BeanMap<String, Product> newModifyListeningMap5() {
BeanMap<String, Product> map = new BeanMap<>();
map.put("1", object1);
map.put("2", object2);
map.put("3", object3);
Expand Down
27 changes: 16 additions & 11 deletions ebean-test/src/test/java/io/ebean/xtest/common/BeanSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.ebean.bean.BeanCollection;
import io.ebean.common.BeanSet;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Product;

import java.util.LinkedHashSet;
import java.util.Set;
Expand All @@ -12,22 +13,22 @@

public class BeanSetTest {

Object object1 = new Object();
Object object2 = new Object();
Object object3 = new Object();
Product object1 = new Product(1);
Product object2 = new Product(2);
Product object3 = new Product(3);

private Set<Object> all() {
Set<Object> all = new LinkedHashSet<>();
all.add(object1);
all.add(object2);
all.add(object3);
all.add(new Product(1));
all.add(new Product(2));
all.add(new Product(3));
return all;
}

private Set<Object> some() {
Set<Object> some = new LinkedHashSet<>();
some.add(object2);
some.add(object3);
some.add(new Product(2));
some.add(new Product(3));
return some;
}

Expand Down Expand Up @@ -99,7 +100,9 @@ public void testRemove_given_beansInAdditions() {

BeanSet<Object> set = new BeanSet<>();
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
set.addAll(all());

Set<Object> all = Set.of(object1, object2, object3);
set.addAll(all);
assertThat(set.modifyAdditions()).containsOnly(object1, object2, object3);

// act
Expand All @@ -115,11 +118,13 @@ public void testRemoveAll_given_beansInAdditions() {

BeanSet<Object> set = new BeanSet<>();
set.setModifyListening(BeanCollection.ModifyListenMode.ALL);
set.addAll(all());

Set<Object> all = Set.of(object1, object2, object3);
set.addAll(all);
assertThat(set.modifyAdditions()).containsOnly(object1, object2, object3);

// act
set.removeAll(some());
set.removeAll(Set.of(object2, object3));

assertThat(set.modifyAdditions()).containsOnly(object1);
assertThat(set.modifyRemovals()).isEmpty();
Expand Down
7 changes: 7 additions & 0 deletions ebean-test/src/test/java/org/tests/model/basic/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public class Product implements Serializable {
@Version
Timestamp updtime;

public Product(Integer id) {
this.id = id;
}

public Product() {
}

/**
* Return id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public void testOverwritingMapping() {
// Refreshing here generates new objects for the associated children that are referred to by the parent.
parent.refresh();

assertNotNull(childList.get(0).getId());
assertEquals(childList.get(0).getId(), parent.getChildren().get(0).getId());
OmBeanListChild child = childList.get(0);
assertNotNull(child.getId());

OmBeanListChild refreshedChild = parent.getChildren().get(0);
assertEquals(child.getId(), refreshedChild.getId());
assertEquals(child, refreshedChild);
assertEquals(childList, parent.getChildren());
}
}

0 comments on commit 66940c0

Please sign in to comment.