From 4b253dfad7980f3316e09fd26b6ae50fde25872b Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 15 Jun 2016 16:46:04 -0700 Subject: [PATCH] HHH-10360 : Added test cases (cherry picked from commit 67c7858dd53aa0af39c9caed46d28910154a98db) --- .../cid/CompositeIdAndNaturalIdTest.java | 12 ++++ .../ImmutableEntityNaturalIdTest.java | 16 ++++- .../immutable/ImmutableNaturalIdTest.java | 12 ++++ .../ImmutableEntityNaturalIdTest.java | 14 +++++ .../inheritance/InheritedNaturalIdTest.java | 13 ++++ .../mutable/MutableNaturalIdTest.java | 14 +++++ .../nullable/NullableNaturalIdTest.java | 45 ++++++++++++++ .../test/naturalid/nullable/User.hbm.xml | 34 +++++++++++ .../test/naturalid/nullable/User.java | 61 +++++++++++++++++++ 9 files changed, 220 insertions(+), 1 deletion(-) create mode 100755 hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.hbm.xml create mode 100755 hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/cid/CompositeIdAndNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/cid/CompositeIdAndNaturalIdTest.java index 244410fb2e..dfc29f90d8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/cid/CompositeIdAndNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/cid/CompositeIdAndNaturalIdTest.java @@ -11,9 +11,12 @@ import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.criterion.Restrictions; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; /** @@ -31,6 +34,15 @@ public class CompositeIdAndNaturalIdTest extends BaseCoreFunctionalTestCase { cfg.setProperty( Environment.GENERATE_STATISTICS, "false" ); } + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + final EntityPersister persister = sessionFactory().getEntityPersister( Account.class.getName() ); + final int propertyIndex = persister.getEntityMetamodel().getPropertyIndex( "shortCode" ); + // the natural ID mapped as non-nullable + assertFalse( persister.getPropertyNullability()[propertyIndex] ); + } + @Test public void testSave() { // prepare some test data... diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java index 00068a5fca..ddee2a07f0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableEntityNaturalIdTest.java @@ -11,14 +11,17 @@ import java.lang.reflect.Field; import org.junit.Test; import org.hibernate.FetchMode; -import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.criterion.Restrictions; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.tuple.entity.EntityMetamodel; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; /** @@ -35,6 +38,17 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase { cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); } + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + final EntityPersister persister = sessionFactory().getEntityPersister( Child.class.getName() ); + // nullability is not specified for either properties making up + // the natural ID, so they should be non-nullable by hbm-specific default + final EntityMetamodel entityMetamodel = persister.getEntityMetamodel(); + assertFalse( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "parent" )] ); + assertFalse( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "name" )] ); + } + @Test public void testNaturalIdCheck() throws Exception { Session s = openSession(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java index bd93169f0b..70c30919d2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java @@ -14,9 +14,12 @@ import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.criterion.Restrictions; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; @@ -35,6 +38,15 @@ public class ImmutableNaturalIdTest extends BaseCoreFunctionalTestCase { cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); } + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + final EntityPersister persister = sessionFactory().getEntityPersister( User.class.getName() ); + final int propertyIndex = persister.getEntityMetamodel().getPropertyIndex( "userName" ); + // nullability is not specified, so it should be non-nullable by hbm-specific default + assertFalse( persister.getPropertyNullability()[propertyIndex] ); + } + @Test public void testUpdate() { // prepare some test data... diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java index f0bfca4a2f..9c93b1aa11 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/immutableentity/ImmutableEntityNaturalIdTest.java @@ -12,10 +12,13 @@ import org.hibernate.Transaction; import org.hibernate.annotations.Immutable; import org.hibernate.cfg.Configuration; import org.hibernate.metadata.ClassMetadata; +import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.Statistics; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.tuple.entity.EntityMetamodel; + import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -44,6 +47,17 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase { assertEquals( "Wrong number of elements", 3, propertiesIndex.length ); } + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + final EntityPersister persister = sessionFactory().getEntityPersister( Building.class.getName() ); + final EntityMetamodel entityMetamodel = persister.getEntityMetamodel(); + // nullability is not specified, so they should be nullable by annotations-specific default + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "address" )] ); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "city" )] ); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "state" )] ); + } + @Test public void testImmutableNaturalIdLifecycle() { Statistics stats = sessionFactory().getStatistics(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/inheritance/InheritedNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/inheritance/InheritedNaturalIdTest.java index 34fdf6a967..92a23305ad 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/inheritance/InheritedNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/inheritance/InheritedNaturalIdTest.java @@ -9,11 +9,15 @@ package org.hibernate.test.naturalid.inheritance; import org.junit.Test; import org.hibernate.Session; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.tuple.entity.EntityMetamodel; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; /** * @author Steve Ebersole @@ -24,6 +28,15 @@ public class InheritedNaturalIdTest extends BaseCoreFunctionalTestCase { return new Class[] { Principal.class, User.class }; } + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + final EntityPersister persister = sessionFactory().getEntityPersister( User.class.getName() ); + final EntityMetamodel entityMetamodel = persister.getEntityMetamodel(); + // nullability is not specified, so it should be nullable by annotations-specific default + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "uid" )] ); + } + @Test public void testIt() { Session s = openSession(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java index 8236d9f323..060ae6fb7e 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java @@ -17,9 +17,13 @@ import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.criterion.Restrictions; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.tuple.entity.EntityMetamodel; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; @@ -40,6 +44,16 @@ public class MutableNaturalIdTest extends BaseCoreFunctionalTestCase { cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); } + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + final EntityPersister persister = sessionFactory().getEntityPersister( User.class.getName() ); + final EntityMetamodel entityMetamodel = persister.getEntityMetamodel(); + // nullability is not specified, so it should be non-nullable by hbm-specific default + assertFalse( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "name" )] ); + assertFalse( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "org" )] ); + } + @Test public void testCacheSynchronizationOnMutation() { Session s = openSession(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/NullableNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/NullableNaturalIdTest.java index 7401b25c5f..cea816331f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/NullableNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/NullableNaturalIdTest.java @@ -9,10 +9,15 @@ package org.hibernate.test.naturalid.nullable; import org.junit.Test; import org.hibernate.Session; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.tuple.entity.EntityMetamodel; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; /** * @author Steve Ebersole @@ -23,6 +28,46 @@ public class NullableNaturalIdTest extends BaseCoreFunctionalTestCase { return new Class[] { A.class, B.class, C.class, D.class }; } + @Override + public String[] getMappings() { + return new String[] { "naturalid/nullable/User.hbm.xml" }; + } + + @Test + @TestForIssue( jiraKey = "HHH-10360") + public void testNaturalIdNullability() { + // A, B, C, and D are mapped using annotations; + // none are mapped to be non-nullable, so all are nullable by annotations-specific default, + // except primitives + EntityPersister persister = sessionFactory().getEntityPersister( A.class.getName() ); + EntityMetamodel entityMetamodel = persister.getEntityMetamodel(); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "assC" )] ); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "myname" )] ); + + persister = sessionFactory().getEntityPersister( B.class.getName() ); + entityMetamodel = persister.getEntityMetamodel(); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "assA" )] ); + // naturalid is a primitive, so it is non-nullable + assertFalse( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "naturalid" )] ); + + persister = sessionFactory().getEntityPersister( C.class.getName() ); + entityMetamodel = persister.getEntityMetamodel(); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "name" )] ); + + persister = sessionFactory().getEntityPersister( D.class.getName() ); + entityMetamodel = persister.getEntityMetamodel(); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "name" )] ); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "associatedC" )] ); + + // User is mapped using hbm.xml; properties are explicitly mapped to be nullable + persister = sessionFactory().getEntityPersister( User.class.getName() ); + entityMetamodel = persister.getEntityMetamodel(); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "name" )] ); + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "org" )] ); + // intVal is a primitive; hbm.xml apparently allows primitive to be nullable + assertTrue( persister.getPropertyNullability()[entityMetamodel.getPropertyIndex( "intVal" )] ); + } + @Test public void testNaturalIdNullValueOnPersist() { Session session = openSession(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.hbm.xml new file mode 100755 index 0000000000..cd5fe4be08 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.hbm.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.java b/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.java new file mode 100755 index 0000000000..c73ba3521e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/naturalid/nullable/User.java @@ -0,0 +1,61 @@ +/* + * 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 . + */ + +//$Id: User.java 6900 2005-05-25 01:24:22Z oneovthafew $ +package org.hibernate.test.naturalid.nullable; + + +/** + * @author Gavin King + */ +public class User { + private Long id; + private String name; + private String org; + private String password; + private int intVal; + + User() {} + + public User(String name, String org, String password) { + this.name = name; + this.org = org; + this.password = password; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOrg() { + return org; + } + + public void setOrg(String org) { + this.org = org; + } + + public void setPassword(String password) { + this.password = password; + } + + public int getIntVal() { + return intVal; + } + + public void setIntVal(int intVal) { + this.intVal = intVal; + } +}