-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for HHH-15634 Lazy basic property does not get updated on change
- Loading branch information
Showing
4 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
206 changes: 206 additions & 0 deletions
206
.../deployment/src/test/java/io/quarkus/hibernate/orm/lazyloading/AbstractLazyBasicTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...loyment/src/test/java/io/quarkus/hibernate/orm/lazyloading/LazyBasicDefaultGroupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.