KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/test/org/apache/commons/transaction/memory/OptimisticMapWrapperTest.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.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.logging.*;
36
37 import org.apache.commons.transaction.util.Jdk14Logger;
38 import org.apache.commons.transaction.util.LoggerFacade;
39 import org.apache.commons.transaction.util.RendezvousBarrier;
40
41 /**
42  * Tests for map wrapper.
43  *
44  * @version $Revision$
45  */

46 public class OptimisticMapWrapperTest extends MapWrapperTest {
47
48     private static final Logger logger = Logger.getLogger(OptimisticMapWrapperTest.class.getName());
49     private static final LoggerFacade sLogger = new Jdk14Logger(logger);
50
51     public static Test suite() {
52         TestSuite suite = new TestSuite(OptimisticMapWrapperTest.class);
53         return suite;
54     }
55
56     public static void main(java.lang.String JavaDoc[] args) {
57         junit.textui.TestRunner.run(suite());
58     }
59
60     public OptimisticMapWrapperTest(String JavaDoc testName) {
61         super(testName);
62     }
63
64     protected TransactionalMapWrapper getNewWrapper(Map JavaDoc map) {
65         return new OptimisticMapWrapper(map);
66     }
67
68     // XXX no need for this code, just to make clear those tests are run as well
69
public void testBasic() throws Throwable JavaDoc {
70         super.testBasic();
71     }
72
73     public void testComplex() throws Throwable JavaDoc {
74         super.testComplex();
75     }
76
77     public void testSets() throws Throwable JavaDoc {
78         super.testSets();
79     }
80
81     public void testMulti() throws Throwable JavaDoc {
82         logger.info("Checking concurrent transaction features");
83
84         final Map JavaDoc map1 = new HashMap JavaDoc();
85
86         final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1);
87
88         final RendezvousBarrier beforeCommitBarrier =
89             new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);
90
91         final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);
92
93         Thread JavaDoc thread1 = new Thread JavaDoc(new Runnable JavaDoc() {
94             public void run() {
95                 txMap1.startTransaction();
96                 try {
97                     beforeCommitBarrier.meet();
98                     txMap1.put("key1", "value2");
99                     txMap1.commitTransaction();
100                     afterCommitBarrier.call();
101                 } catch (InterruptedException JavaDoc e) {
102                     logger.log(Level.WARNING, "Thread interrupted", e);
103                     afterCommitBarrier.reset();
104                     beforeCommitBarrier.reset();
105                 }
106             }
107         }, "Thread1");
108
109         txMap1.put("key1", "value1");
110
111         txMap1.startTransaction();
112         thread1.start();
113
114         report("value1", (String JavaDoc) txMap1.get("key1"));
115         beforeCommitBarrier.call();
116         afterCommitBarrier.meet();
117         // we have serializable as isolation level, that's why I will still see the old value
118
report("value1", (String JavaDoc) txMap1.get("key1"));
119
120         // now when I override it it should of course be my value
121
txMap1.put("key1", "value3");
122         report("value3", (String JavaDoc) txMap1.get("key1"));
123
124         // after rollback it must be the value written by the other thread
125
txMap1.rollbackTransaction();
126         report("value2", (String JavaDoc) txMap1.get("key1"));
127     }
128
129     public void testConflict() throws Throwable JavaDoc {
130         logger.info("Checking concurrent transaction features");
131
132         final Map JavaDoc map1 = new HashMap JavaDoc();
133
134         final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1);
135
136         final RendezvousBarrier beforeCommitBarrier =
137             new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);
138
139         final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);
140
141         Thread JavaDoc thread1 = new Thread JavaDoc(new Runnable JavaDoc() {
142             public void run() {
143                 txMap1.startTransaction();
144                 try {
145                     beforeCommitBarrier.meet();
146                     txMap1.put("key1", "value2");
147                     txMap1.commitTransaction();
148                     afterCommitBarrier.call();
149                 } catch (InterruptedException JavaDoc e) {
150                     logger.log(Level.WARNING, "Thread interrupted", e);
151                     afterCommitBarrier.reset();
152                     beforeCommitBarrier.reset();
153                 }
154             }
155         }, "Thread1");
156
157         txMap1.put("key1", "value1");
158
159         txMap1.startTransaction();
160         thread1.start();
161
162         report("value1", (String JavaDoc) txMap1.get("key1"));
163         beforeCommitBarrier.call();
164         afterCommitBarrier.meet();
165         // we have serializable as isolation level, that's why I will still see the old value
166
report("value1", (String JavaDoc) txMap1.get("key1"));
167
168         // now when I override it it should of course be my value
169
txMap1.put("key1", "value3");
170         report("value3", (String JavaDoc) txMap1.get("key1"));
171         
172         boolean conflict = false;
173         
174         try {
175             txMap1.commitTransaction();
176         } catch (ConflictException ce) {
177             conflict = true;
178         }
179         assertTrue(conflict);
180         // after failed commit it must be the value written by the other thread
181
report("value2", (String JavaDoc) map1.get("key1"));
182
183         // force commit anyhow...
184
txMap1.commitTransaction(true);
185         // after successful commit it must be the value written by this thread
186
report("value3", (String JavaDoc) txMap1.get("key1"));
187         report("value3", (String JavaDoc) map1.get("key1"));
188     }
189
190     public void testTxControl() throws Throwable JavaDoc {
191         super.testTxControl();
192     }
193
194 }
195
Popular Tags