KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > transform > impl > TestTransformingLoader


1 /*
2  * Copyright 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.transform.impl;
17
18 import net.sf.cglib.transform.*;
19 import net.sf.cglib.core.Constants;
20 import net.sf.cglib.core.ReflectUtils;
21 import net.sf.cglib.beans.*;
22 import java.util.*;
23 import java.lang.reflect.Method JavaDoc;
24 import junit.framework.*;
25 import org.objectweb.asm.Type;
26
27 /**
28  * @version $Id: TestTransformingLoader.java,v 1.5 2004/06/24 21:15:15 herbyderby Exp $
29  */

30 public class TestTransformingLoader extends net.sf.cglib.CodeGenTestCase {
31
32     private static final ClassFilter TEST_FILTER = new ClassFilter() {
33         public boolean accept(String JavaDoc name) {
34             System.err.println("Loading " + name);
35             return name.startsWith("net.sf.cglib.");
36         }
37     };
38
39     private ClassTransformer getExampleTransformer(String JavaDoc name, Type type) {
40         return new AddPropertyTransformer(new String JavaDoc[]{ name }, new Type[]{ type });
41     }
42
43     public void testExample() throws Exception JavaDoc {
44         ClassTransformer t1 = getExampleTransformer("herby", Constants.TYPE_STRING);
45         ClassTransformer t2 = getExampleTransformer("derby", Type.DOUBLE_TYPE);
46         ClassTransformer chain = new ClassTransformerChain(new ClassTransformer[]{ t1, t2 });
47         Class JavaDoc loaded = loadHelper(chain, Example.class);
48         Object JavaDoc obj = loaded.newInstance();
49         String JavaDoc value = "HELLO";
50         loaded.getMethod("setHerby", new Class JavaDoc[]{ String JavaDoc.class }).invoke(obj, new Object JavaDoc[]{ value });
51         assertTrue(value.equals(loaded.getMethod("getHerby", null).invoke(obj, null)));
52
53         loaded.getMethod("setDerby", new Class JavaDoc[]{ Double.TYPE }).invoke(obj, new Object JavaDoc[]{ new Double JavaDoc(1.23456789d) });
54     }
55
56     private static Class JavaDoc inited;
57
58     public static void initStatic(Class JavaDoc foo) {
59         System.err.println("INITING: " + foo);
60     }
61
62     public void testAddStatic() throws Exception JavaDoc {
63         Method JavaDoc m = ReflectUtils.findMethod("net.sf.cglib.transform.impl.TestTransformingLoader.initStatic(Class)");
64         ClassTransformer t = new AddStaticInitTransformer(m);
65         // t = new ClassTransformerChain(new ClassTransformer[]{ t, new ClassTransformerTee(new org.objectweb.asm.util.TraceClassVisitor(null, new java.io.PrintWriter(System.out))) });
66
Class JavaDoc loaded = loadHelper(t, Example.class);
67         Object JavaDoc obj = loaded.newInstance();
68         // TODO
69
}
70
71     public void testInterceptField() throws Exception JavaDoc {
72         ClassTransformer t = new InterceptFieldTransformer(new InterceptFieldFilter() {
73             public boolean acceptRead(Type owner, String JavaDoc name) {
74                 return true;
75             }
76             public boolean acceptWrite(Type owner, String JavaDoc name) {
77                 return true;
78             }
79         });
80         Class JavaDoc loaded = loadHelper(t, Example.class);
81         // TODO
82
}
83
84     public void testFieldProvider() throws Exception JavaDoc {
85         ClassTransformer t = new FieldProviderTransformer();
86         Class JavaDoc loaded = loadHelper(t, Example.class);
87         // TODO
88
// FieldProvider fp = (FieldProvider)loaded.newInstance();
89
// assertTrue(((Integer)fp.getField("example")).intValue() == 42);
90
// fp.setField("example", new Integer(6));
91
// assertTrue(((Integer)fp.getField("example")).intValue() == 6);
92
// assertTrue(fp.getField("example") == null);
93
// try {
94
// fp.getField("dsfjkl");
95
// fail("expected exception");
96
// } catch (IllegalArgumentException ignore) { }
97
}
98
99     private static Class JavaDoc loadHelper( final ClassTransformer t, Class JavaDoc target) throws ClassNotFoundException JavaDoc {
100         ClassLoader JavaDoc parent = TestTransformingLoader.class.getClassLoader();
101         TransformingClassLoader loader = new TransformingClassLoader(parent, TEST_FILTER,
102         
103            new ClassTransformerFactory(){
104                   public ClassTransformer newInstance(){
105                      return t;
106                   }
107         }
108         
109         );
110         return loader.loadClass(target.getName());
111     }
112
113     public TestTransformingLoader(String JavaDoc testName) {
114         super(testName);
115     }
116     
117     public static void main(String JavaDoc[] args) {
118         junit.textui.TestRunner.run(suite());
119     }
120     
121     public static Test suite() {
122         return new TestSuite(TestTransformingLoader.class);
123     }
124
125     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
126     }
127     
128     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
129     }
130     
131 }
132
Popular Tags