001: package org.compass.gps.device.hibernate.transaction;
002:
003: import junit.framework.TestCase;
004: import org.compass.core.Compass;
005: import org.compass.core.CompassSession;
006: import org.compass.core.CompassTransaction;
007: import org.compass.core.config.CompassConfiguration;
008: import org.compass.gps.device.hibernate.HibernateSyncTransactionFactory;
009: import org.hibernate.Session;
010: import org.hibernate.SessionFactory;
011: import org.hibernate.Transaction;
012: import org.hibernate.cfg.Configuration;
013: import org.hibernate.cfg.Environment;
014:
015: /**
016: * @author kimchy
017: */
018: public class HibernateTransactionTests extends TestCase {
019:
020: private Compass compass;
021:
022: private SessionFactory sessionFactory;
023:
024: protected void setUp() throws Exception {
025:
026: Configuration conf = new Configuration()
027: .configure(
028: "/org/compass/gps/device/hibernate/transaction/hibernate.cfg.xml")
029: .setProperty(Environment.HBM2DDL_AUTO, "create");
030: sessionFactory = conf.buildSessionFactory();
031:
032: // set the session factory for the Hibernate transcation factory BEFORE we construct the compass instnace
033: HibernateSyncTransactionFactory
034: .setSessionFactory(sessionFactory);
035:
036: CompassConfiguration cpConf = new CompassConfiguration()
037: .configure("/org/compass/gps/device/hibernate/transaction/compass.cfg.xml");
038: compass = cpConf.buildCompass();
039: compass.getSearchEngineIndexManager().deleteIndex();
040: compass.getSearchEngineIndexManager().verifyIndex();
041:
042: }
043:
044: protected void tearDown() throws Exception {
045: compass.close();
046: sessionFactory.close();
047: }
048:
049: public void testInnerHibernateManagement() throws Exception {
050: CompassSession session = compass.openSession();
051: CompassTransaction tr = session.beginTransaction();
052:
053: // save a new instance of A
054: Long id = new Long(1);
055: A a = new A();
056: a.id = id;
057: session.save(a);
058:
059: a = (A) session.get(A.class, id);
060: assertNotNull(a);
061:
062: // check that if we open a new transaction within the current one it
063: // will still work
064: CompassSession newSession = compass.openSession();
065: assertTrue(session == newSession);
066: CompassTransaction newTr = session.beginTransaction();
067: a = (A) session.get(A.class, id);
068: assertNotNull(a);
069: // this one should not commit the jta transaction since the out
070: // controlls it
071: newTr.commit();
072: newSession.close();
073:
074: tr.commit();
075:
076: // verify that the instance was saved
077: tr = session.beginTransaction();
078: a = (A) session.get(A.class, id);
079: assertNotNull(a);
080:
081: tr.commit();
082: session.close();
083: }
084:
085: public void testOuterHibernteManagementWithCommit()
086: throws Exception {
087: Session hibernateSession = sessionFactory.getCurrentSession();
088: Transaction hibernateTr = hibernateSession.beginTransaction();
089:
090: CompassSession session = compass.openSession();
091: CompassTransaction tr = session.beginTransaction();
092: Long id = new Long(1);
093: A a = new A();
094: a.id = id;
095: session.save(a);
096: a = (A) session.get(A.class, id);
097: assertNotNull(a);
098: tr.commit();
099: session.close();
100:
101: CompassSession oldSession = session;
102: session = compass.openSession();
103: assertTrue(oldSession == session);
104: tr = session.beginTransaction();
105: a = (A) session.get(A.class, id);
106: assertNotNull(a);
107: tr.commit();
108: session.close();
109:
110: hibernateTr.commit();
111:
112: session = compass.openSession();
113: tr = session.beginTransaction();
114: a = (A) session.get(A.class, id);
115: assertNotNull(a);
116: tr.commit();
117: session.close();
118: }
119:
120: public void testOuterUTManagementWithCommitAndNoSessionOrTransactionManagement()
121: throws Exception {
122: Session hibernateSession = sessionFactory.getCurrentSession();
123: Transaction hibernateTr = hibernateSession.beginTransaction();
124:
125: CompassSession session = compass.openSession();
126: Long id = new Long(1);
127: A a = new A();
128: a.id = id;
129: session.save(a);
130: a = (A) session.get(A.class, id);
131: assertNotNull(a);
132:
133: CompassSession oldSession = session;
134: session = compass.openSession();
135: assertTrue(oldSession == session);
136: a = (A) session.get(A.class, id);
137: assertNotNull(a);
138:
139: hibernateTr.commit();
140:
141: // now check that things were committed
142: // here we do need explicit session/transaciton mangement
143: // just cause we are lazy and want to let Comapss to manage JTA
144: session = compass.openSession();
145: CompassTransaction tr = session.beginTransaction();
146: a = (A) session.get(A.class, id);
147: assertNotNull(a);
148: tr.commit();
149: session.close();
150: }
151:
152: public void testOuterUTManagementWithRollback() throws Exception {
153: Session hibernateSession = sessionFactory.getCurrentSession();
154: Transaction hibernateTr = hibernateSession.beginTransaction();
155:
156: CompassSession session = compass.openSession();
157: CompassTransaction tr = session.beginTransaction();
158: Long id = new Long(1);
159: A a = new A();
160: a.id = id;
161: session.save(a);
162: a = (A) session.get(A.class, id);
163: assertNotNull(a);
164: tr.commit();
165: session = compass.openSession();
166: tr = session.beginTransaction();
167: a = (A) session.get(A.class, id);
168: assertNotNull(a);
169: tr.commit();
170:
171: hibernateTr.rollback();
172:
173: session = compass.openSession();
174: tr = session.beginTransaction();
175: a = (A) session.get(A.class, id);
176: assertNull(a);
177: tr.commit();
178: }
179: }
|