HHH-18054 Add tests for XML mapping

This commit is contained in:
Andrea Boriero 2024-05-08 13:39:38 +02:00 committed by Steve Ebersole
parent ff07248944
commit 20bc303ca6
10 changed files with 439 additions and 14 deletions

View File

@ -92,7 +92,7 @@ public class AnnotatedColumns {
}
if ( join == null ) {
throw new AnnotationException(
"Secondary table '" + explicitTableName + "' for property '" + getPropertyHolder().getClassName()
"Secondary table '" + explicitTableName + "' for property '" + propertyName + "' of entity'" + getPropertyHolder().getClassName()
+ "' is not declared (use '@SecondaryTable' to declare the secondary table)"
);
}

View File

@ -13,4 +13,20 @@ public class AnotherTestEntity implements Another {
@Column(name = "FIRST_NAME")
private String firstName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}

View File

@ -29,13 +29,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@JiraKey("HHH-18054")
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsColumnCheck.class)
public class ColumnCheckConstraintTest {
static final String COLUMN_CONSTRAINTS = "name_column <> null";
static final String COLUMN_CONSTRAINTS = "name_column is not null";
static final String ONE_TO_ONE_JOIN_COLUMN_CONSTRAINTS = "ID <> null";
static final String ONE_TO_MANY_JOIN_COLUMN_CONSTRAINTS = "ID > 2";
static final String MANY_TO_ONE_JOIN_COLUMN_CONSTRAINTS = "ID > 3 ";
static final String MANY_TO_MANY_JOIN_COLUMN_CONSTRAINTS = "ID > 4";
static final String MANY_TO_MANY_INVERSE_JOIN_COLUMN_CONSTRAINTS = "ID > 5";
static final String ONE_TO_ONE_JOIN_COLUMN_CONSTRAINTS = "ID is not null";
static final String ONE_TO_MANY_JOIN_COLUMN_CONSTRAINTS = "ID = 2";
static final String MANY_TO_ONE_JOIN_COLUMN_CONSTRAINTS = "ID = 3";
static final String MANY_TO_MANY_JOIN_COLUMN_CONSTRAINTS = "ID = 4";
static final String MANY_TO_MANY_INVERSE_JOIN_COLUMN_CONSTRAINTS = "ID = 5";
static final String ANY_JOIN_COLUMN_CONSTRAINTS = "ID > 5";
static final String ONE_TO_ONE_JOIN_COLUMN_NAME = "ONE_TO_ONE_JOIN_COLUMN_NAME";
@ -69,6 +69,14 @@ public class ColumnCheckConstraintTest {
assertThat( fileContent.toUpperCase( Locale.ROOT ) ).contains( COLUMN_CONSTRAINTS.toUpperCase( Locale.ROOT ) );
}
@Test
public void testXmlMappingColumnConstraintsAreApplied() throws Exception {
createSchema( "org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml" );
String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.replace( System.lineSeparator(), "" );
assertThat( fileContent.toUpperCase( Locale.ROOT ) ).contains( COLUMN_CONSTRAINTS.toUpperCase( Locale.ROOT ) );
}
@Test
public void testJoinColumConstraintsAreApplied() throws Exception {
createSchema( TestEntity.class, AnotherTestEntity.class );
@ -91,8 +99,47 @@ public class ColumnCheckConstraintTest {
), "Check Constraints on OneToOne join table have not been created" );
}
@Test
public void testXmlMappingJoinColumConstraintsAreApplied() throws Exception {
createSchema( "org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml" );
String[] fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.split( System.lineSeparator() );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
"TEST_ENTITY",
MANY_TO_ONE_JOIN_COLUMN_CONSTRAINTS
), "Check Constraints on ManyToOne join table have not been created" );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
"TEST_ENTITY",
ONE_TO_ONE_JOIN_COLUMN_CONSTRAINTS
), "Check Constraints on OneToOne join table have not been created" );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
"ANOTHER_TEST_ENTITY",
ONE_TO_MANY_JOIN_COLUMN_CONSTRAINTS
), "Check Constraints on OneToOne join table have not been created" );
}
@Test
public void testJoinColumOfJoinTableConstraintsAreApplied() throws Exception {
createSchema( "org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml" );
String[] fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.split( System.lineSeparator() );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
"MANY_T0_MANY_TABLE",
MANY_TO_MANY_JOIN_COLUMN_CONSTRAINTS
), "Join column Check Constraints on ManyToMany join table have not been created" );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
"MANY_T0_MANY_TABLE",
MANY_TO_MANY_INVERSE_JOIN_COLUMN_CONSTRAINTS
), "Inverse join column Check Constraints on ManyToMany join table have not been created" );
}
@Test
public void testXmlMappingJoinColumOfJoinTableConstraintsAreApplied() throws Exception {
createSchema( TestEntity.class, AnotherTestEntity.class );
String[] fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.split( System.lineSeparator() );
@ -150,4 +197,20 @@ public class ColumnCheckConstraintTest {
.setFormat( false )
.create( EnumSet.of( TargetType.SCRIPT ), metadata );
}
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

@ -117,4 +117,60 @@ public class TestEntity {
)
})
private Another another;
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;
}
public AnotherTestEntity getEntity() {
return entity;
}
public void setEntity(AnotherTestEntity entity) {
this.entity = entity;
}
public AnotherTestEntity getTestEntity() {
return testEntity;
}
public void setTestEntity(AnotherTestEntity testEntity) {
this.testEntity = testEntity;
}
public List<AnotherTestEntity> getTestEntities() {
return testEntities;
}
public void setTestEntities(List<AnotherTestEntity> testEntities) {
this.testEntities = testEntities;
}
public List<AnotherTestEntity> getTestEntities2() {
return testEntities2;
}
public void setTestEntities2(List<AnotherTestEntity> testEntities2) {
this.testEntities2 = testEntities2;
}
public Another getAnother() {
return another;
}
public void setAnother(Another another) {
this.another = another;
}
}

View File

@ -11,4 +11,20 @@ public class AnotherTestEntity implements Another {
@Column(name = "FIRST_NAME")
private String firstName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}

View File

@ -107,5 +107,69 @@ public class EntityWithSecondaryTables {
}
)
private Another another;
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;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public AnotherTestEntity getEntity() {
return entity;
}
public void setEntity(AnotherTestEntity entity) {
this.entity = entity;
}
public AnotherTestEntity getTestEntity() {
return testEntity;
}
public void setTestEntity(AnotherTestEntity testEntity) {
this.testEntity = testEntity;
}
public List<AnotherTestEntity> getTestEntities() {
return testEntities;
}
public void setTestEntities(List<AnotherTestEntity> testEntities) {
this.testEntities = testEntities;
}
public List<AnotherTestEntity> getTestEntities2() {
return testEntities2;
}
public void setTestEntities2(List<AnotherTestEntity> testEntities2) {
this.testEntities2 = testEntities2;
}
public Another getAnother() {
return another;
}
public void setAnother(Another another) {
this.another = another;
}
}

View File

@ -29,13 +29,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@JiraKey("HHH-18054")
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsColumnCheck.class)
public class TableCheckConstraintTest {
static final String CONSTRAINTS = "NAME_COLUMN <> null";
static final String SECONDARY_TABLE_CONSTRAINTS = "SECOND_NAME <> null";
static final String ONE_TO_ONE_JOIN_TABLE_CONSTRAINTS = "ID <> null";
static final String ONE_TO_MANY_JOIN_TABLE_CONSTRAINTS = "ID > 2";
static final String MANY_TO_ONE_JOIN_TABLE_CONSTRAINTS = "ID > 3 ";
static final String MANY_TO_MANY_JOIN_TABLE_CONSTRAINTS = "ID > 4";
static final String ANY_JOIN_TABLE_CONSTRAINTS = "ID > 5";
static final String CONSTRAINTS = "NAME_COLUMN is not null";
static final String SECONDARY_TABLE_CONSTRAINTS = "SECOND_NAME is not null";
static final String ONE_TO_ONE_JOIN_TABLE_CONSTRAINTS = "ID is not null";
static final String ONE_TO_MANY_JOIN_TABLE_CONSTRAINTS = "ID = 2";
static final String MANY_TO_ONE_JOIN_TABLE_CONSTRAINTS = "ID = 3";
static final String MANY_TO_MANY_JOIN_TABLE_CONSTRAINTS = "ID = 4";
static final String ANY_JOIN_TABLE_CONSTRAINTS = "ID = 5";
static final String SECONDARY_TABLE_NAME = "SECOND_TABLE_NAME";
static final String ONE_TO_ONE_JOIN_TABLE_NAME = "ONE_TO_ONE_JOIN_TABLE_NAME";
@ -69,6 +69,14 @@ public class TableCheckConstraintTest {
assertThat( fileContent.toUpperCase( Locale.ROOT ) ).contains( CONSTRAINTS.toUpperCase( Locale.ROOT ) );
}
@Test
public void testXmlMappingTableConstraintsAreApplied() throws Exception {
createSchema( "org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml" );
String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.replace( System.lineSeparator(), "" );
assertThat( fileContent.toUpperCase( Locale.ROOT ) ).contains( CONSTRAINTS.toUpperCase( Locale.ROOT ) );
}
@Test
public void testSecondaryTableConstraintsAreApplied() throws Exception {
createSchema( EntityWithSecondaryTables.class, AnotherTestEntity.class );
@ -81,6 +89,18 @@ public class TableCheckConstraintTest {
), "Check Constraints on secondary table have not been created" );
}
@Test
public void testXmlMappingSecondaryTableConstraintsAreApplied() throws Exception {
createSchema( "org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml" );
String[] fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.split( System.lineSeparator() );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
SECONDARY_TABLE_NAME,
SECONDARY_TABLE_CONSTRAINTS
), "Check Constraints on secondary table have not been created" );
}
@Test
public void testJoinTableConstraintsAreApplied() throws Exception {
createSchema( EntityWithSecondaryTables.class, AnotherTestEntity.class );
@ -108,6 +128,33 @@ public class TableCheckConstraintTest {
), "Check Constraints on OneToOne join table have not been created" );
}
@Test
public void testXmlMappingJoinTableConstraintsAreApplied() throws Exception {
createSchema( "org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml" );
String[] fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.split( System.lineSeparator() );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
MANY_TO_ONE_JOIN_TABLE_NAME,
MANY_TO_ONE_JOIN_TABLE_CONSTRAINTS
), "Check Constraints on ManyToOne join table have not been created" );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
MANY_TO_MANY_JOIN_TABLE_NAME,
MANY_TO_MANY_JOIN_TABLE_CONSTRAINTS
), "Check Constraints on ManyToMany join table have not been created" );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
ONE_TO_ONE_JOIN_TABLE_NAME,
ONE_TO_ONE_JOIN_TABLE_CONSTRAINTS
), "Check Constraints on OneToOne join table have not been created" );
assertTrue( tableCreationStatementContainsConstraints(
fileContent,
ONE_TO_MANY_JOIN_TABLE_NAME,
ONE_TO_MANY_JOIN_TABLE_CONSTRAINTS
), "Check Constraints on OneToOne join table have not been created" );
}
@Test
public void testAnyJoinTableConstraintsAreApplied() throws Exception {
createSchema( EntityWithSecondaryTables.class, AnotherTestEntity.class );
@ -150,4 +197,20 @@ public class TableCheckConstraintTest {
.setFormat( false )
.create( EnumSet.of( TargetType.SCRIPT ), metadata );
}
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

@ -22,4 +22,20 @@ public class TestEntity {
@Column(name = "NAME_COLUMN")
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,61 @@
<?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.checkconstraint.column</package>
<entity class="TestEntity" metadata-complete="true">
<table name="TEST_ENTITY"/>
<attributes>
<id name="id"/>
<basic name="name">
<column name="NAME_COLUMN">
<check-constraint name="column_constraint" constraint="name_column is not null" options="enforced"/>
</column>
</basic>
<basic name="name" >
<column name="NAME_COLUMN" />
</basic>
<many-to-one name="testEntity">
<join-column name="MANY_TO_ONE_JOIN_COLUMN_NAME">
<check-constraint name="MANY_TO_ONE_JOIN_COLUMN_CONSTRAINT" constraint="ID = 3"/>
</join-column>
</many-to-one>
<one-to-many name="testEntities">
<join-column name="ONE_TO_MAIN_JOIN_COLUMN_NAME">
<check-constraint name="ONE_TO_MANY_JOIN_COLUMN_CONSTRAINT" constraint="ID = 2"/>
</join-column>
</one-to-many>
<one-to-one name="entity">
<join-column name="ONE_TO_ONE_JOIN_COLUMN_NAME">
<check-constraint name="ONE_TO_ONE_JOIN_COLUMN_CONSTRAINT" constraint="ID is not null"/>
</join-column>
</one-to-one>
<many-to-many name="testEntities2">
<join-table name="MANY_T0_MANY_TABLE">
<join-column name="MANY_TO_MANY_JOIN_COLUMN_NAME">
<check-constraint name="MANY_TO_MANY_JOIN_COLUMN_CONSTRAINT" constraint="ID = 4"/>
</join-column>
<inverse-join-column name="MANY_TO_MANY_INVERSE_JOIN_COLUMN_NAME">
<check-constraint name="MANY_TO_MANY_INVERSE_JOIN_COLUMN_CONSTRAINT" constraint="ID = 5"/>
</inverse-join-column>
</join-table>
</many-to-many>
<transient name="another"/>
</attributes>
</entity>
<entity class="AnotherTestEntity" metadata-complete="true">
<table name="ANOTHER_TEST_ENTITY"/>
<attributes>
<id name="id"/>
<basic name="firstName">
<column name="FIRST_NAME"/>
</basic>
</attributes>
</entity>
</entity-mappings>

View File

@ -0,0 +1,70 @@
<?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.checkconstraint.table</package>
<entity class="TestEntity" metadata-complete="true">
<table name="TEST_ENTITY_TABLE">
<check-constraint name="TEST_ENTITY_TABLE" constraint="NAME_COLUMN is not null"/>
</table>
<attributes>
<id name="id"/>
<basic name="name">
<column name="NAME_COLUMN"/>
</basic>
<basic name="name" >
<column name="NAME_COLUMN" />
</basic>
</attributes>
</entity>
<entity class="EntityWithSecondaryTables" metadata-complete="true">
<secondary-table name="SECOND_TABLE_NAME">
<check-constraint name="TABLE_CONSTRAINT" constraint="SECOND_NAME is not null"/>
</secondary-table>
<attributes>
<id name="id"/>
<basic name="name">
<column name="NAME_COLUMN"/>
</basic>
<basic name="secondName" >
<column name="SECOND_NAME" table="SECOND_TABLE_NAME" />
</basic>
<many-to-one name="testEntity">
<join-table name="MANY_TO_ONE_JOIN_TABLE_NAME">
<check-constraint name="MANY_TO_ONE_JOIN_TABLE_CONSTRAINT" constraint="ID = 3"/>
</join-table>
</many-to-one>
<one-to-many name="testEntities">
<join-table name="ONE_TO_MAIN_JOIN_TABLE_NAME">
<check-constraint name="ONE_TO_MANY_JOIN_TABLE_CONSTRAINT" constraint="ID = 2"/>
</join-table>
</one-to-many>
<one-to-one name="entity">
<join-table name="ONE_TO_ONE_JOIN_TABLE_NAME">
<check-constraint name="ONE_TO_ONE_JOIN_TABLE_CONSTRAINT" constraint="ID is not null"/>
</join-table>
</one-to-one>
<many-to-many name="testEntities2">
<join-table name="MANY_TO_MANY_JOIN_TABLE_NAME">
<check-constraint name="MANY_TO_MANY_JOIN_TABLE_CONSTRAINT" constraint="ID = 4"/>
</join-table>
</many-to-many>
<transient name="another"/>
</attributes>
</entity>
<entity class="AnotherTestEntity" metadata-complete="true">
<attributes>
<id name="id"/>
<basic name="firstName">
<column name="FIRST_NAME"/>
</basic>
</attributes>
</entity>
</entity-mappings>