001: /*
002: * Copyright 2002,2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib;
017:
018: import java.io.*;
019: import java.io.File;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.net.URLClassLoader;
023: import java.util.*;
024: import java.util.ArrayList;
025: import java.util.StringTokenizer;
026:
027: import net.sf.cglib.core.KeyFactory;
028: import net.sf.cglib.core.Signature;
029: import net.sf.cglib.proxy.*;
030: import net.sf.cglib.proxy.Callback;
031: import net.sf.cglib.proxy.Factory;
032: import net.sf.cglib.reflect.FastClass;
033:
034: import junit.framework.*;
035:
036: /**
037: * @author Chris Nokleberg <a href="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
038: * @version $Id: CodeGenTestCase.java,v 1.9 2005/09/28 09:37:32 baliuka Exp $
039: */
040: abstract public class CodeGenTestCase extends TestCase {
041: public CodeGenTestCase(String testName) {
042: super (testName);
043: }
044:
045: public abstract void perform(ClassLoader loader) throws Throwable;
046:
047: public boolean leaks() throws Throwable {
048:
049: List classPath = new ArrayList();
050:
051: for (StringTokenizer tokenizer = new StringTokenizer(System
052: .getProperty("java.class.path"), File.pathSeparator); tokenizer
053: .hasMoreElements();) {
054:
055: classPath.add(new File(tokenizer.nextToken()).toURL());
056:
057: }
058:
059: final Set coreClasses = new HashSet();
060: coreClasses.add(Factory.class.getName());
061: coreClasses.add(Callback.class.getName());
062: coreClasses.add(MethodInterceptor.class.getName());
063: coreClasses.add(Mixin.class.getName());
064: coreClasses.add(KeyFactory.class.getName());
065: coreClasses.add(FastClass.class.getName());
066: coreClasses.add(FastClass.Generator.class.getName());
067: coreClasses.add(Signature.class.getName());
068:
069: URLClassLoader loader = new URLClassLoader((URL[]) classPath
070: .toArray(new URL[classPath.size()])) {
071:
072: public Class loadClass(String name)
073: throws ClassNotFoundException {
074:
075: String res = name.replace('.', '/') + ".class";
076:
077: if (name.startsWith("java")
078: || name.startsWith("org.objectweb.asm")
079: || name.startsWith("sun.")) {
080: return super .loadClass(name);
081: }
082:
083: if (coreClasses.contains(name)) {
084: return super .loadClass(name);
085: }
086:
087: try {
088:
089: InputStream is = getResourceAsStream(res);
090: ByteArrayOutputStream bout = new ByteArrayOutputStream();
091: try {
092: int b;
093: while ((b = is.read()) != -1) {
094: bout.write((byte) b);
095: }
096:
097: } finally {
098: is.close();
099: }
100: byte data[] = bout.toByteArray();
101: return defineClass(name, data, 0, data.length);
102: } catch (Exception e) {
103: throw new ClassNotFoundException(name + ":"
104: + e.toString());
105: }
106: }
107:
108: };
109:
110: perform(loader);
111:
112: java.lang.ref.Reference ref = new java.lang.ref.WeakReference(
113: loader);
114:
115: loader = null;
116: java.util.List list = new java.util.ArrayList();
117:
118: for (int i = 0; i < 512; i++) {
119:
120: System.gc();
121: System.gc();
122:
123: if (ref.get() == null) {
124:
125: return false;
126:
127: }
128:
129: byte[] garbage = new byte[(i + 1) * 1004];
130: list.add(garbage);
131:
132: }
133:
134: return true;
135:
136: }
137:
138: }
|