HHH-2394 Added test cases

This commit is contained in:
Rob Worsnop 2012-05-20 23:27:27 -04:00 committed by Strong Liu
parent 09547a9051
commit 2aa89290f7
13 changed files with 693 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package org.hibernate.test.annotations.filter.subclass.joined;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="ZOOLOGY_ANIMAL")
@FilterDef(name="ignoreSome", parameters={@ParamDef(name="name", type="string")})
@Filter(name="ignoreSome", condition=":name <> ANIMAL_NAME")
public class Animal {
@Id
@GeneratedValue
@Column(name="ANIMAL_ID")
private Integer id;
@Column(name="ANIMAL_NAME")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,62 @@
package org.hibernate.test.annotations.filter.subclass.joined;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.FilterDefs;
import org.hibernate.annotations.Filters;
import org.hibernate.annotations.ParamDef;
@Entity
@FilterDefs({
@FilterDef(name="iqMin", parameters={@ParamDef(name="min", type="integer")}),
@FilterDef(name="pregnantMembers")})
public class Club {
@Id
@GeneratedValue
@Column(name="CLUB_ID")
private int id;
private String name;
@OneToMany(mappedBy="club")
@Filters({
@Filter(name="iqMin", table="ZOOLOGY_HUMAN", condition="HUMAN_IQ >= :min"),
@Filter(name="pregnantMembers", table="ZOOLOGY_MAMMAL", condition="IS_PREGNANT=1")
})
private Set<Human> members = new HashSet<Human>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Human> getMembers() {
return members;
}
public void setMembers(Set<Human> members) {
this.members = members;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,43 @@
package org.hibernate.test.annotations.filter.subclass.joined;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
@Entity
@Table(name="ZOOLOGY_HUMAN")
@FilterDef(name="iqRange", parameters=
{
@ParamDef(name="min", type="integer"),
@ParamDef(name="max", type="integer"),
})
@Filter(name="iqRange", condition="HUMAN_IQ BETWEEN :min AND :max")
public class Human extends Mammal {
@Column(name="HUMAN_IQ")
private int iq;
@ManyToOne
private Club club;
public int getIq() {
return iq;
}
public void setIq(int iq) {
this.iq = iq;
}
public Club getClub() {
return club;
}
public void setClub(Club club) {
this.club = club;
}
}

View File

@ -0,0 +1,127 @@
package org.hibernate.test.annotations.filter.subclass.joined;
import junit.framework.Assert;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public class JoinedSubClassTest extends BaseCoreFunctionalTestCase{
@Override
protected void afterConfigurationBuilt(Configuration configuration) {
configuration.setProperty("hibernate.show_sql", "true");
super.afterConfigurationBuilt(configuration);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class, Club.class};
}
@Override
protected void prepareTest() throws Exception {
openSession();
session.beginTransaction();
Club club = new Club();
club.setName("Mensa applicants");
club.getMembers().add(createHuman(club, false, 90));
club.getMembers().add(createHuman(club, false, 100));
club.getMembers().add(createHuman(club, true, 110));
session.persist(club);
session.getTransaction().commit();
session.close();
}
@Override
protected void cleanupTest() throws Exception {
openSession();
session.beginTransaction();
session.createQuery("delete from Human").executeUpdate();
session.createQuery("delete from Club").executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testIqFilter(){
openSession();
session.beginTransaction();
assertCount(3);
session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140);
assertCount(1);
session.getTransaction().commit();
session.close();
}
@Test
public void testPregnantFilter(){
openSession();
session.beginTransaction();
assertCount(3);
session.enableFilter("pregnantOnly");
assertCount(1);
session.getTransaction().commit();
session.close();
}
@Test
public void testNonHumanFilter(){
openSession();
session.beginTransaction();
assertCount(3);
session.enableFilter("ignoreSome").setParameter("name", "Homo Sapiens");
assertCount(0);
session.getTransaction().commit();
session.close();
}
@Test
public void testClub(){
openSession();
session.beginTransaction();
Club club = (Club) session.createQuery("from Club").uniqueResult();
Assert.assertEquals(3, club.getMembers().size());
session.clear();
session.enableFilter("pregnantMembers");
club = (Club) session.createQuery("from Club").uniqueResult();
Assert.assertEquals(1, club.getMembers().size());
session.clear();
session.enableFilter("iqMin").setParameter("min", 148);
club = (Club) session.createQuery("from Club").uniqueResult();
Assert.assertEquals(0, club.getMembers().size());
session.getTransaction().commit();
session.close();
}
private Human createHuman(Club club, boolean pregnant, int iq){
Human human = new Human();
human.setClub(club);
human.setName("Homo Sapiens");
human.setPregnant(pregnant);
human.setIq(iq);
session.persist(human);
return human;
}
private void assertCount(long expected){
long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult();
Assert.assertEquals(expected, count);
}
}

View File

@ -0,0 +1,26 @@
package org.hibernate.test.annotations.filter.subclass.joined;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
@Entity
@Table(name="ZOOLOGY_MAMMAL")
@FilterDef(name="pregnantOnly")
@Filter(name="pregnantOnly", condition="IS_PREGNANT = 1")
public class Mammal extends Animal{
@Column(name="IS_PREGNANT")
private boolean isPregnant;
public boolean isPregnant() {
return isPregnant;
}
public void setPregnant(boolean isPregnant) {
this.isPregnant = isPregnant;
}
}

View File

@ -0,0 +1,45 @@
package org.hibernate.test.annotations.filter.subclass.singletable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Table(name="ZOOLOGY_ANIMAL")
@FilterDef(name="ignoreSome", parameters={@ParamDef(name="name", type="string")})
@Filter(name="ignoreSome", condition=":name <> ANIMAL_NAME")
public class Animal {
@Id
@GeneratedValue
@Column(name="ANIMAL_ID")
private Integer id;
@Column(name="ANIMAL_NAME")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,31 @@
package org.hibernate.test.annotations.filter.subclass.singletable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
@Entity
@Table(name="ZOOLOGY_HUMAN")
@FilterDef(name="iqRange", parameters=
{
@ParamDef(name="min", type="integer"),
@ParamDef(name="max", type="integer"),
})
@Filter(name="iqRange", condition="HUMAN_IQ BETWEEN :min AND :max")
public class Human extends Mammal {
@Column(name="HUMAN_IQ")
private int iq;
public int getIq() {
return iq;
}
public void setIq(int iq) {
this.iq = iq;
}
}

View File

@ -0,0 +1,26 @@
package org.hibernate.test.annotations.filter.subclass.singletable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
@Entity
@Table(name="ZOOLOGY_MAMMAL")
@FilterDef(name="pregnantOnly")
@Filter(name="pregnantOnly", condition="IS_PREGNANT = 1")
public class Mammal extends Animal{
@Column(name="IS_PREGNANT")
private boolean isPregnant;
public boolean isPregnant() {
return isPregnant;
}
public void setPregnant(boolean isPregnant) {
this.isPregnant = isPregnant;
}
}

View File

@ -0,0 +1,93 @@
package org.hibernate.test.annotations.filter.subclass.singletable;
import junit.framework.Assert;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public class SingleTableTest extends BaseCoreFunctionalTestCase{
@Override
protected void afterConfigurationBuilt(Configuration configuration) {
configuration.setProperty("hibernate.show_sql", "true");
super.afterConfigurationBuilt(configuration);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class};
}
@Override
protected void prepareTest() throws Exception {
createHuman(false, 90);
createHuman(false, 100);
createHuman(true, 110);
}
@Override
protected void cleanupTest() throws Exception {
openSession();
session.beginTransaction();
session.createQuery("delete from Human").executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testIqFilter(){
session.beginTransaction();
assertCount(3);
session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140);
assertCount(1);
session.getTransaction().commit();
session.close();
}
@Test
public void testPregnantFilter(){
session.beginTransaction();
assertCount(3);
session.enableFilter("pregnantOnly");
assertCount(1);
session.getTransaction().commit();
session.close();
}
@Test
public void testNonHumanFilter(){
session.beginTransaction();
assertCount(3);
session.enableFilter("ignoreSome").setParameter("name", "Homo Sapiens");
assertCount(0);
session.getTransaction().commit();
session.close();
}
private void createHuman(boolean pregnant, int iq){
openSession();
session.beginTransaction();
Human human = new Human();
human.setName("Homo Sapiens");
human.setPregnant(pregnant);
human.setIq(iq);
session.persist(human);
session.getTransaction().commit();
}
private void assertCount(long expected){
long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult();
Assert.assertEquals(expected, count);
}
}

View File

@ -0,0 +1,45 @@
package org.hibernate.test.annotations.filter.subclass.tableperclass;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@Table(name="ZOOLOGY_ANIMAL")
@FilterDef(name="ignoreSome", parameters={@ParamDef(name="name", type="string")})
@Filter(name="ignoreSome", condition=":name <> ANIMAL_NAME")
public class Animal {
@Id
@GeneratedValue
@Column(name="ANIMAL_ID")
private Integer id;
@Column(name="ANIMAL_NAME")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,31 @@
package org.hibernate.test.annotations.filter.subclass.tableperclass;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
@Entity
@Table(name="ZOOLOGY_HUMAN")
@FilterDef(name="iqRange", parameters=
{
@ParamDef(name="min", type="integer"),
@ParamDef(name="max", type="integer"),
})
@Filter(name="iqRange", condition="HUMAN_IQ BETWEEN :min AND :max")
public class Human extends Mammal {
@Column(name="HUMAN_IQ")
private int iq;
public int getIq() {
return iq;
}
public void setIq(int iq) {
this.iq = iq;
}
}

View File

@ -0,0 +1,26 @@
package org.hibernate.test.annotations.filter.subclass.tableperclass;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
@Entity
@Table(name="ZOOLOGY_MAMMAL")
@FilterDef(name="pregnantOnly")
@Filter(name="pregnantOnly", condition="IS_PREGNANT = 1")
public class Mammal extends Animal{
@Column(name="IS_PREGNANT")
private boolean isPregnant;
public boolean isPregnant() {
return isPregnant;
}
public void setPregnant(boolean isPregnant) {
this.isPregnant = isPregnant;
}
}

View File

@ -0,0 +1,93 @@
package org.hibernate.test.annotations.filter.subclass.tableperclass;
import junit.framework.Assert;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public class TablePerClassTest extends BaseCoreFunctionalTestCase{
@Override
protected void afterConfigurationBuilt(Configuration configuration) {
configuration.setProperty("hibernate.show_sql", "true");
super.afterConfigurationBuilt(configuration);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class};
}
@Override
protected void prepareTest() throws Exception {
createHuman(false, 90);
createHuman(false, 100);
createHuman(true, 110);
}
@Override
protected void cleanupTest() throws Exception {
openSession();
session.beginTransaction();
session.createQuery("delete from Human").executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
public void testIqFilter(){
session.beginTransaction();
assertCount(3);
session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140);
assertCount(1);
session.getTransaction().commit();
session.close();
}
@Test
public void testPregnantFilter(){
session.beginTransaction();
assertCount(3);
session.enableFilter("pregnantOnly");
assertCount(1);
session.getTransaction().commit();
session.close();
}
@Test
public void testNonHumanFilter(){
session.beginTransaction();
assertCount(3);
session.enableFilter("ignoreSome").setParameter("name", "Homo Sapiens");
assertCount(0);
session.getTransaction().commit();
session.close();
}
private void createHuman(boolean pregnant, int iq){
openSession();
session.beginTransaction();
Human human = new Human();
human.setName("Homo Sapiens");
human.setPregnant(pregnant);
human.setIq(iq);
session.persist(human);
session.getTransaction().commit();
}
private void assertCount(long expected){
long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult();
Assert.assertEquals(expected, count);
}
}