HHH-13424 : test cases

This commit is contained in:
Gail Badner 2019-07-16 22:20:48 -07:00
parent be0ef0291c
commit ea761ec439
3 changed files with 379 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/*
* 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.jpa.test.secondarytable;
import java.util.Arrays;
import java.util.Map;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.runners.Parameterized;
public abstract class AbstractNonOptionalSecondaryTableTest extends BaseEntityManagerFunctionalTestCase {
public enum JpaComplianceCachingSetting{ DEFAULT, TRUE, FALSE };
private final JpaComplianceCachingSetting jpaComplianceCachingSetting;
@Parameterized.Parameters(name = "JpaComplianceCachingSetting={0}")
public static Iterable<Object[]> parameters() {
return Arrays.asList(
new Object[][] {
{ JpaComplianceCachingSetting.DEFAULT },
{ JpaComplianceCachingSetting.FALSE },
{ JpaComplianceCachingSetting.TRUE }
}
);
}
AbstractNonOptionalSecondaryTableTest(JpaComplianceCachingSetting jpaComplianceCachingSetting) {
this.jpaComplianceCachingSetting = jpaComplianceCachingSetting;
}
@Override
protected void addConfigOptions(Map options) {
switch ( jpaComplianceCachingSetting ) {
case DEFAULT:
// Keep the default (false)
break;
case TRUE:
options.put(
AvailableSettings.JPA_CACHING_COMPLIANCE,
Boolean.TRUE
);
break;
case FALSE:
options.put(
AvailableSettings.JPA_CACHING_COMPLIANCE,
Boolean.FALSE
);
break;
}
}
}

View File

@ -0,0 +1,142 @@
/*
* 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.jpa.test.secondarytable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.SecondaryTable;
import org.hibernate.annotations.Table;
import org.hibernate.testing.junit4.CustomParameterized;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Gail Badner
*/
@RunWith(CustomParameterized.class)
public class JoinedTableNullNonOptionalSecondaryTableTest extends AbstractNonOptionalSecondaryTableTest {
public JoinedTableNullNonOptionalSecondaryTableTest(JpaComplianceCachingSetting jpaComplianceCachingSetting) {
super( jpaComplianceCachingSetting );
}
@Test
public void testRowAddedForNullValue() {
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntity anEntity = new AnEntity( 1 );
entityManager.persist( anEntity );
}
);
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntity anEntity = entityManager.find( AnEntity.class, 1 );
assertNotNull( anEntity );
assertNull( anEntity.aDetail );
// assert that a row was inserted into Details when its property is null
assertEquals(
1,
entityManager.createNativeQuery(
"select id from Details where aDetail is null"
).getSingleResult()
);
}
);
}
@Test
public void testRowAddedForNullValueInSubclassTable() {
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntitySubclass anEntity = new AnEntitySubclass( 1 );
entityManager.persist( anEntity );
}
);
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntity anEntity = entityManager.find( AnEntity.class, 1 );
assertNotNull( anEntity );
assertNull( anEntity.aDetail );
// assert that a row was inserted into Details when its property is null
assertEquals(
1,
entityManager.createNativeQuery(
"select id from Details where aDetail is null"
).getSingleResult()
);
// assert that a row was inserted into MoreDetails when its property is null
assertEquals( 1,
entityManager.createNativeQuery(
"select id from MoreDetails where anotherDetail is null"
).getSingleResult()
);
}
);
}
@After
public void cleanupData() {
doInJPA(
this::entityManagerFactory, entityManager -> {
entityManager.createNativeQuery( "delete from Details" ).executeUpdate();
entityManager.createNativeQuery( "delete from MoreDetails" ).executeUpdate();
entityManager.createNativeQuery( "delete from AnEntitySubclass" ).executeUpdate();
entityManager.createNativeQuery( "delete from AnEntity" ).executeUpdate();
}
);
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { AnEntity.class, AnEntitySubclass.class };
}
@Entity(name = "AnEntity")
@SecondaryTable(name = "Details")
@Table(appliesTo = "Details", optional = false)
@Inheritance(strategy = InheritanceType.JOINED)
public static class AnEntity {
@Id
private int id;
@Column(name = "aDetail", table="Details")
private String aDetail;
public AnEntity() {
}
public AnEntity(int id) {
this.id = id;
}
}
@Entity(name = "AnEntitySubclass")
@SecondaryTable( name = "MoreDetails" )
@Table(appliesTo = "MoreDetails", optional = false)
public static class AnEntitySubclass extends AnEntity {
@Column(name = "anotherDetail", table="MoreDetails")
private String anotherDetail;
public AnEntitySubclass() {
}
public AnEntitySubclass(int id) {
super( id );
}
}
}

View File

@ -0,0 +1,180 @@
/*
* 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.jpa.test.secondarytable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import org.hibernate.annotations.Table;
import org.hibernate.testing.junit4.CustomParameterized;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Gail Badner
*/
@RunWith(CustomParameterized.class)
public class SingleTableNullNonOptionalSecondaryTableTest extends AbstractNonOptionalSecondaryTableTest {
public SingleTableNullNonOptionalSecondaryTableTest(JpaComplianceCachingSetting jpaComplianceCachingSetting) {
super( jpaComplianceCachingSetting );
}
@Test
public void testRowAddedForNullValue() {
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntity anEntity = new AnEntity( 1 );
entityManager.persist( anEntity );
}
);
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntity anEntity = entityManager.find( AnEntity.class, 1 );
assertNotNull( anEntity );
assertNull( anEntity.aDetail );
// assert that a row was inserted into Details when its property is null
assertEquals(
1,
entityManager.createNativeQuery(
"select id from Details where aDetail is null"
).getSingleResult()
);
}
);
}
@Test
public void testRowAddedForNullValueInSubclassTable() {
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntitySubclass anEntity = new AnEntitySubclass( 1 );
entityManager.persist( anEntity );
}
);
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntity anEntity = entityManager.find( AnEntity.class, 1 );
assertNotNull( anEntity );
assertNull( anEntity.aDetail );
// assert that a row was inserted into Details when its property is null
assertEquals(
1,
entityManager.createNativeQuery(
"select id from Details where aDetail is null"
).getSingleResult()
);
// assert that a row was inserted into MoreDetails when its property is null
assertEquals( 1,
entityManager.createNativeQuery(
"select id from MoreDetails where anotherDetail is null"
).getSingleResult()
);
}
);
}
@Test
public void testEntityWithBadDataInBaseSecondaryTableIgnored() {
// Not sure we really want to support this;
// It only works with single-table inheritance.
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntitySubclass anEntity = new AnEntitySubclass( 1 );
entityManager.persist( anEntity );
}
);
doInJPA(
this::entityManagerFactory, entityManager -> {
// Delete the data in a secondary table
entityManager.createNativeQuery( "delete from Details where id = 1" ).executeUpdate();
// The entity with bad data should be ignored.
AnEntitySubclass anEntity = entityManager.find( AnEntitySubclass.class, 1 );
assertNull( anEntity );
}
);
}
@Test
public void testEntityWithBadDataInSubclassSecondaryTableIgnored() {
doInJPA(
this::entityManagerFactory, entityManager -> {
AnEntitySubclass anEntity = new AnEntitySubclass( 1 );
entityManager.persist( anEntity );
}
);
doInJPA(
this::entityManagerFactory, entityManager -> {
// Delete the data in a secondary table
entityManager.createNativeQuery( "delete from MoreDetails where id = 1" ).executeUpdate();
// The entity with bad data should be ignored.
AnEntitySubclass anEntity = entityManager.find( AnEntitySubclass.class, 1 );
assertNull( anEntity );
}
);
}
@After
public void cleanupData() {
doInJPA(
this::entityManagerFactory, entityManager -> {
entityManager.createNativeQuery( "delete from Details" ).executeUpdate();
entityManager.createNativeQuery( "delete from MoreDetails" ).executeUpdate();
entityManager.createNativeQuery( "delete from AnEntity" ).executeUpdate();
}
);
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { AnEntity.class, AnEntitySubclass.class };
}
@Entity(name = "AnEntity")
@SecondaryTable(name = "Details")
@Table(appliesTo = "Details", optional = false)
public static class AnEntity {
@Id
private int id;
@Column(name = "aDetail", table="Details")
private String aDetail;
public AnEntity() {
}
public AnEntity(int id) {
this.id = id;
}
}
@Entity(name = "AnEntitySubclass")
@SecondaryTable( name = "MoreDetails" )
@Table(appliesTo = "MoreDetails", optional = false)
public static class AnEntitySubclass extends AnEntity {
@Column(name = "anotherDetail", table="MoreDetails")
private String anotherDetail;
public AnEntitySubclass() {
}
public AnEntitySubclass(int id) {
super( id );
}
}
}