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.

ScopedRepositoryTestCase.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package scoped;
  17. import java.io.File;
  18. import java.lang.reflect.Method;
  19. import java.math.BigDecimal;
  20. import java.net.URL;
  21. import java.net.URLClassLoader;
  22. import java.util.Arrays;
  23. import java.util.Map;
  24. import java.util.stream.LongStream;
  25. import javassist.ClassPool;
  26. import javassist.CtClass;
  27. import javassist.CtConstructor;
  28. import javassist.CtField;
  29. import javassist.CtMethod;
  30. import javassist.scopedpool.ScopedClassPool;
  31. import javassist.scopedpool.ScopedClassPoolRepository;
  32. import javassist.scopedpool.ScopedClassPoolRepositoryImpl;
  33. import javassist.scopedpool.SoftValueHashMap;
  34. import junit.framework.TestCase;
  35. /**
  36. * ScopedRepositoryTest.
  37. *
  38. * @author <a href="adrian@jboss.com">Adrian Brock</a>
  39. * @version $Revision$
  40. */
  41. public class ScopedRepositoryTestCase extends TestCase
  42. {
  43. private static final ScopedClassPoolRepository repository = ScopedClassPoolRepositoryImpl.getInstance();
  44. public void testJDKClasses() throws Exception
  45. {
  46. ClassPool poolClass = repository.findClassPool(Class.class.getClassLoader());
  47. assertNotNull(poolClass);
  48. ClassPool poolString = repository.findClassPool(String.class.getClassLoader());
  49. assertNotNull(poolString);
  50. assertEquals(poolClass, poolString);
  51. }
  52. public void testScopedClasses() throws Exception
  53. {
  54. ClassLoader cl = getURLClassLoader("test-classes14-jar1");
  55. ClassPool pool1 = repository.findClassPool(cl);
  56. CtClass clazz = pool1.get("scoped.jar1.TestClass1");
  57. assertNotNull(clazz);
  58. ClassPool poolClass = repository.findClassPool(Class.class.getClassLoader());
  59. assertNotNull(poolClass);
  60. assertNotSame(pool1, poolClass);
  61. }
  62. public void testUnscopedAnnotationUsage() throws Exception
  63. {
  64. CtClass clazz = getCtClass(UnscopedAnnotationUsage.class);
  65. checkTestAnnotation(clazz, "notDefault");
  66. }
  67. public void testUnscopedAnnotationDefaultUsage() throws Exception
  68. {
  69. CtClass clazz = getCtClass(UnscopedAnnotationDefaultUsage.class);
  70. checkTestAnnotation(clazz, "defaultValue");
  71. }
  72. public void testScopedAnnotationUsage() throws Exception
  73. {
  74. ClassLoader cl = getURLClassLoader("test-classes14-jar1");
  75. CtClass clazz = getCtClass("scoped.jar1.ScopedAnnotationUsage", cl);
  76. checkTestAnnotation(clazz, "notDefault");
  77. }
  78. public void testScopedAnnotationDefaultUsage() throws Exception
  79. {
  80. ClassLoader cl = getURLClassLoader("test-classes14-jar1");
  81. CtClass clazz = getCtClass("scoped.jar1.ScopedAnnotationDefaultUsage", cl);
  82. checkTestAnnotation(clazz, "defaultValue");
  83. }
  84. public void testFullyScopedAnnotationUsage() throws Exception
  85. {
  86. ClassLoader cl = getURLClassLoader("test-classes14-jar1");
  87. CtClass clazz = getCtClass("scoped.jar1.FullyScopedAnnotationUsage", cl);
  88. checkScopedAnnotation(cl, clazz, "notDefault");
  89. }
  90. public void testFullyScopedAnnotationDefaultUsage() throws Exception
  91. {
  92. ClassLoader cl = getURLClassLoader("test-classes14-jar1");
  93. CtClass clazz = getCtClass("scoped.jar1.FullyScopedAnnotationDefaultUsage", cl);
  94. checkScopedAnnotation(cl, clazz, "defaultValue");
  95. }
  96. public void testSoftValueHashMap() throws Exception {
  97. Map<String,Class<?>> map = new SoftValueHashMap<>();
  98. Class<?> cls = this.getClass();
  99. assertTrue(map.put(cls.getName(), cls) == null);
  100. assertTrue(map.put(cls.getName(), cls) == cls);
  101. assertTrue(map.size() == 1);
  102. assertTrue(map.get(cls.getName()) == cls);
  103. assertTrue(map.values().iterator().next() == cls);
  104. assertTrue(map.entrySet().iterator().next().getValue() == cls);
  105. assertTrue(map.containsValue(cls));
  106. assertTrue(map.remove(cls.getName()) == cls);
  107. assertTrue(map.size() == 0);
  108. }
  109. public void testSoftCache() throws Exception {
  110. // Overload the heap to test that the map auto cleans
  111. Map<String,long[]> map = new SoftValueHashMap<>();
  112. // 12+8*30000000 = +- 252 MB
  113. long[] data = LongStream.range(0, 30000000).toArray();
  114. int current = map.size();
  115. while (current <= map.size()) {
  116. current = map.size();
  117. for (int ii = 0; ii < 5; ii++) {
  118. map.put(current+"-"+ii, Arrays.copyOf(data, data.length));
  119. }
  120. }
  121. assertTrue(current > map.size());
  122. }
  123. protected CtClass getCtClass(Class<?> clazz) throws Exception
  124. {
  125. return getCtClass(clazz.getName(), clazz.getClassLoader());
  126. }
  127. protected CtClass getCtClass(String name, ClassLoader cl) throws Exception
  128. {
  129. ClassPool pool = repository.findClassPool(cl);
  130. assertNotNull(pool);
  131. CtClass clazz = pool.get(name);
  132. assertNotNull(clazz);
  133. return clazz;
  134. }
  135. protected void checkTestAnnotation(CtClass ctClass, String value) throws Exception
  136. {
  137. checkTestAnnotation(ctClass.getAnnotations(), value);
  138. checkTestAnnotation(getFieldAnnotations(ctClass), value);
  139. checkTestAnnotation(getConstructorAnnotations(ctClass), value);
  140. checkTestAnnotation(getConstructorParameterAnnotations(ctClass), value);
  141. checkTestAnnotation(getMethodAnnotations(ctClass), value);
  142. checkTestAnnotation(getMethodParameterAnnotations(ctClass), value);
  143. }
  144. protected void checkTestAnnotation(Object[] annotations, String value) throws Exception
  145. {
  146. assertNotNull(annotations);
  147. assertEquals(1, annotations.length);
  148. assertNotNull(annotations[0]);
  149. assertTrue(annotations[0] instanceof TestAnnotation);
  150. TestAnnotation annotation = (TestAnnotation) annotations[0];
  151. assertEquals(value, annotation.something());
  152. }
  153. protected void checkScopedAnnotation(ClassLoader cl, CtClass ctClass, String value) throws Exception
  154. {
  155. Class<?> annotationClass = cl.loadClass("scoped.jar1.ScopedTestAnnotation");
  156. checkScopedAnnotation(annotationClass, ctClass.getAnnotations(), value);
  157. checkScopedAnnotation(annotationClass, getFieldAnnotations(ctClass), value);
  158. checkScopedAnnotation(annotationClass, getConstructorAnnotations(ctClass), value);
  159. checkScopedAnnotation(annotationClass, getConstructorParameterAnnotations(ctClass), value);
  160. checkScopedAnnotation(annotationClass, getMethodAnnotations(ctClass), value);
  161. checkScopedAnnotation(annotationClass, getMethodParameterAnnotations(ctClass), value);
  162. }
  163. protected void checkScopedAnnotation(Class<?> annotationClass, Object[] annotations, String value) throws Exception
  164. {
  165. assertNotNull(annotations);
  166. assertEquals(1, annotations.length);
  167. assertNotNull(annotations[0]);
  168. assertTrue(annotationClass.isInstance(annotations[0]));
  169. Method method = annotationClass.getMethod("something", new Class<?>[0]);
  170. assertEquals(value, method.invoke(annotations[0], (Object[]) null));
  171. }
  172. protected Object[] getFieldAnnotations(CtClass clazz) throws Exception
  173. {
  174. CtField field = clazz.getField("aField");
  175. assertNotNull(field);
  176. return field.getAnnotations();
  177. }
  178. protected Object[] getMethodAnnotations(CtClass clazz) throws Exception
  179. {
  180. CtMethod method = clazz.getMethod("doSomething", "(I)V");
  181. assertNotNull(method);
  182. return method.getAnnotations();
  183. }
  184. protected Object[] getMethodParameterAnnotations(CtClass clazz) throws Exception
  185. {
  186. CtMethod method = clazz.getMethod("doSomething", "(I)V");
  187. assertNotNull(method);
  188. Object[] paramAnnotations = method.getParameterAnnotations();
  189. assertNotNull(paramAnnotations);
  190. assertEquals(1, paramAnnotations.length);
  191. return (Object[]) paramAnnotations[0];
  192. }
  193. protected Object[] getConstructorAnnotations(CtClass clazz) throws Exception
  194. {
  195. CtConstructor constructor = clazz.getConstructor("(I)V");
  196. assertNotNull(constructor);
  197. return constructor.getAnnotations();
  198. }
  199. protected Object[] getConstructorParameterAnnotations(CtClass clazz) throws Exception
  200. {
  201. CtConstructor constructor = clazz.getConstructor("(I)V");
  202. assertNotNull(constructor);
  203. Object[] paramAnnotations = constructor.getParameterAnnotations();
  204. assertNotNull(paramAnnotations);
  205. assertEquals(1, paramAnnotations.length);
  206. return (Object[]) paramAnnotations[0];
  207. }
  208. protected ClassLoader getURLClassLoader(String context) throws Exception
  209. {
  210. String output = ".";
  211. File file = new File(output + File.separator + context);
  212. URL url = file.toURI().toURL();
  213. return new URLClassLoader(new URL[] { url });
  214. }
  215. }