001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.transaction;
018:
019: import javax.naming.Context;
020: import javax.naming.InitialContext;
021: import javax.transaction.Transaction;
022: import javax.transaction.TransactionManager;
023: import javax.transaction.UserTransaction;
024:
025: import junit.framework.TestCase;
026: import org.compass.core.Compass;
027: import org.compass.core.CompassSession;
028: import org.compass.core.CompassTransaction;
029: import org.compass.core.config.CompassConfiguration;
030: import org.objectweb.jotm.Current;
031: import org.objectweb.jotm.Jotm;
032:
033: /**
034: * @author kimchy
035: */
036: public abstract class AbstractJTATests extends TestCase {
037:
038: private Compass compass;
039:
040: private Jotm jotm;
041:
042: protected abstract String getCompassConfig();
043:
044: protected void setUp() throws Exception {
045: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
046: "com.sun.jndi.rmi.registry.RegistryContextFactory");
047: System
048: .setProperty(Context.PROVIDER_URL,
049: "rmi://localhost:1099");
050:
051: try {
052: java.rmi.registry.LocateRegistry.createRegistry(1099);
053: } catch (Exception e) {
054:
055: }
056:
057: jotm = new Jotm(true, true);
058: Context ctx = new InitialContext();
059: ctx.rebind("java:comp/UserTransaction", jotm
060: .getUserTransaction());
061:
062: CompassConfiguration conf = new CompassConfiguration()
063: .configure(getCompassConfig());
064: compass = conf.buildCompass();
065: compass.getSearchEngineIndexManager().deleteIndex();
066: compass.getSearchEngineIndexManager().verifyIndex();
067: }
068:
069: protected void tearDown() throws Exception {
070: compass.close();
071: compass.getSearchEngineIndexManager().deleteIndex();
072: jotm.stop();
073: }
074:
075: public void testJtaWithLocalTransaction() throws Exception {
076: CompassSession session = compass.openSession();
077: CompassTransaction tr = session.beginLocalTransaction();
078:
079: A a = new A();
080: a.setId(1l);
081: session.save(a);
082:
083: tr.commit();
084: session.close();
085:
086: session = compass.openSession();
087: tr = session.beginLocalTransaction();
088:
089: a = session.load(A.class, 1);
090: assertNotNull(a);
091:
092: tr.commit();
093: session.close();
094: }
095:
096: public void testInnerUTManagement() throws Exception {
097: CompassSession session = compass.openSession();
098: CompassTransaction tr = session.beginTransaction();
099:
100: // save a new instance of A
101: Long id = new Long(1);
102: A a = new A();
103: a.setId(id);
104: session.save(a);
105:
106: a = (A) session.get(A.class, id);
107: assertNotNull(a);
108:
109: // check that if we open a new transaction within the current one it
110: // will still work
111: CompassSession newSession = compass.openSession();
112: assertTrue(session == newSession);
113: CompassTransaction newTr = session.beginTransaction();
114: a = (A) session.get(A.class, id);
115: assertNotNull(a);
116: // this one should not commit the jta transaction since the out
117: // controlls it
118: newTr.commit();
119: newSession.close();
120:
121: tr.commit();
122:
123: // verify that the instance was saved
124: tr = session.beginTransaction();
125: a = (A) session.get(A.class, id);
126: assertNotNull(a);
127:
128: tr.commit();
129: session.close();
130: }
131:
132: public void testOuterUTManagementWithCommit() throws Exception {
133: Context ctx = new InitialContext();
134: UserTransaction ut = (UserTransaction) ctx
135: .lookup("java:comp/UserTransaction");
136: ut.begin();
137:
138: CompassSession session = compass.openSession();
139: CompassTransaction tr = session.beginTransaction();
140: Long id = new Long(1);
141: A a = new A();
142: a.setId(id);
143: session.save(a);
144: a = (A) session.get(A.class, id);
145: assertNotNull(a);
146: tr.commit();
147: session.close();
148:
149: CompassSession oldSession = session;
150: session = compass.openSession();
151: assertTrue(oldSession == session);
152: tr = session.beginTransaction();
153: a = (A) session.get(A.class, id);
154: assertNotNull(a);
155: tr.commit();
156: session.close();
157:
158: ut.commit();
159:
160: session = compass.openSession();
161: tr = session.beginTransaction();
162: a = (A) session.get(A.class, id);
163: assertNotNull(a);
164: tr.commit();
165: session.close();
166: }
167:
168: public void testOuterUTManagementWithCommitAndNoSessionOrTransactionManagement()
169: throws Exception {
170: Context ctx = new InitialContext();
171: UserTransaction ut = (UserTransaction) ctx
172: .lookup("java:comp/UserTransaction");
173: ut.begin();
174:
175: CompassSession session = compass.openSession();
176: Long id = new Long(1);
177: A a = new A();
178: a.setId(id);
179: session.save(a);
180: a = (A) session.get(A.class, id);
181: assertNotNull(a);
182:
183: CompassSession oldSession = session;
184: session = compass.openSession();
185: assertTrue(oldSession == session);
186: a = (A) session.get(A.class, id);
187: assertNotNull(a);
188:
189: ut.commit();
190:
191: // now check that things were committed
192: // here we do need explicit session/transaciton mangement
193: // just cause we are lazy and want to let Comapss to manage JTA
194: session = compass.openSession();
195: CompassTransaction tr = session.beginTransaction();
196: a = (A) session.get(A.class, id);
197: assertNotNull(a);
198: tr.commit();
199: session.close();
200: }
201:
202: public void testOuterUTManagementWithRollback() throws Exception {
203: Context ctx = new InitialContext();
204: UserTransaction ut = (UserTransaction) ctx
205: .lookup("java:comp/UserTransaction");
206: ut.begin();
207:
208: CompassSession session = compass.openSession();
209: CompassTransaction tr = session.beginTransaction();
210: Long id = new Long(1);
211: A a = new A();
212: a.setId(id);
213: session.save(a);
214: a = (A) session.get(A.class, id);
215: assertNotNull(a);
216: tr.commit();
217: session = compass.openSession();
218: tr = session.beginTransaction();
219: a = (A) session.get(A.class, id);
220: assertNotNull(a);
221: tr.commit();
222:
223: ut.rollback();
224:
225: session = compass.openSession();
226: tr = session.beginTransaction();
227: a = (A) session.get(A.class, id);
228: assertNull(a);
229: tr.commit();
230: }
231:
232: public void testOuterUTManagementWithSuspend() throws Exception {
233: Context ctx = new InitialContext();
234: UserTransaction ut = (UserTransaction) ctx
235: .lookup("java:comp/UserTransaction");
236: ut.begin();
237:
238: CompassSession session = compass.openSession();
239: CompassTransaction tr = session.beginTransaction();
240: Long id = new Long(1);
241: A a = new A();
242: a.setId(id);
243: session.save(a);
244: a = (A) session.get(A.class, id);
245: assertNotNull(a);
246:
247: TransactionManager transactionManager = Current
248: .getTransactionManager();
249: Transaction jtaTrans = transactionManager.suspend();
250:
251: UserTransaction newUt = (UserTransaction) ctx
252: .lookup("java:comp/UserTransaction");
253: newUt.begin();
254: CompassSession newSession = compass.openSession();
255: assertTrue(session != newSession);
256: CompassTransaction newTr = newSession.beginTransaction();
257: a = (A) newSession.get(A.class, id);
258: assertNull(a);
259: newTr.commit();
260: newSession.close();
261: newUt.commit();
262:
263: transactionManager.resume(jtaTrans);
264:
265: tr.commit();
266: ut.commit();
267:
268: session = compass.openSession();
269: tr = session.beginTransaction();
270: a = (A) session.get(A.class, id);
271: assertNotNull(a);
272: tr.commit();
273: session.close();
274: }
275:
276: public void testOuterUTManagementWithSuspendAndNoSessionOrTransactionManagement()
277: throws Exception {
278: Context ctx = new InitialContext();
279: UserTransaction ut = (UserTransaction) ctx
280: .lookup("java:comp/UserTransaction");
281: ut.begin();
282:
283: CompassSession session = compass.openSession();
284: Long id = new Long(1);
285: A a = new A();
286: a.setId(id);
287: session.save(a);
288: a = (A) session.get(A.class, id);
289: assertNotNull(a);
290:
291: TransactionManager transactionManager = Current
292: .getTransactionManager();
293: Transaction jtaTrans = transactionManager.suspend();
294:
295: UserTransaction newUt = (UserTransaction) ctx
296: .lookup("java:comp/UserTransaction");
297: newUt.begin();
298: CompassSession newSession = compass.openSession();
299: assertTrue(session != newSession);
300: a = (A) newSession.get(A.class, id);
301: assertNull(a);
302: newUt.commit();
303:
304: transactionManager.resume(jtaTrans);
305:
306: ut.commit();
307:
308: // here we are lazy and let Compass manage a verifying JTa transaction
309: session = compass.openSession();
310: CompassTransaction tr = session.beginTransaction();
311: a = (A) session.get(A.class, id);
312: assertNotNull(a);
313: tr.commit();
314: session.close();
315: }
316:
317: }
|