diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/secondarytable/SecondaryTableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/secondarytable/SecondaryTableTest.java new file mode 100644 index 0000000000..f5c55c93ac --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/secondarytable/SecondaryTableTest.java @@ -0,0 +1,89 @@ +package org.hibernate.orm.test.schemaupdate.secondarytable; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.EnumSet; +import java.util.Locale; + +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.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.schema.TargetType; + +import org.hibernate.testing.orm.junit.BaseUnitTest; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.util.ServiceRegistryUtil; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +@JiraKey("HHH-18081") +@BaseUnitTest +public class SecondaryTableTest { + + private File output; + private StandardServiceRegistry ssr; + private MetadataImplementor metadata; + + @BeforeEach + public void setUp() throws IOException { + output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + ssr = ServiceRegistryUtil.serviceRegistry(); + } + + @AfterEach + public void tearsDown() { + output.delete(); + StandardServiceRegistryBuilder.destroy( ssr ); + } + + @Test + public void testSecondaryTablesAreCreated() throws Exception { + createSchema( "org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.xml" ); + assertTrue( + isTableCreated( output, "CATALOG1.SCHEMA1.SECONDARY_TABLE_1" ), + "Table SECONDARY_TABLE_1 has not been created " + ); + assertTrue( + isTableCreated( output, "SECONDARY_TABLE_2" ), + "Table SECONDARY_TABLE_2 has not been created " + ); + } + + private static boolean isTableCreated( + File output, + String tableName) throws Exception { + String[] fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase() + .split( System.lineSeparator() ); + for ( int i = 0; i < fileContent.length; i++ ) { + String statement = fileContent[i].toUpperCase( Locale.ROOT ); + if ( statement.contains( "CREATE TABLE " + tableName.toUpperCase( Locale.ROOT ) ) ) { + return true; + } + } + return false; + } + + private void createSchema(String... xmlMapping) { + final MetadataSources metadataSources = new MetadataSources( ssr ); + + for ( String xml : xmlMapping ) { + metadataSources.addResource( xml ); + } + metadata = (MetadataImplementor) metadataSources.buildMetadata(); + metadata.orderColumns( false ); + metadata.validate(); + new SchemaExport() + .setHaltOnError( true ) + .setOutputFile( output.getAbsolutePath() ) + .setFormat( false ) + .create( EnumSet.of( TargetType.SCRIPT ), metadata ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.java new file mode 100644 index 0000000000..595dedebc0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.java @@ -0,0 +1,22 @@ +package org.hibernate.orm.test.schemaupdate.secondarytable; + +public class TestEntity { + private Long id; + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.xml new file mode 100644 index 0000000000..abcc274d7c --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/secondarytable/TestEntity.xml @@ -0,0 +1,23 @@ + + + + org.hibernate.orm.test.schemaupdate.secondarytable + + + + + + + + + + + + + + \ No newline at end of file