From 09cc6d7b0013cb10006b1925e6d7ed7ffdff5138 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Sat, 4 Dec 2021 12:43:07 -0600 Subject: [PATCH] HHH-14950 - Support mapping of embeddables with no setters (assuming a custom instantiator or repo-strategy is used) Tests --- .../instantiator/intf/InstantiationTests.java | 6 ++- .../intf/TempInstantiationTests.java | 53 +++++++++++++++++++ .../intf2/InstantiationTests.java | 6 ++- .../intf2/TempInstantiationTests.java | 52 ++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/TempInstantiationTests.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/TempInstantiationTests.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/InstantiationTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/InstantiationTests.java index cdda63da27..82a0e75594 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/InstantiationTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/InstantiationTests.java @@ -28,11 +28,13 @@ import static org.assertj.core.api.Assertions.assertThat; */ @DomainModel( annotatedClasses = { Person.class, NameImpl.class } ) @SessionFactory -@FailureExpected( jiraKey = "HHH-14950" ) +@FailureExpected( jiraKey = "HHH-14950", reason = "Model has no setters, which is not supported" ) @JiraKey( "HHH-14950" ) public class InstantiationTests { - // for some reason, these tests fail a local build even though they are marked @FailureExpected + // these tests fail the build even though they are marked @FailureExpected because the + // failure happens while creating the test "fixtures" (here the boot model) which JUnit + // does not like // @Test public void modelTest(DomainModelScope scope) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/TempInstantiationTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/TempInstantiationTests.java new file mode 100644 index 0000000000..fa73f9c955 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf/TempInstantiationTests.java @@ -0,0 +1,53 @@ +/* + * 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 + */ +package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.intf; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; + +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.ServiceRegistryScope; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Creating a new version of {@link InstantiationTests} that won't fail the build, + * but still allows us to test the behavior. + * + * JUnit does not like when build fixtures (here, the SF) fails + */ +@ServiceRegistry +@FailureExpected( jiraKey = "HHH-14950", reason = "Model has no setters, which is not supported" ) +@JiraKey( "HHH-14950" ) +public class TempInstantiationTests { + @Test + public void basicTest(ServiceRegistryScope registerScope) { + final MetadataSources metadataSources = new MetadataSources( registerScope.getRegistry() ) + .addAnnotatedClass( Person.class ) + .addAnnotatedClass( Name.class ) + .addAnnotatedClass( NameImpl.class ); + + final Metadata metadata = metadataSources.buildMetadata(); + + final PersistentClass personMapping = metadata.getEntityBinding( Person.class.getName() ); + + final Property name = personMapping.getProperty( "name" ); + final Component nameMapping = (Component) name.getValue(); + assertThat( nameMapping.getPropertySpan() ).isEqualTo( 2 ); + + final Property aliases = personMapping.getProperty( "aliases" ); + final Component aliasMapping = (Component) ( (Collection) aliases.getValue() ).getElement(); + assertThat( aliasMapping.getPropertySpan() ).isEqualTo( 2 ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/InstantiationTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/InstantiationTests.java index 060d210484..79f1afe8ea 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/InstantiationTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/InstantiationTests.java @@ -31,11 +31,13 @@ import static org.assertj.core.api.Assertions.assertThat; */ @DomainModel( annotatedClasses = { Person.class, Name.class } ) @SessionFactory -@FailureExpected( jiraKey = "HHH-14950" ) +@FailureExpected( jiraKey = "HHH-14950", reason = "Model has no setters, which is not supported" ) @JiraKey( "HHH-14950" ) public class InstantiationTests { - // for some reason, these tests fail a local build even though they are marked @FailureExpected + // these tests fail the build even though they are marked @FailureExpected because the + // failure happens while creating the test "fixtures" (here the boot model) which JUnit + // does not like // @Test public void modelTest(DomainModelScope scope) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/TempInstantiationTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/TempInstantiationTests.java new file mode 100644 index 0000000000..89b4dab45f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/intf2/TempInstantiationTests.java @@ -0,0 +1,52 @@ +/* + * 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 + */ +package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.intf2; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; + +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.ServiceRegistryScope; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Creating a new version of {@link InstantiationTests} that won't fail the build, + * but still allows us to test the behavior. + * + * JUnit does not like when build fixtures (here, the SF) fails + */ +@ServiceRegistry +@FailureExpected( jiraKey = "HHH-14950", reason = "Model has no setters, which is not supported" ) +@JiraKey( "HHH-14950" ) +public class TempInstantiationTests { + @Test + public void basicTest(ServiceRegistryScope registerScope) { + final MetadataSources metadataSources = new MetadataSources( registerScope.getRegistry() ) + .addAnnotatedClass( org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.intf.Person.class ) + .addAnnotatedClass( Name.class ); + + final Metadata metadata = metadataSources.buildMetadata(); + + final PersistentClass personMapping = metadata.getEntityBinding( Person.class.getName() ); + + final Property name = personMapping.getProperty( "name" ); + final Component nameMapping = (Component) name.getValue(); + assertThat( nameMapping.getPropertySpan() ).isEqualTo( 2 ); + + final Property aliases = personMapping.getProperty( "aliases" ); + final Component aliasMapping = (Component) ( (Collection) aliases.getValue() ).getElement(); + assertThat( aliasMapping.getPropertySpan() ).isEqualTo( 2 ); + } +}