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.openide.filesystems;
042:
043: import java.io.IOException;
044: import java.io.File;
045: import java.util.Enumeration;
046: import java.util.List;
047: import java.util.ArrayList;
048: import java.util.Random;
049: import java.util.Map;
050: import java.util.HashMap;
051:
052: import org.netbeans.performance.Benchmark;
053: import org.netbeans.performance.MapArgBenchmark;
054: import org.openide.filesystems.*;
055:
056: /**
057: * ReadOnlyFSTest is a base class for FileSystem tests. It defines a lot of methods that
058: * exploit interface of the FileSystem class, note, however, that it uses only operations
059: * that do not change state of the FileSystem.
060: */
061: public abstract class ReadOnlyFSTest extends MapArgBenchmark {
062:
063: public static final String FILE_NO_KEY = "FILE_NO";
064:
065: /** number of files for given run */
066: protected int foCount;
067: /** iterations for given run */
068: protected int iterations;
069: /** FileObjects for given run */
070: protected FileObject[] files;
071: /** String attrs for given run */
072: protected String[][] pairs;
073: /** Gives permutation */
074: protected int[] perm;
075:
076: /** Creates new Tests */
077: public ReadOnlyFSTest(String name) {
078: super (name);
079: setArgumentArray(createArguments());
080: }
081:
082: /** Creates new Tests */
083: public ReadOnlyFSTest(String name, Object[] args) {
084: super (name, args);
085: }
086:
087: /** inherited; sets up env */
088: protected void setUp() throws Exception {
089: Map param = (Map) getArgument();
090: foCount = ((Integer) param.get(FILE_NO_KEY)).intValue();
091: iterations = getIterationCount();
092: files = setUpFileObjects(foCount);
093: pairs = generateRandomStrings(new String[files.length][2]);
094: perm = shuffleIntArray(fillIntArray(new int[files.length]));
095: }
096:
097: /** Set up given number of FileObjects */
098: protected abstract FileObject[] setUpFileObjects(int foCount)
099: throws Exception;
100:
101: /** Disposes given FileObjects */
102: protected abstract void tearDownFileObjects(FileObject[] fos)
103: throws Exception;
104:
105: /** Shuts down this test */
106: protected void tearDown() throws Exception {
107: tearDownFileObjects(files);
108: }
109:
110: /** Creates arguments for this instance of Benchmark (not for given configuration) */
111: protected Map[] createArguments() {
112: Map[] ret = new Map[1];
113: ret[0] = createDefaultMap();
114: return ret;
115: }
116:
117: /** Creates a Map with default arguments values */
118: protected Map createDefaultMap() {
119: Map map = super .createDefaultMap();
120: map.put(FILE_NO_KEY, new Integer(1000));
121: return map;
122: }
123:
124: //--------------------------------------------------------------------------
125: //------------------------- attributes section -----------------------------
126:
127: /** Gets all attributes for all FileObjects (their no. given by the
128: * parameter). Attributes are acquired sequentially.
129: */
130: public void testGetAttributesSeq() throws IOException {
131: int iterations = this .iterations;
132:
133: for (int it = 0; it < iterations; it++) {
134: for (int i = 0; i < files.length; i++) {
135: Enumeration enum = files[i].getAttributes();
136: while (enum.hasMoreElements()) {
137: String attr = (String) enum.nextElement();
138: Object val = files[i].getAttribute(attr);
139: }
140: }
141: }
142: }
143:
144: /** Gets all attributes for all FileObjects (their no. given by the
145: * parameter). Attributes are acquired randomly.
146: */
147: public void testGetAttributesRnd() throws IOException {
148: List list = new ArrayList(files.length + 3);
149: int iterations = this .iterations;
150:
151: for (int it = 0; it < iterations; it++) {
152: list.clear();
153: for (int i = 0; i < files.length; i++) {
154: list.add(files[i].getAttributes());
155: }
156:
157: for (int i = 0; i < files.length; i++) {
158: Enumeration enum = (Enumeration) list.get(i);
159: if (enum.hasMoreElements()) {
160: String key = (String) enum.nextElement();
161: files[i].getAttribute(key);
162: }
163: }
164: }
165: }
166:
167: //--------------------------------------------------------------------------
168: //------------------------- utility methods --------------------------------
169:
170: /** Remove all attributes for given files */
171: public void cleanUpAttributes(FileObject[] files) throws Exception {
172: for (int i = 0; i < files.length; i++) {
173: Enumeration enum = files[i].getAttributes();
174: while (enum.hasMoreElements()) {
175: String attr = (String) enum.nextElement();
176: files[i].setAttribute(attr, null);
177: }
178: }
179: }
180:
181: /** Fills in an array of ints so that arr[i] == i */
182: public static final int[] fillIntArray(int[] arr) {
183: for (int i = 0; i < arr.length; i++) {
184: arr[i] = i;
185: }
186:
187: return arr;
188: }
189:
190: /** Shuffles an int array so that arr[i] == i is not very likely */
191: public static final int[] shuffleIntArray(int[] arr) {
192: Random rnd = new Random(97943);
193:
194: for (int i = 0; i < arr.length; i++) {
195: int next = rnd.nextInt(arr.length);
196: swap(arr, i, next);
197: }
198:
199: return arr;
200: }
201:
202: /** Swaps integers from idxa and idxb in the arr array */
203: private static void swap(int[] arr, int idxa, int idxb) {
204: if (idxa == idxb) {
205: return;
206: }
207:
208: int tmp = arr[idxa];
209: arr[idxa] = arr[idxb];
210: arr[idxb] = tmp;
211: }
212:
213: /** Generates random String pairs */
214: public static final String[][] generateRandomStrings(String[][] arr) {
215: Random rnd = new Random(97943);
216:
217: for (int i = 0; i < arr.length; i++) {
218: arr[i][0] = String.valueOf(rnd.nextInt());
219: arr[i][1] = String.valueOf(rnd.nextInt());
220: }
221:
222: return arr;
223: }
224:
225: /** Creates temporary folder with a random name */
226: public static File createTempFolder() throws IOException {
227: File tmp = File.createTempFile("local", "lacol");
228: String name = tmp.getName();
229: File folder = tmp.getParentFile();
230: tmp.delete();
231:
232: folder = new File(folder, name);
233: folder.mkdir();
234:
235: return folder;
236: }
237:
238: /** Deletes (recursively) a folder */
239: public static void delete(File folder) throws Exception {
240: if (folder == null) {
241: return;
242: }
243:
244: File[] files = folder.listFiles();
245: if (files != null) {
246: for (int i = 0; i < files.length; i++) {
247: if (files[i].isDirectory()) {
248: delete(files[i]);
249: }
250: files[i].delete();
251: }
252: }
253:
254: folder.delete();
255: }
256: }
|