KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > FSTest


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.openide.filesystems;
20
21 import java.io.IOException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Random JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.netbeans.performance.Benchmark;
30 import org.openide.filesystems.*;
31
32 /**
33  * FSTest is a base class for FileSystem tests. It defines a lot of methods that
34  * exploit interface of the FileSystem class. It tests operations that change
35  * state of a FileSystem.
36  */

37 public abstract class FSTest extends ReadOnlyFSTest {
38
39     public static final String JavaDoc ATTRIBUTES_NO_KEY = "ATTRIBUTES_NO";
40     
41     /** number of attributes (taken into account) for given run */
42     protected int attrsCount;
43     
44     /** Creates new Tests */
45     public FSTest(String JavaDoc name) {
46         super(name);
47     }
48     
49     /** Creates new Tests */
50     public FSTest(String JavaDoc name, Object JavaDoc[] args) {
51         super(name, args);
52     }
53     
54     /** inherited; sets up env */
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57         if (shouldDefAttrNo()) {
58             attrsCount = getIntValue(ATTRIBUTES_NO_KEY);
59         }
60         postSetup();
61     }
62     
63     /** Hook for operations right after the setup */
64     protected void postSetup() throws Exception JavaDoc {
65         // setup some attributes
66
if (getName().startsWith("testGetAttributes")) {
67             testSetOneAttributeSeq(1);
68         }
69     }
70     
71     /** Disposes given FileObjects */
72     protected void tearDownFileObjects(FileObject[] fos) throws Exception JavaDoc {
73         // setup some attributes
74
if (getName().startsWith("testGetAttributes")) {
75             unsetOneAttributeSeq();
76         }
77     }
78     
79     /** Creates a Map with default arguments values */
80     protected Map JavaDoc createDefaultMap() {
81         Map JavaDoc map = super.createDefaultMap();
82         if (shouldDefAttrNo()) {
83             map.put(ATTRIBUTES_NO_KEY, new Integer JavaDoc(2));
84         }
85         if (getName().startsWith("testSet")) {
86             narrow(map);
87         }
88         return map;
89     }
90     
91     /** Decides whether attributes number should be defined */
92     private boolean shouldDefAttrNo() {
93         return getName().startsWith("testSetMany");
94     }
95     
96     /** Creates arguments for this instance of Benchmark (not for given configuration) */
97     protected Map JavaDoc[] createArguments() {
98         if (shouldDefAttrNo()) {
99             Map JavaDoc[] ret = new Map JavaDoc[2];
100             ret[0] = createDefaultMap();
101
102             ret[1] = createDefaultMap();
103             ret[1].put(ATTRIBUTES_NO_KEY, new Integer JavaDoc(5));
104             return ret;
105         } else {
106             return super.createArguments();
107         }
108     }
109     
110     /** Sets FILE_NO_KEY to one tenth of its original value */
111     private static final void narrow(Map JavaDoc map) {
112         Integer JavaDoc in = (Integer JavaDoc) map.get(FILE_NO_KEY);
113         int ival = Math.max(in.intValue() / 10, 10);
114         map.put(FILE_NO_KEY, new Integer JavaDoc(ival));
115     }
116     
117     //--------------------------------------------------------------------------
118
//------------------------- attributes section -----------------------------
119

120     /** Sets one random attribute for all FileObjects (their no. given by the
121      * parameter). Attributes are added sequentially. Only one iteration
122      */

123     private void testSetOneAttributeSeq(int xiterations) throws IOException JavaDoc {
124         FileObject[] files = this.files;
125         String JavaDoc[][] pairs = this.pairs;
126         
127         for (int it = 0; it < xiterations; it++) {
128             for (int i = 0; i < files.length; i++) {
129                 files[i].setAttribute(pairs[i][0], pairs[i][1]);
130             }
131         }
132     }
133     
134     /** Unsets some attributes */
135     private void unsetOneAttributeSeq() throws IOException JavaDoc {
136         FileObject[] files = this.files;
137         String JavaDoc[][] pairs = this.pairs;
138         
139         for (int i = 0; i < files.length; i++) {
140             files[i].setAttribute(pairs[i][0], null);
141         }
142     }
143     
144     /** Sets one random attribute for all FileObjects (their no. given by the
145      * parameter). Attributes are added sequentially.
146      */

147     public void testSetOneAttributeSeq() throws IOException JavaDoc {
148         testSetOneAttributeSeq(iterations);
149     }
150     
151     /** Sets many random attributes for all FileObjects (their no. given by the
152      * parameter). Attributes are added sequentially.
153      */

154     public void testSetManyAttributesSeq() throws IOException JavaDoc {
155         FileObject[] files = this.files;
156         String JavaDoc[][] pairs = this.pairs;
157         int iterations = this.iterations;
158         
159         for (int it = 0; it < iterations; it++) {
160             for (int i = 0; i < files.length; i++) {
161                 for (int j = 0; (j < pairs.length) && (j < attrsCount); j++) {
162                     files[i].setAttribute(pairs[j][0], pairs[j][1]);
163                 }
164             }
165         }
166     }
167     
168     /** Sets one random attribute for all FileObjects (their no. given by the
169      * parameter). Attributes are added randomly.
170      */

171     public void testSetOneAttributeRnd() throws IOException JavaDoc {
172         FileObject[] files = this.files;
173         String JavaDoc[][] pairs = this.pairs;
174         int iterations = this.iterations;
175         int perm[] = this.perm;
176         
177         for (int it = 0; it < iterations; it++) {
178             for (int i = 0; i < files.length; i++) {
179                 files[perm[i]].setAttribute(pairs[i][0], pairs[i][1]);
180             }
181         }
182     }
183     
184     /** Sets many random attributes for all FileObjects (their no. given by the
185      * parameter). Attributes are added randomly.
186      */

187     public void testSetManyAttributesRnd() throws IOException JavaDoc {
188         FileObject[] files = this.files;
189         String JavaDoc[][] pairs = this.pairs;
190         int iterations = this.iterations;
191         int perm[] = this.perm;
192         
193         for (int it = 0; it < iterations; it++) {
194             for (int i = 0; i < files.length; i++) {
195                 for (int j = 0; (j < pairs.length) && (j < attrsCount); j++) {
196                     files[perm[i]].setAttribute(pairs[j][0], pairs[j][1]);
197                 }
198             }
199         }
200     }
201 }
202
Popular Tags