HHH-17550 Add test for issue

This commit is contained in:
Marco Belladelli 2023-12-20 11:58:34 +01:00 committed by Christian Beikov
parent f8d84f9f7f
commit d7e3217494
1 changed files with 47 additions and 26 deletions

View File

@ -6,69 +6,90 @@
*/ */
package org.hibernate.orm.test.foreignkeys.disabled; package org.hibernate.orm.test.foreignkeys.disabled;
import static org.hamcrest.core.Is.is; import java.util.List;
import static org.junit.Assert.assertThat;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.mapping.Table; import org.hibernate.mapping.Table;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.util.ServiceRegistryUtil; import org.hibernate.testing.util.ServiceRegistryUtil;
import org.junit.Test; import org.junit.Test;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/** /**
* @author Yanming Zhou * @author Yanming Zhou
*/ */
@Jira( "https://hibernate.atlassian.net/browse/HHH-14253" )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17550" )
public class DefaultConstraintModeTest extends BaseUnitTestCase { public class DefaultConstraintModeTest extends BaseUnitTestCase {
private static final String TABLE_NAME = "TestEntity";
@Test @Test
@TestForIssue(jiraKey = "HHH-14253")
public void testForeignKeyShouldNotBeCreated() { public void testForeignKeyShouldNotBeCreated() {
testForeignKeyCreation(false); testForeignKeyCreation( false );
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-14253")
public void testForeignKeyShouldBeCreated() { public void testForeignKeyShouldBeCreated() {
testForeignKeyCreation(true); testForeignKeyCreation( true );
} }
private void testForeignKeyCreation(boolean created) { private void testForeignKeyCreation(boolean created) {
try (StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder() try (StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder()
.applySetting(Environment.HBM2DDL_DEFAULT_CONSTRAINT_MODE, created ? "CONSTRAINT" : "NO_CONSTRAINT").build()) { .applySetting( Environment.HBM2DDL_DEFAULT_CONSTRAINT_MODE, created ? "CONSTRAINT" : "NO_CONSTRAINT" )
Metadata metadata = new MetadataSources( ssr ).addAnnotatedClass( TestEntity.class ).buildMetadata(); .build()) {
assertThat( findTable( metadata, TABLE_NAME ).getForeignKeys().isEmpty(), is( !created ) ); Metadata metadata = new MetadataSources( ssr ).addAnnotatedClasses( TestEntity.class, ChildEntity.class ).buildMetadata();
assertThat( findTable( metadata, "TestEntity" ).getForeignKeys().isEmpty(), is( !created ) );
assertThat( findTable( metadata, "ChildEntity" ).getForeignKeys().isEmpty(), is( !created ) );
} }
} }
private static Table findTable(Metadata metadata, String tableName) { private static Table findTable(Metadata metadata, String tableName) {
return StreamSupport.stream(metadata.getDatabase().getNamespaces().spliterator(), false) return StreamSupport.stream( metadata.getDatabase().getNamespaces().spliterator(), false )
.flatMap(namespace -> namespace.getTables().stream()).filter(t -> t.getName().equals(tableName)) .flatMap( namespace -> namespace.getTables().stream() ).filter( t -> t.getName().equals( tableName ) )
.findFirst().orElse(null); .findFirst().orElse( null );
} }
@Entity @Entity( name = "TestEntity" )
@jakarta.persistence.Table(name = TABLE_NAME) @jakarta.persistence.Table( name = "TestEntity" )
@Inheritance( strategy = InheritanceType.JOINED )
public static class TestEntity { public static class TestEntity {
@Id @Id
private Long id; private Long id;
@ManyToOne @ManyToOne
@JoinColumn private TestEntity toOne;
private TestEntity mate;
@OneToMany
@JoinColumn
private List<ChildEntity> oneToMany;
@ManyToMany
private List<ChildEntity> manyToMany;
@ElementCollection
private List<String> elements;
}
@Entity( name = "ChildEntity" )
@jakarta.persistence.Table( name = "ChildEntity" )
public static class ChildEntity extends TestEntity {
private String childName;
} }
} }