From 20bc303ca646d9c7083a05d2d86c8130dc8afeac Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 8 May 2024 13:39:38 +0200 Subject: [PATCH] HHH-18054 Add tests for XML mapping --- .../boot/model/internal/AnnotatedColumns.java | 2 +- .../column/AnotherTestEntity.java | 16 ++++ .../column/ColumnCheckConstraintTest.java | 75 ++++++++++++++++-- .../checkconstraint/column/TestEntity.java | 56 ++++++++++++++ .../table/AnotherTestEntity.java | 16 ++++ .../table/EntityWithSecondaryTables.java | 64 +++++++++++++++ .../table/TableCheckConstraintTest.java | 77 +++++++++++++++++-- .../checkconstraint/table/TestEntity.java | 16 ++++ .../checkconstraint/column/mapping.xml | 61 +++++++++++++++ .../checkconstraint/table/mapping.xml | 70 +++++++++++++++++ 10 files changed, 439 insertions(+), 14 deletions(-) create mode 100644 hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml create mode 100644 hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumns.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumns.java index 96d0e30b2e..f66829d72b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumns.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumns.java @@ -92,7 +92,7 @@ public Join getJoin() { } 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)" ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/AnotherTestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/AnotherTestEntity.java index ce3303cee1..5feec007f7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/AnotherTestEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/AnotherTestEntity.java @@ -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; + } } \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/ColumnCheckConstraintTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/ColumnCheckConstraintTest.java index 73379201d2..78673c6c2f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/ColumnCheckConstraintTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/ColumnCheckConstraintTest.java @@ -29,13 +29,13 @@ @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 void testColumnConstraintsAreApplied() throws Exception { 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 void testJoinColumConstraintsAreApplied() throws Exception { ), "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 @@ private void createSchema(Class... annotatedClasses) { .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 ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/TestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/TestEntity.java index 3567720aec..acb2489bab 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/TestEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/column/TestEntity.java @@ -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 getTestEntities() { + return testEntities; + } + + public void setTestEntities(List testEntities) { + this.testEntities = testEntities; + } + + public List getTestEntities2() { + return testEntities2; + } + + public void setTestEntities2(List testEntities2) { + this.testEntities2 = testEntities2; + } + + public Another getAnother() { + return another; + } + + public void setAnother(Another another) { + this.another = another; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/AnotherTestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/AnotherTestEntity.java index 9f0955a239..e5b53b4bf8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/AnotherTestEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/AnotherTestEntity.java @@ -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; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/EntityWithSecondaryTables.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/EntityWithSecondaryTables.java index 22e6d17688..4051f4e075 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/EntityWithSecondaryTables.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/EntityWithSecondaryTables.java @@ -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 getTestEntities() { + return testEntities; + } + + public void setTestEntities(List testEntities) { + this.testEntities = testEntities; + } + + public List getTestEntities2() { + return testEntities2; + } + + public void setTestEntities2(List testEntities2) { + this.testEntities2 = testEntities2; + } + + public Another getAnother() { + return another; + } + + public void setAnother(Another another) { + this.another = another; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TableCheckConstraintTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TableCheckConstraintTest.java index 9713f7a7f5..e99831e1c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TableCheckConstraintTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TableCheckConstraintTest.java @@ -29,13 +29,13 @@ @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 void testTableConstraintsAreApplied() throws Exception { 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 void testSecondaryTableConstraintsAreApplied() throws Exception { ), "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 void testJoinTableConstraintsAreApplied() throws Exception { ), "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 @@ private void createSchema(Class... annotatedClasses) { .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 ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TestEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TestEntity.java index aba8989659..28aeda99c2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TestEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/checkconstraint/table/TestEntity.java @@ -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; + } } diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml new file mode 100644 index 0000000000..95d96e111c --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/column/mapping.xml @@ -0,0 +1,61 @@ + + + + org.hibernate.orm.test.schemaupdate.checkconstraint.column + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml new file mode 100644 index 0000000000..d67fb90312 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/checkconstraint/table/mapping.xml @@ -0,0 +1,70 @@ + + + + org.hibernate.orm.test.schemaupdate.checkconstraint.table + +
+ +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file