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.spring.test.transaction;
018:
019: import javax.transaction.UserTransaction;
020:
021: import junit.framework.TestCase;
022: import org.compass.core.Compass;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.config.CompassConfiguration;
026: import org.compass.spring.transaction.SpringSyncTransactionFactory;
027: import org.springframework.transaction.TransactionDefinition;
028: import org.springframework.transaction.TransactionStatus;
029: import org.springframework.transaction.jta.JotmFactoryBean;
030: import org.springframework.transaction.jta.JtaTransactionManager;
031: import org.springframework.transaction.support.TransactionCallbackWithoutResult;
032: import org.springframework.transaction.support.TransactionTemplate;
033:
034: /**
035: * @author kimchy
036: */
037: public class SpringSyncTransactionTests extends TestCase {
038:
039: private Compass compass;
040:
041: private JotmFactoryBean jotmFactoryBean;
042:
043: private JtaTransactionManager transactionManager;
044:
045: protected void setUp() throws Exception {
046:
047: jotmFactoryBean = new JotmFactoryBean();
048:
049: transactionManager = new JtaTransactionManager();
050: transactionManager
051: .setUserTransaction((UserTransaction) jotmFactoryBean
052: .getObject());
053: transactionManager.afterPropertiesSet();
054:
055: CompassConfiguration conf = new CompassConfiguration()
056: .configure("/org/compass/spring/test/transaction/compass.springsync.cfg.xml");
057: SpringSyncTransactionFactory
058: .setTransactionManager(transactionManager);
059: compass = conf.buildCompass();
060: compass.getSearchEngineIndexManager().deleteIndex();
061: compass.getSearchEngineIndexManager().verifyIndex();
062: }
063:
064: protected void tearDown() throws Exception {
065: compass.close();
066: compass.getSearchEngineIndexManager().deleteIndex();
067:
068: jotmFactoryBean.destroy();
069: }
070:
071: public void testWithCommitNoSessionOrTransactionManagment()
072: throws Exception {
073:
074: final Long id = new Long(1);
075:
076: TransactionTemplate transactionTemplate = new TransactionTemplate(
077: transactionManager);
078: transactionTemplate
079: .execute(new TransactionCallbackWithoutResult() {
080: protected void doInTransactionWithoutResult(
081: TransactionStatus status) {
082: CompassSession session = compass.openSession();
083: A a = new A();
084: a.setId(id);
085: session.save(a);
086: a = (A) session.get(A.class, id);
087: assertNotNull(a);
088:
089: CompassSession oldSession = session;
090: session = compass.openSession();
091: assertTrue(oldSession == session);
092: a = (A) session.get(A.class, id);
093: assertNotNull(a);
094: }
095: });
096:
097: transactionTemplate
098: .execute(new TransactionCallbackWithoutResult() {
099: protected void doInTransactionWithoutResult(
100: TransactionStatus status) {
101: CompassSession session = compass.openSession();
102: A a = (A) session.get(A.class, id);
103: assertNotNull(a);
104: }
105: });
106: }
107:
108: public void testWithCommit() throws Exception {
109:
110: final Long id = new Long(1);
111:
112: TransactionTemplate transactionTemplate = new TransactionTemplate(
113: transactionManager);
114: transactionTemplate
115: .execute(new TransactionCallbackWithoutResult() {
116: protected void doInTransactionWithoutResult(
117: TransactionStatus status) {
118: CompassSession session = compass.openSession();
119: CompassTransaction tr = session
120: .beginTransaction();
121: A a = new A();
122: a.setId(id);
123: session.save(a);
124: a = (A) session.get(A.class, id);
125: assertNotNull(a);
126: tr.commit();
127: session.close();
128:
129: CompassSession oldSession = session;
130: session = compass.openSession();
131: assertTrue(oldSession == session);
132: tr = session.beginTransaction();
133: a = (A) session.get(A.class, id);
134: assertNotNull(a);
135: tr.commit();
136: session.close();
137: }
138: });
139:
140: transactionTemplate
141: .execute(new TransactionCallbackWithoutResult() {
142: protected void doInTransactionWithoutResult(
143: TransactionStatus status) {
144: CompassSession session = compass.openSession();
145: CompassTransaction tr = session
146: .beginTransaction();
147: A a = (A) session.get(A.class, id);
148: assertNotNull(a);
149: tr.commit();
150: session.close();
151: }
152: });
153: }
154:
155: public void testWithRollback() throws Exception {
156:
157: final Long id = new Long(1);
158:
159: TransactionTemplate transactionTemplate = new TransactionTemplate(
160: transactionManager);
161: transactionTemplate
162: .execute(new TransactionCallbackWithoutResult() {
163: protected void doInTransactionWithoutResult(
164: TransactionStatus status) {
165: CompassSession session = compass.openSession();
166: CompassTransaction tr = session
167: .beginTransaction();
168: A a = new A();
169: a.setId(id);
170: session.save(a);
171: a = (A) session.get(A.class, id);
172: assertNotNull(a);
173: tr.commit();
174: session.close();
175:
176: CompassSession oldSession = session;
177: session = compass.openSession();
178: assertTrue(oldSession == session);
179: tr = session.beginTransaction();
180: a = (A) session.get(A.class, id);
181: assertNotNull(a);
182: tr.commit();
183: session.close();
184:
185: status.setRollbackOnly();
186: }
187: });
188:
189: transactionTemplate
190: .execute(new TransactionCallbackWithoutResult() {
191: protected void doInTransactionWithoutResult(
192: TransactionStatus status) {
193: CompassSession session = compass.openSession();
194: CompassTransaction tr = session
195: .beginTransaction();
196: A a = (A) session.get(A.class, id);
197: assertNull(a);
198: tr.commit();
199: session.close();
200: }
201: });
202: }
203:
204: public void testWithSuspend() throws Exception {
205:
206: final Long id = new Long(1);
207:
208: TransactionTemplate transactionTemplate = new TransactionTemplate(
209: transactionManager);
210: transactionTemplate
211: .execute(new TransactionCallbackWithoutResult() {
212: protected void doInTransactionWithoutResult(
213: TransactionStatus status) {
214: final CompassSession session = compass
215: .openSession();
216: CompassTransaction tr = session
217: .beginTransaction();
218: A a = new A();
219: a.setId(id);
220: session.save(a);
221: a = (A) session.get(A.class, id);
222: assertNotNull(a);
223: tr.commit();
224: session.close();
225:
226: // start a new transaction
227: TransactionTemplate transactionTemplate = new TransactionTemplate(
228: transactionManager);
229: transactionTemplate
230: .setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
231: transactionTemplate
232: .execute(new TransactionCallbackWithoutResult() {
233: protected void doInTransactionWithoutResult(
234: TransactionStatus status) {
235: CompassSession innerSession = compass
236: .openSession();
237: assertTrue(session != innerSession);
238: CompassTransaction tr = innerSession
239: .beginTransaction();
240: A a = (A) innerSession.get(
241: A.class, id);
242: assertNull(a);
243: tr.commit();
244: innerSession.close();
245: }
246: });
247: }
248: });
249: }
250: }
|