HHH-11910 : SchemaUpdateTest fails on databases using case-insensitive identifiers
(cherry picked from commit cc342dc072
)
This commit is contained in:
parent
70d3d617ec
commit
013daa2ca9
|
@ -33,16 +33,23 @@ import javax.persistence.PrimaryKeyJoinColumn;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
|
import org.hibernate.boot.model.naming.Identifier;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.boot.spi.MetadataImplementor;
|
import org.hibernate.boot.spi.MetadataImplementor;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
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.SchemaExport;
|
||||||
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
|
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
|
||||||
import org.hibernate.tool.hbm2ddl.SchemaValidator;
|
import org.hibernate.tool.hbm2ddl.SchemaValidator;
|
||||||
import org.hibernate.tool.schema.JdbcMetadaAccessStrategy;
|
import org.hibernate.tool.schema.JdbcMetadaAccessStrategy;
|
||||||
import org.hibernate.tool.schema.TargetType;
|
import org.hibernate.tool.schema.TargetType;
|
||||||
|
|
||||||
|
import org.hibernate.testing.SkipLog;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -57,6 +64,9 @@ import static org.junit.Assert.assertThat;
|
||||||
*/
|
*/
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
public class SchemaUpdateTest {
|
public class SchemaUpdateTest {
|
||||||
|
|
||||||
|
private boolean skipTest;
|
||||||
|
|
||||||
@Parameterized.Parameters
|
@Parameterized.Parameters
|
||||||
public static Collection<String[]> parameters() {
|
public static Collection<String[]> parameters() {
|
||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
|
@ -74,12 +84,19 @@ public class SchemaUpdateTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws IOException {
|
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 = File.createTempFile( "update_script", ".sql" );
|
||||||
output.deleteOnExit();
|
output.deleteOnExit();
|
||||||
ssr = new StandardServiceRegistryBuilder()
|
ssr = new StandardServiceRegistryBuilder()
|
||||||
.applySetting( AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true" )
|
.applySetting( AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true" )
|
||||||
.applySetting( AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, jdbcMetadataExtractorStrategy )
|
.applySetting( AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, jdbcMetadataExtractorStrategy )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final MetadataSources metadataSources = new MetadataSources( ssr );
|
final MetadataSources metadataSources = new MetadataSources( ssr );
|
||||||
metadataSources.addAnnotatedClass( LowercaseTableNameEntity.class );
|
metadataSources.addAnnotatedClass( LowercaseTableNameEntity.class );
|
||||||
metadataSources.addAnnotatedClass( TestEntity.class );
|
metadataSources.addAnnotatedClass( TestEntity.class );
|
||||||
|
@ -92,10 +109,28 @@ public class SchemaUpdateTest {
|
||||||
|
|
||||||
metadata = (MetadataImplementor) metadataSources.buildMetadata();
|
metadata = (MetadataImplementor) metadataSources.buildMetadata();
|
||||||
metadata.validate();
|
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
|
@After
|
||||||
public void tearsDown() {
|
public void tearsDown() {
|
||||||
|
if ( skipTest ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
new SchemaExport().setHaltOnError( true )
|
new SchemaExport().setHaltOnError( true )
|
||||||
.setOutputFile( output.getAbsolutePath() )
|
.setOutputFile( output.getAbsolutePath() )
|
||||||
.setFormat( false )
|
.setFormat( false )
|
||||||
|
@ -105,6 +140,10 @@ public class SchemaUpdateTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSchemaUpdateAndValidation() throws Exception {
|
public void testSchemaUpdateAndValidation() throws Exception {
|
||||||
|
if ( skipTest ) {
|
||||||
|
SkipLog.reportSkip( "skipping test because quoted names are not case-sensitive." );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
new SchemaUpdate().setHaltOnError( true )
|
new SchemaUpdate().setHaltOnError( true )
|
||||||
.execute( EnumSet.of( TargetType.DATABASE ), metadata );
|
.execute( EnumSet.of( TargetType.DATABASE ), metadata );
|
||||||
|
|
Loading…
Reference in New Issue