HHH-11473 - Fix tests leaving a dirty database

This commit is contained in:
Andrea Boriero 2017-02-25 18:29:38 +00:00
parent bc3032785e
commit 5e29497d60
4 changed files with 65 additions and 14 deletions

View File

@ -16,6 +16,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
@ -29,6 +30,7 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -40,14 +42,32 @@ import static org.junit.Assert.assertTrue;
@RequiresDialect(MySQL5Dialect.class) @RequiresDialect(MySQL5Dialect.class)
public class MySQLDropConstraintThrowsExceptionTest extends BaseUnitTestCase { public class MySQLDropConstraintThrowsExceptionTest extends BaseUnitTestCase {
@After @Before
public void releaseResources() { public void setUp() {
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.enableAutoClose()
.applySetting( AvailableSettings.HBM2DDL_AUTO, "drop" )
.build();
SessionFactoryImplementor sessionFactory = null;
try {
final Metadata metadata = new MetadataSources( serviceRegistry )
.addAnnotatedClass( Customer.class )
.buildMetadata();
sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory();
}
finally {
if ( sessionFactory != null ) {
sessionFactory.close();
}
StandardServiceRegistryBuilder.destroy( serviceRegistry );
}
} }
@Test @After
public void testEnumTypeInterpretation() { public void tearDown() {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.enableAutoClose() .enableAutoClose()
.applySetting( AvailableSettings.HBM2DDL_AUTO, "drop" ) .applySetting( AvailableSettings.HBM2DDL_AUTO, "drop" )
.build(); .build();
@ -67,9 +87,13 @@ public class MySQLDropConstraintThrowsExceptionTest extends BaseUnitTestCase {
StandardServiceRegistryBuilder.destroy( serviceRegistry ); StandardServiceRegistryBuilder.destroy( serviceRegistry );
} }
PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); }
serviceRegistry = new StandardServiceRegistryBuilder() @Test
public void testEnumTypeInterpretation() {
final PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.enableAutoClose() .enableAutoClose()
.applySetting( AvailableSettings.HBM2DDL_AUTO, "update" ) .applySetting( AvailableSettings.HBM2DDL_AUTO, "update" )
.applySetting( .applySetting(
@ -78,17 +102,19 @@ public class MySQLDropConstraintThrowsExceptionTest extends BaseUnitTestCase {
) )
.build(); .build();
SessionFactory sessionFactory = null;
try { try {
final Metadata metadata = new MetadataSources( serviceRegistry ) final Metadata metadata = new MetadataSources( serviceRegistry )
.addAnnotatedClass( Customer.class ) .addAnnotatedClass( Customer.class )
.buildMetadata(); .buildMetadata();
sessionFactory = (SessionFactoryImplementor) metadata.buildSessionFactory(); sessionFactory = metadata.buildSessionFactory();
List<String> alterStatements = connectionProvider.getExecuteStatements().stream() List<String> alterStatements = connectionProvider.getExecuteStatements().stream()
.filter( .filter(
sql -> sql.toLowerCase().contains( "alter " ) sql -> sql.toLowerCase().contains( "alter " )
).map( String::trim ).collect( Collectors.toList() ); ).map( String::trim ).collect( Collectors.toList() );
assertTrue(alterStatements.get(0).matches( "alter table CUSTOMER\\s+drop index .*?" )); assertTrue( alterStatements.get( 0 ).matches( "alter table CUSTOMER\\s+drop index .*?" ) );
assertTrue(alterStatements.get(1).matches( "alter table CUSTOMER\\s+add constraint .*? unique \\(CUSTOMER_ID\\)" )); assertTrue( alterStatements.get( 1 )
.matches( "alter table CUSTOMER\\s+add constraint .*? unique \\(CUSTOMER_ID\\)" ) );
} }
finally { finally {
if ( sessionFactory != null ) { if ( sessionFactory != null ) {

View File

@ -93,6 +93,17 @@ public class BasicConnectionTest extends BaseCoreFunctionalTestCase {
assertFalse( getResourceRegistry( jdbcCoord ).hasRegisteredResources() ); assertFalse( getResourceRegistry( jdbcCoord ).hasRegisteredResources() );
} }
@Override
protected void cleanupTest() throws Exception {
try (Session session = openSession()) {
session.doWork( connection -> {
final Statement stmnt = connection.createStatement();
stmnt.execute( getDialect().getDropTableString( "SANDBOX_JDBC_TST" ) );
} );
}
}
private ResourceRegistry getResourceRegistry(JdbcCoordinator jdbcCoord) { private ResourceRegistry getResourceRegistry(JdbcCoordinator jdbcCoord) {
return jdbcCoord.getResourceRegistry(); return jdbcCoord.getResourceRegistry();
} }

View File

@ -260,4 +260,15 @@ public class BatchingTest extends BaseCoreFunctionalTestCase implements BatchKey
session.close(); session.close();
} }
@Override
protected void cleanupTest() throws Exception {
try (Session session = openSession()) {
session.doWork( connection -> {
final Statement stmnt = connection.createStatement();
stmnt.execute( getDialect().getDropTableString( "SANDBOX_JDBC_TST" ) );
} );
}
}
} }

View File

@ -23,7 +23,9 @@ import org.hibernate.tool.schema.TargetType;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl; import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor; import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
/** /**
@ -34,7 +36,8 @@ import org.junit.Test;
* @see * @see
*/ */
@TestForIssue( jiraKey = "HHH-9745" ) @TestForIssue( jiraKey = "HHH-9745" )
public class SequenceReadingTest { @RequiresDialect( H2Dialect.class )
public class SequenceReadingTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testSequenceReading() { public void testSequenceReading() {