HHH-18081 Add test for issue

This commit is contained in:
Andrea Boriero 2024-05-07 17:53:44 +02:00 committed by Steve Ebersole
parent 3e56c77092
commit a594a8c9b7
3 changed files with 134 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping" version="3.2">
<package>org.hibernate.orm.test.schemaupdate.secondarytable</package>
<entity class="TestEntity" metadata-complete="true">
<secondary-table name="SECONDARY_TABLE_1" options="option 1" catalog="catalog1" schema="schema1" owned="false"/>
<secondary-table name="SECONDARY_TABLE_2" optional="true">
<primary-key-join-column name="pk_jc"></primary-key-join-column>
<index column-list="name asc"/>
</secondary-table>
<attributes>
<id name="id"/>
<basic name="name">
<column name="NAME_COLUMN" table="SECONDARY_TABLE_2"/>
</basic>
</attributes>
</entity>
</entity-mappings>