001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import java.util.List;
020: import org.apache.log4j.Logger;
021:
022: /**
023: * Test the transaction framework.
024: * @author Brautigam Robert
025: * @version Revision: $Revision$
026: */
027: public class TransactionTests extends AbstractPersistenceTest {
028: private static Logger logger = Logger
029: .getLogger(TransactionTests.class);
030:
031: private boolean called = false; // Helper
032:
033: public TransactionTests(String name) throws Exception {
034: super (name);
035: }
036:
037: public void testAtomicOperations() throws Exception {
038: // Drop tables
039: dropTables("book");
040: // Create stuff
041: Transaction tx = store.getTransactionTracker().getTransaction(
042: TransactionTracker.TX_NEW);
043: tx.begin();
044: store.save(new Book("Wayne's World", "1"));
045: store.save(new Book("Wayne's World II.", "2"));
046: tx.rollback();
047: // Test
048: List result = store.find("find book");
049: assertEquals(0, result.size());
050: }
051:
052: public void testMultilevelTransactionInnerCommit() throws Exception {
053: // Drop tables
054: dropTables("book");
055: // Create stuff
056: Transaction tx = store.getTransactionTracker().getTransaction(
057: TransactionTracker.TX_NEW);
058: tx.begin();
059: store.save(new Book("Wayne's World", "1"));
060: tx.begin();
061: store.save(new Book("Wayne's World II.", "2"));
062: tx.commit();
063: tx.rollback();
064: // Test
065: List result = store.find("find book");
066: assertEquals(0, result.size());
067: }
068:
069: public void testMultilevelTransactionOuterCommit() throws Exception {
070: // Drop tables
071: dropTables("book");
072: // Create stuff
073: Transaction tx = store.getTransactionTracker().getTransaction(
074: TransactionTracker.TX_NEW);
075: tx.begin();
076: store.save(new Book("Wayne's World", "1"));
077: tx.begin();
078: store.save(new Book("Wayne's World II.", "2"));
079: tx.rollback();
080: tx.commit();
081: // Test
082: List result = store.find("find book");
083: assertEquals(0, result.size());
084: }
085:
086: public void testTransactionRequiredSameThread() throws Exception {
087: // Drop tables
088: dropTables("book");
089: // Create stuff
090: Transaction tx = store.getTransactionTracker().getTransaction(
091: TransactionTracker.TX_NEW);
092: tx.begin();
093: store.save(new Book("Wayne's World", "1"));
094: Transaction tx2 = store.getTransactionTracker().getTransaction(
095: TransactionTracker.TX_REQUIRED);
096: assertSame(tx, tx2);
097: tx2.begin();
098: store.save(new Book("Wayne's World II.", "2"));
099: tx2.rollback();
100: tx.commit();
101: // Test
102: List result = store.find("find book");
103: assertEquals(0, result.size());
104: }
105:
106: public void testTransactionNewSameThread() throws Exception {
107: // Drop tables
108: dropTables("book");
109: // Create stuff
110: Transaction tx = store.getTransactionTracker().getTransaction(
111: TransactionTracker.TX_NEW);
112: tx.begin();
113: store.save(new Book("Wayne's World", "1"));
114: Transaction tx2 = store.getTransactionTracker().getTransaction(
115: TransactionTracker.TX_NEW);
116: tx2.begin();
117: store.save(new Book("Wayne's World II.", "2"));
118: tx2.rollback();
119: tx.commit();
120: // Test
121: List result = store.find("find book");
122: assertEquals(1, result.size());
123: }
124:
125: public void testListenerCommit() throws Exception {
126: // Register listener
127: called = false;
128: TransactionTracker tt = store.getTransactionTracker();
129: tt.addListener(new TransactionListener() {
130: public void transactionCommited(Transaction t) {
131: called = true;
132: }
133:
134: public void transactionRolledback(Transaction t) {
135: }
136: });
137: // Do a transaction
138: store.save(new Book("Book Of Bokonon", "1-2-3-4"));
139: // Check
140: assertTrue(called);
141: }
142:
143: public void testListenerRollback() throws Exception {
144: // Register listener
145: called = false;
146: TransactionTracker tt = store.getTransactionTracker();
147: tt.addListener(new TransactionListener() {
148: public void transactionCommited(Transaction t) {
149: }
150:
151: public void transactionRolledback(Transaction t) {
152: called = true;
153: }
154: });
155: // Do a transaction
156: Transaction tx = tt
157: .getTransaction(TransactionTracker.TX_REQUIRED);
158: tx.begin();
159: store.save(new Book("Book Of Bokonon", "1-2-3-4"));
160: tx.rollback();
161: // Check
162: assertTrue(called);
163: }
164:
165: public void testListenerRecursion() throws Exception {
166: called = false;
167: // Register listener
168: final TransactionTracker tt = store.getTransactionTracker();
169: tt.addListener(new TransactionListener() {
170: public void transactionCommited(Transaction t) {
171: // Determine whether internal
172: logger.debug("received commit for transaction: " + t
173: + ", with keys: " + t.keySet());
174: if (t.get("Internal") != null) {
175: called = true;
176: return; // Avoid recursion
177: }
178: // Do transaction
179: Transaction tx = tt
180: .getTransaction(TransactionTracker.TX_NEW);
181: tx.put("Internal", "true");
182: logger.debug("starting internal transaction: " + tx
183: + ", with keys: " + tx.keySet());
184: tx.begin();
185: store.save(new Book("Book Of Bokonon", "1-2-3-4"));
186: tx.commit();
187: logger.debug("internal transaction ended: " + tx);
188: }
189:
190: public void transactionRolledback(Transaction t) {
191: }
192: });
193: // Do a transaction
194: store.save(new Book("Book Of Bokonon", "1-2-3-4"));
195: // Check
196: assertFalse(called);
197: }
198:
199: public void testNewVisibility() throws Exception {
200: // Drop tables
201: dropTables("book");
202: // Create stuff
203: Transaction tx = store.getTransactionTracker().getTransaction(
204: TransactionTracker.TX_NEW);
205: tx.begin();
206: store.save(new Book("Wayne's World", "1"));
207: Transaction tx2 = store.getTransactionTracker().getTransaction(
208: TransactionTracker.TX_NEW);
209: tx2.begin();
210: assertEquals(0, store.find("find book").size());
211: tx2.commit();
212: tx.commit();
213: }
214:
215: public void testNewParameters() throws Exception {
216: // Drop tables
217: dropTables("book");
218: // Create stuff
219: Transaction tx = store.getTransactionTracker().getTransaction(
220: TransactionTracker.TX_NEW);
221: tx.begin();
222: store.save(new Book("Wayne's World", "1"));
223: tx.put("Book", "yes");
224: Transaction tx2 = store.getTransactionTracker().getTransaction(
225: TransactionTracker.TX_NEW);
226: tx2.begin();
227: assertNull(tx2.get("Book"));
228: tx2.commit();
229: tx.commit();
230: }
231:
232: public void testNewParametersBack() throws Exception {
233: // Drop tables
234: dropTables("book");
235: // Create stuff
236: Transaction tx = store.getTransactionTracker().getTransaction(
237: TransactionTracker.TX_NEW);
238: tx.begin();
239: store.save(new Book("Wayne's World", "1"));
240: Transaction tx2 = store.getTransactionTracker().getTransaction(
241: TransactionTracker.TX_NEW);
242: tx2.begin();
243: tx2.put("Book", "no");
244: tx2.commit();
245: assertNull(tx.get("Book"));
246: tx.commit();
247: }
248:
249: public void testListOutOfActiveTransaction() throws Exception {
250: // Drop tables
251: dropTables("book");
252: // Register class
253: store.save(new Book("Wayne's World", "1"));
254: // Get a list which has transaction bindings
255: Transaction tx = store.getTransactionTracker().getTransaction(
256: TransactionTracker.TX_NEW);
257: tx.begin();
258: store.save(new Book("Bill and Ted", "2"));
259: List result = store.find("find book");
260: // Before closing try to use the list in another transaction
261: Transaction tx2 = store.getTransactionTracker().getTransaction(
262: TransactionTracker.TX_NEW);
263: tx2.begin();
264: try {
265: result.size();
266: fail("could query a list outside it's still open transaction, that should be prohibited");
267: } catch (StoreException e) {
268: // Ok
269: }
270: tx2.commit();
271: // Close all
272: tx.commit();
273: }
274:
275: public void testDoubleSave() throws Exception {
276: // Drop tables
277: dropTables("referrer");
278: dropTables("referrersubclass");
279: // Create
280: ReferrerSubclass obj = new ReferrerSubclass(1, 1);
281: store.save(obj);
282: // Double save
283: Transaction tx = store.getTransactionTracker().getTransaction(
284: TransactionTracker.TX_NEW);
285: tx.begin();
286: obj.setIdentity(2);
287: store.save(obj);
288: obj.setIdentity(3);
289: store.save(obj);
290: tx.commit();
291: // Test
292: List result = store.find("find referrersubclass");
293: assertEquals(1, result.size());
294: }
295:
296: public void testCombinedSaveInsert() throws Exception {
297: // Drop tables
298: dropTables("referrer");
299: dropTables("referrersubclass");
300: // Create
301: ReferrerSubclass obj = new ReferrerSubclass(1, 1);
302: store.save(obj);
303: // Double save
304: Transaction tx = store.getTransactionTracker().getTransaction(
305: TransactionTracker.TX_NEW);
306: tx.begin();
307: obj.setIdentity(2);
308: store.save(obj);
309: store.save(new ReferrerSubclass(2, 2));
310: obj.setIdentity(3);
311: store.save(obj);
312: tx.commit();
313: // Test
314: List result = store.find("find referrersubclass");
315: assertEquals(2, result.size());
316: }
317:
318: }
|