HHH-11647 - Fix failing tests
This commit is contained in:
parent
f9231bed0d
commit
374a2cae09
|
@ -17,6 +17,7 @@ import static org.junit.Assert.assertEquals;
|
|||
*
|
||||
* @author Christoph Dreis
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-11647")
|
||||
public class PostgreSQL92DialectTestCase extends BaseUnitTestCase {
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,8 +17,8 @@ import org.hibernate.boot.MetadataSources;
|
|||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.hbm2ddl.TargetTypeHelper;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
@ -62,12 +62,14 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
alter table USER_SETTING add constraint FK_TO_USER foreign key (USERS_ID) references USERS
|
||||
*/
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"USERS",
|
||||
"FK_TO_USER_SETTING",
|
||||
"USER_SETTING_ID",
|
||||
"USER_SETTING"
|
||||
) );
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"USER_SETTING",
|
||||
"FK_TO_USER",
|
||||
"USER_ID",
|
||||
|
@ -85,6 +87,7 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
alter table GROUP add constraint FK_USER_GROUP foreign key (USER_ID) references USERS
|
||||
*/
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"GROUP",
|
||||
"FK_USER_GROUP",
|
||||
"USER_ID",
|
||||
|
@ -103,12 +106,14 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
alter table PERSON_PHONE add constraint PHONE_ID_FK foreign key (PHONE_ID) references PHONE
|
||||
*/
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"PERSON_PHONE",
|
||||
"PERSON_ID_FK",
|
||||
"PERSON_ID",
|
||||
"PERSON"
|
||||
) );
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"PERSON_PHONE",
|
||||
"PHONE_ID_FK",
|
||||
"PHONE_ID",
|
||||
|
@ -127,12 +132,14 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
alter table EMPLOYEE_PROJECT add constraint FK_PROJECT foreign key (PROJECT_ID) references PROJECT
|
||||
*/
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"EMPLOYEE_PROJECT",
|
||||
"FK_EMPLOYEE",
|
||||
"EMPLOYEE_ID",
|
||||
"EMPLOYEE"
|
||||
) );
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
ssr,
|
||||
"EMPLOYEE_PROJECT",
|
||||
"FK_PROJECT",
|
||||
"PROJECT_ID",
|
||||
|
@ -161,7 +168,7 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
final List<String> sqlLines = Files.readAllLines( output.toPath(), Charset.defaultCharset() );
|
||||
boolean found = false;
|
||||
for ( String line : sqlLines ) {
|
||||
if ( line.matches( expectedAlterTableStatement ) ) {
|
||||
if ( line.contains( expectedAlterTableStatement ) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -169,16 +176,19 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
}
|
||||
|
||||
private static class AlterTableStatement {
|
||||
final StandardServiceRegistry ssr;
|
||||
final String tableName;
|
||||
final String fkConstraintName;
|
||||
final String fkColumnName;
|
||||
final String referenceTableName;
|
||||
|
||||
public AlterTableStatement(
|
||||
StandardServiceRegistry ssr,
|
||||
String tableName,
|
||||
String fkConstraintName,
|
||||
String fkColumnName,
|
||||
String referenceTableName) {
|
||||
this.ssr = ssr;
|
||||
this.tableName = tableName;
|
||||
this.fkConstraintName = fkConstraintName;
|
||||
this.fkColumnName = fkColumnName;
|
||||
|
@ -186,7 +196,7 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase {
|
|||
}
|
||||
|
||||
public String toSQL() {
|
||||
return "alter table (?:if exists) " + tableName + " add constraint " + fkConstraintName + " foreign key \\(" + fkColumnName + "\\) references " + referenceTableName;
|
||||
return ssr.getService( JdbcEnvironment.class ).getDialect().getAlterTableString( tableName ) + " add constraint " + fkConstraintName + " foreign key (" + fkColumnName + ") references " + referenceTableName;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.hibernate.boot.MetadataSources;
|
|||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
|
||||
|
@ -65,7 +66,7 @@ public class JoinedInheritanceForeignKeyTest extends BaseUnitTestCase {
|
|||
public void testForeignKeyHasCorrectName() throws Exception {
|
||||
createSchema( new Class[] {Role.class, User.class, Person.class} );
|
||||
checkAlterTableStatement( new AlterTableStatement(
|
||||
"User",
|
||||
ssr, "User",
|
||||
"FK_PERSON_ROLE",
|
||||
"USER_ID",
|
||||
"PersonRole"
|
||||
|
@ -78,7 +79,7 @@ public class JoinedInheritanceForeignKeyTest extends BaseUnitTestCase {
|
|||
final List<String> sqlLines = Files.readAllLines( output.toPath(), Charset.defaultCharset() );
|
||||
boolean found = false;
|
||||
for ( String line : sqlLines ) {
|
||||
if ( line.matches( expectedAlterTableStatement ) ) {
|
||||
if ( line.contains( expectedAlterTableStatement ) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -86,16 +87,18 @@ public class JoinedInheritanceForeignKeyTest extends BaseUnitTestCase {
|
|||
}
|
||||
|
||||
private static class AlterTableStatement {
|
||||
final StandardServiceRegistry ssr;
|
||||
final String tableName;
|
||||
final String fkConstraintName;
|
||||
final String fkColumnName;
|
||||
final String referenceTableName;
|
||||
|
||||
public AlterTableStatement(
|
||||
String tableName,
|
||||
StandardServiceRegistry ssr, String tableName,
|
||||
String fkConstraintName,
|
||||
String fkColumnName,
|
||||
String referenceTableName) {
|
||||
this.ssr = ssr;
|
||||
this.tableName = tableName;
|
||||
this.fkConstraintName = fkConstraintName;
|
||||
this.fkColumnName = fkColumnName;
|
||||
|
@ -103,7 +106,7 @@ public class JoinedInheritanceForeignKeyTest extends BaseUnitTestCase {
|
|||
}
|
||||
|
||||
public String toSQL() {
|
||||
return "alter table (?:if exists) " + tableName + " add constraint " + fkConstraintName + " foreign key \\(" + fkColumnName + "\\) references " + referenceTableName;
|
||||
return ssr.getService( JdbcEnvironment.class ).getDialect().getAlterTableString( tableName ) + " add constraint " + fkConstraintName + " foreign key (" + fkColumnName + ") references " + referenceTableName;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
|
|||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.dialect.DB2Dialect;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.PostgreSQL81Dialect;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
|
@ -39,12 +40,14 @@ public class SchemaCreationTest {
|
|||
private File output;
|
||||
private StandardServiceRegistry ssr;
|
||||
private MetadataImplementor metadata;
|
||||
private Dialect dialect;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
output = File.createTempFile( "update_script", ".sql" );
|
||||
output.deleteOnExit();
|
||||
ssr = new StandardServiceRegistryBuilder().build();
|
||||
dialect = ssr.getService(JdbcEnvironment.class).getDialect();
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -74,16 +77,17 @@ public class SchemaCreationTest {
|
|||
for ( String statement : sqlLines ) {
|
||||
assertThat(
|
||||
"Should not try to create the unique constraint for the non existing table element",
|
||||
statement.toLowerCase().matches( "alter table (?:if exists) element" ),
|
||||
statement.toLowerCase()
|
||||
.matches( dialect.getAlterTableString( "element" ) ),
|
||||
is( false )
|
||||
);
|
||||
if (ssr.getService(JdbcEnvironment.class).getDialect() instanceof DB2Dialect) {
|
||||
if ( dialect instanceof DB2Dialect) {
|
||||
if (statement.toLowerCase().startsWith("create unique index")
|
||||
&& statement.toLowerCase().contains("category (code)")) {
|
||||
isUniqueConstraintCreated = true;
|
||||
}
|
||||
}
|
||||
else if (ssr.getService(JdbcEnvironment.class).getDialect() instanceof PostgreSQL81Dialect) {
|
||||
else if ( dialect instanceof PostgreSQL81Dialect) {
|
||||
if (statement.toLowerCase().startsWith("alter table if exists category add constraint")
|
||||
&& statement.toLowerCase().contains("unique (code)")) {
|
||||
isUniqueConstraintCreated = true;
|
||||
|
|
|
@ -126,7 +126,7 @@ public class UniqueConstraintDropTest {
|
|||
|
||||
private boolean checkDropConstraint(String tableName, String columnName) throws IOException {
|
||||
boolean matches = false;
|
||||
String regex = "alter table (?:if exists) " + tableName + " drop constraint";
|
||||
String regex = getDialect().getAlterTableString( tableName ) + " drop constraint";
|
||||
|
||||
if ( getDialect().supportsIfExistsBeforeConstraintName() ) {
|
||||
regex += " if exists";
|
||||
|
|
|
@ -19,7 +19,7 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
|||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.dialect.DB2Dialect;
|
||||
import org.hibernate.dialect.PostgreSQL81Dialect;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
|
@ -65,7 +65,7 @@ public class UniqueConstraintGenerationTest {
|
|||
.setOutputFile( output.getAbsolutePath() )
|
||||
.create( EnumSet.of( TargetType.SCRIPT ), metadata );
|
||||
|
||||
if (ssr.getService(JdbcEnvironment.class).getDialect() instanceof DB2Dialect) {
|
||||
if ( getDialect() instanceof DB2Dialect) {
|
||||
assertThat(
|
||||
"The test_entity_item table unique constraint has not been generated",
|
||||
isCreateUniqueIndexGenerated("test_entity_item", "item"),
|
||||
|
@ -87,9 +87,13 @@ public class UniqueConstraintGenerationTest {
|
|||
);
|
||||
}
|
||||
|
||||
private Dialect getDialect() {
|
||||
return ssr.getService(JdbcEnvironment.class).getDialect();
|
||||
}
|
||||
|
||||
private boolean isUniqueConstraintGenerated(String tableName, String columnName) throws IOException {
|
||||
boolean matches = false;
|
||||
final String regex = "alter table (?:if exists) " + tableName + " add constraint uk_(.)* unique \\(" + columnName + "\\)";
|
||||
final String regex = getDialect().getAlterTableString( tableName ) + " add constraint uk_(.)* unique \\(" + columnName + "\\)";
|
||||
|
||||
final String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase();
|
||||
final String[] split = fileContent.split( System.lineSeparator() );
|
||||
|
|
Loading…
Reference in New Issue