HHH-13373 ensure testing code correctness by local testing

This commit is contained in:
Nathan Xu 2020-01-22 18:56:56 -05:00 committed by Andrea Boriero
parent 7beb96a2ab
commit bc2aa159a1
1 changed files with 48 additions and 42 deletions

View File

@ -1,17 +1,15 @@
package org.hibernate.test.dialect.functional;
import java.sql.*;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.MariaDB103Dialect;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.testing.*;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
import java.sql.ResultSet;
import java.sql.Statement;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
/**
* @author Nathan Xu
*/
@ -19,49 +17,57 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
@FailureExpected(jiraKey = "HHH-13373", message = "currently db user has no privilege to create secondary db and sequence within it")
public class MariaDBExtractSequenceMatadataTest extends BaseCoreFunctionalTestCase {
private String secondaryDbName = "test_db";
private String secondarySequenceName = "test_sequence";
private static String primaryDbName;
private static String primarySequenceName = "seq_HHH13373";
private static String secondaryDbName = "secondary_db_HHH13373";
private static String secondarySequenceName = "secondary_seq_HHH13373";
@Override
protected void prepareTest() throws Exception {
doInHibernate( this::sessionFactory, session -> {
String currentDbName = session.doReturningWork( connection -> {
try ( Statement stmt = connection.createStatement() ) {
try ( ResultSet resultSet = stmt.executeQuery( "SELECT DATABASE()" ) ) {
return resultSet.getString( 0 );
}
@BeforeClassOnce
public static void setUpDBs() throws Exception {
try ( Connection conn = getConnection() ) {
try ( Statement stmt = conn.createStatement() ) {
try ( ResultSet resultSet = stmt.executeQuery( "SELECT DATABASE()" ) ) {
assert resultSet.next();
primaryDbName = resultSet.getString( 1 );
}
});
session.doWork( connection -> {
try ( Statement stmt = connection.createStatement() ) {
stmt.execute( "CREATE DATABASE " + secondaryDbName );
stmt.execute( "USE " + secondaryDbName );
stmt.execute( "CREATE SEQUENCE " + secondarySequenceName );
stmt.execute( "USE " + currentDbName );
stmt.execute( "DROP SEQUENCE IF EXISTS " + secondarySequenceName );
}
} );
} );
stmt.execute( "CREATE DATABASE " + secondaryDbName );
stmt.execute( "USE " + secondaryDbName );
stmt.execute( "CREATE SEQUENCE " + secondarySequenceName );
stmt.execute( "USE " + primaryDbName );
stmt.execute( "DROP SEQUENCE IF EXISTS " + secondarySequenceName );
stmt.execute( "CREATE SEQUENCE IF NOT EXISTS " + primarySequenceName );
}
}
}
@Test
@TestForIssue(jiraKey = "HHH-13373")
public void testHibernateLaunchedSuccessfully() {
doInHibernate( this::sessionFactory, s -> {} );
JdbcEnvironment jdbcEnvironment = serviceRegistry().getService( JdbcEnvironment.class );
Assert.assertFalse( jdbcEnvironment.getExtractedDatabaseMetaData().getSequenceInformationList().isEmpty() );
}
@Override
protected void cleanupTest() {
doInHibernate( this::sessionFactory, session -> {
session.doWork( connection -> {
try ( Statement stmt = connection.createStatement() ) {
stmt.execute( "DROP DATABASE " + secondaryDbName );
}
catch (Exception e) {
// Ignore
}
} );
} );
@AfterClassOnce
public static void tearDownDBs() {
try ( Connection conn = getConnection() ) {
try ( Statement stmt = conn.createStatement() ) {
stmt.execute( "DROP DATABASE " + secondaryDbName );
}
catch ( Exception e ) {
// Ignore
}
}
catch ( Exception e ) {
// Ignore
}
}
private static Connection getConnection() throws SQLException {
String url = Environment.getProperties().getProperty( Environment.URL );
String user = Environment.getProperties().getProperty( Environment.USER );
String password = Environment.getProperties().getProperty( Environment.PASS );
return DriverManager.getConnection( url, user, password );
}
}