Skip to content

Commit

Permalink
HHH-11910 : SchemaUpdateTest fails on databases using case-insensitiv…
Browse files Browse the repository at this point in the history
…e identifiers

(cherry picked from commit cc342dc)
  • Loading branch information
gbadner committed Aug 4, 2017
1 parent 70d3d61 commit 013daa2
Showing 1 changed file with 39 additions and 0 deletions.
Expand Up @@ -33,16 +33,23 @@
import javax.persistence.Table;

import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2ddl.SchemaValidator;
import org.hibernate.tool.schema.JdbcMetadaAccessStrategy;
import org.hibernate.tool.schema.TargetType;

import org.hibernate.testing.SkipLog;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -57,6 +64,9 @@
*/
@RunWith(Parameterized.class)
public class SchemaUpdateTest {

private boolean skipTest;

@Parameterized.Parameters
public static Collection<String[]> parameters() {
return Arrays.asList(
Expand All @@ -74,12 +84,19 @@ public static Collection<String[]> parameters() {

@Before
public void setUp() throws IOException {
if(SQLServerDialect.class.isAssignableFrom( Dialect.getDialect().getClass() )) {
// SQLServerDialect stores case-insensitive quoted identifiers in mixed case,
// so the checks at the end of this method won't work.
skipTest = true;
return;
}
output = File.createTempFile( "update_script", ".sql" );
output.deleteOnExit();
ssr = new StandardServiceRegistryBuilder()
.applySetting( AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true" )
.applySetting( AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, jdbcMetadataExtractorStrategy )
.build();

final MetadataSources metadataSources = new MetadataSources( ssr );
metadataSources.addAnnotatedClass( LowercaseTableNameEntity.class );
metadataSources.addAnnotatedClass( TestEntity.class );
Expand All @@ -92,10 +109,28 @@ public void setUp() throws IOException {

metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.validate();

// Databases that use case-insensitive quoted identifiers need to be skipped.
// The following checks will work for checking those dialects that store case-insensitive
// quoted identifiers as upper-case or lower-case. It does not work for dialects that
// store case-insensitive identifiers in mixed case (like SQL Server).
final IdentifierHelper identifierHelper = ssr.getService( JdbcEnvironment.class ).getIdentifierHelper();
final String lowerCaseName = identifierHelper.toMetaDataObjectName( Identifier.toIdentifier( "testentity", true ) );
final String upperCaseName = identifierHelper.toMetaDataObjectName( Identifier.toIdentifier("TESTENTITY", true ) );
final String mixedCaseName = identifierHelper.toMetaDataObjectName( Identifier.toIdentifier("TESTentity", true ) );
if ( lowerCaseName.equals( upperCaseName ) ||
lowerCaseName.equals( mixedCaseName ) ||
upperCaseName.equals( mixedCaseName ) ) {
StandardServiceRegistryBuilder.destroy( ssr );
skipTest = true;
}
}

@After
public void tearsDown() {
if ( skipTest ) {
return;
}
new SchemaExport().setHaltOnError( true )
.setOutputFile( output.getAbsolutePath() )
.setFormat( false )
Expand All @@ -105,6 +140,10 @@ public void tearsDown() {

@Test
public void testSchemaUpdateAndValidation() throws Exception {
if ( skipTest ) {
SkipLog.reportSkip( "skipping test because quoted names are not case-sensitive." );
return;
}

new SchemaUpdate().setHaltOnError( true )
.execute( EnumSet.of( TargetType.DATABASE ), metadata );
Expand Down

0 comments on commit 013daa2

Please sign in to comment.