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 15 public class SMMultiThreadedTest extends org.apache.ojb.junit.JUnitExtensions.MultiThreadedTestCase 16 { 17 String goodName; 18 String badName; 19 20 public SMMultiThreadedTest(String s) 21 { 22 super(s); 23 } 24 25 public static void main(String [] args) 26 { 27 String [] arr = {SMMultiThreadedTest.class.getName()}; 28 junit.textui.TestRunner.main(arr); 29 } 30 31 protected void setUp() throws Exception 32 { 33 super.setUp(); 34 } 35 36 protected void tearDown() throws Exception 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 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 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 } 125 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 } 140 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 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 } 188 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 } 203 broker.commitTransaction(); 205 } 206 finally 207 { 208 cleanup(); 209 } 210 } 211 } 212 213 public static class MTObjectA 214 { 215 private Integer objId; 216 private String name; 217 218 public Integer getObjId() 219 { 220 return objId; 221 } 222 223 public void setObjId(Integer objId) 224 { 225 this.objId = objId; 226 } 227 228 public String getName() 229 { 230 return name; 231 } 232 233 public void setName(String name) 234 { 235 this.name = name; 236 } 237 } 238 239 public static class MTObjectB extends MTObjectA 240 { 241 242 } 243 } 244 | Popular Tags |