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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* *******************************************************************
  2. * Copyright (c) 2006 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.File;
  14. import java.lang.ref.Reference;
  15. import java.lang.reflect.Field;
  16. import java.net.URL;
  17. import java.net.URLClassLoader;
  18. import java.util.Enumeration;
  19. import java.util.Map;
  20. import java.util.zip.ZipEntry;
  21. import java.util.zip.ZipFile;
  22. import org.aspectj.apache.bcel.util.ClassLoaderRepository;
  23. import junit.framework.TestCase;
  24. /** NOT YET INCLUDED IN A FULL TEST RUN - WORK IN PROGRESS CHECKING CLASSLOADERREPOSITORY OPTIMIZATIONS */
  25. public class ClassLoaderRepositoryTest extends TestCase {
  26. private File f;
  27. private ZipFile zf;
  28. private Enumeration entries;
  29. private Map map;
  30. public void setUp() throws Exception {
  31. f = new File("../lib/aspectj/lib/aspectjtools.jar");
  32. assertTrue("Couldn't find aspectjtools to test. Tried: "+f.getAbsolutePath(),f.exists());
  33. zf = new ZipFile(f);
  34. entries = zf.entries();
  35. // ClassLoaderRepository.sharedCacheCompactFrequency = 16384;
  36. map = getSharedMap();
  37. }
  38. public void tearDown() {
  39. new ClassLoaderRepository((ClassLoader) null).reset();
  40. }
  41. private ClassLoaderRepository setupRepository() throws Exception {
  42. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  43. ClassLoader res = new URLClassLoader(new URL[]{f.toURL()},cl);
  44. ClassLoaderRepository rep = new ClassLoaderRepository(res);
  45. return rep;
  46. }
  47. private void compareTwoRepositories() throws Exception {
  48. ClassLoaderRepository rep1 = setupRepository();
  49. ClassLoaderRepository rep2 = setupRepository();
  50. int i = 0;
  51. while (entries.hasMoreElements()) {
  52. ZipEntry zfe = (ZipEntry)entries.nextElement();
  53. String classfileName = zfe.getName();
  54. if (classfileName.endsWith(".class")) {
  55. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  56. // twice by each
  57. rep1.loadClass(clazzname);
  58. rep1.loadClass(clazzname);
  59. rep2.loadClass(clazzname);
  60. rep2.loadClass(clazzname);
  61. i++;
  62. }
  63. }
  64. System.err.println("Successfully compared "+i+" entries!!");
  65. System.err.println(rep1.report());
  66. System.err.println(rep2.report());
  67. }
  68. // private void loadOnce() throws Exception {
  69. // ClassLoaderRepository rep = setupRepository();
  70. // while (entries.hasMoreElements()) {
  71. // ZipEntry zfe = (ZipEntry) entries.nextElement();
  72. // String classfileName = zfe.getName();
  73. // if (classfileName.endsWith(".class")) {
  74. // String clazzname = classfileName.substring(0,
  75. // classfileName.length() - 6).replace('/', '.');
  76. //
  77. // rep.loadClass(clazzname);
  78. // }
  79. // }
  80. // }
  81. public void testMultiThreaded() throws Throwable {
  82. ClassLoaderRepository.useSharedCache=true;
  83. // ClassLoaderRepository.sharedCacheCompactFrequency = 200;
  84. //loadOnce();
  85. TestThread threads[] = new TestThread[6];
  86. for (int i=0; i<threads.length; i++) {
  87. threads[i] = new TestThread((i%3)*1000);
  88. threads[i].start();
  89. }
  90. for (int i=0; i<threads.length; i++) {
  91. threads[i].join();
  92. if (threads[i].getFailure() != null) {
  93. throw threads[i].getFailure();
  94. }
  95. }
  96. }
  97. private class TestThread extends Thread {
  98. public Throwable failure = null;
  99. Enumeration entries;
  100. // ensure the threads are loading DIFFERENT shared classes at the same time...
  101. public TestThread(int skip) {
  102. entries = zf.entries();
  103. for (int i=0; i<skip && entries.hasMoreElements(); i++) {
  104. entries.nextElement();
  105. }
  106. }
  107. public void run() {
  108. try {
  109. ClassLoaderRepository rep = setupRepository();
  110. int i = 0;
  111. while (entries.hasMoreElements()) {
  112. ZipEntry zfe = (ZipEntry)entries.nextElement();
  113. String classfileName = zfe.getName();
  114. if (classfileName.endsWith(".class")) {
  115. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  116. rep.loadClass(clazzname);
  117. rep.loadClass(clazzname);
  118. i++;
  119. }
  120. }
  121. System.err.println("Thread finished: "+rep.report());
  122. } catch (Throwable t) {
  123. failure = t;
  124. }
  125. }
  126. public Throwable getFailure() {
  127. return failure;
  128. }
  129. }
  130. public void testNotSharedRepository() throws Exception {
  131. ClassLoaderRepository.useSharedCache=false;
  132. compareTwoRepositories();
  133. }
  134. public void testSharedUrlRepository() throws Exception {
  135. ClassLoaderRepository.useSharedCache=true;
  136. compareTwoRepositories();
  137. // ClassLoaderRepository.compactSharedCache();
  138. }
  139. public void testPurgeUrlRepository() throws Exception {
  140. ClassLoaderRepository.useSharedCache = true;
  141. ClassLoaderRepository rep = setupRepository();
  142. Reference ref = null;
  143. while (ref==null && entries.hasMoreElements()) {
  144. ZipEntry zfe = (ZipEntry)entries.nextElement();
  145. String classfileName = zfe.getName();
  146. if (classfileName.endsWith(".class")) {
  147. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  148. rep.loadClass(clazzname);
  149. assertEquals("expected one entry in shared URL cache "+map.size()+": "+map, 1, map.size());
  150. ref = (Reference)map.values().iterator().next();
  151. ref.clear();
  152. ref.enqueue();
  153. map.size();//force purge
  154. }
  155. }
  156. assertEquals("expected empty shared URL cache "+map.size(), 0, map.size());
  157. }
  158. public void testAutoPurgeUrlRepository() throws Exception {
  159. ClassLoaderRepository.useSharedCache = true;
  160. assertEquals("expected empty shared URL cache "+map.size(), 0, map.size());
  161. ClassLoaderRepository rep = setupRepository();
  162. Reference ref = null;
  163. int i = 0;
  164. while (i<3 && entries.hasMoreElements()) {
  165. ZipEntry zfe = (ZipEntry)entries.nextElement();
  166. String classfileName = zfe.getName();
  167. if (classfileName.endsWith(".class")) {
  168. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  169. rep.loadClass(clazzname);
  170. ref = (Reference)map.values().iterator().next();
  171. ref.clear();
  172. ref.enqueue();
  173. i++;
  174. }
  175. }
  176. assertTrue("expected smaller shared URL cache "+map.size(), map.size()<3);
  177. }
  178. private Field getSharedMapField() throws Exception {
  179. Field field = ClassLoaderRepository.class.getDeclaredField("sharedCache");
  180. field.setAccessible(true);
  181. return field;
  182. }
  183. private Map getSharedMap() throws Exception {
  184. return (Map)getSharedMapField() .get(null);
  185. }
  186. }