HHH-18025 tests for RefreshOptions and LockOptions

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-28 19:57:57 +02:00 committed by Steve Ebersole
parent 63adaaaff7
commit 3e423ce900
2 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,88 @@
package org.hibernate.orm.test.jpa.findoptions;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Timeout;
import org.hibernate.HibernateException;
import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.FetchProfileOverride;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.hibernate.Hibernate.isInitialized;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
@Jpa(annotatedClasses = LockOptionsTest.MyEntity.class)
public class LockOptionsTest {
@Test
void test(EntityManagerFactoryScope scope) {
MyEntity hello = new MyEntity("Hello");
scope.getEntityManagerFactory()
.runInTransaction(em -> em.persist(hello));
scope.getEntityManagerFactory()
.runInTransaction(em -> {
MyEntity entity = em.find(MyEntity.class, hello.id);
em.lock(entity, LockModeType.PESSIMISTIC_WRITE, Timeout.seconds(10));
assertEquals(LockModeType.PESSIMISTIC_WRITE, em.getLockMode(entity));
assertEquals(hello.name, entity.name);
assertFalse(isInitialized(entity.list));
});
scope.getEntityManagerFactory()
.runInTransaction(em -> {
try {
em.lock(em.find(MyEntity.class, hello.id), LockModeType.OPTIMISTIC);
}
catch (HibernateException he) {
return;
}
fail();
});
scope.getEntityManagerFactory()
.runInTransaction(em -> {
try {
em.lock(em.find(MyEntity.class, hello.id), LockModeType.OPTIMISTIC_FORCE_INCREMENT);
}
catch (HibernateException he) {
return;
}
fail();
});
scope.getEntityManagerFactory()
.runInTransaction(em -> {
try {
em.lock(em.find(MyEntity.class, hello.id), LockModeType.PESSIMISTIC_FORCE_INCREMENT);
}
catch (HibernateException he) {
return;
}
fail();
});
}
@Entity
@FetchProfile(name = "hello world")
public static class MyEntity {
@Id @GeneratedValue long id;
String name;
@ElementCollection(fetch = FetchType.LAZY)
@FetchProfileOverride(profile = "hello world",
fetch = FetchType.EAGER)
List<String> list;
public MyEntity(String name) {
this.name = name;
}
MyEntity() {
}
}
}

View File

@ -0,0 +1,91 @@
package org.hibernate.orm.test.jpa.findoptions;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Timeout;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.FetchProfileOverride;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.hibernate.Hibernate.isInitialized;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
@Jpa(annotatedClasses = RefreshOptionsTest.MyEntity.class)
public class RefreshOptionsTest {
@Test
void test(EntityManagerFactoryScope scope) {
MyEntity hello = new MyEntity("Hello");
scope.getEntityManagerFactory()
.runInTransaction(em -> em.persist(hello));
scope.getEntityManagerFactory()
.runInTransaction(em -> {
MyEntity entity = em.find(MyEntity.class, hello.id);
em.refresh(entity, LockModeType.PESSIMISTIC_WRITE, Timeout.seconds(10),
CacheStoreMode.BYPASS);
assertEquals(LockModeType.PESSIMISTIC_WRITE, em.getLockMode(entity));
assertEquals(hello.name, entity.name);
assertFalse(isInitialized(entity.list));
});
scope.getEntityManagerFactory()
.runInTransaction(em -> {
try {
em.refresh(em.find(MyEntity.class, hello.id), LockMode.OPTIMISTIC);
}
catch (HibernateException he) {
return;
}
fail();
});
scope.getEntityManagerFactory()
.runInTransaction(em -> {
try {
em.refresh(em.find(MyEntity.class, hello.id), LockMode.OPTIMISTIC_FORCE_INCREMENT);
}
catch (HibernateException he) {
return;
}
fail();
});
scope.getEntityManagerFactory()
.runInTransaction(em -> {
try {
em.refresh(em.find(MyEntity.class, hello.id), LockMode.PESSIMISTIC_FORCE_INCREMENT);
}
catch (HibernateException he) {
return;
}
fail();
});
}
@Entity
@FetchProfile(name = "hello world")
public static class MyEntity {
@Id @GeneratedValue long id;
String name;
@ElementCollection(fetch = FetchType.LAZY)
@FetchProfileOverride(profile = "hello world",
fetch = FetchType.EAGER)
List<String> list;
public MyEntity(String name) {
this.name = name;
}
MyEntity() {
}
}
}