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.localfs;
042:
043: import java.io.*;
044: import java.util.*;
045:
046: import org.openide.*;
047: import org.openide.filesystems.*;
048: import org.openide.filesystems.Utilities.Matcher;
049:
050: import org.netbeans.performance.DataManager;
051: import org.netbeans.performance.DataDescriptor;
052:
053: /**
054: * Test class for LocalFileSystem. All tests are inherited, this class only
055: * sets up operation environment - creates files, mounts filesystem, ...
056: */
057: public class LocalFSTest extends FSTest implements DataManager {
058:
059: public static final String RES_NAME = "JavaSrc";
060: public static final String RES_EXT = ".java";
061:
062: public static final String getPackage(int base) {
063: StringBuffer buff = new StringBuffer(100);
064: buff.append("org/openide/filesystems/data");
065: if (base != 0) {
066: buff.append(base);
067: }
068: buff.append("/");
069: return buff.toString();
070: }
071:
072: public static final String getPackageSysDep(int base) {
073: return getPackage(base).replace('/', File.separatorChar);
074: }
075:
076: public static final String getResource(int base) {
077: return getPackage(base) + RES_NAME + RES_EXT;
078: }
079:
080: protected LocalFileSystem localFS;
081: protected File mnt;
082: protected List ddescs;
083:
084: /** Creates new DataGenerator */
085: public LocalFSTest(String name) {
086: super (name);
087:
088: ddescs = new ArrayList();
089: }
090:
091: /** Set up given number of FileObjects */
092: protected FileObject[] setUpFileObjects(int foCount)
093: throws Exception {
094:
095: localFS = new LocalFileSystem();
096: localFS.setRootDirectory(mnt);
097:
098: FileObject folder = localFS.findResource(getPackage(0));
099: return folder.getChildren();
100: }
101:
102: /** Delete mnt */
103: protected void tearDownFileObjects(FileObject[] fos)
104: throws Exception {
105: }
106:
107: /** Creates a given number of files in a given folder (actually in a subfolder)
108: * @param
109: * @retun a folder in which reside the created files (it is a sub folder of destRoot)
110: */
111: public static File createFiles(int foCount, int foBase,
112: File destRoot) throws Exception {
113: InputStream is = LocalFSTest.class.getClassLoader()
114: .getResourceAsStream(getResource(0));
115: StringResult result = load(is, foCount, foBase);
116: return makeCopies(destRoot, foCount, foBase, result);
117: }
118:
119: /** Copies the content of <tt>result copyNo</tt> times under given <tt>destRoot</tt> */
120: private static File makeCopies(File destRoot, int copyNo,
121: int foBase, StringResult result) throws Exception {
122: File folder;
123: File targetFolder;
124:
125: {
126: targetFolder = new File(destRoot, getPackageSysDep(foBase));
127: targetFolder.mkdirs();
128: }
129:
130: for (int i = 0; i < copyNo; i++) {
131: String name = "JavaSrc" + result.getVersionString();
132: File target = new File(targetFolder, name + ".java");
133: OutputStream os = new FileOutputStream(target);
134: Writer writer = new OutputStreamWriter(os);
135: writer.write(result.toString());
136: writer.flush();
137: writer.close();
138:
139: result.increment();
140: }
141:
142: return targetFolder;
143: }
144:
145: /** Loads content of the given stream, searching for predefined pattern, replacing
146: * that pattern with a new pattern.
147: */
148: private static StringResult load(InputStream is, int foCount,
149: int foBase) throws Exception {
150: try {
151: int paddingSize = Utilities.expPaddingSize(foCount + foBase
152: - 1);
153: int packPaddingSize = Utilities.expPaddingSize(foBase);
154: StringResult ret = new StringResult(paddingSize, foBase);
155: Reader reader = new BufferedReader(
156: new InputStreamReader(is));
157:
158: PaddingMaker matcher = new PaddingMaker();
159: int c;
160: while ((c = reader.read()) >= 0) {
161: char ch = (char) c;
162: ret.append(ch);
163: if (matcher.test(ch)) {
164: if (matcher.isPackageHit()) {
165: ret.rawAppend(String.valueOf(foBase));
166: } else {
167: ret.append(matcher.getPadding(paddingSize));
168: }
169: }
170: }
171:
172: return ret;
173: } finally {
174: if (is != null) {
175: is.close();
176: }
177: }
178: }
179:
180: /** Called after tearDown() */
181: public void tearDownData() throws Exception {
182: for (Iterator it = ddescs.iterator(); it.hasNext();) {
183: LFSDataDescriptor dd = (LFSDataDescriptor) it.next();
184: delete(dd.getRootDir());
185: }
186: }
187:
188: /** Called before setUp() */
189: public DataDescriptor createDataDescriptor() {
190: LFSDataDescriptor dd = new LFSDataDescriptor(
191: getIntValue(FILE_NO_KEY));
192: ddescs.add(dd);
193: return dd;
194: }
195:
196: /** Called before setUp() */
197: public void setUpData(DataDescriptor ddesc) throws Exception {
198: LFSDataDescriptor dd = (LFSDataDescriptor) ddesc;
199: File root = dd.getRootDir();
200: if (root == null) {
201: mnt = createTempFolder();
202: createFiles(dd.getFileNo(), 0, mnt);
203: dd.setFile(mnt);
204: } else {
205: mnt = root;
206: }
207: }
208:
209: /** Computes padding for a character Stream */
210: static final class PaddingMaker {
211: private static final String PACKAGE = "package org.openide.filesystems.data";
212:
213: private int paddingSize;
214: private String paddingString;
215: private Matcher.State state;
216:
217: public PaddingMaker() {
218: paddingSize = -1;
219: paddingString = null;
220: Matcher matcher = new Matcher(new String[] { "JavaSrc",
221: PACKAGE });
222: state = matcher.getInitState();
223: }
224:
225: /** Tests whether c is the last char in the found char sequence */
226: boolean test(char c) {
227: state = state.getNext(c);
228: return state.isTerminal();
229: }
230:
231: boolean isPackageHit() {
232: return state.getMatches()[0].equals(PACKAGE);
233: }
234:
235: /** @return a String with a given number of '0' chars */
236: String getPadding(int paddingSize) {
237: if (this .paddingSize != paddingSize) {
238: paddingString = createPadding(paddingSize);
239: this .paddingSize = paddingSize;
240: }
241:
242: return paddingString;
243: }
244:
245: static String createPadding(int paddingSize) {
246: StringBuffer sbuffer = new StringBuffer(paddingSize);
247: for (int i = 0; i < paddingSize; i++) {
248: sbuffer.append('0');
249: }
250: return sbuffer.toString();
251: }
252: }
253:
254: /** Holds in memory content of a file, so that a given number of versions
255: * of that file can be made.
256: */
257: static final class StringResult {
258: private StringBuffer buffer;
259: private List positions;
260: private int version;
261: private int patternLength;
262: private boolean shouldRunPadding;
263:
264: StringResult(int patternLength, int foBase) {
265: buffer = new StringBuffer(10000);
266: positions = new ArrayList(10);
267: version = foBase;
268: this .patternLength = patternLength;
269: this .shouldRunPadding = true;
270: }
271:
272: void append(char c) {
273: buffer.append(c);
274: }
275:
276: void append(String s) {
277: positions.add(new Integer(buffer.length()));
278: buffer.append(s);
279: }
280:
281: void rawAppend(String s) {
282: buffer.append(s);
283: }
284:
285: void increment() {
286: version++;
287: runPadding();
288: }
289:
290: private void runPadding() {
291: String versStr = getVersionString();
292: newPadding(versStr);
293: shouldRunPadding = false;
294: }
295:
296: private void newPadding(String str) {
297: for (int i = 0; i < positions.size(); i++) {
298: int idx = ((Integer) positions.get(i)).intValue();
299: buffer.replace(idx, idx + str.length(), str);
300: }
301: }
302:
303: String getVersionString() {
304: StringBuffer vbuffer = new StringBuffer(patternLength);
305: Utilities.appendNDigits(version, patternLength, vbuffer);
306: return vbuffer.toString();
307: }
308:
309: public String toString() {
310: if (shouldRunPadding) {
311: runPadding();
312: }
313: return buffer.toString();
314: }
315: }
316:
317: /*
318: public static void main(String[] args) throws Exception {
319: LocalFSTest lfstest = new LocalFSTest("first test");
320: lfstest.setUpFileObjects(500);
321: }
322: */
323: }
|