HHH-13373 Remove FailureExpected annotation from tests and assure tearDownDBs method is executed even if an error occurs in the setUpDBs method

This commit is contained in:
Andrea Boriero 2020-01-24 09:59:02 +00:00
parent bc2aa159a1
commit d04a1fe3fd
1 changed files with 31 additions and 20 deletions

View File

@ -1,12 +1,20 @@
package org.hibernate.test.dialect.functional;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.MariaDB103Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.testing.*;
import org.hibernate.testing.BeforeClassOnce;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;
@ -14,7 +22,6 @@ import org.junit.Test;
* @author Nathan Xu
*/
@RequiresDialect(MariaDB103Dialect.class)
@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 static String primaryDbName;
@ -22,18 +29,18 @@ public class MariaDBExtractSequenceMatadataTest extends BaseCoreFunctionalTestCa
private static String secondaryDbName = "secondary_db_HHH13373";
private static String secondarySequenceName = "secondary_seq_HHH13373";
@BeforeClassOnce
public static void setUpDBs() throws Exception {
try ( Connection conn = getConnection() ) {
try ( Statement stmt = conn.createStatement() ) {
try ( ResultSet resultSet = stmt.executeQuery( "SELECT DATABASE()" ) ) {
try (Connection conn = getConnection()) {
try (Statement stmt = conn.createStatement()) {
try (ResultSet resultSet = stmt.executeQuery( "SELECT DATABASE()" )) {
assert resultSet.next();
primaryDbName = resultSet.getString( 1 );
}
stmt.execute( "CREATE DATABASE " + secondaryDbName );
stmt.execute( "CREATE DATABASE IF NOT EXISTS " + secondaryDbName );
stmt.execute( "USE " + secondaryDbName );
stmt.execute( "CREATE SEQUENCE " + secondarySequenceName );
stmt.execute( "CREATE SEQUENCE IF NOT EXISTS " + secondarySequenceName );
stmt.execute( "USE " + primaryDbName );
stmt.execute( "DROP SEQUENCE IF EXISTS " + secondarySequenceName );
stmt.execute( "CREATE SEQUENCE IF NOT EXISTS " + primarySequenceName );
@ -48,26 +55,30 @@ public class MariaDBExtractSequenceMatadataTest extends BaseCoreFunctionalTestCa
Assert.assertFalse( jdbcEnvironment.getExtractedDatabaseMetaData().getSequenceInformationList().isEmpty() );
}
@AfterClassOnce
public static void tearDownDBs() {
try ( Connection conn = getConnection() ) {
try ( Statement stmt = conn.createStatement() ) {
stmt.execute( "DROP DATABASE " + secondaryDbName );
@AfterClass
public static void tearDownDBs() throws SQLException {
try (Connection conn = getConnection()) {
try (Statement stmt = conn.createStatement()) {
stmt.execute( "DROP DATABASE IF EXISTS " + secondaryDbName );
}
catch ( Exception e ) {
catch (Exception e) {
// Ignore
}
try (Statement stmt = conn.createStatement()) {
stmt.execute( "DROP SEQUENCE IF EXISTS " + primarySequenceName );
}
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 );
}
}