diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/AbstractLazyBasicTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/AbstractLazyBasicTest.java index d19a4dd62178a..5ddea7aa3a26e 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/AbstractLazyBasicTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/AbstractLazyBasicTest.java @@ -1,11 +1,16 @@ package io.quarkus.hibernate.orm.lazyloading; import static io.quarkus.hibernate.orm.TransactionTestUtils.inTransaction; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.transaction.UserTransaction; +import org.hibernate.resource.jdbc.spi.StatementInspector; import org.junit.jupiter.api.Test; public abstract class AbstractLazyBasicTest { @@ -23,6 +28,50 @@ public AbstractLazyBasicTest(AccessDelegate delegate) { this.delegate = delegate; } + @Test + public void update_all_nullToNull() { + initNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateAllProperties(em, entityId, null, null, null); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, null, null, null); + }); + } + + @Test + public void update_allLazy_nullToNull() { + initNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateAllLazyProperties(em, entityId, null, null); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, null, null, null); + }); + } + + @Test + public void update_oneEager_nullToNull() { + initNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateOneEagerProperty(em, entityId, null); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, null, null, null); + }); + } + + @Test + public void update_oneLazy_nullToNull() { + initNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateOneLazyProperty(em, entityId, null); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, null, null, null); + }); + } + @Test public void update_all_nullToNonNull() { initNull(); @@ -68,7 +117,7 @@ public void update_oneLazy_nullToNonNull() { } @Test - public void update_all_nonNullToNonNull() { + public void update_all_nonNullToNonNull_differentValue() { initNonNull(); inTransaction(transaction, () -> { delegate.updateAllProperties(em, entityId, "updated1", "updated2", "updated3"); @@ -79,7 +128,18 @@ public void update_all_nonNullToNonNull() { } @Test - public void update_allLazy_nonNullToNonNull() { + public void update_all_nonNullToNonNull_sameValue() { + initNonNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateAllProperties(em, entityId, "initial1", "initial2", "initial3"); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "initial2", "initial3"); + }); + } + + @Test + public void update_allLazy_nonNullToNonNull_differentValue() { initNonNull(); inTransaction(transaction, () -> { delegate.updateAllLazyProperties(em, entityId, "updated1", "updated2"); @@ -90,7 +150,18 @@ public void update_allLazy_nonNullToNonNull() { } @Test - public void update_oneEager_nonNullToNonNull() { + public void update_allLazy_nonNullToNonNull_sameValue() { + initNonNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateAllLazyProperties(em, entityId, "initial2", "initial3"); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "initial2", "initial3"); + }); + } + + @Test + public void update_oneEager_nonNullToNonNull_differentValue() { initNonNull(); inTransaction(transaction, () -> { delegate.updateOneEagerProperty(em, entityId, "updated1"); @@ -101,7 +172,18 @@ public void update_oneEager_nonNullToNonNull() { } @Test - public void update_oneLazy_nonNullToNonNull() { + public void update_oneEager_nonNullToNonNull_sameValue() { + initNonNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateOneEagerProperty(em, entityId, "initial1"); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "initial2", "initial3"); + }); + } + + @Test + public void update_oneLazy_nonNullToNonNull_differentValue() { initNonNull(); inTransaction(transaction, () -> { delegate.updateOneLazyProperty(em, entityId, "updated2"); @@ -111,6 +193,17 @@ public void update_oneLazy_nonNullToNonNull() { }); } + @Test + public void update_oneLazy_nonNullToNonNull_sameValue() { + initNonNull(); + StatementSpy.checkNoUpdate(() -> inTransaction(transaction, () -> { + delegate.updateOneLazyProperty(em, entityId, "initial2"); + })); + inTransaction(transaction, () -> { + delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "initial2", "initial3"); + }); + } + @Test public void update_all_nonNullToNull() { initNonNull(); @@ -197,4 +290,30 @@ void testLazyLoadingAndPersistedValues(EntityManager entityManager, long entityI String expectedLazyProperty1, String expectedLazyProperty2); } + + public static class StatementSpy implements StatementInspector { + private static final ThreadLocal> statements = new ThreadLocal<>(); + + public static void checkNoUpdate(Runnable runnable) { + List list = new ArrayList<>(); + if (statements.get() != null) { + throw new IllegalStateException("Cannot nest checkNoUpdate()"); + } + statements.set(list); + runnable.run(); + statements.remove(); + assertThat(list) + .isNotEmpty() // Something is wrong if we didn't even load an entity + .allSatisfy(sql -> assertThat(sql).doesNotContainIgnoringCase("update")); + } + + @Override + public String inspect(String sql) { + List list = statements.get(); + if (list != null) { + list.add(sql); + } + return sql; + } + } } diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicDefaultGroupTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicDefaultGroupTest.java index 9de6fb25239b1..d75c2aa211169 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicDefaultGroupTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicDefaultGroupTest.java @@ -25,7 +25,9 @@ public class LazyBasicDefaultGroupTest extends AbstractLazyBasicTest { .addClass(MyEntity.class) .addClass(AccessDelegate.class) .addClass(AccessDelegateImpl.class)) - .withConfigurationResource("application.properties"); + .withConfigurationResource("application.properties") + .overrideConfigKey("quarkus.hibernate-orm.unsupported-properties.\"hibernate.session_factory.statement_inspector\"", + StatementSpy.class.getName()); public LazyBasicDefaultGroupTest() { super(new AccessDelegateImpl()); diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicMultiNonDefaultGroupTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicMultiNonDefaultGroupTest.java index b55f00ab4e8f1..6f32d4c6a2b91 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicMultiNonDefaultGroupTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicMultiNonDefaultGroupTest.java @@ -26,7 +26,9 @@ public class LazyBasicMultiNonDefaultGroupTest extends AbstractLazyBasicTest { .addClass(MyEntity.class) .addClass(AccessDelegate.class) .addClass(AccessDelegateImpl.class)) - .withConfigurationResource("application.properties"); + .withConfigurationResource("application.properties") + .overrideConfigKey("quarkus.hibernate-orm.unsupported-properties.\"hibernate.session_factory.statement_inspector\"", + StatementSpy.class.getName()); public LazyBasicMultiNonDefaultGroupTest() { super(new AccessDelegateImpl()); diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicNonDefaultGroupTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicNonDefaultGroupTest.java index 066369407e320..0166fc381d4b4 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicNonDefaultGroupTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicNonDefaultGroupTest.java @@ -26,7 +26,9 @@ public class LazyBasicNonDefaultGroupTest extends AbstractLazyBasicTest { .addClass(MyEntity.class) .addClass(AccessDelegate.class) .addClass(AccessDelegateImpl.class)) - .withConfigurationResource("application.properties"); + .withConfigurationResource("application.properties") + .overrideConfigKey("quarkus.hibernate-orm.unsupported-properties.\"hibernate.session_factory.statement_inspector\"", + StatementSpy.class.getName()); public LazyBasicNonDefaultGroupTest() { super(new AccessDelegateImpl());