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 @@ 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.hibernate.test.annotations.filter.subclass.SubClassTest;
import org.junit.Test;
public class JoinedSubClassTest extends BaseCoreFunctionalTestCase{
@Override
protected void afterConfigurationBuilt(Configuration configuration) {
configuration.setProperty("hibernate.show_sql", "true");
super.afterConfigurationBuilt(configuration);
}
public class JoinedSubClassTest extends SubClassTest{
@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 {
super.cleanupTest();
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();
@Override
protected void persistTestData() {
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);
}
@Test
@ -119,9 +66,5 @@ public class JoinedSubClassTest extends BaseCoreFunctionalTestCase{
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;
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 SingleTableTest extends BaseCoreFunctionalTestCase{
public class SingleTableTest extends SubClassTest{
@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 {
protected void persistTestData() {
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

@ -1,93 +1,27 @@
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);
}
import org.hibernate.test.annotations.filter.subclass.SubClassTest;
public class TablePerClassTest extends SubClassTest{
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{Animal.class, Mammal.class, Human.class};
}
@Override
protected void prepareTest() throws Exception {
protected void persistTestData() {
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);
}
}