HHH-2394 refactored unit tests

This commit is contained in:
Rob Worsnop 2012-05-21 09:44:37 -04:00 committed by Strong Liu
parent 2aa89290f7
commit dc4e87340d
4 changed files with 96 additions and 203 deletions

View File

@ -0,0 +1,78 @@
package org.hibernate.test.annotations.filter.subclass;
import junit.framework.Assert;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public abstract class SubClassTest extends BaseCoreFunctionalTestCase{
@Override
protected void prepareTest() throws Exception {
openSession();
session.beginTransaction();
persistTestData();
session.getTransaction().commit();
session.close();
}
protected abstract void persistTestData();
@Override
protected void cleanupTest() throws Exception {
openSession();
session.beginTransaction();
session.createQuery("delete from Human").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();
}
private void assertCount(long expected){
long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult();
Assert.assertEquals(expected, count);
}
}

View File

@ -2,89 +2,36 @@
import junit.framework.Assert; import junit.framework.Assert;
import org.hibernate.cfg.Configuration; import org.hibernate.test.annotations.filter.subclass.SubClassTest;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
public class JoinedSubClassTest extends BaseCoreFunctionalTestCase{ public class JoinedSubClassTest extends SubClassTest{
@Override
protected void afterConfigurationBuilt(Configuration configuration) {
configuration.setProperty("hibernate.show_sql", "true");
super.afterConfigurationBuilt(configuration);
}
@Override @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class, Club.class}; 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 @Override
protected void cleanupTest() throws Exception { protected void cleanupTest() throws Exception {
super.cleanupTest();
openSession(); openSession();
session.beginTransaction(); session.beginTransaction();
session.createQuery("delete from Human").executeUpdate();
session.createQuery("delete from Club").executeUpdate(); session.createQuery("delete from Club").executeUpdate();
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
} }
@Test @Override
public void testIqFilter(){ protected void persistTestData() {
openSession(); Club club = new Club();
session.beginTransaction(); club.setName("Mensa applicants");
club.getMembers().add(createHuman(club, false, 90));
assertCount(3); club.getMembers().add(createHuman(club, false, 100));
session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140); club.getMembers().add(createHuman(club, true, 110));
assertCount(1); session.persist(club);
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 @Test
@ -119,9 +66,5 @@ private Human createHuman(Club club, boolean pregnant, int iq){
return 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

@ -1,93 +1,31 @@
package org.hibernate.test.annotations.filter.subclass.singletable; package org.hibernate.test.annotations.filter.subclass.singletable;
import junit.framework.Assert; import org.hibernate.test.annotations.filter.subclass.SubClassTest;
import org.hibernate.cfg.Configuration; public class SingleTableTest extends SubClassTest{
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 @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class}; return new Class[]{Animal.class, Mammal.class, Human.class};
} }
@Override @Override
protected void prepareTest() throws Exception { protected void persistTestData() {
createHuman(false, 90); createHuman(false, 90);
createHuman(false, 100); createHuman(false, 100);
createHuman(true, 110); 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){ private void createHuman(boolean pregnant, int iq){
openSession();
session.beginTransaction();
Human human = new Human(); Human human = new Human();
human.setName("Homo Sapiens"); human.setName("Homo Sapiens");
human.setPregnant(pregnant); human.setPregnant(pregnant);
human.setIq(iq); human.setIq(iq);
session.persist(human); 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

@ -1,93 +1,27 @@
package org.hibernate.test.annotations.filter.subclass.tableperclass; package org.hibernate.test.annotations.filter.subclass.tableperclass;
import junit.framework.Assert; import org.hibernate.test.annotations.filter.subclass.SubClassTest;
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);
}
public class TablePerClassTest extends SubClassTest{
@Override @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class}; return new Class[]{Animal.class, Mammal.class, Human.class};
} }
@Override @Override
protected void prepareTest() throws Exception { protected void persistTestData() {
createHuman(false, 90); createHuman(false, 90);
createHuman(false, 100); createHuman(false, 100);
createHuman(true, 110); 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){ private void createHuman(boolean pregnant, int iq){
openSession();
session.beginTransaction();
Human human = new Human(); Human human = new Human();
human.setName("Homo Sapiens"); human.setName("Homo Sapiens");
human.setPregnant(pregnant); human.setPregnant(pregnant);
human.setIq(iq); human.setIq(iq);
session.persist(human); 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);
}
} }