001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.core;
042:
043: import java.net.URL;
044: import java.net.URLClassLoader;
045: import java.util.ArrayList;
046: import java.util.Map;
047:
048: import org.openide.filesystems.MultiFileSystem;
049: import org.openide.filesystems.FileObject;
050: import org.openide.loaders.InstanceDataObject;
051: import org.openide.loaders.DataObject;
052:
053: import org.openide.filesystems.multifs.MultiXMLFSTest;
054: import org.openide.filesystems.multifs.MultiXMLFSTest.FileWrapper;
055: import org.openide.filesystems.ReadOnlyFSTest;
056: import org.openide.loaders.Utilities;
057:
058: import org.netbeans.ProxyClassLoader;
059:
060: import org.netbeans.performance.MapArgBenchmark;
061: import org.netbeans.performance.DataManager;
062: import org.netbeans.performance.DataDescriptor;
063:
064: /**
065: * Performance test for <code>ProxyClassLoader</code>
066: * <em>Note:</em> originally it was created for <code>MultiURLClassLoader</code>
067: * therefore the name, now just changed for ProxyClassLoader.
068: */
069: public class MultiURLClassLoaderTest extends MapArgBenchmark implements
070: DataManager {
071:
072: private static final String CLASS_NO_KEY = "CLASS_NO";
073:
074: private MultiXMLFSTest mfstest;
075: private InstanceDataObject[] instanceObjects;
076:
077: /** Creates new Benchmark without arguments for given test method
078: * @param name the name fo the testing method
079: */
080: public MultiURLClassLoaderTest(String name) {
081: super (name);
082: setArgumentArray(createArguments());
083: init();
084: }
085:
086: /** Creates an argument array */
087: private Map[] createArguments() {
088: Map[] ret = new Map[] { createDefaultMap(), createDefaultMap() };
089: ret[1].put(CLASS_NO_KEY, new Integer(1000));
090: return ret;
091: }
092:
093: /** Creates new Benchmark for given test method with given set of arguments
094: * @param name the name fo the testing method
095: * @param args the array of objects describing arguments to testing method
096: */
097: public MultiURLClassLoaderTest(String name, Object[] args) {
098: super (name, args);
099: init();
100: }
101:
102: /** init */
103: private void init() {
104: mfstest = new MultiXMLFSTest(getName());
105: }
106:
107: // things to override by the implementation of a particular Benchmark
108:
109: /** This method is called before the actual test method to allow
110: * the benchmark to prepare accordingly to informations available
111: * through {@link #getIterationCount}, {@link #getArgument} and {@link #getName}.
112: * This method can use assertions to signal failure of the test.
113: * @throws Exception This method can throw any exception which is treated as a error in the testing code
114: * or testing enviroment.
115: */
116: protected void setUp() throws Exception {
117: int size = getIntValue(CLASS_NO_KEY);
118:
119: FileObject[] fileObjects = mfstest.setUpFileObjects(size);
120: FileWrapper[] wrappers = mfstest.getFileWrappers();
121:
122: URLClassLoader[] parents = new URLClassLoader[wrappers.length];
123: for (int i = 0; i < parents.length; i++) {
124: parents[i] = new URLClassLoader(new URL[] { wrappers[i]
125: .getMnt().toURL() });
126: }
127:
128: // MultiURLClassLoader multicloader = new MultiURLClassLoader(new URL[] {}, parents);
129: ClassLoader multicloader = new ProxyClassLoader(parents);
130:
131: instanceObjects = fileObjects2InstanceDataObjects(fileObjects);
132: setClassLoader(instanceObjects, multicloader);
133: }
134:
135: private static InstanceDataObject[] fileObjects2InstanceDataObjects(
136: FileObject[] fos) throws Exception {
137: ArrayList list = new ArrayList(fos.length);
138: for (int i = 0; i < fos.length; i++) {
139: DataObject res = DataObject.find(fos[i]);
140: if (res instanceof InstanceDataObject) {
141: list.add(res);
142: }
143: }
144:
145: return (InstanceDataObject[]) list
146: .toArray(new InstanceDataObject[list.size()]);
147: }
148:
149: private static void setClassLoader(InstanceDataObject[] idos,
150: ClassLoader cl) throws Exception {
151: for (int i = 0; i < idos.length; i++) {
152: Utilities.setCustomClassLoader(idos[i], cl);
153: }
154: }
155:
156: /** This method is called after every finished test method.
157: * It is intended to be used to free all the resources allocated
158: * during {@link #setUp} or the test itself.
159: * This method can use assertions to signal failure of the test.
160: * @throws Exception This method can throw any exception which is treated as a error in the testing code
161: * or testing enviroment.
162: */
163: protected void tearDown() throws Exception {
164: }
165:
166: /** Creates a Map with default arguments values */
167: protected Map createDefaultMap() {
168: Map map = super .createDefaultMap();
169: map.put(CLASS_NO_KEY, new Integer(500));
170: map.put(MultiXMLFSTest.XMLFS_NO_KEY, new Integer(30));
171: return map;
172: }
173:
174: /** Called after tearDown() */
175: public void tearDownData() throws Exception {
176: mfstest.tearDownData();
177: }
178:
179: /** Called before setUp() */
180: public DataDescriptor createDataDescriptor() {
181: return new MUCDataDescriptor(getIntValue(CLASS_NO_KEY),
182: getIntValue(MultiXMLFSTest.XMLFS_NO_KEY));
183: }
184:
185: /** Called before setUp() */
186: public void setUpData(DataDescriptor ddesc) throws Exception {
187: MUCDataDescriptor dd = (MUCDataDescriptor) ddesc;
188: DataDescriptor other = dd.getDD();
189:
190: int fileNo = getIntValue(CLASS_NO_KEY);
191: Map map = (Map) getArgument();
192: map.put(ReadOnlyFSTest.FILE_NO_KEY, new Integer(fileNo));
193:
194: mfstest.setParent(this );
195:
196: if (other == null) {
197: other = mfstest.createDataDescriptor();
198: dd.setDD(other);
199: }
200:
201: mfstest.setUpData(other);
202: }
203:
204: //--------------------- tests ---------------------
205: /** MultiURLClassLoader */
206: public void testInstanceClasses() throws Exception {
207: for (int i = 0; i < instanceObjects.length; i++) {
208: String klass = instanceObjects[i].instanceName();
209: instanceObjects[i].instanceClass();
210: }
211: }
212:
213: /*
214: public static void main(String[] args) throws Exception {
215: MultiURLClassLoaderTest mcltest = new MultiURLClassLoaderTest("test");
216: mcltest.setUp();
217:
218: System.out.println("ORDINARY: " + mcltest.wrappers[1].getClassLoader().loadClass("org.openide.filesystems.data10.JavaSrc15"));
219: System.out.println("Multi: " + mcltest.multicloader.loadClass("org.openide.filesystems.data10.JavaSrc15"));
220: System.out.println("Multi2: " + mcltest.multicloader.loadClass("org.openide.filesystems.data90.JavaSrc99"));
221: }
222: */
223: }
|