package com.acme.test; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.orm.hibernate3.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; import com.acme.business.DefaultWorkManager; import com.acme.data.ChildDao; import com.acme.data.ParentDao; import com.acme.domain.Child; import com.acme.domain.Parent; import junit.framework.TestCase; public class HibernateTest extends TestCase { protected ApplicationContext applicationContext; protected ParentDao parentDao; protected ChildDao childDao; protected SessionFactory sessionFactory; protected Session session; protected DefaultWorkManager workManager; @Override protected void setUp() throws Exception { session = SessionFactoryUtils.getSession(this.sessionFactory, true); TransactionSynchronizationManager.bindResource(this.sessionFactory, new SessionHolder(session)); } @Override protected void tearDown() throws Exception { TransactionSynchronizationManager.unbindResource(this.sessionFactory); SessionFactoryUtils.releaseSession(this.session, this.sessionFactory); } public HibernateTest() { applicationContext = new FileSystemXmlApplicationContext( new String[] {"applicationContext.xml"}); parentDao = (ParentDao)applicationContext.getBean("parentDao"); childDao = (ChildDao)applicationContext.getBean("childDao"); sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory"); workManager = (DefaultWorkManager)applicationContext.getBean("workManager"); } public void testLazyLoading() { Parent parent = parentDao.getById("1"); if (parent == null) { fail("parent is null!"); } else { assertTrue(parent.getName().equals("Dad")); Set kids = parent.getChildren(); assertTrue(kids.isEmpty() == false); for (Child kid : kids) { System.out.println(kid.getName()); assertTrue(kid.getName(), kid.getParent().getId() == parent.getId()); } } } public void testCascadedDelete() { // add a new parent with a couple of children Parent parent = new Parent(); parent.setName("Newone"); Child one = new Child(); one.setName("One"); Child two = new Child(); two.setName("Two"); parent.addChild(one); parent.addChild(two); parentDao.save(parent); // by default the database has 3 parents (see init.sql) assertTrue(parentDao.getAll().size() > 3); // now delete the new parent parentDao.delete(parent); assertTrue(parentDao.getAll().size() == 3); } public void testObjectIdentity() { // both p1 & p2 are referencing the same row of the database Parent p1 = parentDao.getById("3"); Parent p2 = parentDao.getById("3"); String name = p1.getName(); // when you change one object the other is also changed. p1.setName("update 1"); p2.setName("update 2"); assertTrue(p1.getName().equals(p2.getName())); parentDao.save(p1); assertTrue(p1.getName().equals("update 2")); // change name to what it was before p2.setName(name); parentDao.save(p2); } public void testPersistenceWithoutSave() { Parent p = parentDao.getById("3"); // since p is a persistent object any modifications to it // should be saved back to the database when the session // is flushed (or closed). // TODO: this is not working. Looks like I need to do an explicit // parentDao.save(p) String oldName = p.getName(); String v = String.valueOf(p.getVersion()+1000); String newName = (oldName.endsWith(v)) ? oldName.replaceAll("0-9", v) : oldName + v; p.setName(newName); assertFalse(oldName.equals(newName)); } public void testTransaction() { int countBefore = workManager.getAllParents().size(); Parent p = new Parent(); p.setName("Big Daddy"); Child c = new Child(); c.setName("Johnny"); p.addChild(c); try { workManager.saveParentAndThrowException(p); fail("Should have thrown RuntimeException"); } catch (Exception x) { assertTrue(x instanceof RuntimeException); } int countAfter = workManager.getAllParents().size(); assertTrue("The new parent should not have been added", countBefore==countAfter); } }