KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > MultiURLClassLoaderTest


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 package org.netbeans.core;
20
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.openide.filesystems.MultiFileSystem;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.InstanceDataObject;
29 import org.openide.loaders.DataObject;
30
31 import org.openide.filesystems.multifs.MultiXMLFSTest;
32 import org.openide.filesystems.multifs.MultiXMLFSTest.FileWrapper;
33 import org.openide.filesystems.ReadOnlyFSTest;
34 import org.openide.loaders.Utilities;
35
36 import org.netbeans.ProxyClassLoader;
37
38 import org.netbeans.performance.MapArgBenchmark;
39 import org.netbeans.performance.DataManager;
40 import org.netbeans.performance.DataDescriptor;
41
42 /**
43  * Performance test for <code>ProxyClassLoader</code>
44  * <em>Note:</em> originally it was created for <code>MultiURLClassLoader</code>
45  * therefore the name, now just changed for ProxyClassLoader.
46  */

47 public class MultiURLClassLoaderTest extends MapArgBenchmark implements DataManager {
48     
49     private static final String JavaDoc CLASS_NO_KEY = "CLASS_NO";
50     
51     private MultiXMLFSTest mfstest;
52     private InstanceDataObject[] instanceObjects;
53     
54     /** Creates new Benchmark without arguments for given test method
55      * @param name the name fo the testing method
56      */

57     public MultiURLClassLoaderTest(String JavaDoc name) {
58         super(name);
59         setArgumentArray(createArguments());
60         init();
61     }
62     
63     /** Creates an argument array */
64     private Map JavaDoc[] createArguments() {
65         Map JavaDoc[] ret = new Map JavaDoc[] { createDefaultMap(), createDefaultMap() };
66         ret[1].put(CLASS_NO_KEY, new Integer JavaDoc(1000));
67         return ret;
68     }
69
70     /** Creates new Benchmark for given test method with given set of arguments
71      * @param name the name fo the testing method
72      * @param args the array of objects describing arguments to testing method
73      */

74     public MultiURLClassLoaderTest(String JavaDoc name, Object JavaDoc[] args) {
75         super(name, args);
76         init();
77     }
78     
79     /** init */
80     private void init() {
81         mfstest = new MultiXMLFSTest(getName());
82     }
83     
84     // things to override by the implementation of a particular Benchmark
85

86     /** This method is called before the actual test method to allow
87      * the benchmark to prepare accordingly to informations available
88      * through {@link #getIterationCount}, {@link #getArgument} and {@link #getName}.
89      * This method can use assertions to signal failure of the test.
90      * @throws Exception This method can throw any exception which is treated as a error in the testing code
91      * or testing enviroment.
92      */

93     protected void setUp() throws Exception JavaDoc {
94         int size = getIntValue(CLASS_NO_KEY);
95         
96         FileObject[] fileObjects = mfstest.setUpFileObjects(size);
97         FileWrapper[] wrappers = mfstest.getFileWrappers();
98         
99         URLClassLoader JavaDoc[] parents = new URLClassLoader JavaDoc[wrappers.length];
100         for (int i = 0; i < parents.length; i++) {
101             parents[i] = new URLClassLoader JavaDoc(new URL JavaDoc[] { wrappers[i].getMnt().toURL() });
102         }
103         
104 // MultiURLClassLoader multicloader = new MultiURLClassLoader(new URL[] {}, parents);
105
ClassLoader JavaDoc multicloader = new ProxyClassLoader(parents);
106         
107         instanceObjects = fileObjects2InstanceDataObjects(fileObjects);
108         setClassLoader(instanceObjects, multicloader);
109     }
110     
111     private static InstanceDataObject[] fileObjects2InstanceDataObjects(FileObject[] fos) throws Exception JavaDoc {
112         ArrayList JavaDoc list = new ArrayList JavaDoc(fos.length);
113         for (int i = 0; i < fos.length; i++) {
114             DataObject res = DataObject.find(fos[i]);
115             if (res instanceof InstanceDataObject) {
116                 list.add(res);
117             }
118         }
119         
120         return (InstanceDataObject[]) list.toArray(new InstanceDataObject[list.size()]);
121     }
122     
123     private static void setClassLoader(InstanceDataObject[] idos, ClassLoader JavaDoc cl) throws Exception JavaDoc {
124         for (int i = 0; i < idos.length; i++) {
125             Utilities.setCustomClassLoader(idos[i], cl);
126         }
127     }
128
129     /** This method is called after every finished test method.
130      * It is intended to be used to free all the resources allocated
131      * during {@link #setUp} or the test itself.
132      * This method can use assertions to signal failure of the test.
133      * @throws Exception This method can throw any exception which is treated as a error in the testing code
134      * or testing enviroment.
135      */

136     protected void tearDown() throws Exception JavaDoc {
137     }
138     
139     /** Creates a Map with default arguments values */
140     protected Map JavaDoc createDefaultMap() {
141         Map JavaDoc map = super.createDefaultMap();
142         map.put(CLASS_NO_KEY, new Integer JavaDoc(500));
143         map.put(MultiXMLFSTest.XMLFS_NO_KEY, new Integer JavaDoc(30));
144         return map;
145     }
146     
147     /** Called after tearDown() */
148     public void tearDownData() throws Exception JavaDoc {
149         mfstest.tearDownData();
150     }
151
152     /** Called before setUp() */
153     public DataDescriptor createDataDescriptor() {
154         return new MUCDataDescriptor(getIntValue(CLASS_NO_KEY), getIntValue(MultiXMLFSTest.XMLFS_NO_KEY));
155     }
156     
157     /** Called before setUp() */
158     public void setUpData(DataDescriptor ddesc) throws Exception JavaDoc {
159         MUCDataDescriptor dd = (MUCDataDescriptor) ddesc;
160         DataDescriptor other = dd.getDD();
161         
162         int fileNo = getIntValue(CLASS_NO_KEY);
163         Map JavaDoc map = (Map JavaDoc) getArgument();
164         map.put(ReadOnlyFSTest.FILE_NO_KEY, new Integer JavaDoc(fileNo));
165         
166         mfstest.setParent(this);
167         
168         if (other == null) {
169             other = mfstest.createDataDescriptor();
170             dd.setDD(other);
171         }
172         
173         mfstest.setUpData(other);
174     }
175
176     //--------------------- tests ---------------------
177
/** MultiURLClassLoader */
178     public void testInstanceClasses() throws Exception JavaDoc {
179         for (int i = 0; i < instanceObjects.length; i++) {
180             String JavaDoc klass = instanceObjects[i].instanceName();
181             instanceObjects[i].instanceClass();
182         }
183     }
184     
185     /*
186     public static void main(String[] args) throws Exception {
187         MultiURLClassLoaderTest mcltest = new MultiURLClassLoaderTest("test");
188         mcltest.setUp();
189         
190         System.out.println("ORDINARY: " + mcltest.wrappers[1].getClassLoader().loadClass("org.openide.filesystems.data10.JavaSrc15"));
191         System.out.println("Multi: " + mcltest.multicloader.loadClass("org.openide.filesystems.data10.JavaSrc15"));
192         System.out.println("Multi2: " + mcltest.multicloader.loadClass("org.openide.filesystems.data90.JavaSrc99"));
193     }
194      */

195 }
Popular Tags