Fix some issues with tests migration

This commit is contained in:
Andrea Boriero 2021-01-25 15:07:53 +01:00 committed by Jan Schatteman
parent 491cbabc6c
commit e0e44433a9
12 changed files with 174 additions and 431 deletions

View File

@ -0,0 +1,58 @@
/*
* 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 org.hibernate.jpa.test.cacheable.api;
import javax.persistence.EntityManagerFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Steve Ebersole
*/
// TODO Convert to annotation based testing? Setting the CachingRegionFactory as below leads to a CNFE
@Jpa(
annotatedClasses = Order.class,
integrationSettings = {
@Setting(name = AvailableSettings.CACHE_REGION_FACTORY, value = "org.hibernate.testing.cache.CachingRegionFactory"),
@Setting(name = AvailableSettings.JPA_SHARED_CACHE_MODE, value = "ALL"),
@Setting(name = AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, value = "read-write"),
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"),
}
)
public class JpaCacheApiUsageTest {
@Test
public void testEviction(EntityManagerFactoryScope scope) {
// first create an Order
scope.inTransaction(
entityManager ->
entityManager.persist( new Order( 1, 500 ) )
);
final EntityManagerFactory entityManagerFactory = scope.getEntityManagerFactory();
assertTrue( entityManagerFactory.getCache().contains( Order.class, 1 ) );
scope.inTransaction(
entityManager -> {
assertTrue( entityManagerFactory.getCache().contains( Order.class, 1 ) );
entityManager.createQuery( "delete Order" ).executeUpdate();
}
);
assertFalse( entityManagerFactory.getCache().contains( Order.class, 1 ) );
}
}

View File

@ -4,7 +4,7 @@
* 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 org.hibernate.orm.test.jpa.cacheable.api;
package org.hibernate.jpa.test.cacheable.api;
import javax.persistence.Cacheable;
import javax.persistence.Entity;

View File

@ -1,110 +0,0 @@
/*
* 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 org.hibernate.orm.test.jpa.cacheable.api;
import javax.persistence.EntityManager;
import javax.persistence.SharedCacheMode;
import java.util.Map;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.cache.CachingRegionFactory;
import org.hibernate.testing.orm.junit.NotImplementedYet;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Steve Ebersole
*/
// TODO Convert to annotation based testing? Setting the CachingRegionFactory as below leads to a CNFE
//@DomainModel(
// annotatedClasses = Order.class
//)
//@ServiceRegistry(
// settings = {
// @Setting(name = AvailableSettings.CACHE_REGION_FACTORY, value = "CachingRegionFactory.class"),
// @Setting(name = AvailableSettings.JPA_SHARED_CACHE_MODE, value = "ALL")
// }
//)
//@SessionFactory
public class JpaCacheApiUsageTest /*extends BaseEntityManagerFunctionalTestCase*/ {
// @Override
// protected Class<?>[] getAnnotatedClasses() {
// return new Class[] { Order.class };
// }
//
// @Override
// @SuppressWarnings("unchecked")
// protected void addConfigOptions(Map options) {
//// options.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
// options.put( AvailableSettings.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() );
//// options.put( AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "read-write" );
// options.put( org.hibernate.jpa.AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.ALL );
// }
@Test
@NotImplementedYet(reason = "BulkOperationCleanupAction is never created", expectedVersion = "6.0")
public void testEviction() {
throw new NotYetImplementedFor6Exception();
// first create an Order
// EntityManager em = getOrCreateEntityManager();
// em.getTransaction().begin();
// em.persist( new Order( 1, 500 ) );
// em.getTransaction().commit();
// em.close();
//
// assertTrue( entityManagerFactory().getCache().contains( Order.class, 1 ) );
//
// em = getOrCreateEntityManager();
// em.getTransaction().begin();
// assertTrue( entityManagerFactory().getCache().contains( Order.class, 1 ) );
// em.createQuery( "delete Order" ).executeUpdate();
// em.getTransaction().commit();
// em.close();
//
// assertFalse( entityManagerFactory().getCache().contains( Order.class, 1 ) );
}
// @Test
// public void testEviction(SessionFactoryScope scope) {
// scope.inTransaction(
// session -> {
// session.getTransaction().begin();
// session.persist( new Order( 1, 500 ) );
// session.getTransaction().commit();
// session.close();
// }
// );
//
// scope.inSession(
// session -> {
// assertTrue( session.getEntityManagerFactory().getCache().contains( Order.class, 1 ) );
// }
// );
//
// scope.inTransaction(
// session -> {
// session.getTransaction().begin();
// assertTrue( session.getEntityManagerFactory().getCache().contains( Order.class, 1 ) );
// session.createQuery( "delete Order" ).executeUpdate();
// session.getTransaction().commit();
// session.close();
// }
// );
//
// scope.inSession(
// session -> {
// assertFalse( session.getEntityManagerFactory().getCache().contains( Order.class, 1 ) );
// }
// );
// }
}

View File

@ -15,7 +15,6 @@ import org.hibernate.jpa.test.Kitten;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -39,28 +38,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class CallbacksDisabledTest {
@Test
public void testCallbacksAreDisabled(EntityManagerFactoryScope scope) throws Exception {
int id = scope.fromTransaction(
public void testCallbacksAreDisabled(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Cat c = new Cat();
c.setName( "Kitty" );
c.setDateOfBirth( new Date( 90, 11, 15 ) );
entityManager.persist( c );
return c.getId();
}
);
scope.inTransaction(
entityManager -> {
Cat _c = entityManager.find( Cat.class, id );
entityManager.getTransaction().commit();
entityManager.clear();
entityManager.getTransaction().begin();
Cat _c = entityManager.find( Cat.class, c.getId() );
assertTrue( _c.getAge() == 0 ); // With listeners enabled this would be false. Proven by org.hibernate.orm.test.jpa.callbacks.CallbacksTest.testCallbackMethod
}
);
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Cat" ).executeUpdate();
}
);
}
}

View File

@ -16,7 +16,7 @@ import org.hibernate.jpa.test.Kitten;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -38,34 +38,40 @@ import static org.junit.jupiter.api.Assertions.fail;
Rythm.class
})
public class CallbacksTest {
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Cat" ).executeUpdate();
entityManager.createQuery( "delete from Television" ).executeUpdate();
entityManager.createQuery( "delete from Plant" ).executeUpdate();
entityManager.createQuery( "delete from Kitten" ).executeUpdate();
}
);
}
@Test
public void testCallbackMethod(EntityManagerFactoryScope scope) {
int id = scope.fromTransaction(
scope.inTransaction(
entityManager -> {
Cat c = new Cat();
c.setName( "Kitty" );
c.setDateOfBirth( new Date( 90, 11, 15 ) );
entityManager.persist( c );
return c.getId();
}
);
scope.inTransaction(
entityManager -> {
Cat _c = entityManager.find( Cat.class, id );
assertFalse( _c.getAge() == 0 );
_c.setName( "Tomcat" ); //update this entity
}
);
scope.inTransaction(
entityManager -> {
Cat _c = entityManager.find( Cat.class, id );
assertEquals( "Tomcat", _c.getName() );
}
);
entityManager.getTransaction().commit();
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Cat" ).executeUpdate();
entityManager.clear();
entityManager.getTransaction().begin();
c = entityManager.find( Cat.class, c.getId() );
assertFalse( c.getAge() == 0 );
c.setName( "Tomcat" ); //update this entity
entityManager.getTransaction().commit();
entityManager.clear();
entityManager.getTransaction().begin();
c = entityManager.find( Cat.class, c.getId() );
assertEquals( "Tomcat", c.getName() );
}
);
}
@ -76,44 +82,38 @@ public class CallbacksTest {
PV(int version) {
this.version = version;
}
private int version;
private void set(int version) {
this.version = version;
}
private int get() {
return version;
}
}
Cat c = new Cat();
c.setName( "Kitty" );
c.setLength( 12 );
c.setDateOfBirth( new Date( 90, 11, 15 ) );
PV previousVersion = new PV( c.getManualVersion() );
scope.inTransaction(
entityManager -> entityManager.persist( c )
);
scope.inTransaction(
entityManager -> {
Cat _c = entityManager.find( Cat.class, c.getId() );
assertNotNull( _c.getLastUpdate() );
assertTrue( previousVersion.get() < _c.getManualVersion() );
assertEquals( 12, _c.getLength() );
previousVersion.set( _c.getManualVersion() );
_c.setName( "new name" );
}
);
scope.inTransaction(
entityManager -> {
Cat _c = entityManager.find( Cat.class, c.getId() );
assertTrue( previousVersion.get() < _c.getManualVersion() );
}
);
Cat c = new Cat();
c.setName( "Kitty" );
c.setLength( 12 );
c.setDateOfBirth( new Date( 90, 11, 15 ) );
PV previousVersion = new PV( c.getManualVersion() );
entityManager.persist( c );
entityManager.getTransaction().commit();
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Cat" ).executeUpdate();
entityManager.getTransaction().begin();
c = entityManager.find( Cat.class, c.getId() );
assertNotNull( c.getLastUpdate() );
assertTrue( previousVersion.get() < c.getManualVersion() );
assertEquals( 12, c.getLength() );
previousVersion.set( c.getManualVersion() );
c.setName( "new name" );
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
c = entityManager.find( Cat.class, c.getId() );
assertTrue( previousVersion.get() < c.getManualVersion() );
}
);
}
@ -135,12 +135,6 @@ public class CallbacksTest {
List ids = Cat.getIdList();
Object id = Cat.getIdList().get( ids.size() - 1 );
assertNotNull( id );
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Cat" ).executeUpdate();
}
);
}
//Not a test since the spec did not make the proper change on listeners
@ -173,20 +167,14 @@ public class CallbacksTest {
public void testPrePersistOnCascade(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Television tv = new Television();
RemoteControl rc = new RemoteControl();
entityManager.persist( tv );
entityManager.flush();
tv.setControl( rc );
tv.init();
entityManager.flush();
assertNotNull( rc.getCreationDate() );
}
);
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Television" ).executeUpdate();
Television tv = new Television();
RemoteControl rc = new RemoteControl();
entityManager.persist( tv );
entityManager.flush();
tv.setControl( rc );
tv.init();
entityManager.flush();
assertNotNull( rc.getCreationDate() );
}
);
}
@ -205,12 +193,6 @@ public class CallbacksTest {
assertTrue( tv.isLast );
}
);
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Television" ).executeUpdate();
}
);
}
@Test
@ -229,7 +211,8 @@ public class CallbacksTest {
}
catch (Exception e) {
fail( "should have raised an ArythmeticException:" + e.getClass() );
} finally {
}
finally {
entityManager.getTransaction().rollback();
entityManager.close();
}
@ -247,81 +230,62 @@ public class CallbacksTest {
entityManager.flush();
}
);
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Plant" ).executeUpdate();
}
);
}
@Test
@FailureExpected(reason = "collection change does not trigger an event", jiraKey = "EJB-288")
public void testPostUpdateCollection(EntityManagerFactoryScope scope) {
// create a cat
Cat cat = new Cat();
cat.setLength( 23 );
cat.setAge( 2 );
cat.setName( "Beetle" );
cat.setDateOfBirth( new Date() );
scope.inTransaction(
scope.inEntityManager(
entityManager -> {
entityManager.persist( cat );
}
);
try {
// create a cat
Cat cat = new Cat();
cat.setLength( 23 );
cat.setAge( 2 );
cat.setName( "Beetle" );
cat.setDateOfBirth( new Date() );
entityManager.getTransaction().begin();
entityManager.persist( cat );
entityManager.getTransaction().commit();
// assert it is persisted
List ids = Cat.getIdList();
Object id = Cat.getIdList().get( ids.size() - 1 );
assertNotNull( id );
// assert it is persisted
List ids = Cat.getIdList();
Object id = Cat.getIdList().get( ids.size() - 1 );
assertNotNull( id );
// add a kitten to the cat - triggers PostCollectionRecreateEvent
int postVersion = Cat.postVersion;
entityManager.getTransaction().begin();
Kitten kitty = new Kitten();
kitty.setName( "kitty" );
List kittens = new ArrayList<Kitten>();
kittens.add( kitty );
cat.setKittens( kittens );
entityManager.getTransaction().commit();
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
// add a kitten to the cat - triggers PostCollectionRecreateEvent
scope.inTransaction(
entityManager -> {
int postVersion = Cat.postVersion;
Kitten kitty = new Kitten();
kitty.setName( "kitty" );
List kittens = new ArrayList<Kitten>();
kittens.add( kitty );
cat.setKittens( kittens );
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
}
);
Kitten tom = new Kitten();
tom.setName( "Tom" );
Kitten tom = new Kitten();
tom.setName( "Tom" );
scope.inTransaction(
entityManager -> {
// add another kitten - triggers PostCollectionUpdateEvent.
int postVersion = Cat.postVersion;
cat.getKittens().add( tom );
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
}
);
// add another kitten - triggers PostCollectionUpdateEvent.
postVersion = Cat.postVersion;
entityManager.getTransaction().begin();
cat.getKittens().add( tom );
entityManager.getTransaction().commit();
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
scope.inTransaction(
entityManager -> {
// delete a kitty - triggers PostCollectionUpdateEvent
int postVersion = Cat.postVersion;
cat.getKittens().remove( tom );
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
}
);
scope.inTransaction(
entityManager -> {
// delete and recreate kittens - triggers PostCollectionRemoveEvent and PostCollectionRecreateEvent)
int postVersion = Cat.postVersion;
cat.setKittens( new ArrayList<Kitten>() );
assertEquals( postVersion + 2, Cat.postVersion, "Post version should have been incremented." );
}
);
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Cat" ).executeUpdate();
entityManager.createQuery( "delete from Kitten" ).executeUpdate();
// delete a kitty - triggers PostCollectionUpdateEvent
postVersion = Cat.postVersion;
entityManager.getTransaction().begin();
cat.getKittens().remove( tom );
entityManager.getTransaction().commit();
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
}
);
}

View File

@ -35,11 +35,9 @@ public class CascadeTest {
student.setPrimaryTeacher( teacher );
entityManager.persist( teacher );
}
);
entityManager.getTransaction().commit();
scope.inTransaction(
entityManager -> {
entityManager.getTransaction().begin();
Teacher foundTeacher = (Teacher) entityManager.createQuery( "select t from Teacher as t" )
.getSingleResult();

View File

@ -11,8 +11,9 @@ import java.lang.reflect.Method;
import javax.persistence.EntityManagerFactory;
import org.hibernate.testing.junit4.ClassLoadingIsolater;
import org.hibernate.testing.orm.junit.BaseUnitTest;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.fail;
@ -21,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*
* @author Steve Ebersole
*/
@BaseUnitTest
public class NoCdiAvailableTest {
public static final String[] EXCLUDED_PACKAGES = new String[] {
"javax.enterprise.inject.",

View File

@ -1,38 +0,0 @@
/*
* 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 org.hibernate.orm.test.jpa.cdi.extended;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
/**
* @author Vlad Mihalcea
*/
@Stateful
@LocalBean
public class ConversationalEventManager {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public Event saveEvent(String eventName) {
final Event event = new Event();
event.setName( eventName );
em.persist( event );
return event;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void endConversation() {
}
}

View File

@ -1,41 +0,0 @@
/*
* 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 org.hibernate.orm.test.jpa.cdi.extended;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author Vlad Mihalcea
*/
@Entity(name = "Event")
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -1,44 +0,0 @@
/*
* 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 org.hibernate.orm.test.jpa.cdi.extended;
import javax.annotation.PostConstruct;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.hibernate.FlushMode;
import org.hibernate.Session;
/**
* @author Vlad Mihalcea
*/
@Stateful
@LocalBean
public class ManualFlushConversationalEventManager {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
@PostConstruct
public void init() {
em.unwrap( Session.class ).setHibernateFlushMode( FlushMode.MANUAL );
}
public Event saveEvent(String eventName) {
final Event event = new Event();
event.setName( eventName );
em.persist( event );
return event;
}
public void endConversation() {
em.flush();
}
}

View File

@ -1,35 +0,0 @@
/*
* 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 org.hibernate.orm.test.jpa.cdi.extended;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
/**
* @author Vlad Mihalcea
*/
@Stateful
@LocalBean
public class NonConversationalEventManager {
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
public Event saveEvent(String eventName) {
final Event event = new Event();
event.setName( eventName );
em.persist( event );
return event;
}
public void endConversation() {
em.flush();
}
}

View File

@ -13,7 +13,6 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ParameterContext;
@Inherited
@Target( ElementType.TYPE )