From b1951f5c26ac70f560503f3064786110b49651da Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 5 Feb 2021 00:24:19 +0100 Subject: [PATCH] Migrate tests from org.hibernate.jpa.test to org.hibernate.orm.test.jpa (#5) --- .../ConnectionProviderDecorator.java | 68 +++++++++ .../ConnectionsReleaseAutoCommitTest.java | 81 ++++++++++ .../orm/test/jpa/criteria/OnKeywordTest.java | 29 ++-- ...EntityManagerFactorySerializationTest.java | 93 ++++++----- .../NamingStrategyConfigurationTest.java | 2 +- .../ConnectionsReleaseAutoCommitTest.java | 144 ------------------ .../testing/orm/junit/ExtraAssertions.java | 82 ++++++++++ 7 files changed, 295 insertions(+), 204 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionProviderDecorator.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionsReleaseAutoCommitTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/connections/ConnectionsReleaseAutoCommitTest.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ExtraAssertions.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionProviderDecorator.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionProviderDecorator.java new file mode 100644 index 0000000000..97a1a803c2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionProviderDecorator.java @@ -0,0 +1,68 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.connection; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; +import javax.sql.DataSource; + +import org.hibernate.cfg.Environment; +import org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl; + +import static org.mockito.Mockito.spy; + +public class ConnectionProviderDecorator extends UserSuppliedConnectionProviderImpl { + + private final DataSource dataSource; + + private int connectionCount; + + public Connection connection; + + public ConnectionProviderDecorator(){ + String url = Environment.getProperties().getProperty( Environment.URL ); + + Properties connectionProps = new Properties(); + connectionProps.put( "user", Environment.getProperties().getProperty( Environment.USER ) ); + connectionProps.put( "password", Environment.getProperties().getProperty( Environment.PASS ) ); + + dataSource = new BaseDataSource() { + @Override + public Connection getConnection() throws SQLException { + return DriverManager.getConnection( url, connectionProps ); + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return DriverManager.getConnection( url, connectionProps ); + } + }; + } + + @Override + public Connection getConnection() throws SQLException { + connectionCount++; + connection = spy( dataSource.getConnection() ); + return connection; + } + + @Override + public void closeConnection(Connection connection) throws SQLException { + connection.close(); + } + + public int getConnectionCount() { + return this.connectionCount; + } + + public void clear() { + connectionCount = 0; + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionsReleaseAutoCommitTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionsReleaseAutoCommitTest.java new file mode 100644 index 0000000000..0320fc75f1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/connection/ConnectionsReleaseAutoCommitTest.java @@ -0,0 +1,81 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.connection; + +import java.sql.Connection; +import java.sql.SQLException; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.engine.spi.SessionFactoryImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue(jiraKey = "HHH-12197") +@RequiresDialect(H2Dialect.class) +@Jpa( + annotatedClasses = { ConnectionsReleaseAutoCommitTest.Thing.class }, + integrationSettings = @Setting(name = AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.orm.test.jpa.connection.ConnectionProviderDecorator") +) +public class ConnectionsReleaseAutoCommitTest { + + private Connection connection; + + @Test + public void testConnectionAcquisitionCount(EntityManagerFactoryScope scope) throws SQLException { + ConnectionProviderDecorator connectionProvider = getConnectionProvider( scope ); + connectionProvider.clear(); + + scope.inTransaction( entityManager -> { + assertEquals( 1, connectionProvider.getConnectionCount() ); + Thing thing = new Thing(); + thing.setId( 1 ); + entityManager.persist( thing ); + assertEquals( 1, connectionProvider.getConnectionCount() ); + } ); + + assertEquals( 1, connectionProvider.getConnectionCount() ); + verify( connectionProvider.connection, times( 1 ) ).close(); + } + + private ConnectionProviderDecorator getConnectionProvider(EntityManagerFactoryScope scope) { + return (ConnectionProviderDecorator) ( (SessionFactoryImplementor) ( scope + .getEntityManagerFactory() ) ).getServiceRegistry().getService( ConnectionProvider.class ); + } + + @Entity(name = "Thing") + @Table(name = "Thing") + public static class Thing { + @Id + public Integer id; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/OnKeywordTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/OnKeywordTest.java index d4a07d8642..00d5f609c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/OnKeywordTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/OnKeywordTest.java @@ -27,24 +27,19 @@ public class OnKeywordTest extends AbstractCriteriaTest { @Test public void basicTest(EntityManagerFactoryScope scope) { - scope.inTransaction( + scope.inEntityManager( entityManager -> { - try { - CriteriaQuery criteria = entityManager.getCriteriaBuilder().createQuery( Order.class ); - Root root = criteria.from( Order.class ); - criteria.select( root ); - CollectionJoin lineItemsJoin = root.join( Order_.lineItems ); - lineItemsJoin.on( - entityManager.getCriteriaBuilder().gt( - lineItemsJoin.get( LineItem_.quantity ), - entityManager.getCriteriaBuilder().literal( 20 ) - ) - ); - entityManager.createQuery( criteria ).getResultList(); - } - catch (Exception e) { - throw e; - } + CriteriaQuery criteria = entityManager.getCriteriaBuilder().createQuery( Order.class ); + Root root = criteria.from( Order.class ); + criteria.select( root ); + CollectionJoin lineItemsJoin = root.join( Order_.lineItems ); + lineItemsJoin.on( + entityManager.getCriteriaBuilder().gt( + lineItemsJoin.get( LineItem_.quantity ), + entityManager.getCriteriaBuilder().literal( 20 ) + ) + ); + entityManager.createQuery( criteria ).getResultList(); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EntityManagerFactorySerializationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EntityManagerFactorySerializationTest.java index ea818245c1..def8c055c0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EntityManagerFactorySerializationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EntityManagerFactorySerializationTest.java @@ -58,49 +58,58 @@ public class EntityManagerFactorySerializationTest { in.close(); byteIn.close(); EntityManager em = serializedFactory.createEntityManager(); - //em.getTransaction().begin(); - //em.setFlushMode( FlushModeType.NEVER ); - Cat cat = new Cat(); - cat.setAge( 3 ); - cat.setDateOfBirth( new Date() ); - cat.setLength( 22 ); - cat.setName( "Kitty" ); - em.persist( cat ); - Item item = new Item(); - item.setName( "Train Ticket" ); - item.setDescr( "Paris-London" ); - em.persist( item ); - //em.getTransaction().commit(); - //em.getTransaction().begin(); - item.setDescr( "Paris-Bruxelles" ); - //em.getTransaction().commit(); + try { + //em.getTransaction().begin(); + //em.setFlushMode( FlushModeType.NEVER ); + Cat cat = new Cat(); + cat.setAge( 3 ); + cat.setDateOfBirth( new Date() ); + cat.setLength( 22 ); + cat.setName( "Kitty" ); + em.persist( cat ); + Item item = new Item(); + item.setName( "Train Ticket" ); + item.setDescr( "Paris-London" ); + em.persist( item ); + //em.getTransaction().commit(); + //em.getTransaction().begin(); + item.setDescr( "Paris-Bruxelles" ); + //em.getTransaction().commit(); - //fake the in container work - em.unwrap( Session.class ).disconnect(); - stream = new ByteArrayOutputStream(); - out = new ObjectOutputStream( stream ); - out.writeObject( em ); - out.close(); - serialized = stream.toByteArray(); - stream.close(); - byteIn = new ByteArrayInputStream( serialized ); - in = new ObjectInputStream( byteIn ); - em = (EntityManager) in.readObject(); - in.close(); - byteIn.close(); - //fake the in container work - em.getTransaction().begin(); - item = em.find( Item.class, item.getName() ); - item.setDescr( item.getDescr() + "-Amsterdam" ); - cat = (Cat) em.createQuery( "select c from " + Cat.class.getName() + " c" ).getSingleResult(); - cat.setLength( 34 ); - em.flush(); - em.remove( item ); - em.remove( cat ); - em.flush(); - em.getTransaction().commit(); - - em.close(); + //fake the in container work + em.unwrap( Session.class ).disconnect(); + stream = new ByteArrayOutputStream(); + out = new ObjectOutputStream( stream ); + out.writeObject( em ); + out.close(); + serialized = stream.toByteArray(); + stream.close(); + byteIn = new ByteArrayInputStream( serialized ); + in = new ObjectInputStream( byteIn ); + em = (EntityManager) in.readObject(); + in.close(); + byteIn.close(); + //fake the in container work + em.getTransaction().begin(); + item = em.find( Item.class, item.getName() ); + item.setDescr( item.getDescr() + "-Amsterdam" ); + cat = (Cat) em.createQuery( "select c from " + Cat.class.getName() + " c" ).getSingleResult(); + cat.setLength( 34 ); + em.flush(); + em.remove( item ); + em.remove( cat ); + em.flush(); + em.getTransaction().commit(); + } + catch (Exception e) { + if ( em.getTransaction().isActive() ) { + em.getTransaction().rollback(); + } + throw e; + } + finally { + em.close(); + } } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/NamingStrategyConfigurationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/NamingStrategyConfigurationTest.java index e733592ca1..d6e20b767f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/NamingStrategyConfigurationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/NamingStrategyConfigurationTest.java @@ -18,7 +18,7 @@ import org.hibernate.jpa.test.PersistenceUnitInfoAdapter; import org.hibernate.testing.orm.junit.BaseUnitTest; import org.junit.jupiter.api.Test; -import static org.hibernate.testing.junit5.ExtraAssertions.assertTyping; +import static org.hibernate.testing.orm.junit.ExtraAssertions.assertTyping; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/hibernate-core/src/test/java/org/hibernate/test/connections/ConnectionsReleaseAutoCommitTest.java b/hibernate-core/src/test/java/org/hibernate/test/connections/ConnectionsReleaseAutoCommitTest.java deleted file mode 100644 index b7d97fd32c..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/connections/ConnectionsReleaseAutoCommitTest.java +++ /dev/null @@ -1,144 +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 . - */ -package org.hibernate.test.connections; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.Map; -import java.util.Properties; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; -import javax.sql.DataSource; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.H2Dialect; -import org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.hibernate.orm.test.jpa.connection.BaseDataSource; - -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -/** - * @author Vlad Mihalcea - */ -@TestForIssue(jiraKey = "HHH-12197") -@RequiresDialect(H2Dialect.class) -public class ConnectionsReleaseAutoCommitTest extends BaseEntityManagerFunctionalTestCase { - - private ConnectionProviderDecorator connectionProvider; - - private Connection connection; - - @Override - protected Map getConfig() { - Map config = super.getConfig(); - - String url = Environment.getProperties().getProperty( Environment.URL ); - - Properties connectionProps = new Properties(); - connectionProps.put("user", Environment.getProperties().getProperty( Environment.USER )); - connectionProps.put("password", Environment.getProperties().getProperty( Environment.PASS )); - - BaseDataSource dataSource = new BaseDataSource() { - @Override - public Connection getConnection() throws SQLException { - return DriverManager.getConnection(url, connectionProps); - } - - @Override - public Connection getConnection(String username, String password) throws SQLException { - return DriverManager.getConnection(url, connectionProps); - } - }; - - connectionProvider = new ConnectionProviderDecorator( dataSource ); - config.put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider ); - return config; - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Thing.class, - }; - } - - @Test - public void testConnectionAcquisitionCount() throws SQLException { - connectionProvider.clear(); - - doInJPA( this::entityManagerFactory, entityManager -> { - assertEquals( 1, connectionProvider.getConnectionCount() ); - Thing thing = new Thing(); - thing.setId( 1 ); - entityManager.persist( thing ); - assertEquals( 1, connectionProvider.getConnectionCount() ); - } ); - - assertEquals( 1, connectionProvider.getConnectionCount() ); - verify( connectionProvider.connection, times( 1 ) ).close(); - } - - @Entity(name = "Thing") - @Table(name = "Thing") - public static class Thing { - @Id - public Integer id; - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - } - - public static class ConnectionProviderDecorator extends UserSuppliedConnectionProviderImpl { - - private final DataSource dataSource; - - private int connectionCount; - - private Connection connection; - - public ConnectionProviderDecorator(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override - public Connection getConnection() throws SQLException { - connectionCount++; - connection = spy(dataSource.getConnection()); - return connection; - } - - @Override - public void closeConnection(Connection connection) throws SQLException { - connection.close(); - } - - public int getConnectionCount() { - return this.connectionCount; - } - - public void clear() { - connectionCount = 0; - } - } -} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ExtraAssertions.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ExtraAssertions.java new file mode 100644 index 0000000000..161abce27b --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ExtraAssertions.java @@ -0,0 +1,82 @@ +/* + * 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 . + */ +package org.hibernate.testing.orm.junit; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.sql.Types; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Steve Ebersole + */ +public final class ExtraAssertions { + private ExtraAssertions() { + } + + public static void assertClassAssignability(Class expected, Class actual) { + if ( !expected.isAssignableFrom( actual ) ) { + fail( "Expected class [" + expected.getName() + "] was not assignable from actual [" + actual.getName() + "]" ); + } + } + + @SuppressWarnings("unchecked") + public static T assertTyping(Class expectedType, Object value) { + if ( !expectedType.isInstance( value ) ) { + fail( + String.format( + "Expecting value of type [%s], but found [%s]", + expectedType.getName(), + value == null ? "" : value + ) + ); + } + return (T) value; + } + + public static void assertJdbcTypeCode(int expected, int actual) { + if ( expected != actual ) { + final String message = String.format( + "JDBC type codes did not match...\n" + + "Expected: %s (%s)\n" + + "Actual : %s (%s)", + jdbcTypeCodeMap().get( expected ), + expected, + jdbcTypeCodeMap().get( actual ), + actual + ); + fail( message ); + } + } + + private static Map jdbcTypeCodeMap; + + private static synchronized Map jdbcTypeCodeMap() { + if ( jdbcTypeCodeMap == null ) { + jdbcTypeCodeMap = generateJdbcTypeCache(); + } + return jdbcTypeCodeMap; + } + + private static Map generateJdbcTypeCache() { + final Field[] fields = Types.class.getFields(); + Map cache = new HashMap( (int) ( fields.length * .75 ) + 1 ); + for ( Field field : fields ) { + if ( Modifier.isStatic( field.getModifiers() ) ) { + try { + cache.put( field.get( null ), field.getName() ); + } + catch (Throwable ignore) { + } + } + } + return cache; + } +}