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.

ClassLoaderRepositoryTests.java 6.5KB

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 junit.framework.TestCase;
  23. import org.aspectj.apache.bcel.util.ClassLoaderRepository;
  24. /** NOT YET INCLUDED IN A FULL TEST RUN - WORK IN PROGRESS CHECKING CLASSLOADERREPOSITORY OPTIMIZATIONS */
  25. public class ClassLoaderRepositoryTests 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(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. rep.loadClass(clazzname);
  77. }
  78. }
  79. }
  80. public void testMultiThreaded() throws Throwable {
  81. ClassLoaderRepository.useSharedCache=true;
  82. // ClassLoaderRepository.sharedCacheCompactFrequency = 200;
  83. //loadOnce();
  84. TestThread threads[] = new TestThread[6];
  85. for (int i=0; i<threads.length; i++) {
  86. threads[i] = new TestThread((i%3)*1000);
  87. threads[i].start();
  88. }
  89. for (int i=0; i<threads.length; i++) {
  90. threads[i].join();
  91. if (threads[i].getFailure() != null) {
  92. throw threads[i].getFailure();
  93. }
  94. }
  95. }
  96. private class TestThread extends Thread {
  97. public Throwable failure = null;
  98. Enumeration entries;
  99. // ensure the threads are loading DIFFERENT shared classes at the same time...
  100. public TestThread(int skip) {
  101. entries = zf.entries();
  102. for (int i=0; i<skip && entries.hasMoreElements(); i++) {
  103. entries.nextElement();
  104. }
  105. }
  106. public void run() {
  107. try {
  108. ClassLoaderRepository rep = setupRepository();
  109. int i = 0;
  110. while (entries.hasMoreElements()) {
  111. ZipEntry zfe = (ZipEntry)entries.nextElement();
  112. String classfileName = zfe.getName();
  113. if (classfileName.endsWith(".class")) {
  114. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  115. rep.loadClass(clazzname);
  116. rep.loadClass(clazzname);
  117. i++;
  118. }
  119. }
  120. System.err.println("Thread finished: "+rep.report());
  121. } catch (Throwable t) {
  122. failure = t;
  123. }
  124. }
  125. public Throwable getFailure() {
  126. return failure;
  127. }
  128. }
  129. public void testNotSharedRepository() throws Exception {
  130. ClassLoaderRepository.useSharedCache=false;
  131. compareTwoRepositories();
  132. }
  133. public void testSharedUrlRepository() throws Exception {
  134. ClassLoaderRepository.useSharedCache=true;
  135. compareTwoRepositories();
  136. // ClassLoaderRepository.compactSharedCache();
  137. }
  138. public void testPurgeUrlRepository() throws Exception {
  139. ClassLoaderRepository.useSharedCache = true;
  140. ClassLoaderRepository rep = setupRepository();
  141. Reference ref = null;
  142. while (ref==null && entries.hasMoreElements()) {
  143. ZipEntry zfe = (ZipEntry)entries.nextElement();
  144. String classfileName = zfe.getName();
  145. if (classfileName.endsWith(".class")) {
  146. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  147. rep.loadClass(clazzname);
  148. assertEquals("expected one entry in shared URL cache "+map.size(), 1, map.size());
  149. ref = (Reference)map.values().iterator().next();
  150. ref.clear();
  151. ref.enqueue();
  152. map.size();//force purge
  153. }
  154. }
  155. assertEquals("expected empty shared URL cache "+map.size(), 0, map.size());
  156. }
  157. public void testAutoPurgeUrlRepository() throws Exception {
  158. ClassLoaderRepository.useSharedCache = true;
  159. assertEquals("expected empty shared URL cache "+map.size(), 0, map.size());
  160. ClassLoaderRepository rep = setupRepository();
  161. Reference ref = null;
  162. int i = 0;
  163. while (i<3 && entries.hasMoreElements()) {
  164. ZipEntry zfe = (ZipEntry)entries.nextElement();
  165. String classfileName = zfe.getName();
  166. if (classfileName.endsWith(".class")) {
  167. String clazzname = classfileName.substring(0,classfileName.length()-6).replace('/','.');
  168. rep.loadClass(clazzname);
  169. ref = (Reference)map.values().iterator().next();
  170. ref.clear();
  171. ref.enqueue();
  172. i++;
  173. }
  174. }
  175. assertTrue("expected smaller shared URL cache "+map.size(), map.size()<3);
  176. }
  177. private Field getSharedMapField() throws Exception {
  178. Field field = ClassLoaderRepository.class.getDeclaredField("sharedCache");
  179. field.setAccessible(true);
  180. return field;
  181. }
  182. private Map getSharedMap() throws Exception {
  183. return (Map)getSharedMapField() .get(null);
  184. }
  185. }