package tpdrg;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
@Test
@ContextConfiguration(locations = "classpath*:spring-config.xml")
public class TEST extends AbstractTestNGSpringContextTests {
@Autowired
@Qualifier("Deals")
protected org.hibernate.SessionFactory sf;
Session session;
@BeforeMethod
public void init() {
session = sf.openSession();
}
@AfterMethod
public void close() {
session.close();
}
@Test
public void AddNew(){
Parent p = new Parent();
p.setName("Parent");
ArrayList children = new ArrayList();
for (int i = 0 ; i < 3 ;i++){
Child c = new Child();
c.setName("child "+i);
c.setParent(p);
children.add(c);
}
p.setChild(children);
Transaction tx = session.getTransaction();
tx.begin();
session.saveOrUpdate(p);
tx.commit();
}
@Test(dependsOnMethods = {"AddNew"})
public void Remove(){
Transaction tx = session.getTransaction();
tx.begin();
Parent p = (Parent)session.get(Parent.class,"Parent");
session.delete(p);
tx.commit();
}
@Test(dependsOnMethods = {"Remove"})
public void AuditReader(){
Transaction tx = session.getTransaction();
tx.begin();
AuditReader reader = AuditReaderFactory.get(session);
Parent deletedObject = reader.find(Parent.class,Parent.class.getName(), "Parent", Integer.MAX_VALUE, true);
List<Child> children = (List<Child>)deletedObject.getChild();
assert children.size()>0:"Expect the result should not zero";
tx.commit();
}