001: package org.apache.ojb.broker.sequence;
002:
003: import org.apache.ojb.broker.PersistenceBroker;
004: import org.apache.ojb.broker.PersistenceBrokerFactory;
005: import org.apache.ojb.broker.query.Criteria;
006: import org.apache.ojb.broker.query.Query;
007: import org.apache.ojb.broker.query.QueryByCriteria;
008:
009: /**
010: * Test SequenceManager implementation with multiple threads.
011: *
012: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
013: * @version $Id: SMMultiThreadedTest.java,v 1.3 2004/04/05 17:13:27 arminw Exp $
014: */
015: public class SMMultiThreadedTest extends
016: org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase {
017: String goodName;
018: String badName;
019:
020: public SMMultiThreadedTest(String s) {
021: super (s);
022: }
023:
024: public static void main(String[] args) {
025: String[] arr = { SMMultiThreadedTest.class.getName() };
026: junit.textui.TestRunner.main(arr);
027: }
028:
029: protected void setUp() throws Exception {
030: super .setUp();
031: }
032:
033: protected void tearDown() throws Exception {
034: super .tearDown();
035: }
036:
037: public void testMultipleInsertAndRollback() {
038: int testObjects = 200;
039: int threads = 10;
040: TestCaseRunnable tct[] = new TestCaseRunnable[threads];
041: for (int i = 0; i < threads; i++) {
042: if (i % 2 == 1)
043: tct[i] = new BadThenGoodHandle(testObjects);
044: else
045: tct[i] = new GoodThenBadHandle(testObjects);
046: }
047: // run test classes
048: runTestCaseRunnables(tct);
049: checkGeneratedObjects(testObjects * threads);
050:
051: }
052:
053: private void checkGeneratedObjects(int testObjects) {
054: PersistenceBroker broker = PersistenceBrokerFactory
055: .defaultPersistenceBroker();
056: int res_1;
057: int res_2;
058: try {
059: Criteria crit = new Criteria();
060: crit.addLike("name", goodName + "%");
061: Criteria crit2 = new Criteria();
062: crit2.addLike("name", badName + "%");
063: crit.addOrCriteria(crit2);
064: Query q_1 = new QueryByCriteria(MTObjectA.class, crit);
065: Query q_2 = new QueryByCriteria(MTObjectB.class, crit);
066: res_1 = broker.getCount(q_1);
067: res_2 = broker.getCount(q_2);
068: } finally {
069: if (broker != null && !broker.isClosed())
070: broker.close();
071: }
072: assertEquals(testObjects, res_1);
073: assertEquals(testObjects, res_2);
074:
075: }
076:
077: class GoodThenBadHandle
078: extends
079: org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable {
080: int testObjects;
081:
082: PersistenceBroker broker;
083:
084: public GoodThenBadHandle(int testObjects) {
085: this .testObjects = testObjects;
086: goodName = "GoodThenBadHandle_"
087: + (long) (System.currentTimeMillis() * Math
088: .random()) + "_";
089: }
090:
091: void prepare() {
092: broker = PersistenceBrokerFactory
093: .defaultPersistenceBroker();
094: }
095:
096: void cleanup() {
097: if (broker != null && !broker.isClosed())
098: broker.close();
099: }
100:
101: public void runTestCase() throws Throwable {
102: prepare();
103: try {
104: broker.beginTransaction();
105: for (int i = testObjects - 1; i >= 0; i--) {
106: MTObjectA obj = new MTObjectA();
107: obj.setName(goodName + (i + 1));
108: MTObjectB obj_2 = new MTObjectB();
109: obj_2.setName(goodName + (i + 1));
110:
111: broker.store(obj);
112: broker.store(obj_2);
113: // Thread.sleep((int) (Math.random() * 5));
114: }
115: // Thread.sleep((int)(Math.random()*10));
116: broker.commitTransaction();
117:
118: broker.beginTransaction();
119: for (int i = testObjects - 1; i >= 0; i--) {
120: MTObjectA obj = new MTObjectA();
121: obj.setName(badName + (i + 1));
122: MTObjectB obj_2 = new MTObjectB();
123: obj_2.setName(badName + (i + 1));
124:
125: broker.store(obj);
126: broker.store(obj_2);
127: // Thread.sleep((int) (Math.random() * 5));
128: }
129: // Thread.sleep((int)(Math.random()*10));
130: broker.abortTransaction();
131: } finally {
132: cleanup();
133: }
134: }
135: }
136:
137: class BadThenGoodHandle
138: extends
139: org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable {
140: int testObjects;
141: PersistenceBroker broker;
142:
143: public BadThenGoodHandle(int testObjects) {
144: this .testObjects = testObjects;
145: badName = "BadThenGoodHandle_" + System.currentTimeMillis()
146: + "_";
147: }
148:
149: void prepare() {
150: broker = PersistenceBrokerFactory
151: .defaultPersistenceBroker();
152: }
153:
154: void cleanup() {
155: if (broker != null && !broker.isClosed())
156: broker.close();
157: }
158:
159: public void runTestCase() throws Throwable {
160: prepare();
161: try {
162: broker.beginTransaction();
163: for (int i = testObjects - 1; i >= 0; i--) {
164: MTObjectA obj = new MTObjectA();
165: obj.setName(badName + (i + 1));
166: MTObjectB obj_2 = new MTObjectB();
167: obj_2.setName(badName + (i + 1));
168:
169: broker.store(obj);
170: broker.store(obj_2);
171: // Thread.sleep((int) (Math.random() * 5));
172: }
173: // Thread.sleep((int)(Math.random()*10));
174: broker.abortTransaction();
175:
176: broker.beginTransaction();
177: for (int i = testObjects - 1; i >= 0; i--) {
178: MTObjectA obj = new MTObjectA();
179: obj.setName(badName + (i + 1));
180: MTObjectB obj_2 = new MTObjectB();
181: obj_2.setName(badName + (i + 1));
182:
183: broker.store(obj);
184: broker.store(obj_2);
185: // Thread.sleep((int) (Math.random() * 5));
186: }
187: // Thread.sleep((int)(Math.random()*10));
188: broker.commitTransaction();
189: } finally {
190: cleanup();
191: }
192: }
193: }
194:
195: public static class MTObjectA {
196: private Integer objId;
197: private String name;
198:
199: public Integer getObjId() {
200: return objId;
201: }
202:
203: public void setObjId(Integer objId) {
204: this .objId = objId;
205: }
206:
207: public String getName() {
208: return name;
209: }
210:
211: public void setName(String name) {
212: this .name = name;
213: }
214: }
215:
216: public static class MTObjectB extends MTObjectA {
217:
218: }
219: }
|