KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > memory > MapWrapperTest


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/test/org/apache/commons/transaction/memory/MapWrapperTest.java,v 1.1 2004/11/18 23:27:19 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.1 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.memory;
30
31 import junit.framework.*;
32
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.logging.*;
38
39 import org.apache.commons.transaction.util.Jdk14Logger;
40 import org.apache.commons.transaction.util.LoggerFacade;
41 import org.apache.commons.transaction.util.RendezvousBarrier;
42
43 /**
44  * Tests for map wrapper.
45  *
46  * @version $Revision$
47  */

48 public class MapWrapperTest extends TestCase {
49
50     private static final Logger logger = Logger.getLogger(MapWrapperTest.class.getName());
51     private static final LoggerFacade sLogger = new Jdk14Logger(logger);
52
53     protected static final long BARRIER_TIMEOUT = 20000;
54
55     // XXX need this, as JUnit seems to print only part of these strings
56
protected static void report(String JavaDoc should, String JavaDoc is) {
57         if (!should.equals(is)) {
58             fail("\nWrong output:\n'" + is + "'\nShould be:\n'" + should + "'\n");
59         }
60     }
61
62     protected static void checkCollection(Collection JavaDoc col, Object JavaDoc[] values) {
63         int cnt = 0;
64         int trueCnt = 0;
65
66         for (Iterator JavaDoc it = col.iterator(); it.hasNext();) {
67             cnt++;
68             Object JavaDoc value1 = it.next();
69             for (int i = 0; i < values.length; i++) {
70                 Object JavaDoc value2 = values[i];
71                 if (value2.equals(value1))
72                     trueCnt++;
73             }
74         }
75         assertEquals(cnt, values.length);
76         assertEquals(trueCnt, values.length);
77     }
78
79     public static Test suite() {
80         TestSuite suite = new TestSuite(MapWrapperTest.class);
81         return suite;
82     }
83
84     public static void main(java.lang.String JavaDoc[] args) {
85         junit.textui.TestRunner.run(suite());
86     }
87
88     public MapWrapperTest(String JavaDoc testName) {
89         super(testName);
90     }
91
92     protected TransactionalMapWrapper getNewWrapper(Map JavaDoc map) {
93         return new TransactionalMapWrapper(map);
94     }
95
96     public void testBasic() throws Throwable JavaDoc {
97
98         logger.info("Checking basic transaction features");
99
100         final Map JavaDoc map1 = new HashMap JavaDoc();
101
102         final TransactionalMapWrapper txMap1 = getNewWrapper(map1);
103
104         assertTrue(txMap1.isEmpty());
105
106         // make sure changes are propagated to wrapped map outside tx
107
txMap1.put("key1", "value1");
108         report("value1", (String JavaDoc) map1.get("key1"));
109         assertFalse(txMap1.isEmpty());
110
111         // make sure changes are progated to wrapped map only after commit
112
txMap1.startTransaction();
113         assertFalse(txMap1.isEmpty());
114         txMap1.put("key1", "value2");
115         report("value1", (String JavaDoc) map1.get("key1"));
116         report("value2", (String JavaDoc) txMap1.get("key1"));
117         txMap1.commitTransaction();
118         report("value2", (String JavaDoc) map1.get("key1"));
119         report("value2", (String JavaDoc) txMap1.get("key1"));
120
121         // make sure changes are reverted after rollback
122
txMap1.startTransaction();
123         txMap1.put("key1", "value3");
124         txMap1.rollbackTransaction();
125         report("value2", (String JavaDoc) map1.get("key1"));
126         report("value2", (String JavaDoc) txMap1.get("key1"));
127     }
128
129     public void testComplex() throws Throwable JavaDoc {
130
131         logger.info("Checking advanced and complex transaction features");
132
133         final Map JavaDoc map1 = new HashMap JavaDoc();
134
135         final TransactionalMapWrapper txMap1 = getNewWrapper(map1);
136
137         // first fill in some global values:
138
txMap1.put("key1", "value1");
139         txMap1.put("key2", "value2");
140
141         // let's see if we have all values:
142
logger.info("Checking if global values are present");
143
144         assertTrue(txMap1.containsValue("value1"));
145         assertTrue(txMap1.containsValue("value2"));
146         assertFalse(txMap1.containsValue("novalue"));
147
148         // ... and all keys
149
logger.info("Checking if global keys are present");
150         assertTrue(txMap1.containsKey("key1"));
151         assertTrue(txMap1.containsKey("key2"));
152         assertFalse(txMap1.containsKey("nokey"));
153
154         // and now some inside a transaction
155
txMap1.startTransaction();
156         txMap1.put("key3", "value3");
157         txMap1.put("key4", "value4");
158
159         // let's see if we have all values:
160
logger.info("Checking if values inside transactions are present");
161         assertTrue(txMap1.containsValue("value1"));
162         assertTrue(txMap1.containsValue("value2"));
163         assertTrue(txMap1.containsValue("value3"));
164         assertTrue(txMap1.containsValue("value4"));
165         assertFalse(txMap1.containsValue("novalue"));
166
167         // ... and all keys
168
logger.info("Checking if keys inside transactions are present");
169         assertTrue(txMap1.containsKey("key1"));
170         assertTrue(txMap1.containsKey("key2"));
171         assertTrue(txMap1.containsKey("key3"));
172         assertTrue(txMap1.containsKey("key4"));
173         assertFalse(txMap1.containsKey("nokey"));
174
175         // now let's delete some old stuff
176
logger.info("Checking remove inside transactions");
177         txMap1.remove("key1");
178         assertFalse(txMap1.containsKey("key1"));
179         assertFalse(txMap1.containsValue("value1"));
180         assertNull(txMap1.get("key1"));
181         assertEquals(3, txMap1.size());
182
183         // and some newly created
184
txMap1.remove("key3");
185         assertFalse(txMap1.containsKey("key3"));
186         assertFalse(txMap1.containsValue("value3"));
187         assertNull(txMap1.get("key3"));
188         assertEquals(2, txMap1.size());
189
190         logger.info("Checking remove and propagation after commit");
191         txMap1.commitTransaction();
192
193         txMap1.remove("key1");
194         assertFalse(txMap1.containsKey("key1"));
195         assertFalse(txMap1.containsValue("value1"));
196         assertNull(txMap1.get("key1"));
197         assertFalse(txMap1.containsKey("key3"));
198         assertFalse(txMap1.containsValue("value3"));
199         assertNull(txMap1.get("key3"));
200         assertEquals(2, txMap1.size());
201     }
202
203     public void testSets() throws Throwable JavaDoc {
204
205         logger.info("Checking set opertaions");
206
207         final Map JavaDoc map1 = new HashMap JavaDoc();
208
209         final TransactionalMapWrapper txMap1 = getNewWrapper(map1);
210
211         // first fill in some global values:
212
txMap1.put("key1", "value1");
213         txMap1.put("key2", "value200");
214
215         // and now some inside a transaction
216
txMap1.startTransaction();
217         txMap1.put("key2", "value2"); // modify
218
txMap1.put("key3", "value3");
219         txMap1.put("key4", "value4");
220
221         // check entry set
222
boolean key1P, key2P, key3P, key4P;
223         key1P = key2P = key3P = key4P = false;
224         int cnt = 0;
225         for (Iterator JavaDoc it = txMap1.entrySet().iterator(); it.hasNext();) {
226             cnt++;
227             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
228             if (entry.getKey().equals("key1") && entry.getValue().equals("value1"))
229                 key1P = true;
230             else if (entry.getKey().equals("key2") && entry.getValue().equals("value2"))
231                 key2P = true;
232             else if (entry.getKey().equals("key3") && entry.getValue().equals("value3"))
233                 key3P = true;
234             else if (entry.getKey().equals("key4") && entry.getValue().equals("value4"))
235                 key4P = true;
236         }
237         assertEquals(cnt, 4);
238         assertTrue(key1P && key2P && key3P && key4P);
239
240         checkCollection(txMap1.values(), new String JavaDoc[] { "value1", "value2", "value3", "value4" });
241         checkCollection(txMap1.keySet(), new String JavaDoc[] { "key1", "key2", "key3", "key4" });
242
243         txMap1.commitTransaction();
244
245         // check again after commit (should be the same)
246
key1P = key2P = key3P = key4P = false;
247         cnt = 0;
248         for (Iterator JavaDoc it = txMap1.entrySet().iterator(); it.hasNext();) {
249             cnt++;
250             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
251             if (entry.getKey().equals("key1") && entry.getValue().equals("value1"))
252                 key1P = true;
253             else if (entry.getKey().equals("key2") && entry.getValue().equals("value2"))
254                 key2P = true;
255             else if (entry.getKey().equals("key3") && entry.getValue().equals("value3"))
256                 key3P = true;
257             else if (entry.getKey().equals("key4") && entry.getValue().equals("value4"))
258                 key4P = true;
259         }
260         assertEquals(cnt, 4);
261         assertTrue(key1P && key2P && key3P && key4P);
262
263         checkCollection(txMap1.values(), new String JavaDoc[] { "value1", "value2", "value3", "value4" });
264         checkCollection(txMap1.keySet(), new String JavaDoc[] { "key1", "key2", "key3", "key4" });
265
266         // now try clean
267

268         txMap1.startTransaction();
269
270         // add
271
txMap1.put("key5", "value5");
272         // modify
273
txMap1.put("key4", "value400");
274         // delete
275
txMap1.remove("key1");
276
277         assertEquals(txMap1.size(), 4);
278
279         txMap1.clear();
280         assertEquals(txMap1.size(), 0);
281         assertEquals(map1.size(), 4);
282
283         // add
284
txMap1.put("key5", "value5");
285         // delete
286
txMap1.remove("key1");
287
288         // adding one, not removing anything gives size 1
289
assertEquals(txMap1.size(), 1);
290         assertEquals(map1.size(), 4);
291         assertNull(txMap1.get("key4"));
292         assertNotNull(txMap1.get("key5"));
293
294         txMap1.commitTransaction();
295
296         // after commit clear must have been propagated to wrapped map:
297
assertEquals(txMap1.size(), 1);
298         assertEquals(map1.size(), 1);
299         assertNull(txMap1.get("key4"));
300         assertNotNull(txMap1.get("key5"));
301         assertNull(map1.get("key4"));
302         assertNotNull(map1.get("key5"));
303     }
304
305     public void testMulti() throws Throwable JavaDoc {
306         logger.info("Checking concurrent transaction features");
307
308         final Map JavaDoc map1 = new HashMap JavaDoc();
309
310         final TransactionalMapWrapper txMap1 = getNewWrapper(map1);
311
312         final RendezvousBarrier beforeCommitBarrier =
313             new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);
314
315         final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);
316
317         Thread JavaDoc thread1 = new Thread JavaDoc(new Runnable JavaDoc() {
318             public void run() {
319                 txMap1.startTransaction();
320                 try {
321                     beforeCommitBarrier.meet();
322                     txMap1.put("key1", "value2");
323                     txMap1.commitTransaction();
324                     afterCommitBarrier.call();
325                 } catch (InterruptedException JavaDoc e) {
326                     logger.log(Level.WARNING, "Thread interrupted", e);
327                     afterCommitBarrier.reset();
328                     beforeCommitBarrier.reset();
329                 }
330             }
331         }, "Thread1");
332
333         txMap1.put("key1", "value1");
334
335         txMap1.startTransaction();
336         thread1.start();
337
338         report("value1", (String JavaDoc) txMap1.get("key1"));
339         beforeCommitBarrier.call();
340         afterCommitBarrier.meet();
341         // we have read committed as isolation level, that's why I will see the new value of the other thread now
342
report("value2", (String JavaDoc) txMap1.get("key1"));
343
344         // now when I override it it should of course be my value again
345
txMap1.put("key1", "value3");
346         report("value3", (String JavaDoc) txMap1.get("key1"));
347
348         // after rollback it must be the value written by the other thread again
349
txMap1.rollbackTransaction();
350         report("value2", (String JavaDoc) txMap1.get("key1"));
351     }
352
353     public void testTxControl() throws Throwable JavaDoc {
354         logger.info("Checking advanced transaction control (heavily used in JCA implementation)");
355
356         final Map JavaDoc map1 = new HashMap JavaDoc();
357
358         final TransactionalMapWrapper txMap1 = getNewWrapper(map1);
359
360         assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_NO_TRANSACTION);
361         txMap1.startTransaction();
362         assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_ACTIVE);
363
364         assertTrue(txMap1.isReadOnly());
365         txMap1.put("key", "value");
366         assertFalse(txMap1.isReadOnly());
367
368         assertFalse(txMap1.isTransactionMarkedForRollback());
369         txMap1.markTransactionForRollback();
370         assertTrue(txMap1.isTransactionMarkedForRollback());
371
372         boolean failed = false;
373         try {
374             txMap1.commitTransaction();
375         } catch (IllegalStateException JavaDoc ise) {
376             failed = true;
377         }
378         assertTrue(failed);
379         txMap1.rollbackTransaction();
380         assertEquals(txMap1.getTransactionState(), TransactionalMapWrapper.STATUS_NO_TRANSACTION);
381
382         txMap1.startTransaction();
383         final TransactionalMapWrapper.TxContext ctx = txMap1.suspendTransaction();
384         final RendezvousBarrier afterSuspendBarrier =
385             new RendezvousBarrier("After Suspend", 2, BARRIER_TIMEOUT, sLogger);
386
387         new Thread JavaDoc(new Runnable JavaDoc() {
388             public void run() {
389                 txMap1.resumeTransaction(ctx);
390                 txMap1.put("key2", "value2");
391                 txMap1.suspendTransaction();
392                 afterSuspendBarrier.call();
393             }
394         }).start();
395
396         afterSuspendBarrier.meet();
397         txMap1.resumeTransaction(ctx);
398
399         assertEquals(txMap1.size(), 1);
400         txMap1.put("key3", "value3");
401         assertEquals(txMap1.size(), 2);
402         assertEquals(map1.size(), 0);
403
404         txMap1.commitTransaction();
405         assertEquals(txMap1.size(), 2);
406         assertEquals(map1.size(), 2);
407     }
408
409 }
410
Popular Tags