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:
051: import org.netbeans.performance.Benchmark;
052: import org.openide.filesystems.*;
053:
054: /**
055: * FSTest is a base class for FileSystem tests. It defines a lot of methods that
056: * exploit interface of the FileSystem class. It tests operations that change
057: * state of a FileSystem.
058: */
059: public abstract class FSTest extends ReadOnlyFSTest {
060:
061: public static final String ATTRIBUTES_NO_KEY = "ATTRIBUTES_NO";
062:
063: /** number of attributes (taken into account) for given run */
064: protected int attrsCount;
065:
066: /** Creates new Tests */
067: public FSTest(String name) {
068: super (name);
069: }
070:
071: /** Creates new Tests */
072: public FSTest(String name, Object[] args) {
073: super (name, args);
074: }
075:
076: /** inherited; sets up env */
077: protected void setUp() throws Exception {
078: super .setUp();
079: if (shouldDefAttrNo()) {
080: attrsCount = getIntValue(ATTRIBUTES_NO_KEY);
081: }
082: postSetup();
083: }
084:
085: /** Hook for operations right after the setup */
086: protected void postSetup() throws Exception {
087: // setup some attributes
088: if (getName().startsWith("testGetAttributes")) {
089: testSetOneAttributeSeq(1);
090: }
091: }
092:
093: /** Disposes given FileObjects */
094: protected void tearDownFileObjects(FileObject[] fos)
095: throws Exception {
096: // setup some attributes
097: if (getName().startsWith("testGetAttributes")) {
098: unsetOneAttributeSeq();
099: }
100: }
101:
102: /** Creates a Map with default arguments values */
103: protected Map createDefaultMap() {
104: Map map = super .createDefaultMap();
105: if (shouldDefAttrNo()) {
106: map.put(ATTRIBUTES_NO_KEY, new Integer(2));
107: }
108: if (getName().startsWith("testSet")) {
109: narrow(map);
110: }
111: return map;
112: }
113:
114: /** Decides whether attributes number should be defined */
115: private boolean shouldDefAttrNo() {
116: return getName().startsWith("testSetMany");
117: }
118:
119: /** Creates arguments for this instance of Benchmark (not for given configuration) */
120: protected Map[] createArguments() {
121: if (shouldDefAttrNo()) {
122: Map[] ret = new Map[2];
123: ret[0] = createDefaultMap();
124:
125: ret[1] = createDefaultMap();
126: ret[1].put(ATTRIBUTES_NO_KEY, new Integer(5));
127: return ret;
128: } else {
129: return super .createArguments();
130: }
131: }
132:
133: /** Sets FILE_NO_KEY to one tenth of its original value */
134: private static final void narrow(Map map) {
135: Integer in = (Integer) map.get(FILE_NO_KEY);
136: int ival = Math.max(in.intValue() / 10, 10);
137: map.put(FILE_NO_KEY, new Integer(ival));
138: }
139:
140: //--------------------------------------------------------------------------
141: //------------------------- attributes section -----------------------------
142:
143: /** Sets one random attribute for all FileObjects (their no. given by the
144: * parameter). Attributes are added sequentially. Only one iteration
145: */
146: private void testSetOneAttributeSeq(int xiterations)
147: throws IOException {
148: FileObject[] files = this .files;
149: String[][] pairs = this .pairs;
150:
151: for (int it = 0; it < xiterations; it++) {
152: for (int i = 0; i < files.length; i++) {
153: files[i].setAttribute(pairs[i][0], pairs[i][1]);
154: }
155: }
156: }
157:
158: /** Unsets some attributes */
159: private void unsetOneAttributeSeq() throws IOException {
160: FileObject[] files = this .files;
161: String[][] pairs = this .pairs;
162:
163: for (int i = 0; i < files.length; i++) {
164: files[i].setAttribute(pairs[i][0], null);
165: }
166: }
167:
168: /** Sets one random attribute for all FileObjects (their no. given by the
169: * parameter). Attributes are added sequentially.
170: */
171: public void testSetOneAttributeSeq() throws IOException {
172: testSetOneAttributeSeq(iterations);
173: }
174:
175: /** Sets many random attributes for all FileObjects (their no. given by the
176: * parameter). Attributes are added sequentially.
177: */
178: public void testSetManyAttributesSeq() throws IOException {
179: FileObject[] files = this .files;
180: String[][] pairs = this .pairs;
181: int iterations = this .iterations;
182:
183: for (int it = 0; it < iterations; it++) {
184: for (int i = 0; i < files.length; i++) {
185: for (int j = 0; (j < pairs.length) && (j < attrsCount); j++) {
186: files[i].setAttribute(pairs[j][0], pairs[j][1]);
187: }
188: }
189: }
190: }
191:
192: /** Sets one random attribute for all FileObjects (their no. given by the
193: * parameter). Attributes are added randomly.
194: */
195: public void testSetOneAttributeRnd() throws IOException {
196: FileObject[] files = this .files;
197: String[][] pairs = this .pairs;
198: int iterations = this .iterations;
199: int perm[] = this .perm;
200:
201: for (int it = 0; it < iterations; it++) {
202: for (int i = 0; i < files.length; i++) {
203: files[perm[i]].setAttribute(pairs[i][0], pairs[i][1]);
204: }
205: }
206: }
207:
208: /** Sets many random attributes for all FileObjects (their no. given by the
209: * parameter). Attributes are added randomly.
210: */
211: public void testSetManyAttributesRnd() throws IOException {
212: FileObject[] files = this .files;
213: String[][] pairs = this .pairs;
214: int iterations = this .iterations;
215: int perm[] = this .perm;
216:
217: for (int it = 0; it < iterations; it++) {
218: for (int i = 0; i < files.length; i++) {
219: for (int j = 0; (j < pairs.length) && (j < attrsCount); j++) {
220: files[perm[i]].setAttribute(pairs[j][0],
221: pairs[j][1]);
222: }
223: }
224: }
225: }
226: }
|