aboutsummaryrefslogtreecommitdiff
path: root/src/test/scoped/ScopedRepositoryTestCase.java
blob: 9dca4f56851194cce45aeb41c40ee457d9416a2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */
package scoped;

import java.io.File;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.LongStream;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.scopedpool.ScopedClassPool;
import javassist.scopedpool.ScopedClassPoolRepository;
import javassist.scopedpool.ScopedClassPoolRepositoryImpl;
import javassist.scopedpool.SoftValueHashMap;
import junit.framework.TestCase;


/**
 * ScopedRepositoryTest.
 * 
 * @author <a href="adrian@jboss.com">Adrian Brock</a>
 * @version $Revision$
 */
public class ScopedRepositoryTestCase extends TestCase
{
   private static final ScopedClassPoolRepository repository = ScopedClassPoolRepositoryImpl.getInstance();
   
   public void testJDKClasses() throws Exception
   {
      ClassPool poolClass = repository.findClassPool(Class.class.getClassLoader());
      assertNotNull(poolClass);
      ClassPool poolString = repository.findClassPool(String.class.getClassLoader());
      assertNotNull(poolString);
      assertEquals(poolClass, poolString);
   }
   
   public void testScopedClasses() throws Exception
   {
      ClassLoader cl = getURLClassLoader("test-classes14-jar1");
      ClassPool pool1 = repository.findClassPool(cl);
      CtClass clazz = pool1.get("scoped.jar1.TestClass1");
      assertNotNull(clazz);
      ClassPool poolClass = repository.findClassPool(Class.class.getClassLoader());
      assertNotNull(poolClass);
      assertNotSame(pool1, poolClass);
   }
   
   public void testUnscopedAnnotationUsage() throws Exception
   {
      CtClass clazz = getCtClass(UnscopedAnnotationUsage.class);
      checkTestAnnotation(clazz, "notDefault");
   }
   
   public void testUnscopedAnnotationDefaultUsage() throws Exception
   {
      CtClass clazz = getCtClass(UnscopedAnnotationDefaultUsage.class);
      checkTestAnnotation(clazz, "defaultValue");
   }
   
   public void testScopedAnnotationUsage() throws Exception
   {
      ClassLoader cl = getURLClassLoader("test-classes14-jar1");
      CtClass clazz = getCtClass("scoped.jar1.ScopedAnnotationUsage", cl);
      checkTestAnnotation(clazz, "notDefault");
   }
   
   public void testScopedAnnotationDefaultUsage() throws Exception
   {
      ClassLoader cl = getURLClassLoader("test-classes14-jar1");
      CtClass clazz = getCtClass("scoped.jar1.ScopedAnnotationDefaultUsage", cl);
      checkTestAnnotation(clazz, "defaultValue");
   }
   
   public void testFullyScopedAnnotationUsage() throws Exception
   {
      ClassLoader cl = getURLClassLoader("test-classes14-jar1");
      CtClass clazz = getCtClass("scoped.jar1.FullyScopedAnnotationUsage", cl);
      checkScopedAnnotation(cl, clazz, "notDefault");
   }
   
   public void testFullyScopedAnnotationDefaultUsage() throws Exception
   {
      ClassLoader cl = getURLClassLoader("test-classes14-jar1");
      CtClass clazz = getCtClass("scoped.jar1.FullyScopedAnnotationDefaultUsage", cl);
      checkScopedAnnotation(cl, clazz, "defaultValue");
   }
   
   public void testSoftValueHashMap() throws Exception {
       Map<String,Class<?>> map = new SoftValueHashMap<>();
       Class<?> cls = this.getClass();
       assertTrue(map.put(cls.getName(), cls) == null);
       assertTrue(map.put(cls.getName(), cls) == cls);
       assertTrue(map.size() == 1);
       assertTrue(map.get(cls.getName()) == cls);
       assertTrue(map.values().iterator().next() == cls);
       assertTrue(map.entrySet().iterator().next().getValue() == cls);
       assertTrue(map.containsValue(cls));
       assertTrue(map.remove(cls.getName()) == cls);
       assertTrue(map.size() == 0);
   }

   public void testSoftCache() throws Exception {     
       // Overload the heap to test that the map auto cleans
       Map<String,long[]> map = new SoftValueHashMap<>();
       // 12+8*30000000 = +- 252 MB 
       long[] data = LongStream.range(0, 30000000).toArray();
       int current = map.size();
       while (current <= map.size()) {
           current = map.size();
           for (int ii = 0; ii < 5; ii++) {
               map.put(current+"-"+ii, Arrays.copyOf(data, data.length));
           }
       }
       assertTrue(current > map.size());
   }
   
   protected CtClass getCtClass(Class<?> clazz) throws Exception
   {
      return getCtClass(clazz.getName(), clazz.getClassLoader());
   }
   
   protected CtClass getCtClass(String name, ClassLoader cl) throws Exception
   {
      ClassPool pool = repository.findClassPool(cl);
      assertNotNull(pool);
      CtClass clazz = pool.get(name);
      assertNotNull(clazz);
      return clazz;
   }
   
   protected void checkTestAnnotation(CtClass ctClass, String value) throws Exception
   {
      checkTestAnnotation(ctClass.getAnnotations(), value);
      checkTestAnnotation(getFieldAnnotations(ctClass), value);
      checkTestAnnotation(getConstructorAnnotations(ctClass), value);
      checkTestAnnotation(getConstructorParameterAnnotations(ctClass), value);
      checkTestAnnotation(getMethodAnnotations(ctClass), value);
      checkTestAnnotation(getMethodParameterAnnotations(ctClass), value);
   }
   
   protected void checkTestAnnotation(Object[] annotations, String value) throws Exception
   {
      assertNotNull(annotations);
      assertEquals(1, annotations.length);
      assertNotNull(annotations[0]);
      assertTrue(annotations[0] instanceof TestAnnotation);
      TestAnnotation annotation = (TestAnnotation) annotations[0];
      assertEquals(value, annotation.something());
   }
   
   protected void checkScopedAnnotation(ClassLoader cl, CtClass ctClass, String value) throws Exception
   {
      Class<?> annotationClass = cl.loadClass("scoped.jar1.ScopedTestAnnotation");
      checkScopedAnnotation(annotationClass, ctClass.getAnnotations(), value);
      checkScopedAnnotation(annotationClass, getFieldAnnotations(ctClass), value);
      checkScopedAnnotation(annotationClass, getConstructorAnnotations(ctClass), value);
      checkScopedAnnotation(annotationClass, getConstructorParameterAnnotations(ctClass), value);
      checkScopedAnnotation(annotationClass, getMethodAnnotations(ctClass), value);
      checkScopedAnnotation(annotationClass, getMethodParameterAnnotations(ctClass), value);
   }
   
   protected void checkScopedAnnotation(Class<?> annotationClass, Object[] annotations, String value) throws Exception
   {
      assertNotNull(annotations);
      assertEquals(1, annotations.length);
      assertNotNull(annotations[0]);
      assertTrue(annotationClass.isInstance(annotations[0]));
      
      Method method = annotationClass.getMethod("something", new Class<?>[0]);
      assertEquals(value, method.invoke(annotations[0], (Object[]) null));
   }
   
   protected Object[] getFieldAnnotations(CtClass clazz) throws Exception
   {
      CtField field = clazz.getField("aField");
      assertNotNull(field);
      return field.getAnnotations();
   }
   
   protected Object[] getMethodAnnotations(CtClass clazz) throws Exception
   {
      CtMethod method = clazz.getMethod("doSomething", "(I)V");
      assertNotNull(method);
      return method.getAnnotations();
   }
   
   protected Object[] getMethodParameterAnnotations(CtClass clazz) throws Exception
   {
      CtMethod method = clazz.getMethod("doSomething", "(I)V");
      assertNotNull(method);
      Object[] paramAnnotations = method.getParameterAnnotations();
      assertNotNull(paramAnnotations);
      assertEquals(1, paramAnnotations.length);
      return (Object[]) paramAnnotations[0];
   }
   
   protected Object[] getConstructorAnnotations(CtClass clazz) throws Exception
   {
      CtConstructor constructor = clazz.getConstructor("(I)V");
      assertNotNull(constructor);
      return constructor.getAnnotations();
   }
   
   protected Object[] getConstructorParameterAnnotations(CtClass clazz) throws Exception
   {
      CtConstructor constructor = clazz.getConstructor("(I)V");
      assertNotNull(constructor);
      Object[] paramAnnotations = constructor.getParameterAnnotations();
      assertNotNull(paramAnnotations);
      assertEquals(1, paramAnnotations.length);
      return (Object[]) paramAnnotations[0];
   }
   
   protected ClassLoader getURLClassLoader(String context) throws Exception
   {
      String output = ".";
      File file = new File(output + File.separator + context);
      URL url = file.toURI().toURL();
      return new URLClassLoader(new URL[] { url });
   }
}