Skip to content

Commit

Permalink
HHH-11647 - Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed May 6, 2017
1 parent f9231be commit 374a2ca
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
Expand Up @@ -17,6 +17,7 @@
*
* @author Christoph Dreis
*/
@TestForIssue( jiraKey = "HHH-11647")
public class PostgreSQL92DialectTestCase extends BaseUnitTestCase {

/**
Expand Down
Expand Up @@ -17,8 +17,8 @@
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;
Expand Down Expand Up @@ -62,12 +62,14 @@ alter table USERS add constraint FK_TO_USER_SETTING foreign key (USER_SETTING_ID
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",
Expand All @@ -85,6 +87,7 @@ public void oneToManyTest() throws Exception {
alter table GROUP add constraint FK_USER_GROUP foreign key (USER_ID) references USERS
*/
checkAlterTableStatement( new AlterTableStatement(
ssr,
"GROUP",
"FK_USER_GROUP",
"USER_ID",
Expand All @@ -103,12 +106,14 @@ alter table PERSON_PHONE add constraint PERSON_ID_FK foreign key (PERSON_ID) ref
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",
Expand All @@ -127,12 +132,14 @@ alter table EMPLOYEE_PROJECT add constraint FK_EMPLOYEE foreign key (EMPLOYEE_ID
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",
Expand Down Expand Up @@ -161,32 +168,35 @@ private void checkAlterTableStatement(AlterTableStatement alterTableStatement)
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;
}
}
assertThat( "Expected alter table statement not found : " + expectedAlterTableStatement, found, is( true ) );
}

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;
this.referenceTableName = referenceTableName;
}

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;
}
}

Expand Down
Expand Up @@ -27,6 +27,7 @@
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;

Expand Down Expand Up @@ -65,7 +66,7 @@ public void tearsDown() {
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"
Expand All @@ -78,32 +79,34 @@ private void checkAlterTableStatement(AlterTableStatement alterTableStatement)
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;
}
}
assertThat( "Expected alter table statement not found : " + expectedAlterTableStatement, found, is( true ) );
}

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;
this.referenceTableName = referenceTableName;
}

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;
}
}

Expand Down
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -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
Expand Down Expand Up @@ -74,16 +77,17 @@ public void testUniqueConstraintIsCorrectlyGenerated() throws Exception {
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;
Expand Down
Expand Up @@ -126,7 +126,7 @@ protected Dialect getDialect() {

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";
Expand Down
Expand Up @@ -19,7 +19,7 @@
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;
Expand Down Expand Up @@ -65,7 +65,7 @@ public void testUniqueConstraintIsGenerated() throws Exception {
.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"),
Expand All @@ -87,9 +87,13 @@ public void testUniqueConstraintIsGenerated() throws Exception {
);
}

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() );
Expand Down

0 comments on commit 374a2ca

Please sign in to comment.