- Creating abstract test with one session-per-test

- Fixing typo error.
This commit is contained in:
Hernan 2010-12-17 20:18:48 -02:00 committed by adamw
parent 102faba8f4
commit fe31aeda16
7 changed files with 253 additions and 213 deletions

View File

@ -0,0 +1,99 @@
package org.hibernate.envers.test;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
/**
* Base class for testing envers with Session when the same session and
* auditReader must be used for the hole test.
*
* @author Hernán Chanfreau
*
*/
public abstract class AbstractOneSessionTest {
protected Configuration config;
private SessionFactory sessionFactory;
private Session session ;
private AuditReader auditReader;
@BeforeClass
@Parameters("auditStrategy")
public void init(@Optional String auditStrategy) throws URISyntaxException {
config = new Configuration();
URL url = Thread.currentThread().getContextClassLoader().getResource(getHibernateConfigurationFileName());
config.configure(new File(url.toURI()));
if (auditStrategy != null && !"".equals(auditStrategy)) {
config.setProperty("org.hibernate.envers.audit_strategy", auditStrategy);
}
this.initMappings();
sessionFactory = config.buildSessionFactory();
}
protected abstract void initMappings() throws MappingException, URISyntaxException ;
protected String getHibernateConfigurationFileName(){
return "hibernate.test.session-cfg.xml";
}
private SessionFactory getSessionFactory(){
return sessionFactory;
}
@AfterClass
public void closeSessionFactory() {
sessionFactory.close();
}
/**
* Creates a new session and auditReader only if there is nothing created
* before
*/
@BeforeMethod
public void initializeSession() {
if (getSession() == null) {
session = getSessionFactory().openSession();
auditReader = AuditReaderFactory.get(session);
}
}
/**
* Creates a new session and auditReader.
*/
public void forceNewSession() {
session = getSessionFactory().openSession();
auditReader = AuditReaderFactory.get(session);
}
protected Session getSession() {
return session;
}
protected AuditReader getAuditReader() {
return auditReader;
}
}

View File

@ -6,7 +6,7 @@ import java.net.URL;
import java.util.List;
import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -16,7 +16,7 @@ import org.testng.annotations.Test;
*/
@Test(sequential=true)
public class ReadEntityWhtiEntityNameTest extends AbstractSessionTest{
public class ReadEntityWhitEntityNameTest extends AbstractOneSessionTest{
private long id_pers1;
private long id_pers2;
@ -33,20 +33,11 @@ public class ReadEntityWhtiEntityNameTest extends AbstractSessionTest{
config.addFile(new File(url.toURI()));
}
/**
* The test needs to run with the same session and auditReader.
*/
@Override
public void newSessionFactory() {
if (getSession() == null) {
super.newSessionFactory();
}
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
newSessionFactory();
initializeSession();
Person pers1 = new Person("Hernan", 28);
Person pers2 = new Person("Leandro", 29);
@ -103,12 +94,10 @@ public class ReadEntityWhtiEntityNameTest extends AbstractSessionTest{
person1_2 = getAuditReader().find(Person.class, "Personaje", id_pers1, 2);
person1_3 = getAuditReader().find(Person.class, "Personaje", id_pers1, 3);
person1_1.getName();
person1_1.getAge();
person1_2.getName();
person1_2.getAge();
person1_3.getName();
person1_3.getAge();
assert(person1_1 != null);
assert(person1_2 != null);
assert(person1_3 != null);
}
@Test(dependsOnMethods="testRetrieveAuditedEntityWithEntityName")
@ -128,21 +117,18 @@ public class ReadEntityWhtiEntityNameTest extends AbstractSessionTest{
}
@Test(dependsOnMethods="testObtainEntityNameAuditedEntityWithEntityName")
public void testFindHistoricAndCurrentForAuditedEntityWithEntityName() {
public void testRetrieveAuditedEntityWithEntityNameWithNewSession() {
// force a new session and AR
super.newSessionFactory();
forceNewSession();
person1_1 = getAuditReader().find(Person.class, "Personaje", id_pers1, 1);
person1_2 = getAuditReader().find(Person.class, "Personaje", id_pers1, 2);
person1_3 = getAuditReader().find(Person.class, "Personaje", id_pers1, 3);
person1_1.getName();
person1_1.getAge();
person1_2.getName();
person1_2.getAge();
person1_3.getName();
person1_3.getAge();
assert(person1_1 != null);
assert(person1_2 != null);
assert(person1_3 != null);
}

View File

@ -7,7 +7,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -17,7 +17,7 @@ import org.testng.annotations.Test;
*/
@Test(sequential=true)
public class ReadEntityWithAuditedManyToManyTest extends AbstractSessionTest{
public class ReadEntityWithAuditedManyToManyTest extends AbstractOneSessionTest{
private long id_car1;
private long id_car2;
@ -35,20 +35,11 @@ public class ReadEntityWithAuditedManyToManyTest extends AbstractSessionTest{
config.addFile(new File(url.toURI()));
}
/**
* The test needs to run with the same session and auditReader.
*/
@Override
public void newSessionFactory() {
if (getSession() == null) {
super.newSessionFactory();
}
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
newSessionFactory();
initializeSession();
Person pers1 = new Person("Hernan", 28);
Person pers2 = new Person("Leandro", 29);
@ -84,12 +75,12 @@ public class ReadEntityWithAuditedManyToManyTest extends AbstractSessionTest{
id_car2 = car2.getId();
}
@Test
public void testObtainManyYoManyWithEntityName() {
private void loadDataOnSessionAndAuditReader() {
car1_2 = getAuditReader().find(Car.class, id_car1, 2);
Car car2_2 = getAuditReader().find(Car.class, id_car2, 2);
// navigate through relations to load objects
for (Person owner : car1_2.getOwners()) {
for (Car ownedCar : owner.getCars()) {
ownedCar.getNumber();
@ -106,9 +97,10 @@ public class ReadEntityWithAuditedManyToManyTest extends AbstractSessionTest{
person1_1 = getAuditReader().find(Person.class, "Personaje", id_pers1, 1);
}
@Test(dependsOnMethods="testObtainManyYoManyWithEntityName")
public void testGetEntityNameManyYoManyWithEntityName() {
String currPerson1EN = getSession().getEntityName(person1);
private void checkEntityNames() {
String currPerson1EN = getSession().getEntityName(person1);
String currCar1EN = getSession().getEntityName(car1);
String person1_1EN = getAuditReader().getEntityName(id_pers1, 1, person1_1);
@ -116,31 +108,24 @@ public class ReadEntityWithAuditedManyToManyTest extends AbstractSessionTest{
String car1_2EN = getAuditReader().getEntityName(id_car1, 2, car1_2);
assert(currCar1EN.equals(car1_2EN));
}
@Test
public void testGetEntityNameManyYoManyWithEntityName() {
loadDataOnSessionAndAuditReader();
checkEntityNames();
}
@Test(dependsOnMethods="testGetEntityNameManyYoManyWithEntityName")
public void testFindHistoricAndCurrentForManyYoManyWithEntityName() {
public void testGetEntityNameManyYoManyWithEntityNameInNewSession() {
//force new session and AR
super.newSessionFactory();
forceNewSession();
loadDataOnSessionAndAuditReader();
car1_2 = getAuditReader().find(Car.class, id_car1, 2);
Car car2_2 = getAuditReader().find(Car.class, id_car2, 2);
checkEntityNames();
for (Person owner : car1_2.getOwners()) {
for (Car ownedCar : owner.getCars()) {
ownedCar.getNumber();
}
}
for (Person owner : car2_2.getOwners()) {
for (Car ownedCar : owner.getCars()) {
ownedCar.getNumber();
}
}
person1_1 = getAuditReader().find(Person.class, "Personaje", id_pers1, 1);
for (Car ownedCar : person1_1.getCars()) {
ownedCar.getNumber();
}
}
}

View File

@ -7,7 +7,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -17,7 +17,7 @@ import org.testng.annotations.Test;
*/
@Test(sequential=true)
public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
public class ReadEntityWithAuditedCollectionTest extends AbstractOneSessionTest{
private long id_car1;
private long id_car2;
@ -35,20 +35,11 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
config.addFile(new File(url.toURI()));
}
/**
* The test needs to run with the same session and auditReader.
*/
@Override
public void newSessionFactory() {
if (getSession() == null) {
super.newSessionFactory();
}
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
newSessionFactory();
initializeSession();
Person pers1 = new Person("Hernan", 28);
Person pers2 = new Person("Leandro", 29);
@ -82,12 +73,12 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
}
@Test
public void testObtainAuditedCollectionWithEntityName() {
private void loadDataOnSessionAndAuditReader() {
currentCar1 = (Car)getSession().get(Car.class, id_car1);
currentPerson1 = (Person)getSession().get("Personaje", id_pers1);
person1_1 = getAuditReader().find(Person.class,"Personaje", id_pers1, 1);
car1_1 = getAuditReader().find(Car.class, id_car1, 2);
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
@ -101,12 +92,8 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
}
}
@Test(dependsOnMethods="testObtainAuditedCollectionWithEntityName")
public void testObtainEntityNameAuditedCollectionWithEntityName() {
person1_1 = getAuditReader().find(Person.class,"Personaje", id_pers1, 1);
String currCar1EN = getSession().getEntityName(currentCar1);
private void checkEntityNames() {
String currCar1EN = getSession().getEntityName(currentCar1);
String currPerson1EN = getSession().getEntityName(currentPerson1);
String car1_1EN = getAuditReader().getEntityName(id_car1, 2, car1_1);
@ -114,29 +101,28 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
String person1_1EN = getAuditReader().getEntityName(id_pers1, 1, person1_1);
assert(currPerson1EN.equals(person1_1EN));
}
@Test
public void testObtainEntityNameAuditedCollectionWithEntityName() {
this.loadDataOnSessionAndAuditReader();
checkEntityNames();
}
@Test(dependsOnMethods="testObtainEntityNameAuditedCollectionWithEntityName")
public void testObtainAuditedCollectionWithEntityNameWithNewSession() {
public void testObtainEntityNameAuditedCollectionWithEntityNameInNewSession() {
// force a new session and AR
super.newSessionFactory();
forceNewSession();
Car car1_1 = getAuditReader().find(Car.class, id_car1, 2);
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
Person person1_1 = getAuditReader().find(Person.class,"Personaje", id_pers1, 1);
loadDataOnSessionAndAuditReader();
for (Person owner : car1_1.getOwners()) {
owner.getName();
owner.getAge();
}
for (Person owner : car2.getOwners()) {
owner.getName();
owner.getAge();
}
checkEntityNames();
person1_1.getName();
person1_1.getAge();
}
}

View File

@ -7,7 +7,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -17,7 +17,7 @@ import org.testng.annotations.Test;
*/
@Test(sequential=true)
public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
public class ReadEntityWithAuditedCollectionTest extends AbstractOneSessionTest{
private long id_car1;
private long id_car2;
@ -28,27 +28,17 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
private long id_pers1;
private Car car1_1;
private Person person1_1;
protected void initMappings() throws MappingException, URISyntaxException {
URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/entityNames/oneToManyNotAudited/mappings.hbm.xml");
config.addFile(new File(url.toURI()));
}
/**
* The test needs to run with the same session and auditReader.
*/
@Override
public void newSessionFactory() {
if (getSession() == null) {
super.newSessionFactory();
}
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
newSessionFactory();
initializeSession();
Person pers1 = new Person("Hernan", 28);
Person pers2 = new Person("Leandro", 29);
@ -82,8 +72,7 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
}
@Test
public void testObtainCollectionWithEntityNameAndNotAuditedMode() {
private void loadDataOnSessionAndAuditReader() {
currentCar1 = (Car)getSession().get(Car.class, id_car1);
currentPerson1 = (Person)getSession().get("Personaje", id_pers1);
@ -101,11 +90,7 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
}
}
@Test(dependsOnMethods="testObtainCollectionWithEntityNameAndNotAuditedMode")
public void testObtainEntityNameCollectionWithEntityNameAndNotAuditedMode() {
// entityName personaje is marked as NOT_AUDITED
person1_1 = (Person)getSession().get("Personaje", id_pers1);
private void checkEntityNames() {
String currCar1EN = getSession().getEntityName(currentCar1);
String currPerson1EN = getSession().getEntityName(currentPerson1);
@ -113,32 +98,28 @@ public class ReadEntityWithAuditedCollectionTest extends AbstractSessionTest{
String car1_1EN = getAuditReader().getEntityName(id_car1, 2, car1_1);
assert(currCar1EN.equals(car1_1EN));
String person1_1EN = getSession().getEntityName(person1_1);
String person1_1EN = getSession().getEntityName(currentPerson1);
assert(currPerson1EN.equals(person1_1EN));
}
@Test
public void testObtainEntityNameCollectionWithEntityNameAndNotAuditedMode() {
loadDataOnSessionAndAuditReader();
checkEntityNames();
}
@Test(dependsOnMethods="testObtainEntityNameCollectionWithEntityNameAndNotAuditedMode")
public void testObtainCollectionWithEntityNameAndNotAuditedModeWithNewSession() {
public void testObtainEntityNameCollectionWithEntityNameAndNotAuditedModeInNewSession() {
// force new session and AR
super.newSessionFactory();
forceNewSession();
Car car1_1 = getAuditReader().find(Car.class, id_car1, 2);
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
loadDataOnSessionAndAuditReader();
for (Person owner : car1_1.getOwners()) {
owner.getName();
owner.getAge();
}
for (Person owner : car2.getOwners()) {
owner.getName();
owner.getAge();
}
checkEntityNames();
// entityName personaje is marked as NOT_AUDITED
person1_1 = (Person)getSession().get("Personaje", id_pers1);
person1_1.getName();
person1_1.getAge();
}

View File

@ -5,7 +5,7 @@ import java.net.URISyntaxException;
import java.net.URL;
import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -15,7 +15,7 @@ import org.testng.annotations.Test;
*/
@Test(sequential=true)
public class ReadEntityAssociatedAuditedTest extends AbstractSessionTest{
public class ReadEntityAssociatedAuditedTest extends AbstractOneSessionTest{
private long id_car1;
private long id_car2;
@ -37,21 +37,11 @@ public class ReadEntityAssociatedAuditedTest extends AbstractSessionTest{
config.addFile(new File(url.toURI()));
}
/**
* The test needs to run with the same session and auditReader.
*/
@Override
public void newSessionFactory() {
if (getSession() == null) {
super.newSessionFactory();
}
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
newSessionFactory();
initializeSession();
Person pers1 = new Person("Hernan", 15);
Person pers2 = new Person("Leandro", 19);
@ -77,29 +67,28 @@ public class ReadEntityAssociatedAuditedTest extends AbstractSessionTest{
id_car2 = car2.getId();
id_pers2 = pers2.getId();
getSession().getTransaction().begin();
currentCar1 = (Car)getSession().get(Car.class, id_car1);
currentPerson1 = (Person)getSession().get("Personaje", id_pers1);
getSession().getTransaction().commit();
}
@Test
public void testGetAssociationWithEntityName() {
car1 = getAuditReader().find(Car.class, id_car1, 1);
private void loadDataOnSessionAndAuditReader() {
currentCar1 = (Car)getSession().get(Car.class, id_car1);
currentPerson1 = (Person)getSession().get("Personaje", id_pers1);
car1 = getAuditReader().find(Car.class, id_car1, 1);
person1 = car1.getOwner();
}
private void checkEntities() {
assert(currentPerson1.getAge() != person1.getAge());
Person person2 = (Person)getSession().get("Personaje", id_pers2);
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
Person person2_1 = car2.getOwner();
assert(person2.getAge() == person2_1.getAge());
}
}
@Test(dependsOnMethods="testGetAssociationWithEntityName")
public void testObtainEntityNameAssociationWithEntityName() {
String currentCar1EN = getSession().getEntityName(currentCar1);
private void checkEntityNames() {
String currentCar1EN = getSession().getEntityName(currentCar1);
String currentPerson1EN = getSession().getEntityName(currentPerson1);
@ -108,22 +97,35 @@ public class ReadEntityAssociatedAuditedTest extends AbstractSessionTest{
String person1EN = getAuditReader().getEntityName(id_pers1, 1, person1);
assert (currentPerson1EN.equals(person1EN));
}
@Test
public void testGetAssociationWithEntityName() {
loadDataOnSessionAndAuditReader();
checkEntities();
checkEntityNames();
}
@Test(dependsOnMethods="testObtainEntityNameAssociationWithEntityName")
public void testFindHistoricAndCurrentGetAssociationWithEntityName() {
@Test(dependsOnMethods="testGetAssociationWithEntityName")
public void testGetAssociationWithEntityNameInNewSession() {
//force a new session and AR
super.newSessionFactory();
forceNewSession();
Car car1 = getAuditReader().find(Car.class, id_car1, 1);
car1.getOwner().getName();
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
car2.getOwner().getName();
loadDataOnSessionAndAuditReader();
Person person = getAuditReader().find(Person.class, "Personaje", id_car2, 2);
person.getName();
checkEntities();
checkEntityNames();
//
// Car car1 = getAuditReader().find(Car.class, id_car1, 1);
// car1.getOwner().getName();
// Car car2 = getAuditReader().find(Car.class, id_car2, 2);
// car2.getOwner().getName();
//
// Person person = getAuditReader().find(Person.class, "Personaje", id_car2, 2);
// person.getName();
}

View File

@ -5,7 +5,7 @@ import java.net.URISyntaxException;
import java.net.URL;
import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -15,7 +15,7 @@ import org.testng.annotations.Test;
*/
@Test(sequential=true)
public class ReadEntityAssociatedNotAuditedTest extends AbstractSessionTest{
public class ReadEntityAssociatedNotAuditedTest extends AbstractOneSessionTest {
private long id_car1;
private long id_car2;
@ -24,7 +24,9 @@ public class ReadEntityAssociatedNotAuditedTest extends AbstractSessionTest{
private long id_pers2;
private Car car1;
private Car car2;
private Person person1_1;
private Person person2;
private Person currentPerson1;
private Car currentCar1;
@ -34,20 +36,10 @@ public class ReadEntityAssociatedNotAuditedTest extends AbstractSessionTest{
config.addFile(new File(url.toURI()));
}
/**
* The test needs to run with the same session and auditReader.
*/
@Override
public void newSessionFactory() {
if (getSession() == null) {
super.newSessionFactory();
}
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
newSessionFactory();
initializeSession();
Person pers1 = new Person("Hernan", 15);
Person pers2 = new Person("Leandro", 19);
@ -75,18 +67,29 @@ public class ReadEntityAssociatedNotAuditedTest extends AbstractSessionTest{
}
@Test
public void testGetAssociationWithEntityNameAndNotAuditedMode() {
// persons from "actual" model
private void loadDataOnSessionAndAuditReader() {
currentPerson1 = (Person)getSession().get("Personaje", id_pers1);
Person person2 = (Person)getSession().get("Personaje", id_pers2);
person2 = (Person)getSession().get("Personaje", id_pers2);
currentCar1 = (Car)getSession().get(Car.class, id_car1);
car1 = getAuditReader().find(Car.class, id_car1, 1);
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
car2 = getAuditReader().find(Car.class, id_car2, 2);
}
private void checkEntityNames() {
String currentCar1EN = getSession().getEntityName(currentCar1);
String car1EN = getAuditReader().getEntityName(id_car1, 1, car1);
assert (currentCar1EN.equals(car1EN));
}
private void checkEntities() {
// persons from "historic" model
person1_1 = car1.getOwner();
Person person2_1 = car2.getOwner();
@ -94,29 +97,27 @@ public class ReadEntityAssociatedNotAuditedTest extends AbstractSessionTest{
assert(person2.getAge() == person2_1.getAge());
}
@Test(dependsOnMethods="testGetAssociationWithEntityNameAndNotAuditedMode")
@Test
public void testObtainEntityNameAssociationWithEntityNameAndNotAuditedMode() {
loadDataOnSessionAndAuditReader();
String currentCar1EN = getSession().getEntityName(currentCar1);
String car1EN = getAuditReader().getEntityName(id_car1, 1, car1);
assert (currentCar1EN.equals(car1EN));
checkEntities();
checkEntityNames();
}
@Test(dependsOnMethods="testObtainEntityNameAssociationWithEntityNameAndNotAuditedMode")
public void testFindHistoricAndCurrentGetAssociationWithEntityNameAndNotAuditedMode() {
public void testObtainEntityNameAssociationWithEntityNameAndNotAuditedModeInNewSession() {
//force a new session and AR
super.newSessionFactory();
forceNewSession();
Car car1 = getAuditReader().find(Car.class, id_car1, 1);
car1.getOwner().getName();
Car car2 = getAuditReader().find(Car.class, id_car2, 2);
car2.getOwner().getName();
loadDataOnSessionAndAuditReader();
checkEntities();
checkEntityNames();
// entityName personaje is marked as NOT_AUDITED
Person person = (Person)getSession().get("Personaje", id_pers1);
person.getName();
}
}