KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > PatchByteCodeTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans;
21
22 import junit.framework.AssertionFailedError;
23 import junit.textui.TestRunner;
24 import org.netbeans.junit.*;
25 import java.io.InputStream JavaDoc;
26 import java.lang.reflect.*;
27 import java.util.*;
28
29 /** Test patching of openide.jar byte code for compatibility.
30  * @author Jaroslav Tulach
31  */

32 public class PatchByteCodeTest extends NbTestCase {
33
34     public PatchByteCodeTest(String JavaDoc name) {
35         super(name);
36     }
37
38     public static void main(String JavaDoc[] args) {
39         TestRunner.run(new NbTestSuite(PatchByteCodeTest.class));
40     }
41     
42     protected void setUp() throws Exception JavaDoc {
43         super.setUp();
44     }
45
46     public void testBeanTreeViewLoad () throws Exception JavaDoc {
47         checkPatching (
48             "org.openide.explorer.view.BeanTreeView",
49             "data/BeanTreeView.clazz",
50             null, null
51         , null);
52     }
53
54     /* XXX This module is obsolete. Does the test do anything else useful?
55     public void testCompilerGroupLoad () throws Exception {
56         checkPatching (
57             "org.openide.compiler.CompilerGroup",
58             "data/CompilerGroup.clazz",
59             null, null
60         );
61         
62         InputStream is = getClass ().getResourceAsStream ("data/CompilerGroup.clazz");
63         assertNotNull ("Class has not been found", is);
64         
65         byte[] arr = new byte[is.available ()];
66         int l = is.read (arr);
67         assertEquals ("Read exactly as much as expected", l, arr.length);
68         
69
70         HashMap args = new HashMap ();
71         args.put ("netbeans.public", Arrays.asList(new String[] { "addCompilerListener", "removeCompilerListener" }) );
72         byte[] res = PatchByteCode.enhanceClass(arr, args);
73         PatchClassLoader loader = new PatchClassLoader ("org.openide.compiler.CompilerGroup", res, ClassLoader.getSystemClassLoader());
74         
75         Class c = loader.loadClass ("org.openide.compiler.CompilerGroup");
76         
77         Method m = c.getDeclaredMethod("addCompilerListener", new Class[] { org.openide.compiler.CompilerListener.class });
78         assertTrue ("Is not final", !Modifier.isFinal (m.getModifiers ()));
79         
80         m = c.getDeclaredMethod("removeCompilerListener", new Class[] { org.openide.compiler.CompilerListener.class });
81         assertTrue ("Is not final", !Modifier.isFinal (m.getModifiers ()));
82     }
83     */

84     
85     public void testClassCanBeAlsoInstantiated () throws Exception JavaDoc {
86         Class JavaDoc c = checkPatching (
87             Sample.class.getName (),
88             "Sample.class",
89             "java.lang.Throwable",
90             null,
91             null
92         );
93         
94         c.newInstance ();
95     }
96
97     public void testConstructorCanBeAlsoInstantiated () throws Exception JavaDoc {
98         Class JavaDoc c = checkPatching (
99             Sample2.class.getName (),
100             "Sample2.class",
101             "java.lang.Throwable",
102             null,
103             new String JavaDoc[] { "<init>" }
104         );
105         
106         c.newInstance ();
107     }
108     
109     public void testChangingSetOfSuperInterfaces () throws Exception JavaDoc {
110         Class JavaDoc c = checkPatching (
111             Sample.class.getName (),
112             "Sample.class",
113             "java.lang.Throwable",
114             new String JavaDoc[] { "org.openide.nodes.Node$Cookie", "java.lang.Cloneable" }
115         , null);
116         
117         assertEquals ("Super class is throwable", Throwable JavaDoc.class, c.getSuperclass());
118         Class JavaDoc[] ifaces = c.getInterfaces();
119         assertEquals ("Two of them", 2, ifaces.length);
120
121         
122         Object JavaDoc obj = c.newInstance ();
123         assertTrue ("Is instance of Cookie", obj instanceof org.openide.nodes.Node.Cookie);
124         assertTrue ("Is instance of Cloneable", obj instanceof Cloneable JavaDoc);
125     }
126     
127     
128     public void testPatchingOfFieldsAndMethodsToPublicAndNonFinal () throws Exception JavaDoc {
129         InputStream JavaDoc is = getClass ().getResourceAsStream ("Sample.class");
130         assertNotNull ("Class has not been found", is);
131         
132         byte[] arr = new byte[is.available ()];
133         int l = is.read (arr);
134         assertEquals ("Read exactly as much as expected", l, arr.length);
135
136         
137         
138         HashMap args = new HashMap ();
139         args.put ("netbeans.public", Arrays.asList(new String JavaDoc[] { "member", "field", "method", "staticmethod" }) );
140         byte[] res = PatchByteCode.enhanceClass(arr, args);
141         PatchClassLoader loader = new PatchClassLoader (Sample.class.getName (), res, ClassLoader.getSystemClassLoader());
142         
143         Class JavaDoc c = loader.loadClass (Sample.class.getName ());
144
145         assertTrue ("Class should be public", Modifier.isPublic (c.getModifiers()));
146
147         Method m = c.getDeclaredMethod("method", new Class JavaDoc[0]);
148         assertNotNull ("Mehtod method is there", m);
149         assertTrue ("And is public", Modifier.isPublic (m.getModifiers()));
150         assertTrue ("And is not final", !Modifier.isFinal(m.getModifiers ()));
151         assertTrue ("And is not static", !Modifier.isStatic(m.getModifiers()));
152         assertTrue ("And is not synchronzied", !Modifier.isSynchronized(m.getModifiers()));
153         
154         m = c.getDeclaredMethod("member", new Class JavaDoc[] { Object JavaDoc.class });
155         assertNotNull ("Member method is there", m);
156         assertTrue ("And is public", Modifier.isPublic (m.getModifiers()));
157         assertTrue ("And is not final", !Modifier.isFinal(m.getModifiers ()));
158         assertTrue ("And is not static", !Modifier.isStatic(m.getModifiers()));
159         assertTrue ("And is synchronzied", Modifier.isSynchronized(m.getModifiers()));
160         
161         m = c.getDeclaredMethod("staticmethod", new Class JavaDoc[] { });
162         assertNotNull ("Member method is there", m);
163         assertTrue ("And is public", Modifier.isPublic (m.getModifiers()));
164         assertTrue ("And is not final", !Modifier.isFinal(m.getModifiers ()));
165         assertTrue ("And is not static", Modifier.isStatic(m.getModifiers()));
166         assertTrue ("And is not synchronzied", !Modifier.isSynchronized(m.getModifiers()));
167         
168         java.lang.reflect.Field JavaDoc f;
169         
170         f = c.getDeclaredField("member");
171         assertNotNull ("Really exists", f);
172         assertTrue ("Is public", Modifier.isPublic (f.getModifiers ()));
173         assertTrue ("Is not final", !Modifier.isFinal (f.getModifiers ()));
174         assertTrue ("Is static", Modifier.isStatic (f.getModifiers ()));
175         
176         f = c.getDeclaredField("field");
177         assertNotNull ("Really exists", f);
178         assertTrue ("Is public", Modifier.isPublic (f.getModifiers ()));
179         assertTrue ("Is not final", !Modifier.isFinal (f.getModifiers ()));
180         assertTrue ("Is static", !Modifier.isStatic (f.getModifiers ()));
181         
182     }
183
184     public void testRenameOfAMember () throws Exception JavaDoc {
185         InputStream JavaDoc is = getClass ().getResourceAsStream ("Sample.class");
186         assertNotNull ("Class has not been found", is);
187         
188         byte[] arr = new byte[is.available ()];
189         int l = is.read (arr);
190         assertEquals ("Read exactly as much as expected", l, arr.length);
191
192         
193         
194         HashMap args = new HashMap ();
195         args.put ("netbeans.rename", Arrays.asList(new String JavaDoc[] { "staticmethod", "StaticMethod" }) );
196         byte[] res = PatchByteCode.enhanceClass(arr, args);
197
198         PatchClassLoader loader = new PatchClassLoader (Sample.class.getName (), res, ClassLoader.getSystemClassLoader());
199         
200         Class JavaDoc c = loader.loadClass (Sample.class.getName ());
201
202         try {
203             c.getDeclaredMethod("staticmethod", new Class JavaDoc[] { });
204             fail ("The old method is still present");
205         } catch (NoSuchMethodException JavaDoc ex) {
206             // ok, should not be there
207
}
208         
209         java.lang.reflect.Method JavaDoc m = c.getDeclaredMethod("StaticMethod", new Class JavaDoc[] { });
210         assertNotNull ("Renamed method found", m);
211     }
212         
213     private Class JavaDoc checkPatching (
214         String JavaDoc className,
215         String JavaDoc resource,
216         String JavaDoc superclass,
217         String JavaDoc[] interfaces,
218         String JavaDoc[] publc
219     ) throws Exception JavaDoc {
220         if (superclass == null) {
221             superclass = PatchByteCodeTest.class.getName ();
222         }
223         
224         InputStream JavaDoc is = getClass ().getResourceAsStream (resource);
225         assertNotNull ("Resource has been found " + resource, is);
226         
227         byte[] arr = new byte[is.available ()];
228         int l = is.read (arr);
229         assertEquals ("Read exactly as much as expected", l, arr.length);
230         
231         HashMap args = new HashMap ();
232         args.put ("netbeans.superclass", superclass.replace ('.', '/'));
233         
234         if (interfaces != null) {
235             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
236             String JavaDoc ap = "";
237             for (int i = 0; i < interfaces.length; i++) {
238                 sb.append (ap);
239                 sb.append (interfaces[i]);
240                 ap = ",";
241             }
242             args.put ("netbeans.interfaces", sb.toString().replace('.', '/'));
243         }
244         
245         if (publc != null) {
246             args.put ("netbeans.public", Arrays.asList(publc));
247         }
248         
249         byte[] res = PatchByteCode.enhanceClass(arr, args);
250         PatchClassLoader loader = new PatchClassLoader (className, res);
251
252         Class JavaDoc c = loader.loadClass (className);
253         
254         assertEquals (
255             "Superclass changed appropriately",
256             superclass,
257             c.getSuperclass().getName ()
258         );
259         
260         return c;
261     }
262     
263     
264     private static final class PatchClassLoader extends ClassLoader JavaDoc {
265         private String JavaDoc res;
266         private byte[] arr;
267         
268         public PatchClassLoader (String JavaDoc res, byte[] arr) {
269             this (res, arr, PatchClassLoader.class.getClassLoader ());
270         }
271         
272         public PatchClassLoader (String JavaDoc res, byte[] arr, ClassLoader JavaDoc c) {
273             super (c);
274             
275             this.res = res;
276             this.arr = arr;
277         }
278         
279         protected synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
280         throws ClassNotFoundException JavaDoc {
281             if (res.equals (name)) {
282                 byte[] patch = PatchByteCode.patch(arr, name);
283                 
284                 return defineClass (name, patch, 0, patch.length);
285             } else {
286                 return super.loadClass (name, resolve);
287             }
288         }
289     }
290 }
291
Popular Tags