HHH-10360 : Added test cases

(cherry picked from commit 67c7858dd5)
This commit is contained in:
Gail Badner 2016-06-15 16:46:04 -07:00
parent fbe730abf1
commit e72ff7687c
9 changed files with 220 additions and 1 deletions

View File

@ -11,9 +11,12 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Restrictions; 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.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
/** /**
@ -31,6 +34,15 @@ public class CompositeIdAndNaturalIdTest extends BaseCoreFunctionalTestCase {
cfg.setProperty( Environment.GENERATE_STATISTICS, "false" ); 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 @Test
public void testSave() { public void testSave() {
// prepare some test data... // prepare some test data...

View File

@ -11,14 +11,17 @@ import java.lang.reflect.Field;
import org.junit.Test; import org.junit.Test;
import org.hibernate.FetchMode; import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Restrictions; 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.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.tuple.entity.EntityMetamodel;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -35,6 +38,17 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); 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 @Test
public void testNaturalIdCheck() throws Exception { public void testNaturalIdCheck() throws Exception {
Session s = openSession(); Session s = openSession();

View File

@ -14,9 +14,12 @@ import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Restrictions; 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.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -35,6 +38,15 @@ public class ImmutableNaturalIdTest extends BaseCoreFunctionalTestCase {
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); 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 @Test
public void testUpdate() { public void testUpdate() {
// prepare some test data... // prepare some test data...

View File

@ -12,10 +12,13 @@ import org.hibernate.Transaction;
import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Immutable;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata; import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.stat.Statistics; import org.hibernate.stat.Statistics;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -44,6 +47,17 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase {
assertEquals( "Wrong number of elements", 3, propertiesIndex.length ); 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 @Test
public void testImmutableNaturalIdLifecycle() { public void testImmutableNaturalIdLifecycle() {
Statistics stats = sessionFactory().getStatistics(); Statistics stats = sessionFactory().getStatistics();

View File

@ -9,11 +9,15 @@ package org.hibernate.test.naturalid.inheritance;
import org.junit.Test; import org.junit.Test;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.tuple.entity.EntityMetamodel;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -24,6 +28,15 @@ public class InheritedNaturalIdTest extends BaseCoreFunctionalTestCase {
return new Class[] { Principal.class, User.class }; 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 @Test
public void testIt() { public void testIt() {
Session s = openSession(); Session s = openSession();

View File

@ -17,9 +17,13 @@ import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Restrictions; 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.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.tuple.entity.EntityMetamodel;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
@ -40,6 +44,16 @@ public class MutableNaturalIdTest extends BaseCoreFunctionalTestCase {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); 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 @Test
public void testCacheSynchronizationOnMutation() { public void testCacheSynchronizationOnMutation() {
Session s = openSession(); Session s = openSession();

View File

@ -9,10 +9,15 @@ package org.hibernate.test.naturalid.nullable;
import org.junit.Test; import org.junit.Test;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; 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.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -23,6 +28,46 @@ public class NullableNaturalIdTest extends BaseCoreFunctionalTestCase {
return new Class[] { A.class, B.class, C.class, D.class }; 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 @Test
public void testNaturalIdNullValueOnPersist() { public void testNaturalIdNullValueOnPersist() {
Session session = openSession(); Session session = openSession();

View File

@ -0,0 +1,34 @@
<?xml version="1.0"?>
<!--
~ 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>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping illustrates use of <natural-id mutable="true"/>
-->
<hibernate-mapping
package="org.hibernate.test.naturalid"
default-access="field">
<class name="org.hibernate.test.naturalid.nullable.User" table="SystemUserInfo">
<id name="id">
<generator class="increment"/>
</id>
<natural-id mutable="true">
<property name="name" not-null="false"/>
<property name="org" not-null="false"/>
<property name="intVal" not-null="false"/>
</natural-id>
<property name="password" column="`password`"/>
</class>
</hibernate-mapping>

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
//$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;
}
}