KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > localfs > LocalFSTest


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.localfs;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.openide.*;
25 import org.openide.filesystems.*;
26 import org.openide.filesystems.Utilities.Matcher;
27
28 import org.netbeans.performance.DataManager;
29 import org.netbeans.performance.DataDescriptor;
30
31 /**
32  * Test class for LocalFileSystem. All tests are inherited, this class only
33  * sets up operation environment - creates files, mounts filesystem, ...
34  */

35 public class LocalFSTest extends FSTest implements DataManager {
36
37     public static final String JavaDoc RES_NAME = "JavaSrc";
38     public static final String JavaDoc RES_EXT = ".java";
39     
40     public static final String JavaDoc getPackage(int base) {
41         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(100);
42         buff.append("org/openide/filesystems/data");
43         if (base != 0) {
44             buff.append(base);
45         }
46         buff.append("/");
47         return buff.toString();
48     }
49     
50     public static final String JavaDoc getPackageSysDep(int base) {
51         return getPackage(base).replace('/', File.separatorChar);
52     }
53     
54     public static final String JavaDoc getResource(int base) {
55         return getPackage(base) + RES_NAME + RES_EXT;
56     }
57     
58     protected LocalFileSystem localFS;
59     protected File mnt;
60     protected List ddescs;
61
62     /** Creates new DataGenerator */
63     public LocalFSTest(String JavaDoc name) {
64         super(name);
65         
66         ddescs = new ArrayList();
67     }
68    
69     /** Set up given number of FileObjects */
70     protected FileObject[] setUpFileObjects(int foCount) throws Exception JavaDoc {
71         
72         localFS = new LocalFileSystem();
73         localFS.setRootDirectory(mnt);
74         
75         FileObject folder = localFS.findResource(getPackage(0));
76         return folder.getChildren();
77     }
78     
79     /** Delete mnt */
80     protected void tearDownFileObjects(FileObject[] fos) throws Exception JavaDoc {
81     }
82     
83     /** Creates a given number of files in a given folder (actually in a subfolder)
84      * @param
85      * @retun a folder in which reside the created files (it is a sub folder of destRoot)
86      */

87     public static File createFiles(int foCount, int foBase, File destRoot) throws Exception JavaDoc {
88         InputStream is = LocalFSTest.class.getClassLoader().getResourceAsStream(getResource(0));
89         StringResult result = load(is, foCount, foBase);
90         return makeCopies(destRoot, foCount, foBase, result);
91     }
92     
93     /** Copies the content of <tt>result copyNo</tt> times under given <tt>destRoot</tt> */
94     private static File makeCopies(File destRoot, int copyNo, int foBase, StringResult result) throws Exception JavaDoc {
95         File folder;
96         File targetFolder;
97         
98         {
99             targetFolder = new File(destRoot, getPackageSysDep(foBase));
100             targetFolder.mkdirs();
101         }
102         
103         for (int i = 0; i < copyNo; i++) {
104             String JavaDoc name = "JavaSrc" + result.getVersionString();
105             File target = new File(targetFolder, name + ".java");
106             OutputStream os = new FileOutputStream(target);
107             Writer writer = new OutputStreamWriter(os);
108             writer.write(result.toString());
109             writer.flush();
110             writer.close();
111             
112             result.increment();
113         }
114         
115         return targetFolder;
116     }
117     
118     /** Loads content of the given stream, searching for predefined pattern, replacing
119      * that pattern with a new pattern.
120      */

121     private static StringResult load(InputStream is, int foCount, int foBase) throws Exception JavaDoc {
122         try {
123             int paddingSize = Utilities.expPaddingSize(foCount + foBase - 1);
124             int packPaddingSize = Utilities.expPaddingSize(foBase);
125             StringResult ret = new StringResult(paddingSize, foBase);
126             Reader reader = new BufferedReader(new InputStreamReader(is));
127             
128             PaddingMaker matcher = new PaddingMaker();
129             int c;
130             while ((c = reader.read()) >= 0) {
131                 char ch = (char) c;
132                 ret.append(ch);
133                 if (matcher.test(ch)) {
134                     if (matcher.isPackageHit()) {
135                         ret.rawAppend(String.valueOf(foBase));
136                     } else {
137                         ret.append(matcher.getPadding(paddingSize));
138                     }
139                 }
140             }
141             
142             return ret;
143         } finally {
144             if (is != null) {
145                 is.close();
146             }
147         }
148     }
149
150     /** Called after tearDown() */
151     public void tearDownData() throws Exception JavaDoc {
152         for (Iterator it = ddescs.iterator(); it.hasNext(); ) {
153             LFSDataDescriptor dd = (LFSDataDescriptor) it.next();
154             delete(dd.getRootDir());
155         }
156     }
157     
158     /** Called before setUp() */
159     public DataDescriptor createDataDescriptor() {
160         LFSDataDescriptor dd = new LFSDataDescriptor(getIntValue(FILE_NO_KEY));
161         ddescs.add(dd);
162         return dd;
163     }
164     
165     /** Called before setUp() */
166     public void setUpData(DataDescriptor ddesc) throws Exception JavaDoc {
167         LFSDataDescriptor dd = (LFSDataDescriptor) ddesc;
168         File root = dd.getRootDir();
169         if (root == null) {
170             mnt = createTempFolder();
171             createFiles(dd.getFileNo(), 0, mnt);
172             dd.setFile(mnt);
173         } else {
174             mnt = root;
175         }
176     }
177     
178     /** Computes padding for a character Stream */
179     static final class PaddingMaker {
180         private static final String JavaDoc PACKAGE = "package org.openide.filesystems.data";
181         
182         private int paddingSize;
183         private String JavaDoc paddingString;
184         private Matcher.State state;
185         
186         public PaddingMaker() {
187             paddingSize = -1;
188             paddingString = null;
189             Matcher matcher = new Matcher(new String JavaDoc[] { "JavaSrc", PACKAGE });
190             state = matcher.getInitState();
191         }
192         
193         /** Tests whether c is the last char in the found char sequence */
194         boolean test(char c) {
195             state = state.getNext(c);
196             return state.isTerminal();
197         }
198         
199         boolean isPackageHit() {
200             return state.getMatches()[0].equals(PACKAGE);
201         }
202         
203         /** @return a String with a given number of '0' chars */
204         String JavaDoc getPadding(int paddingSize) {
205             if (this.paddingSize != paddingSize) {
206                 paddingString = createPadding(paddingSize);
207                 this.paddingSize = paddingSize;
208             }
209             
210             return paddingString;
211         }
212         
213         static String JavaDoc createPadding(int paddingSize) {
214             StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc(paddingSize);
215             for (int i = 0; i < paddingSize; i++) {
216                 sbuffer.append('0');
217             }
218             return sbuffer.toString();
219         }
220     }
221     
222     /** Holds in memory content of a file, so that a given number of versions
223      * of that file can be made.
224      */

225     static final class StringResult {
226         private StringBuffer JavaDoc buffer;
227         private List positions;
228         private int version;
229         private int patternLength;
230         private boolean shouldRunPadding;
231         
232         StringResult(int patternLength, int foBase) {
233             buffer = new StringBuffer JavaDoc(10000);
234             positions = new ArrayList(10);
235             version = foBase;
236             this.patternLength = patternLength;
237             this.shouldRunPadding = true;
238         }
239         
240         void append(char c) {
241             buffer.append(c);
242         }
243         
244         void append(String JavaDoc s) {
245             positions.add(new Integer JavaDoc(buffer.length()));
246             buffer.append(s);
247         }
248         
249         void rawAppend(String JavaDoc s) {
250             buffer.append(s);
251         }
252         
253         void increment() {
254             version++;
255             runPadding();
256         }
257         
258         private void runPadding() {
259             String JavaDoc versStr = getVersionString();
260             newPadding(versStr);
261             shouldRunPadding = false;
262         }
263         
264         private void newPadding(String JavaDoc str) {
265             for (int i = 0; i < positions.size(); i++) {
266                 int idx = ((Integer JavaDoc) positions.get(i)).intValue();
267                 buffer.replace(idx, idx + str.length(), str);
268             }
269         }
270         
271         String JavaDoc getVersionString() {
272             StringBuffer JavaDoc vbuffer = new StringBuffer JavaDoc(patternLength);
273             Utilities.appendNDigits(version, patternLength, vbuffer);
274             return vbuffer.toString();
275         }
276         
277         public String JavaDoc toString() {
278             if (shouldRunPadding) {
279                 runPadding();
280             }
281             return buffer.toString();
282         }
283     }
284     
285 /*
286     public static void main(String[] args) throws Exception {
287         LocalFSTest lfstest = new LocalFSTest("first test");
288         lfstest.setUpFileObjects(500);
289     }
290   */

291 }
292
Popular Tags