You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassloaderRepositoryTest.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package org.aspectj.apache.bcel.classfile.tests;
  2. import java.net.URL;
  3. import java.net.URLClassLoader;
  4. import org.aspectj.apache.bcel.classfile.JavaClass;
  5. import org.aspectj.apache.bcel.util.ClassLoaderRepository;
  6. import junit.framework.TestCase;
  7. /*
  8. * Tests create a simple classloader repository configuration and check sharing of information.
  9. */
  10. public class ClassloaderRepositoryTest extends TestCase {
  11. private ClassLoaderRepository rep1,rep2;
  12. public void setUp() throws Exception {
  13. super.setUp();
  14. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  15. ClassLoader cl1 = new URLClassLoader(new URL[]{},cl);
  16. ClassLoader cl2 = new URLClassLoader(new URL[]{},cl);
  17. rep1 = new ClassLoaderRepository(cl1);
  18. rep2 = new ClassLoaderRepository(cl2);
  19. }
  20. // Retrieve string 5 times from same repository, 4 hits should be from local cache
  21. public void testLocalCacheWorks() throws ClassNotFoundException {
  22. JavaClass jc = rep1.loadClass("java.lang.String");
  23. jc = rep1.loadClass("java.lang.String");
  24. jc = rep1.loadClass("java.lang.String");
  25. jc = rep1.loadClass("java.lang.String");
  26. jc = rep1.loadClass("java.lang.String");
  27. assertTrue("Should have used local cache 4 times: "+rep1.reportLocalCacheHits(),rep1.reportLocalCacheHits()==4);
  28. }
  29. // Retrieve String through one repository then load again through another, should be shared cache hit
  30. public void testSharedCacheWorks() throws ClassNotFoundException {
  31. JavaClass jc = rep1.loadClass("java.lang.String");
  32. jc = rep2.loadClass("java.lang.String");
  33. assertTrue("Should have retrieved String from shared cache: "+ClassLoaderRepository.reportSharedCacheHits(),
  34. ClassLoaderRepository.reportSharedCacheHits()==1);
  35. }
  36. // Shared cache OFF, shouldn't get a shared cache hit
  37. public void testSharedCacheCanBeDeactivated() throws ClassNotFoundException {
  38. try {
  39. ClassLoaderRepository.useSharedCache=false;
  40. JavaClass jc = rep1.loadClass("java.lang.String");
  41. jc = rep2.loadClass("java.lang.String");
  42. assertTrue("Should not have retrieved String from shared cache: "+
  43. ClassLoaderRepository.reportSharedCacheHits(),
  44. ClassLoaderRepository.reportSharedCacheHits()==0);
  45. } finally {
  46. ClassLoaderRepository.useSharedCache=true;
  47. }
  48. }
  49. public void tearDown() throws Exception {
  50. super.tearDown();
  51. System.err.println("Rep1: "+rep1.reportAllStatistics());
  52. System.err.println("Rep2: "+rep2.reportAllStatistics());
  53. rep1.reset();
  54. rep2.reset();
  55. }
  56. }