KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > CodeGenTestCase


1 /*
2  * Copyright 2002,2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib;
17
18 import java.io.*;
19 import java.io.File JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import net.sf.cglib.core.KeyFactory;
28 import net.sf.cglib.core.Signature;
29 import net.sf.cglib.proxy.*;
30 import net.sf.cglib.proxy.Callback;
31 import net.sf.cglib.proxy.Factory;
32 import net.sf.cglib.reflect.FastClass;
33
34 import junit.framework.*;
35
36 /**
37  * @author Chris Nokleberg <a HREF="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
38  * @version $Id: CodeGenTestCase.java,v 1.7 2005/06/09 10:29:25 baliuka Exp $
39  */

40 abstract public class CodeGenTestCase extends TestCase {
41     public CodeGenTestCase(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     public abstract void perform(ClassLoader JavaDoc loader)throws Throwable JavaDoc;
46     
47     
48     public boolean leaks()throws Throwable JavaDoc{
49         
50         List JavaDoc classPath = new ArrayList JavaDoc();
51         
52         for( StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(System.getProperty("java.class.path"),File.pathSeparator); tokenizer.hasMoreElements(); ){
53             
54             classPath.add( new File JavaDoc(tokenizer.nextToken()).toURL() );
55             
56         }
57         
58         
59         final Set coreClasses = new HashSet();
60         coreClasses.add(Factory.class.getName());
61         coreClasses.add(Callback.class.getName());
62         coreClasses.add(MethodInterceptor.class.getName());
63         coreClasses.add(Mixin.class.getName());
64         coreClasses.add(KeyFactory.class.getName());
65         coreClasses.add(FastClass.class.getName());
66         coreClasses.add(Signature.class.getName());
67         
68         
69         
70         
71         
72         
73         URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc((URL JavaDoc[]) classPath.toArray(new URL JavaDoc[classPath.size()])){
74             
75             public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
76                 
77                 String JavaDoc res = name.replace('.','/') + ".class";
78                 
79                 if(name.startsWith("java") || name.startsWith("org.objectweb.asm")){
80                     return super.loadClass(name);
81                 }
82                 
83                 
84                 
85                 if( coreClasses.contains(name) ){
86                     return super.loadClass(name);
87                 }
88                 
89                 
90                 try{
91                     
92                     
93                     InputStream JavaDoc is = getResourceAsStream(res);
94                     ByteArrayOutputStream bout = new ByteArrayOutputStream();
95                     try{
96                         int b;
97                         while( (b = is.read()) != -1 ){
98                             bout.write((byte)b);
99                         }
100                         
101                     }finally{
102                         is.close();
103                     }
104                     byte data [] = bout.toByteArray();
105                     return defineClass(name,data,0,data.length );
106                 }catch(Exception JavaDoc e){
107                     throw new ClassNotFoundException JavaDoc( name + ":" + e.toString());
108                 }
109             }
110             
111         };
112         
113         perform(loader);
114         
115         java.lang.ref.Reference JavaDoc ref = new java.lang.ref.WeakReference JavaDoc(loader);
116         
117         loader = null;
118         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
119         
120         for(int i = 0; i < 512; i++ ){
121             
122             System.gc();
123             System.gc();
124             
125             if(ref.get() == null ){
126              
127                 return false;
128                 
129             }
130             
131           byte[] garbage = new byte[ (i + 1)*1004 ];
132           list.add(garbage);
133         
134           
135         }
136         
137         return true;
138         
139         
140     }
141     
142     
143     
144 }
145
146
Popular Tags