From f63e7ed5af167ede8d2cf37da5128e4bec24315c Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 8 Feb 2024 00:14:22 +1300 Subject: [PATCH] Add test only for orphan removal to TestOrphanRemovalOverwrite Using clear() and add() when the same id value is used --- .../model/orphanremoval/OmBeanListChild.java | 5 +++ .../TestOrphanRemovalOverwrite.java | 40 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ebean-test/src/test/java/org/tests/model/orphanremoval/OmBeanListChild.java b/ebean-test/src/test/java/org/tests/model/orphanremoval/OmBeanListChild.java index c4b07d3cdd..7ed80c3020 100644 --- a/ebean-test/src/test/java/org/tests/model/orphanremoval/OmBeanListChild.java +++ b/ebean-test/src/test/java/org/tests/model/orphanremoval/OmBeanListChild.java @@ -28,6 +28,11 @@ public OmBeanListChild(String name) { public Long getId() { return id; } + + public OmBeanListChild setId(Long id) { + this.id = id; + return this; + } } diff --git a/ebean-test/src/test/java/org/tests/model/orphanremoval/TestOrphanRemovalOverwrite.java b/ebean-test/src/test/java/org/tests/model/orphanremoval/TestOrphanRemovalOverwrite.java index c99f13483b..d03575e8c8 100644 --- a/ebean-test/src/test/java/org/tests/model/orphanremoval/TestOrphanRemovalOverwrite.java +++ b/ebean-test/src/test/java/org/tests/model/orphanremoval/TestOrphanRemovalOverwrite.java @@ -1,17 +1,20 @@ package org.tests.model.orphanremoval; +import io.ebean.DB; +import io.ebean.test.LoggedSql; import org.junit.jupiter.api.Test; import java.util.List; import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -public class TestOrphanRemovalOverwrite { +class TestOrphanRemovalOverwrite { @Test - public void testOverwritingMapping() { + void testOverwritingMapping() { OmBeanListParent parent = new OmBeanListParent(); parent.save(); @@ -37,4 +40,37 @@ public void testOverwritingMapping() { assertEquals(child, refreshedChild); assertEquals(childList, parent.getChildren()); } + + @Test + void clearAddAll() { + OmBeanListChild c1 = new OmBeanListChild("c1"); + OmBeanListChild c2 = new OmBeanListChild("c2"); + + OmBeanListParent parent = new OmBeanListParent(); + parent.getChildren().add(c1); + parent.getChildren().add(c2); + parent.save(); + + + OmBeanListChild c3 = new OmBeanListChild("c3"); + c3.setId(c1.getId()); + + OmBeanListParent p1 = DB.find(OmBeanListParent.class, parent.getId()); + List children = p1.getChildren(); + children.clear(); + children.add(c3); + + LoggedSql.start(); + DB.save(p1); + + List sql = LoggedSql.stop(); + assertThat(sql).hasSize(7); + assertThat(sql.get(0)).contains("delete from om_bean_list_child where id=?"); + assertThat(sql.get(1)).contains(" -- bind"); + assertThat(sql.get(2)).contains(" -- bind"); + assertThat(sql.get(3)).contains(" -- executeBatch()"); + assertThat(sql.get(4)).contains(" insert into om_bean_list_child"); + assertThat(sql.get(5)).contains(" -- bind"); + assertThat(sql.get(6)).contains(" -- executeBatch()"); + } }