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

View File

@ -93,6 +93,17 @@ public class BasicConnectionTest extends BaseCoreFunctionalTestCase {
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) {
return jdbcCoord.getResourceRegistry();
}

View File

@ -260,4 +260,15 @@ public class BatchingTest extends BaseCoreFunctionalTestCase implements BatchKey
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.spi.SequenceInformationExtractor;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
@ -34,7 +36,8 @@ import org.junit.Test;
* @see
*/
@TestForIssue( jiraKey = "HHH-9745" )
public class SequenceReadingTest {
@RequiresDialect( H2Dialect.class )
public class SequenceReadingTest extends BaseCoreFunctionalTestCase {
@Test
public void testSequenceReading() {