From c0eb896e1fa0b7bd5889693a7c62cd51c22c6851 Mon Sep 17 00:00:00 2001 From: Noemi Praml <63635801+nPraml@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:10:50 +0100 Subject: [PATCH 1/2] No productive code change: cleanup basic test data (#114) * Erste Tests bereinigt * fix * noch mehr Tests bereinigt * modify output * reformat --- .../batchinsert/TestBatchInsertFlush.java | 3 ++ .../cache/TestBeanCacheContactLazyLoad.java | 38 +++++++++++-------- .../org/tests/model/basic/ResetBasicData.java | 15 +++++--- .../transaction/TestExecuteComplete.java | 6 ++- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java b/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java index 370aa41f9b..efe5a52db7 100644 --- a/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java +++ b/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java @@ -55,6 +55,9 @@ public void batchFlush() { DB.saveAll(customers); txn.commit(); + } finally { + DB.find(Customer.class).where().startsWith("name", "BatchFlush").delete(); + DB.find(Contact.class).where().startsWith("firstName", "Fred").startsWith("lastName", "Blue").delete(); } } diff --git a/ebean-test/src/test/java/org/tests/cache/TestBeanCacheContactLazyLoad.java b/ebean-test/src/test/java/org/tests/cache/TestBeanCacheContactLazyLoad.java index 9f4e077924..89a8c2bb0f 100644 --- a/ebean-test/src/test/java/org/tests/cache/TestBeanCacheContactLazyLoad.java +++ b/ebean-test/src/test/java/org/tests/cache/TestBeanCacheContactLazyLoad.java @@ -52,24 +52,30 @@ public void testBeanCacheWithLazyLoading() { DB.save(contact); - // Only get two properties, so we have to lazy-load later - final Contact contactDb = DB.find(Contact.class).where().eq("email", "tim@button.com").select("email,lastName").findOne(); - assertThat(contactDb).isNotNull(); - LoggedSql.start(); - contactDb.setLastName("Buttonnnn"); - List sql = LoggedSql.collect(); - assertThat(sql).isEmpty(); // setter did not trigger lazy load + try { - // trigger lazy load - assertThat(contactDb.getPhone()).isEqualTo("1234567890"); - sql = LoggedSql.collect(); - assertThat(sql).isNotEmpty(); // Lazy-load took place + // Only get two properties, so we have to lazy-load later + final Contact contactDb = DB.find(Contact.class).where().eq("email", "tim@button.com").select("email,lastName").findOne(); + assertThat(contactDb).isNotNull(); + LoggedSql.start(); + contactDb.setLastName("Buttonnnn"); + List sql = LoggedSql.collect(); + assertThat(sql).isEmpty(); // setter did not trigger lazy load - final Contact contactDb2 = DB.find(Contact.class).where().eq("email", "tim@button.com").select("email,lastName").findOne(); - sql = LoggedSql.stop(); - assertThat(sql).isEmpty(); // We expect that the bean was loaded from cache - assertThat(contactDb2).isNotNull(); - assertThat(contactDb2.getLastName()).isEqualTo("Button"); + // trigger lazy load + assertThat(contactDb.getPhone()).isEqualTo("1234567890"); + sql = LoggedSql.collect(); + assertThat(sql).isNotEmpty(); // Lazy-load took place + + final Contact contactDb2 = DB.find(Contact.class).where().eq("email", "tim@button.com").select("email,lastName").findOne(); + sql = LoggedSql.stop(); + assertThat(sql).isEmpty(); // We expect that the bean was loaded from cache + assertThat(contactDb2).isNotNull(); + assertThat(contactDb2.getLastName()).isEqualTo("Button"); + } finally { + DB.delete(customer); + DB.find(Contact.class).where().eq("email", "tim@button.com").delete(); + } } } diff --git a/ebean-test/src/test/java/org/tests/model/basic/ResetBasicData.java b/ebean-test/src/test/java/org/tests/model/basic/ResetBasicData.java index 063411b19c..46bbae04d9 100644 --- a/ebean-test/src/test/java/org/tests/model/basic/ResetBasicData.java +++ b/ebean-test/src/test/java/org/tests/model/basic/ResetBasicData.java @@ -26,7 +26,7 @@ public static synchronized void reset() { // OK, test data not modified outputCustomerIds(); } else { - outputState(); + outputState(true); } return; } @@ -45,11 +45,11 @@ public static synchronized void reset() { me.insertProducts(); me.insertTestCustAndOrders(); }); - outputState(); + outputState(false); runOnce = true; } - private static void outputState() { + private static void outputState(boolean warning) { StringBuilder sb = new StringBuilder(); sb.append("CustomerIds:"); server.find(Customer.class).findEach(c -> sb.append(' ').append(c.getId())); @@ -59,8 +59,13 @@ private static void outputState() { server.find(Country.class).findEach(c -> sb.append(' ').append(c.getCode())); sb.append(", Products:"); server.find(Product.class).findEach(c -> sb.append(' ').append(c.getId())); - System.err.println("WARNING: basic test data was modified. Current content:"); - System.err.println(sb); + if (warning) { + System.err.println("WARNING: basic test data was modified. Current content:"); + System.err.println(sb); + } else { + System.out.println("ResetBasicData. basic test data was initialized Current content:"); + System.out.println(sb); + } } private static void outputCustomerIds() { diff --git a/ebean-test/src/test/java/org/tests/transaction/TestExecuteComplete.java b/ebean-test/src/test/java/org/tests/transaction/TestExecuteComplete.java index 17841d1d88..db31d630fd 100644 --- a/ebean-test/src/test/java/org/tests/transaction/TestExecuteComplete.java +++ b/ebean-test/src/test/java/org/tests/transaction/TestExecuteComplete.java @@ -141,7 +141,11 @@ public void implicit_save_expect_threadScopeCleanup() { cust.setName("Roland"); DB.save(cust); - assertThat(getInScopeTransaction()).isNull(); + try { + assertThat(getInScopeTransaction()).isNull(); + } finally { + DB.delete(cust); + } } @ForPlatform(Platform.H2) From 59ed0bb015b132c1005129f46ea602d333ca3bb4 Mon Sep 17 00:00:00 2001 From: Noemi Praml Date: Tue, 5 Nov 2024 14:02:43 +0100 Subject: [PATCH 2/2] fix delete contacts --- .../java/org/tests/batchinsert/TestBatchInsertFlush.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java b/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java index efe5a52db7..174b01767d 100644 --- a/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java +++ b/ebean-test/src/test/java/org/tests/batchinsert/TestBatchInsertFlush.java @@ -42,13 +42,13 @@ public void batchFlush() { for (int i = 0; i < 3; i++) { Customer customer = ResetBasicData.createCustomer("BatchFlushPreInsert " + i, null, null, 3); - customer.addContact(new Contact("Fred" + i, "Blue")); + customer.addContact(new Contact("BatchFlush" + i, "Blue")); customers.add(customer); } for (int i = 3; i < 6; i++) { Customer customer = ResetBasicData.createCustomer("BatchFlushPostInsert " + i, null, null, 3); - customer.addContact(new Contact("Fred" + i, "Blue")); + customer.addContact(new Contact("BatchFlush" + i, "Blue")); customers.add(customer); } @@ -57,7 +57,7 @@ public void batchFlush() { txn.commit(); } finally { DB.find(Customer.class).where().startsWith("name", "BatchFlush").delete(); - DB.find(Contact.class).where().startsWith("firstName", "Fred").startsWith("lastName", "Blue").delete(); + DB.find(Contact.class).where().startsWith("firstName", "BatchFlush").startsWith("lastName", "Blue").delete(); } }