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:
042:
043:package org.netbeans.core.lookup;
044:
045:
046:import java.util.Collection;
047:
048:import junit.framework.TestSuite;
049:
050:import org.netbeans.performance.Benchmark;
051:
052:import org.openide.util.Lookup;
053:import org.openide.util.lookup.AbstractLookup;
054:import org.openide.util.lookup.InstanceContent;
055:
056:
057:public class NbLookupBenchmark extends Benchmark {
058: /** how many times objects in INSTANCES should be added in */
059: private static Object[] ARGS = {
060: new Integer (1)
061: };
062:
063:
064: public NbLookupBenchmark(java.lang.String testName) {
065: super (testName, ARGS);
066: }
067:
068: public static void main(java.lang.String[] args) {
069: junit.textui.TestRunner.run(new TestSuite (NbLookupBenchmark.class));
070: }
071:
072: /** Lookup which simulates instance lookup. */
073: private AbstractLookup lookup;
074:
075: /** instances that we register */
076: private static Object[] INSTANCES = new Object[] {
077: new Integer (10),
078: new Object (),
079: "Ahoj",
080: new C4 (), new C3 (), new C2 (), new C1 ()
081: };
082:
083: /** Fills the lookup with instances */
084: protected void setUp () {
085: Integer integer = (Integer)getArgument ();
086: int cnt = integer.intValue ();
087:
088: boolean reverse = cnt < 0;
089: if (reverse) cnt = -cnt;
090:
091: InstanceContent iContent = new InstanceContent();
092:
093: lookup = new AbstractLookup(iContent);
094:
095: while (cnt-- > 0) {
096: for (int i = 0; i < INSTANCES.length; i++) {
097: if (reverse) {
098: iContent.add (INSTANCES[INSTANCES.length - i - 1]);
099: } else {
100: iContent.add (INSTANCES[i]);
101: }
102: }
103: }
104: }
105:
106: /** Clears the lookup.
107: */
108: protected void tearDown () {
109: lookup = null;
110: }
111:
112: /** Test to find the first registered object.
113: */
114: public void testInteger () {
115: enum (Integer.class);
116: }
117:
118: /** Test object.
119: */
120: public void testObject () {
121: enum (Object.class);
122: }
123:
124: /** Test string.
125: */
126: public void testString () {
127: enum (String.class);
128: }
129:
130: public void testC1 () {
131: enum (C1.class);
132: }
133:
134: public void testC2 () {
135: enum (C2.class);
136: }
137:
138: public void testC3 () {
139: enum (C3.class);
140: }
141:
142: public void testC4 () {
143: enum (C4.class);
144: }
145:
146: public void testI1 () {
147: enum (I1.class);
148: }
149:
150: public void testI2 () {
151: enum (I2.class);
152: }
153:
154: public void testI3 () {
155: enum (I3.class);
156: }
157:
158: public void testI4 () {
159: enum (I4.class);
160: }
161:
162:
163:
164: /** Enumerates over instances of given class.
165: * @param clazz the class to find instances of
166: */
167: private void enum (Class clazz) {
168: int cnt = getIterationCount ();
169:
170: while (cnt-- > 0) {
171: Lookup.Result res = lookup.lookup (new Lookup.Template (clazz));
172:
173: Collection c = res.allInstances ();
174: }
175: }
176:
177:
178: private static interface I1 {}
179: private static interface I2 extends I1 {}
180: private static interface I3 extends I1 {}
181: private static interface I4 extends I2, I3 {}
182: private static class C1 extends Object implements I2 {}
183: private static class C2 extends C1 {}
184: private static class C3 extends C2 implements I3 {}
185: private static class C4 extends C3 implements I4 {}
186:}
|