Migrate tests from org.hibernate.jpa.test to org.hibernate.orm.test.jpa (#5)
This commit is contained in:
parent
470af28795
commit
b1951f5c26
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -27,24 +27,19 @@ public class OnKeywordTest extends AbstractCriteriaTest {
|
|||
@Test
|
||||
public void basicTest(EntityManagerFactoryScope scope) {
|
||||
|
||||
scope.inTransaction(
|
||||
scope.inEntityManager(
|
||||
entityManager -> {
|
||||
try {
|
||||
CriteriaQuery<Order> criteria = entityManager.getCriteriaBuilder().createQuery( Order.class );
|
||||
Root<Order> root = criteria.from( Order.class );
|
||||
criteria.select( root );
|
||||
CollectionJoin<Order,LineItem> 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<Order> criteria = entityManager.getCriteriaBuilder().createQuery( Order.class );
|
||||
Root<Order> root = criteria.from( Order.class );
|
||||
criteria.select( root );
|
||||
CollectionJoin<Order, LineItem> lineItemsJoin = root.join( Order_.lineItems );
|
||||
lineItemsJoin.on(
|
||||
entityManager.getCriteriaBuilder().gt(
|
||||
lineItemsJoin.get( LineItem_.quantity ),
|
||||
entityManager.getCriteriaBuilder().literal( 20 )
|
||||
)
|
||||
);
|
||||
entityManager.createQuery( criteria ).getResultList();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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> T assertTyping(Class<T> expectedType, Object value) {
|
||||
if ( !expectedType.isInstance( value ) ) {
|
||||
fail(
|
||||
String.format(
|
||||
"Expecting value of type [%s], but found [%s]",
|
||||
expectedType.getName(),
|
||||
value == null ? "<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<Integer, String> jdbcTypeCodeMap;
|
||||
|
||||
private static synchronized Map<Integer, String> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue