Skip to content

Commit

Permalink
Tests for HHH-15634 Lazy basic property does not get updated on change
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and Sanne committed Nov 4, 2022
1 parent 96d4511 commit 4591f78
Show file tree
Hide file tree
Showing 4 changed files with 584 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package io.quarkus.hibernate.orm.lazyloading;

import static io.quarkus.hibernate.orm.TransactionTestUtils.inTransaction;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;

import org.junit.jupiter.api.Test;

public abstract class AbstractLazyBasicTest {

@Inject
EntityManager em;

@Inject
UserTransaction transaction;

private final AccessDelegate delegate;
private Long entityId;

public AbstractLazyBasicTest(AccessDelegate delegate) {
this.delegate = delegate;
}

@Test
public void update_all_nullToNonNull() {
initNull();
inTransaction(transaction, () -> {
delegate.updateAllProperties(em, entityId, "updated1", "updated2", "updated3");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "updated1", "updated2", "updated3");
});
}

@Test
public void update_allLazy_nullToNonNull() {
initNull();
inTransaction(transaction, () -> {
delegate.updateAllLazyProperties(em, entityId, "updated1", "updated2");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, null, "updated1", "updated2");
});
}

@Test
public void update_oneEager_nullToNonNull() {
initNull();
inTransaction(transaction, () -> {
delegate.updateOneEagerProperty(em, entityId, "updated1");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "updated1", null, null);
});
}

@Test
public void update_oneLazy_nullToNonNull() {
initNull();
inTransaction(transaction, () -> {
delegate.updateOneLazyProperty(em, entityId, "updated2");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, null, "updated2", null);
});
}

@Test
public void update_all_nonNullToNonNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateAllProperties(em, entityId, "updated1", "updated2", "updated3");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "updated1", "updated2", "updated3");
});
}

@Test
public void update_allLazy_nonNullToNonNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateAllLazyProperties(em, entityId, "updated1", "updated2");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "updated1", "updated2");
});
}

@Test
public void update_oneEager_nonNullToNonNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateOneEagerProperty(em, entityId, "updated1");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "updated1", "initial2", "initial3");
});
}

@Test
public void update_oneLazy_nonNullToNonNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateOneLazyProperty(em, entityId, "updated2");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "updated2", "initial3");
});
}

@Test
public void update_all_nonNullToNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateAllProperties(em, entityId, null, null, null);
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, null, null, null);
});
}

@Test
public void update_allLazy_nonNullToNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateAllLazyProperties(em, entityId, null, null);
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", null, null);
});
}

@Test
public void update_oneEager_nonNullToNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateOneEagerProperty(em, entityId, null);
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, null, "initial2", "initial3");
});
}

@Test
public void update_oneLazy_nonNullToNull() {
initNonNull();
inTransaction(transaction, () -> {
delegate.updateOneLazyProperty(em, entityId, null);
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", null, "initial3");
});
}

private void initNull() {
inTransaction(transaction, () -> {
entityId = delegate.create(em, null, null, null);
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, null, null, null);
});
}

private void initNonNull() {
inTransaction(transaction, () -> {
entityId = delegate.create(em, "initial1", "initial2", "initial3");
});
inTransaction(transaction, () -> {
delegate.testLazyLoadingAndPersistedValues(em, entityId, "initial1", "initial2", "initial3");
});
}

/**
* An interface for delegate classes,
* classes whose bytecode is transformed by Quarkus to replace public field access with getter/setter access.
* <p>
* (Test bytecode was not transformed by Quarkus when using QuarkusUnitTest last time I checked).
*/
interface AccessDelegate {

long create(EntityManager entityManager, String eagerProperty1, String lazyProperty1, String lazyProperty2);

void updateAllProperties(EntityManager entityManager, long entityId, String eagerProperty1, String lazyProperty1,
String lazyProperty2);

void updateAllLazyProperties(EntityManager entityManager, long entityId, String lazyProperty1, String lazyProperty2);

void updateOneEagerProperty(EntityManager entityManager, long entityId, String eagerProperty1);

void updateOneLazyProperty(EntityManager entityManager, long entityId, String lazyProperty1);

void testLazyLoadingAndPersistedValues(EntityManager entityManager, long entityId,
String expectedEagerProperty1,
String expectedLazyProperty1,
String expectedLazyProperty2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package io.quarkus.hibernate.orm.lazyloading;

import static org.assertj.core.api.Assertions.assertThat;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.Hibernate;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.hibernate.orm.TransactionTestUtils;
import io.quarkus.test.QuarkusUnitTest;

public class LazyBasicDefaultGroupTest extends AbstractLazyBasicTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(TransactionTestUtils.class)
.addClass(MyEntity.class)
.addClass(AccessDelegate.class)
.addClass(AccessDelegateImpl.class))
.withConfigurationResource("application.properties");

public LazyBasicDefaultGroupTest() {
super(new AccessDelegateImpl());
}

@Entity(name = "MyEntity")
public static class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;

@Basic
public String eagerProperty1;

@Basic(fetch = FetchType.LAZY)
public String lazyProperty1;

@Basic(fetch = FetchType.LAZY)
public String lazyProperty2;
}

private static class AccessDelegateImpl implements AccessDelegate {

@Override
public long create(EntityManager entityManager, String eagerProperty1, String lazyProperty1, String lazyProperty2) {
MyEntity myEntity = new MyEntity();
myEntity.eagerProperty1 = eagerProperty1;
myEntity.lazyProperty1 = lazyProperty1;
myEntity.lazyProperty2 = lazyProperty2;
entityManager.persist(myEntity);
return myEntity.id;
}

@Override
public void updateAllProperties(EntityManager entityManager, long entityId, String eagerProperty1, String lazyProperty1,
String lazyProperty2) {
MyEntity entity = entityManager.find(MyEntity.class, entityId);
entity.eagerProperty1 = eagerProperty1;
entity.lazyProperty1 = lazyProperty1;
entity.lazyProperty2 = lazyProperty2;
}

@Override
public void updateAllLazyProperties(EntityManager entityManager, long entityId, String lazyProperty1,
String lazyProperty2) {
MyEntity entity = entityManager.find(MyEntity.class, entityId);
entity.lazyProperty1 = lazyProperty1;
entity.lazyProperty2 = lazyProperty2;
}

@Override
public void updateOneEagerProperty(EntityManager entityManager, long entityId, String eagerProperty1) {
MyEntity entity = entityManager.find(MyEntity.class, entityId);
entity.eagerProperty1 = eagerProperty1;
}

@Override
public void updateOneLazyProperty(EntityManager entityManager, long entityId, String lazyProperty1) {
MyEntity entity = entityManager.find(MyEntity.class, entityId);
entity.lazyProperty1 = lazyProperty1;
}

@Override
public void testLazyLoadingAndPersistedValues(EntityManager entityManager, long entityId,
String expectedEagerProperty1,
String expectedLazyProperty1,
String expectedLazyProperty2) {
MyEntity entity = entityManager.find(MyEntity.class, entityId);
assertThat(entity).isNotNull();
assertThat(Hibernate.isPropertyInitialized(entity, "eagerProperty1")).isTrue();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty1")).isFalse();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty2")).isFalse();

assertThat(entity.eagerProperty1).isEqualTo(expectedEagerProperty1);
assertThat(Hibernate.isPropertyInitialized(entity, "eagerProperty1")).isTrue();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty1")).isFalse();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty2")).isFalse();

assertThat(entity.lazyProperty1).isEqualTo(expectedLazyProperty1);
assertThat(Hibernate.isPropertyInitialized(entity, "eagerProperty1")).isTrue();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty1")).isTrue();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty2")).isTrue();

assertThat(entity.lazyProperty2).isEqualTo(expectedLazyProperty2);
assertThat(Hibernate.isPropertyInitialized(entity, "eagerProperty1")).isTrue();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty1")).isTrue();
assertThat(Hibernate.isPropertyInitialized(entity, "lazyProperty2")).isTrue();
}
}
}
Loading

0 comments on commit 4591f78

Please sign in to comment.