1 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 35 public class LocalFSTest extends FSTest implements DataManager { 36 37 public static final String RES_NAME = "JavaSrc"; 38 public static final String RES_EXT = ".java"; 39 40 public static final String getPackage(int base) { 41 StringBuffer buff = new StringBuffer (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 getPackageSysDep(int base) { 51 return getPackage(base).replace('/', File.separatorChar); 52 } 53 54 public static final String 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 63 public LocalFSTest(String name) { 64 super(name); 65 66 ddescs = new ArrayList(); 67 } 68 69 70 protected FileObject[] setUpFileObjects(int foCount) throws Exception { 71 72 localFS = new LocalFileSystem(); 73 localFS.setRootDirectory(mnt); 74 75 FileObject folder = localFS.findResource(getPackage(0)); 76 return folder.getChildren(); 77 } 78 79 80 protected void tearDownFileObjects(FileObject[] fos) throws Exception { 81 } 82 83 87 public static File createFiles(int foCount, int foBase, File destRoot) throws Exception { 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 94 private static File makeCopies(File destRoot, int copyNo, int foBase, StringResult result) throws Exception { 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 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 121 private static StringResult load(InputStream is, int foCount, int foBase) throws Exception { 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 151 public void tearDownData() throws Exception { 152 for (Iterator it = ddescs.iterator(); it.hasNext(); ) { 153 LFSDataDescriptor dd = (LFSDataDescriptor) it.next(); 154 delete(dd.getRootDir()); 155 } 156 } 157 158 159 public DataDescriptor createDataDescriptor() { 160 LFSDataDescriptor dd = new LFSDataDescriptor(getIntValue(FILE_NO_KEY)); 161 ddescs.add(dd); 162 return dd; 163 } 164 165 166 public void setUpData(DataDescriptor ddesc) throws Exception { 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 179 static final class PaddingMaker { 180 private static final String PACKAGE = "package org.openide.filesystems.data"; 181 182 private int paddingSize; 183 private String paddingString; 184 private Matcher.State state; 185 186 public PaddingMaker() { 187 paddingSize = -1; 188 paddingString = null; 189 Matcher matcher = new Matcher(new String [] { "JavaSrc", PACKAGE }); 190 state = matcher.getInitState(); 191 } 192 193 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 204 String 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 createPadding(int paddingSize) { 214 StringBuffer sbuffer = new StringBuffer (paddingSize); 215 for (int i = 0; i < paddingSize; i++) { 216 sbuffer.append('0'); 217 } 218 return sbuffer.toString(); 219 } 220 } 221 222 225 static final class StringResult { 226 private StringBuffer 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 (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 s) { 245 positions.add(new Integer (buffer.length())); 246 buffer.append(s); 247 } 248 249 void rawAppend(String s) { 250 buffer.append(s); 251 } 252 253 void increment() { 254 version++; 255 runPadding(); 256 } 257 258 private void runPadding() { 259 String versStr = getVersionString(); 260 newPadding(versStr); 261 shouldRunPadding = false; 262 } 263 264 private void newPadding(String str) { 265 for (int i = 0; i < positions.size(); i++) { 266 int idx = ((Integer ) positions.get(i)).intValue(); 267 buffer.replace(idx, idx + str.length(), str); 268 } 269 } 270 271 String getVersionString() { 272 StringBuffer vbuffer = new StringBuffer (patternLength); 273 Utilities.appendNDigits(version, patternLength, vbuffer); 274 return vbuffer.toString(); 275 } 276 277 public String toString() { 278 if (shouldRunPadding) { 279 runPadding(); 280 } 281 return buffer.toString(); 282 } 283 } 284 285 291 } 292 | Popular Tags |