KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > sequence > SMMultiThreadedTest


1 package org.apache.ojb.broker.sequence;
2
3 import org.apache.ojb.broker.PersistenceBroker;
4 import org.apache.ojb.broker.PersistenceBrokerFactory;
5 import org.apache.ojb.broker.query.Criteria;
6 import org.apache.ojb.broker.query.Query;
7 import org.apache.ojb.broker.query.QueryByCriteria;
8
9 /**
10  * Test SequenceManager implementation with multiple threads.
11  *
12  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
13  * @version $Id: SMMultiThreadedTest.java,v 1.3 2004/04/05 17:13:27 arminw Exp $
14  */

15 public class SMMultiThreadedTest extends org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase
16 {
17     String JavaDoc goodName;
18     String JavaDoc badName;
19
20     public SMMultiThreadedTest(String JavaDoc s)
21     {
22         super(s);
23     }
24
25     public static void main(String JavaDoc[] args)
26     {
27         String JavaDoc[] arr = {SMMultiThreadedTest.class.getName()};
28         junit.textui.TestRunner.main(arr);
29     }
30
31     protected void setUp() throws Exception JavaDoc
32     {
33         super.setUp();
34     }
35
36     protected void tearDown() throws Exception JavaDoc
37     {
38         super.tearDown();
39     }
40
41     public void testMultipleInsertAndRollback()
42     {
43         int testObjects = 200;
44         int threads = 10;
45         TestCaseRunnable tct [] = new TestCaseRunnable[threads];
46         for (int i = 0; i < threads; i++)
47         {
48             if (i % 2 == 1)
49                 tct[i] = new BadThenGoodHandle(testObjects);
50             else
51                 tct[i] = new GoodThenBadHandle(testObjects);
52         }
53         // run test classes
54
runTestCaseRunnables(tct);
55         checkGeneratedObjects(testObjects*threads);
56
57     }
58
59     private void checkGeneratedObjects(int testObjects)
60     {
61         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
62         int res_1;
63         int res_2;
64         try
65         {
66             Criteria crit = new Criteria();
67             crit.addLike("name", goodName+"%");
68             Criteria crit2 = new Criteria();
69             crit2.addLike("name", badName+"%");
70             crit.addOrCriteria(crit2);
71             Query q_1 = new QueryByCriteria(MTObjectA.class, crit);
72             Query q_2 = new QueryByCriteria(MTObjectB.class, crit);
73             res_1 = broker.getCount(q_1);
74             res_2 = broker.getCount(q_2);
75         }
76         finally
77         {
78             if(broker != null && !broker.isClosed()) broker.close();
79         }
80         assertEquals(testObjects, res_1);
81         assertEquals(testObjects, res_2);
82
83     }
84
85
86     class GoodThenBadHandle extends org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
87     {
88         int testObjects;
89
90         PersistenceBroker broker;
91
92         public GoodThenBadHandle(int testObjects)
93         {
94             this.testObjects = testObjects;
95             goodName = "GoodThenBadHandle_" + (long)(System.currentTimeMillis()*Math.random()) + "_";
96         }
97
98         void prepare()
99         {
100             broker = PersistenceBrokerFactory.defaultPersistenceBroker();
101         }
102
103         void cleanup()
104         {
105             if (broker != null && !broker.isClosed()) broker.close();
106         }
107
108         public void runTestCase() throws Throwable JavaDoc
109         {
110             prepare();
111             try
112             {
113                 broker.beginTransaction();
114                 for (int i = testObjects - 1; i >= 0; i--)
115                 {
116                     MTObjectA obj = new MTObjectA();
117                     obj.setName(goodName + (i + 1));
118                     MTObjectB obj_2 = new MTObjectB();
119                     obj_2.setName(goodName + (i + 1));
120
121                     broker.store(obj);
122                     broker.store(obj_2);
123                     // Thread.sleep((int) (Math.random() * 5));
124
}
125                 // Thread.sleep((int)(Math.random()*10));
126
broker.commitTransaction();
127
128                 broker.beginTransaction();
129                 for (int i = testObjects - 1; i >= 0; i--)
130                 {
131                     MTObjectA obj = new MTObjectA();
132                     obj.setName(badName + (i + 1));
133                     MTObjectB obj_2 = new MTObjectB();
134                     obj_2.setName(badName + (i + 1));
135
136                     broker.store(obj);
137                     broker.store(obj_2);
138                     // Thread.sleep((int) (Math.random() * 5));
139
}
140                 // Thread.sleep((int)(Math.random()*10));
141
broker.abortTransaction();
142             }
143             finally
144             {
145                 cleanup();
146             }
147         }
148     }
149
150     class BadThenGoodHandle extends org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable
151     {
152         int testObjects;
153         PersistenceBroker broker;
154
155         public BadThenGoodHandle(int testObjects)
156         {
157             this.testObjects = testObjects;
158             badName = "BadThenGoodHandle_" + System.currentTimeMillis() + "_";
159         }
160
161         void prepare()
162         {
163             broker = PersistenceBrokerFactory.defaultPersistenceBroker();
164         }
165
166         void cleanup()
167         {
168             if (broker != null && !broker.isClosed()) broker.close();
169         }
170
171         public void runTestCase() throws Throwable JavaDoc
172         {
173             prepare();
174             try
175             {
176                 broker.beginTransaction();
177                 for (int i = testObjects - 1; i >= 0; i--)
178                 {
179                     MTObjectA obj = new MTObjectA();
180                     obj.setName(badName + (i + 1));
181                     MTObjectB obj_2 = new MTObjectB();
182                     obj_2.setName(badName + (i + 1));
183
184                     broker.store(obj);
185                     broker.store(obj_2);
186                     // Thread.sleep((int) (Math.random() * 5));
187
}
188                 // Thread.sleep((int)(Math.random()*10));
189
broker.abortTransaction();
190
191                 broker.beginTransaction();
192                 for (int i = testObjects - 1; i >= 0; i--)
193                 {
194                     MTObjectA obj = new MTObjectA();
195                     obj.setName(badName + (i + 1));
196                     MTObjectB obj_2 = new MTObjectB();
197                     obj_2.setName(badName + (i + 1));
198
199                     broker.store(obj);
200                     broker.store(obj_2);
201                     // Thread.sleep((int) (Math.random() * 5));
202
}
203                 // Thread.sleep((int)(Math.random()*10));
204
broker.commitTransaction();
205             }
206             finally
207             {
208                 cleanup();
209             }
210         }
211     }
212
213     public static class MTObjectA
214     {
215         private Integer JavaDoc objId;
216         private String JavaDoc name;
217
218         public Integer JavaDoc getObjId()
219         {
220             return objId;
221         }
222
223         public void setObjId(Integer JavaDoc objId)
224         {
225             this.objId = objId;
226         }
227
228         public String JavaDoc getName()
229         {
230             return name;
231         }
232
233         public void setName(String JavaDoc name)
234         {
235             this.name = name;
236         }
237     }
238
239     public static class MTObjectB extends MTObjectA
240     {
241
242     }
243 }
244
Popular Tags