KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > asm1 > NotifierClassVisitorTest


1 /*
2  * Java bytecode manipulation with ASM toolkit
3  * Copyright (c) 2004, Eugene Kuleshov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package asm1;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.objectweb.asm.ClassReader;
29 import org.objectweb.asm.ClassWriter;
30
31
32 public class NotifierClassVisitorTest extends TestCase {
33   private static final String JavaDoc TEST_CLASS = "asm1.Counter1";
34   
35   private TestListener listener;
36   private Counter counter;
37
38
39   public void setUp() throws Exception JavaDoc {
40     super.setUp();
41     
42     listener = new TestListener();
43     
44     Class JavaDoc cc = loadClass(TEST_CLASS);
45     counter = ( Counter) cc.newInstance();
46     
47     (( Notifier) counter).addListener( listener);
48   }
49   
50   
51   public void testCounter() {
52     int n1 = counter.count();
53     counter.increment();
54     int n2 = counter.count();
55     assertEquals( n1+1, n2);
56   }
57
58   public void testNotifier() {
59     counter.count();
60     counter.increment();
61     counter.count();
62
63     List JavaDoc events = listener.getEvents();
64     assertEquals( 3, events.size());
65   }
66
67   
68   private Class JavaDoc loadClass(final String JavaDoc className) throws ClassNotFoundException JavaDoc {
69     ClassLoader JavaDoc cl = new TestClassLoader(getClass().getClassLoader(), className);
70     return cl.loadClass(className);
71   }
72
73   
74   private static final class TestClassLoader extends ClassLoader JavaDoc {
75     private final String JavaDoc className;
76     private final ClassLoader JavaDoc cl;
77
78     public TestClassLoader(ClassLoader JavaDoc cl, String JavaDoc className) {
79       super();
80       this.cl = cl;
81       this.className = className;
82     }
83
84     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
85       if( className.equals(name)) {
86         try {
87           byte[] bytecode = transformClass( className);
88           return super.defineClass( className, bytecode, 0, bytecode.length);
89         
90         } catch( IOException JavaDoc ex) {
91           throw new ClassNotFoundException JavaDoc( "Load error: "+ex.toString(), ex);
92         
93         }
94       }
95       return cl.loadClass( name);
96     }
97     
98     private byte[] transformClass( String JavaDoc className) throws IOException JavaDoc {
99       ClassWriter cw = new ClassWriter( true, true);
100       NotifierClassVisitor ncv = new NotifierClassVisitor( cw);
101    
102       ClassReader cr = new ClassReader( getClass().getResourceAsStream( "/"+className.replace('.','/')+".class"));
103       cr.accept( ncv, false);
104       return cw.toByteArray();
105     }
106     
107   }
108
109
110   private static final class TestListener implements Listener {
111     private List JavaDoc events = new ArrayList JavaDoc();
112
113     public void notify( Object JavaDoc src, Object JavaDoc msg) {
114       events.add( new Object JavaDoc[] { src, msg});
115     }
116
117     public List JavaDoc getEvents() {
118       return events;
119     }
120     
121   }
122   
123 }
124
125
Popular Tags