xref: /aosp_15_r20/external/javassist/src/test/scoped/ScopedRepositoryTestCase.java (revision f1fbf3c2ab775ce834e0af96b7a85bdc7a0eac65)
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 
18 import java.io.File;
19 import java.lang.reflect.Method;
20 import java.math.BigDecimal;
21 import java.net.URL;
22 import java.net.URLClassLoader;
23 import java.util.Arrays;
24 import java.util.Map;
25 import java.util.stream.LongStream;
26 
27 import javassist.ClassPool;
28 import javassist.CtClass;
29 import javassist.CtConstructor;
30 import javassist.CtField;
31 import javassist.CtMethod;
32 import javassist.scopedpool.ScopedClassPool;
33 import javassist.scopedpool.ScopedClassPoolRepository;
34 import javassist.scopedpool.ScopedClassPoolRepositoryImpl;
35 import javassist.scopedpool.SoftValueHashMap;
36 import junit.framework.TestCase;
37 
38 
39 /**
40  * ScopedRepositoryTest.
41  *
42  * @author <a href="[email protected]">Adrian Brock</a>
43  * @version $Revision$
44  */
45 public class ScopedRepositoryTestCase extends TestCase
46 {
47    private static final ScopedClassPoolRepository repository = ScopedClassPoolRepositoryImpl.getInstance();
48 
testJDKClasses()49    public void testJDKClasses() throws Exception
50    {
51       ClassPool poolClass = repository.findClassPool(Class.class.getClassLoader());
52       assertNotNull(poolClass);
53       ClassPool poolString = repository.findClassPool(String.class.getClassLoader());
54       assertNotNull(poolString);
55       assertEquals(poolClass, poolString);
56    }
57 
testScopedClasses()58    public void testScopedClasses() throws Exception
59    {
60       ClassLoader cl = getURLClassLoader("test-classes14-jar1");
61       ClassPool pool1 = repository.findClassPool(cl);
62       CtClass clazz = pool1.get("scoped.jar1.TestClass1");
63       assertNotNull(clazz);
64       ClassPool poolClass = repository.findClassPool(Class.class.getClassLoader());
65       assertNotNull(poolClass);
66       assertNotSame(pool1, poolClass);
67    }
68 
testUnscopedAnnotationUsage()69    public void testUnscopedAnnotationUsage() throws Exception
70    {
71       CtClass clazz = getCtClass(UnscopedAnnotationUsage.class);
72       checkTestAnnotation(clazz, "notDefault");
73    }
74 
testUnscopedAnnotationDefaultUsage()75    public void testUnscopedAnnotationDefaultUsage() throws Exception
76    {
77       CtClass clazz = getCtClass(UnscopedAnnotationDefaultUsage.class);
78       checkTestAnnotation(clazz, "defaultValue");
79    }
80 
testScopedAnnotationUsage()81    public void testScopedAnnotationUsage() throws Exception
82    {
83       ClassLoader cl = getURLClassLoader("test-classes14-jar1");
84       CtClass clazz = getCtClass("scoped.jar1.ScopedAnnotationUsage", cl);
85       checkTestAnnotation(clazz, "notDefault");
86    }
87 
testScopedAnnotationDefaultUsage()88    public void testScopedAnnotationDefaultUsage() throws Exception
89    {
90       ClassLoader cl = getURLClassLoader("test-classes14-jar1");
91       CtClass clazz = getCtClass("scoped.jar1.ScopedAnnotationDefaultUsage", cl);
92       checkTestAnnotation(clazz, "defaultValue");
93    }
94 
testFullyScopedAnnotationUsage()95    public void testFullyScopedAnnotationUsage() throws Exception
96    {
97       ClassLoader cl = getURLClassLoader("test-classes14-jar1");
98       CtClass clazz = getCtClass("scoped.jar1.FullyScopedAnnotationUsage", cl);
99       checkScopedAnnotation(cl, clazz, "notDefault");
100    }
101 
testFullyScopedAnnotationDefaultUsage()102    public void testFullyScopedAnnotationDefaultUsage() throws Exception
103    {
104       ClassLoader cl = getURLClassLoader("test-classes14-jar1");
105       CtClass clazz = getCtClass("scoped.jar1.FullyScopedAnnotationDefaultUsage", cl);
106       checkScopedAnnotation(cl, clazz, "defaultValue");
107    }
108 
testSoftValueHashMap()109    public void testSoftValueHashMap() throws Exception {
110        Map<String,Class<?>> map = new SoftValueHashMap<>();
111        Class<?> cls = this.getClass();
112        assertTrue(map.put(cls.getName(), cls) == null);
113        assertTrue(map.put(cls.getName(), cls) == cls);
114        assertTrue(map.size() == 1);
115        assertTrue(map.get(cls.getName()) == cls);
116        assertTrue(map.values().iterator().next() == cls);
117        assertTrue(map.entrySet().iterator().next().getValue() == cls);
118        assertTrue(map.containsValue(cls));
119        assertTrue(map.remove(cls.getName()) == cls);
120        assertTrue(map.size() == 0);
121    }
122 
testSoftCache()123    public void testSoftCache() throws Exception {
124        // Overload the heap to test that the map auto cleans
125        Map<String,long[]> map = new SoftValueHashMap<>();
126        // 12+8*30000000 = +- 252 MB
127        long[] data = LongStream.range(0, 30000000).toArray();
128        int current = map.size();
129        while (current <= map.size()) {
130            current = map.size();
131            for (int ii = 0; ii < 5; ii++) {
132                map.put(current+"-"+ii, Arrays.copyOf(data, data.length));
133            }
134        }
135        assertTrue(current > map.size());
136    }
137 
getCtClass(Class<?> clazz)138    protected CtClass getCtClass(Class<?> clazz) throws Exception
139    {
140       return getCtClass(clazz.getName(), clazz.getClassLoader());
141    }
142 
getCtClass(String name, ClassLoader cl)143    protected CtClass getCtClass(String name, ClassLoader cl) throws Exception
144    {
145       ClassPool pool = repository.findClassPool(cl);
146       assertNotNull(pool);
147       CtClass clazz = pool.get(name);
148       assertNotNull(clazz);
149       return clazz;
150    }
151 
checkTestAnnotation(CtClass ctClass, String value)152    protected void checkTestAnnotation(CtClass ctClass, String value) throws Exception
153    {
154       checkTestAnnotation(ctClass.getAnnotations(), value);
155       checkTestAnnotation(getFieldAnnotations(ctClass), value);
156       checkTestAnnotation(getConstructorAnnotations(ctClass), value);
157       checkTestAnnotation(getConstructorParameterAnnotations(ctClass), value);
158       checkTestAnnotation(getMethodAnnotations(ctClass), value);
159       checkTestAnnotation(getMethodParameterAnnotations(ctClass), value);
160    }
161 
checkTestAnnotation(Object[] annotations, String value)162    protected void checkTestAnnotation(Object[] annotations, String value) throws Exception
163    {
164       assertNotNull(annotations);
165       assertEquals(1, annotations.length);
166       assertNotNull(annotations[0]);
167       assertTrue(annotations[0] instanceof TestAnnotation);
168       TestAnnotation annotation = (TestAnnotation) annotations[0];
169       assertEquals(value, annotation.something());
170    }
171 
checkScopedAnnotation(ClassLoader cl, CtClass ctClass, String value)172    protected void checkScopedAnnotation(ClassLoader cl, CtClass ctClass, String value) throws Exception
173    {
174       Class<?> annotationClass = cl.loadClass("scoped.jar1.ScopedTestAnnotation");
175       checkScopedAnnotation(annotationClass, ctClass.getAnnotations(), value);
176       checkScopedAnnotation(annotationClass, getFieldAnnotations(ctClass), value);
177       checkScopedAnnotation(annotationClass, getConstructorAnnotations(ctClass), value);
178       checkScopedAnnotation(annotationClass, getConstructorParameterAnnotations(ctClass), value);
179       checkScopedAnnotation(annotationClass, getMethodAnnotations(ctClass), value);
180       checkScopedAnnotation(annotationClass, getMethodParameterAnnotations(ctClass), value);
181    }
182 
checkScopedAnnotation(Class<?> annotationClass, Object[] annotations, String value)183    protected void checkScopedAnnotation(Class<?> annotationClass, Object[] annotations, String value) throws Exception
184    {
185       assertNotNull(annotations);
186       assertEquals(1, annotations.length);
187       assertNotNull(annotations[0]);
188       assertTrue(annotationClass.isInstance(annotations[0]));
189 
190       Method method = annotationClass.getMethod("something", new Class<?>[0]);
191       assertEquals(value, method.invoke(annotations[0], (Object[]) null));
192    }
193 
getFieldAnnotations(CtClass clazz)194    protected Object[] getFieldAnnotations(CtClass clazz) throws Exception
195    {
196       CtField field = clazz.getField("aField");
197       assertNotNull(field);
198       return field.getAnnotations();
199    }
200 
getMethodAnnotations(CtClass clazz)201    protected Object[] getMethodAnnotations(CtClass clazz) throws Exception
202    {
203       CtMethod method = clazz.getMethod("doSomething", "(I)V");
204       assertNotNull(method);
205       return method.getAnnotations();
206    }
207 
getMethodParameterAnnotations(CtClass clazz)208    protected Object[] getMethodParameterAnnotations(CtClass clazz) throws Exception
209    {
210       CtMethod method = clazz.getMethod("doSomething", "(I)V");
211       assertNotNull(method);
212       Object[] paramAnnotations = method.getParameterAnnotations();
213       assertNotNull(paramAnnotations);
214       assertEquals(1, paramAnnotations.length);
215       return (Object[]) paramAnnotations[0];
216    }
217 
getConstructorAnnotations(CtClass clazz)218    protected Object[] getConstructorAnnotations(CtClass clazz) throws Exception
219    {
220       CtConstructor constructor = clazz.getConstructor("(I)V");
221       assertNotNull(constructor);
222       return constructor.getAnnotations();
223    }
224 
getConstructorParameterAnnotations(CtClass clazz)225    protected Object[] getConstructorParameterAnnotations(CtClass clazz) throws Exception
226    {
227       CtConstructor constructor = clazz.getConstructor("(I)V");
228       assertNotNull(constructor);
229       Object[] paramAnnotations = constructor.getParameterAnnotations();
230       assertNotNull(paramAnnotations);
231       assertEquals(1, paramAnnotations.length);
232       return (Object[]) paramAnnotations[0];
233    }
234 
getURLClassLoader(String context)235    protected ClassLoader getURLClassLoader(String context) throws Exception
236    {
237       String output = ".";
238       File file = new File(output + File.separator + context);
239       URL url = file.toURI().toURL();
240       return new URLClassLoader(new URL[] { url });
241    }
242 }
243