1 package org.apache.ojb.broker.metadata; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 8 import org.apache.commons.lang.ClassUtils; 9 import org.apache.ojb.broker.*; 10 import org.apache.ojb.broker.accesslayer.OJBIterator; 11 import org.apache.ojb.broker.query.Query; 12 import org.apache.ojb.broker.query.QueryByCriteria; 13 import org.apache.ojb.broker.query.QueryFactory; 14 import org.apache.ojb.broker.sequence.Repository; 15 import org.apache.ojb.broker.util.ClassHelper; 16 import org.apache.ojb.broker.util.logging.Logger; 17 import org.apache.ojb.broker.util.logging.LoggerFactory; 18 import org.apache.ojb.junit.JUnitExtensions; 19 20 25 public class MetadataMultithreadedTest extends JUnitExtensions.MultiThreadedTestCase 26 { 27 private String newTestObjectString = "SM_TAB_MAX_AA"; 29 private Class targetTestClass = Repository.SMMaxA.class; 30 int loops = 7; 31 int threads = 4; 32 int minimalFreeMemAfterTest = 80; 35 36 private String oldTestObjectString; 37 DescriptorRepository defaultRepository; 38 39 protected final Logger logger = LoggerFactory.getLogger(this.getClass()); 40 41 public MetadataMultithreadedTest(String s) 42 { 43 super(s); 44 } 45 46 public static void main(String [] args) 47 { 48 String [] arr = {MetadataMultithreadedTest.class.getName()}; 49 junit.textui.TestRunner.main(arr); 50 } 51 52 private long getTotalMemory() 53 { 54 long result = Long.MAX_VALUE; 55 return result; 58 } 59 60 protected void setUp() throws Exception 61 { 62 super.setUp(); 63 MetadataManager mm = MetadataManager.getInstance(); 64 mm.setEnablePerThreadChanges(true); 66 defaultRepository = mm.copyOfGlobalRepository(); 67 } 68 69 protected void tearDown() throws Exception 70 { 71 super.tearDown(); 72 MetadataManager.getInstance().setEnablePerThreadChanges(false); 73 } 74 75 private String getTestObjectString() 76 { 77 return oldTestObjectString; 78 } 79 80 public void testProxiedLoading() throws Exception 81 { 82 PersistenceBroker broker = null; 83 try 84 { 85 MetadataManager mm = MetadataManager.getInstance(); 86 DescriptorRepository repository = mm.getRepository(); 88 String profileKey = "TestMappings"; 89 mm.addProfile(profileKey, repository); 90 91 mm.setDescriptor(defaultRepository); 93 94 ProductGroupWithCollectionProxy pgTemplate = new ProductGroupWithCollectionProxy(); 95 pgTemplate.setGroupId(new Integer (6)); 96 Query query = QueryFactory.newQueryByExample(pgTemplate); 97 98 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 99 Collection groups; 100 Iterator groupIter; 101 ProductGroupWithCollectionProxy pg; 102 103 assertNotNull(groupIter = (OJBIterator) broker.getIteratorByQuery(query)); 104 assertTrue(groupIter.hasNext()); 105 106 Throwable expectedThrowable = null; 109 try { 110 System.err.println("------ The following exception is part of the tests..."); 111 groupIter.next(); 112 } catch (Throwable t) { 113 expectedThrowable = t; 114 System.err.println("------"); 115 } 116 assertNotNull("Should get metadata exception from proxy", expectedThrowable); 117 ((OJBIterator) groupIter).releaseDbResources(); 118 119 broker.clearCache(); 121 mm.loadProfile(profileKey); 122 assertNotNull(groups = broker.getCollectionByQuery(query)); 123 assertEquals(1, groups.size()); 124 assertNotNull(groupIter = groups.iterator()); 125 assertTrue(groupIter.hasNext()); 126 assertNotNull(pg = (ProductGroupWithCollectionProxy) groupIter.next()); 127 assertFalse(groupIter.hasNext()); 128 assertEquals(pgTemplate.getGroupId(), pg.getGroupId()); 129 Collection articles; 130 assertNotNull(articles = pg.getAllArticlesInGroup()); 131 assertEquals(6, articles.size()); 132 133 TestCaseRunnable tct [] = new TestCaseRunnable[]{new LazyLoading(articles)}; 134 runTestCaseRunnables(tct); 135 } 136 finally 137 { 138 if (broker != null) broker.close(); 139 } 140 141 } 142 143 149 public void testCollectionProxySwapProfiles() throws Exception 150 { 151 PersistenceBroker broker = null; 152 try 153 { 154 MetadataManager mm = MetadataManager.getInstance(); 155 156 DescriptorRepository repository = mm.getRepository(); 158 String profileKey = "TestMappings"; 159 mm.addProfile(profileKey, repository); 160 mm.loadProfile(profileKey); 161 162 ProductGroupWithCollectionProxy pgTemplate = new ProductGroupWithCollectionProxy(); 164 pgTemplate.setGroupId(new Integer (6)); 165 Query query = QueryFactory.newQueryByExample(pgTemplate); 166 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 167 ProductGroupWithCollectionProxy productGroup; 168 assertNotNull(productGroup = 169 (ProductGroupWithCollectionProxy) broker.getObjectByQuery(query)); 170 171 broker.close(); 173 174 final String emptyKey = "EMPTY"; 176 DescriptorRepository emptyDr = new DescriptorRepository(); 177 mm.addProfile(emptyKey, emptyDr); 178 mm.loadProfile(emptyKey); 179 180 List collectionProxy = productGroup.getAllArticlesInGroup(); 181 assertNotNull(collectionProxy); 182 183 assertNotNull(collectionProxy.get(0)); 186 } 187 finally 188 { 189 if (broker != null) broker.close(); 190 } 191 } 192 193 public void testRuntimeMetadataChanges() throws Exception 194 { 195 PersistenceBroker broker = null; 196 try 197 { 198 MetadataManager.getInstance().setDescriptor(defaultRepository); 199 200 ClassDescriptor cld; 201 long memoryUseBeforeTest; 202 long memoryUseAfterTest; 203 try 204 { 205 long period = System.currentTimeMillis(); 207 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 208 cld = broker.getClassDescriptor(targetTestClass); 209 210 oldTestObjectString = cld.getFullTableName(); 213 broker.close(); 214 215 Runtime.getRuntime().gc(); 217 Thread.sleep(200); 218 Runtime.getRuntime().gc(); 219 220 long memory = Runtime.getRuntime().freeMemory(); 222 long totalMemory = getTotalMemory(); 223 224 int count = 0; 225 for (int k = 0; k < loops; k++) 226 { 227 TestCaseRunnable tct [] = new TestCaseRunnable[threads]; 228 for (int i = 0; i < threads; i++) 229 { 230 if (i % 2 == 0) 231 tct[i] = new ThreadedUsingBroker(loops); 232 else 233 tct[i] = new GlobalUsingBroker(loops); 234 } 235 runTestCaseRunnables(tct); 237 ++count; 238 if (logger.isDebugEnabled()) 239 { 240 logger.debug("Free/total Memory after loop " + count + ": " 241 + convertToMB(Runtime.getRuntime().freeMemory()) 242 + "/" + convertToMB(getTotalMemory()) + "MB"); 243 } 244 } 245 period = System.currentTimeMillis() - period; 246 if (logger.isDebugEnabled()) 247 { 248 logger.debug(ClassUtils.getShortClassName(MetadataMultithreadedTest.class) + " take: " 249 + period + " ms for " + loops + " loops, creating each with " + threads + " threads"); 250 logger.debug("Free/total Memory before test: " 251 + convertToMB(memory) + "/" + convertToMB(totalMemory) + "MB"); 252 } 253 Runtime.getRuntime().gc(); 254 Thread.sleep(200); 255 Runtime.getRuntime().gc(); 256 Runtime.getRuntime().gc(); 257 258 memoryUseBeforeTest = convertToMB(memory); 259 memoryUseAfterTest = convertToMB(Runtime.getRuntime().freeMemory()); 260 if (logger.isDebugEnabled()) 261 { 262 logger.debug("Free/total Memory after test and gc: " 263 + memoryUseAfterTest 264 + "/" + convertToMB(getTotalMemory()) + "MB"); 265 logger.debug("Do cleanup now ..."); 266 } 267 } 268 finally 269 { 270 MetadataManager.getInstance().setEnablePerThreadChanges(false); 271 } 272 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 274 cld = broker.getClassDescriptor(targetTestClass); 275 String name = cld.getFullTableName(); 276 assertEquals(oldTestObjectString, name); 277 assertFalse(MetadataManager.getInstance().isEnablePerThreadChanges()); 278 double d = ((double) memoryUseAfterTest) / ((double) memoryUseBeforeTest); 279 int result = (int) (d * 100); 280 if (result < minimalFreeMemAfterTest) 281 { 282 fail("** When using a offical version of OJB, ignore this failure! **" + 283 " Memory usage after this test differs more than "+(100 - minimalFreeMemAfterTest) 284 +"% from beginning, this may indicate" + 285 " a memory leak (GC can't free unused metadata objects), but this could also be a result" + 286 " of your JVM settings. Please re-run test."); 287 } 288 } 289 finally 290 { 291 if (broker != null) broker.close(); 292 } 293 } 294 295 private long convertToMB(long byteValue) 296 { 297 return (byteValue / 1024) / 1024; 298 } 299 300 class ThreadedUsingBroker extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable 304 { 305 int loops; 306 String title = "ThreadedUsingBroker_" + System.currentTimeMillis(); 307 308 public ThreadedUsingBroker() 309 { 310 } 311 312 public ThreadedUsingBroker(int loops) 313 { 314 this.loops = loops; 315 } 316 317 public void runTestCase() throws Exception 318 { 319 MetadataManager mm = MetadataManager.getInstance(); 320 DescriptorRepository dr = mm.copyOfGlobalRepository(); 321 ClassDescriptor cld = dr.getDescriptorFor(targetTestClass); 322 cld.setTableName(newTestObjectString); 324 mm.setDescriptor(dr); 326 327 int k = 0; 328 while (k < loops) 329 { 330 PersistenceBroker broker = null; 331 try 332 { 333 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 334 cld = broker.getClassDescriptor(targetTestClass); 335 String name = cld.getFullTableName(); 336 assertEquals(newTestObjectString, name); 337 assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges()); 338 } 339 finally 340 { 341 if (broker != null) broker.close(); 342 } 343 344 try 345 { 346 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 347 cld = broker.getClassDescriptor(targetTestClass); 349 String name = cld.getFullTableName(); 350 assertEquals(newTestObjectString, name); 351 assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges()); 352 353 Query query = new QueryByCriteria(Person.class, null, true); 355 broker.getCollectionByQuery(query); 356 361 Project project = new Project(); 362 project.setTitle(title); 363 364 Person p1 = new Person(); 365 p1.setFirstname(title); 366 List l1 = new ArrayList (); 367 l1.add(project); 368 p1.setProjects(l1); 369 370 Person p2 = new Person(); 371 p2.setFirstname(title); 372 List l2 = new ArrayList (); 373 l2.add(project); 374 p2.setProjects(l2); 375 376 Role r1 = new Role(); 377 r1.setPerson(p1); 378 r1.setRoleName(title); 379 r1.setProject(project); 380 List roles1 = new ArrayList (); 381 roles1.add(r1); 382 383 Role r2 = new Role(); 384 r2.setPerson(p2); 385 r2.setRoleName(title); 386 r2.setProject(project); 387 List roles2 = new ArrayList (); 388 roles2.add(r2); 389 390 p1.setRoles(roles1); 391 p2.setRoles(roles2); 392 393 Object obj = ClassHelper.newInstance(targetTestClass); 394 395 broker.beginTransaction(); 396 broker.store(obj); 397 broker.store(p1); 398 broker.store(p2); 399 broker.commitTransaction(); 400 broker.beginTransaction(); 402 broker.delete(obj); 403 broker.commitTransaction(); 406 } 407 finally 408 { 409 if (broker != null) broker.close(); 410 } 411 412 k++; 413 try 414 { 415 Thread.sleep(5); 416 } 417 catch (InterruptedException e) 418 { 419 } 420 } 421 422 } 423 } 424 425 class GlobalUsingBroker extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable 429 { 430 int loops; 431 432 public GlobalUsingBroker(int loops) 433 { 434 this.loops = loops; 435 } 436 437 public void runTestCase() 438 { 439 PersistenceBroker broker = null; 440 int k = 0; 441 try 442 { 443 while (k < loops) 444 { 445 try 446 { 447 MetadataManager.getInstance().setDescriptor(defaultRepository); 448 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 449 ClassDescriptor cld = broker.getClassDescriptor(targetTestClass); 450 assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges()); 451 String name = cld.getFullTableName(); 452 assertEquals(getTestObjectString(), name); 454 } 455 finally 456 { 457 if (broker != null) broker.close(); 458 } 459 try 460 { 461 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 462 ClassDescriptor cld = broker.getClassDescriptor(targetTestClass); 463 assertTrue(MetadataManager.getInstance().isEnablePerThreadChanges()); 464 String name = cld.getFullTableName(); 465 assertEquals(getTestObjectString(), name); 467 469 Query query = new QueryByCriteria(Person.class, null, true); 471 broker.getCollectionByQuery(query); 472 Object obj = ClassHelper.newInstance(targetTestClass); 474 broker.beginTransaction(); 475 broker.store(obj); 476 broker.commitTransaction(); 477 broker.beginTransaction(); 479 broker.delete(obj); 480 broker.commitTransaction(); 481 } 482 finally 483 { 484 if (broker != null) broker.close(); 485 } 486 487 k++; 488 try 489 { 490 Thread.sleep(5); 491 } 492 catch (InterruptedException e) 493 { 494 } 495 } 496 } 497 catch (Exception e) 498 { 499 e.printStackTrace(); 500 throw new OJBRuntimeException(e); 501 } 502 } 503 } 504 505 508 protected class LazyLoading extends JUnitExtensions.MultiThreadedTestCase.TestCaseRunnable 509 { 510 private Collection articles; 511 512 public LazyLoading(Collection articles) 513 { 514 assertNotNull(this.articles = articles); 515 } 516 517 public void runTestCase() throws Throwable 518 { 519 DescriptorRepository dr = new DescriptorRepository(); 522 MetadataManager.getInstance().setDescriptor(dr); 523 Article article; 524 int numArticles = 0; 525 for (Iterator iterator = articles.iterator(); iterator.hasNext();) 526 { 527 assertNotNull(article = (Article) iterator.next()); 528 assertNotNull(article.getArticleId()); 529 assertFalse(new Integer (0).equals(article.getArticleId())); 530 numArticles++; 531 } 532 assertEquals(6, numArticles); 533 } 534 } 535 536 } 537 | Popular Tags |