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 java.util.Vector;
021: import java.util.Date;
022: import org.apache.log4j.Logger;
023:
024: /**
025: * Test the concurrence capabilities of library.
026: * @author Brautigam Robert
027: * @version Revision: $Revision$
028: */
029: public class ConcurrenceTests extends AbstractPersistenceTest {
030: private static Logger logger = Logger
031: .getLogger(ConcurrenceTests.class);
032:
033: public ConcurrenceTests(String name) throws Exception {
034: super (name);
035: }
036:
037: public void testSaveBetweenLoadAndSave() throws Exception {
038: // Drop
039: dropTables("book");
040: // Create book
041: Book book = new Book("Learn Mutexes in 7 days", "1-2-3-5");
042: store.save(book);
043: // Load into list for later save
044: List result1 = store.find("find book");
045: assertEquals(1, result1.size());
046: Book book1 = (Book) result1.get(0);
047: // Save to another form
048: book.setTitle("Learn Semaphores in 7 days");
049: store.save(book);
050: // Save old object
051: book1
052: .setTitle("Learn Mutexes instead of Semaphores in 7 days.");
053: store.save(book1);
054: // Check whether object is again the old one
055: List result2 = store.find("find book");
056: assertEquals(1, result2.size());
057: assertEquals(book1, result2.get(0));
058: }
059:
060: public void testSaveBetweenSaveAndCommit() throws Exception {
061: // Drop
062: dropTables("book");
063: // Create book
064: Book book = new Book("Learn Mutexes in 7 days", "1-2-3-5");
065: store.save(book);
066: // Save but try to save in another transaction
067: TransactionTracker tt = store.getTransactionTracker();
068: Transaction tx1 = tt.getTransaction(TransactionTracker.TX_NEW);
069: tx1.begin();
070: book.setTitle("Learn Semaphores in 7 days");
071: store.save(book);
072: // Try to save again in another transaction
073: Transaction tx2 = tt.getTransaction(TransactionTracker.TX_NEW);
074: tx2.begin();
075: try {
076: store.save(book);
077: fail("could save book while another transaction is currently saving it.");
078: } catch (ConcurrentModificationException e) {
079: } finally {
080: tx2.commit();
081: tx1.commit();
082: }
083: }
084:
085: public void testTrySaveWhileRemovedAfterSelected() throws Exception {
086: // Drop
087: dropTables("book");
088: // Create book
089: Book book = new Book("Learn Mutexes in 7 days", "1-2-3-5");
090: store.save(book);
091: // Select another instance
092: List result = store.find("find book");
093: assertEquals(1, result.size());
094: Book book1 = (Book) result.get(0);
095: // Now remove it
096: store.remove(book);
097: // Try to save the second instance
098: book1.setTitle("Hamlet");
099: store.save(book1);
100: // Select
101: result = store.find("find book");
102: assertEquals(1, result.size());
103: assertEquals(book1, result.get(0));
104: }
105:
106: public void testCrossTransactionList() throws Exception {
107: TransactionTracker tt = store.getTransactionTracker();
108: // Drop
109: dropTables("book");
110: // Create book in a transaction
111: Transaction bookTx = tt
112: .getTransaction(TransactionTracker.TX_NEW);
113: bookTx.begin();
114: Book book = new Book("Title", "ISBN");
115: store.save(book);
116: List books = store.find("find book");
117: assertEquals(1, books.size()); // Should see the book
118: bookTx.commit();
119: // Now in another transaction remove the book
120: bookTx = tt.getTransaction(TransactionTracker.TX_NEW);
121: bookTx.begin();
122: store.remove(book);
123: ((LazyList) books).refresh();
124: assertEquals(1, books.size());
125: bookTx.commit();
126: }
127:
128: public void testCommitWhileSelect() throws Exception {
129: TransactionTracker tt = store.getTransactionTracker();
130: // Drop
131: dropTables("book");
132: // Create book in a transaction but do not commit yet
133: Transaction bookTx = tt
134: .getTransaction(TransactionTracker.TX_NEW);
135: bookTx.begin();
136: Book book = new Book("Title", "ISBN");
137: store.save(book);
138: // Now in another transaction, make a query for the books
139: Transaction queryTx = tt
140: .getTransaction(TransactionTracker.TX_NEW);
141: queryTx.begin();
142: List books = store.find("find book");
143: assertEquals(0, books.size());
144: queryTx.commit(); // Close the query transaction
145: // Now commit the book transaction
146: bookTx.commit();
147: // Now the query should not see the commited book, since
148: // it didn't saw it the first time
149: ((LazyList) books).refresh();
150: assertEquals(0, books.size());
151: }
152:
153: public void testNormalQueryInsensitive() throws Exception {
154: TransactionTracker tt = store.getTransactionTracker();
155: // Drop
156: dropTables("book");
157: // Start a transaction
158: Transaction bookTx = tt
159: .getTransaction(TransactionTracker.TX_NEW);
160: bookTx.begin();
161: List books = store.find("find book");
162: assertEquals(0, books.size());
163: // Now in another transaction, make a book
164: Transaction createTx = tt
165: .getTransaction(TransactionTracker.TX_NEW);
166: createTx.begin();
167: store.save(new Book("Book of Bokonon", "10"));
168: createTx.commit();
169: // Now, the query in the original transaction should not
170: // see this new book
171: books = store.find("find book");
172: assertEquals(0, books.size());
173: bookTx.commit();
174: }
175:
176: public void testNormalQuerySeesTransaction() throws Exception {
177: TransactionTracker tt = store.getTransactionTracker();
178: // Drop
179: dropTables("book");
180: // Start a transaction
181: Transaction bookTx = tt
182: .getTransaction(TransactionTracker.TX_NEW);
183: bookTx.begin();
184: // Now create a book in the same transaction
185: store.save(new Book("Book of Bokonon", "10"));
186: // Now, the query in the original transaction should see this new book
187: List books = store.find("find book");
188: assertEquals(1, books.size());
189: bookTx.commit();
190: }
191:
192: public void testHistoricalQueryInsensitive() throws Exception {
193: TransactionTracker tt = store.getTransactionTracker();
194: // Drop
195: dropTables("book");
196: // Start a transaction
197: Transaction bookTx = tt
198: .getTransaction(TransactionTracker.TX_NEW);
199: bookTx.begin();
200: List books = store.find("find book");
201: assertEquals(0, books.size());
202: // Now in another transaction, make a book
203: Transaction createTx = tt
204: .getTransaction(TransactionTracker.TX_NEW);
205: createTx.begin();
206: store.save(new Book("Book of Bokonon", "10"));
207: createTx.commit();
208: // Note the date and wait
209: Thread.currentThread().sleep(1000);
210: Date currentDate = new Date();
211: Thread.currentThread().sleep(1000);
212: // Now, the query in the original transaction should not
213: // see this new book
214: books = store.find("find book at ?",
215: new Object[] { currentDate });
216: assertEquals(0, books.size());
217: bookTx.commit();
218: }
219:
220: public void testHistoricalQuerySeesTransaction() throws Exception {
221: TransactionTracker tt = store.getTransactionTracker();
222: // Drop
223: dropTables("book");
224: // Start a transaction
225: Transaction bookTx = tt
226: .getTransaction(TransactionTracker.TX_NEW);
227: bookTx.begin();
228: // Now in the same transaction, make a book
229: store.save(new Book("Book of Bokonon", "10"));
230: // Note the date and wait
231: Thread.currentThread().sleep(1000);
232: Date currentDate = new Date();
233: Thread.currentThread().sleep(1000);
234: // Now, the query in the original transaction should
235: // see this new book
236: List books = store.find("find book at ?",
237: new Object[] { currentDate });
238: assertEquals(1, books.size());
239: bookTx.commit();
240: }
241: }
|