1 19 20 21 package org.netbeans.modules.editor.mimelookup.impl; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileUtil; 28 import org.openide.filesystems.Repository; 29 import org.openide.util.RequestProcessor; 30 31 32 36 public class TestUtilities { 37 38 39 private TestUtilities(){ 40 } 41 42 51 public static boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object requiredValue){ 52 int time = (int) maxMiliSeconds / 100; 53 while (time > 0) { 54 Object resolvedValue = resolver.getValue(); 55 if (requiredValue == null && resolvedValue == null){ 56 return true; 57 } 58 if (requiredValue != null && requiredValue.equals(resolvedValue)){ 59 return true; 60 } 61 try { 62 Thread.currentThread().sleep(100); 63 } catch (InterruptedException ex) { 64 time=0; 65 } 66 time--; 67 } 68 return false; 69 } 70 71 74 public static interface ValueResolver{ 75 76 Object getValue(); 77 } 78 79 private static void deleteFileImpl(File workDir, String path) throws IOException { 80 FileObject fo = FileUtil.toFileObject(new File (workDir, path)); 81 if (fo == null) { 82 fo = Repository.getDefault().getDefaultFileSystem().findResource(path); if (fo == null){ 84 return; 85 } 86 } 87 fo.delete(); 88 } 89 90 public static void deleteFile(final File workDir, final String path) { 91 RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable (){ 93 public void run(){ 94 try { 95 deleteFileImpl(workDir, path); 96 } catch (IOException ioe){ 97 ioe.printStackTrace(); 98 } 99 } 100 }); 101 102 try { 103 task.waitFinished(1000); 104 } catch (InterruptedException e) { 105 } 107 } 108 109 private static void createFileImpl(File workDir, String path) throws IOException { 110 FileObject fo = FileUtil.toFileObject(workDir); 111 if (fo == null) { 112 throw new IOException ("Can't map '" + workDir.getAbsolutePath() + "' to the filesystem repository."); 113 } 114 115 String [] pathElements = path.split("/", -1); 116 for (int i = 0; i < pathElements.length; i++ ) { 117 String elementName = pathElements[i]; 118 119 if (elementName.length() == 0) { 120 continue; 121 } 122 123 FileObject f = fo.getFileObject(elementName); 124 if (f != null && f.isValid()) { 125 fo = f; 126 } else { 127 if (i + 1 < pathElements.length) { 128 fo = fo.createFolder(elementName); 129 } else { 130 fo = fo.createData(elementName); 132 } 133 } 134 135 fo.refresh(); 136 } 137 } 138 139 public static void createFile(final File workDir, final String path) { 140 RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable (){ 142 public void run(){ 143 try { 144 createFileImpl(workDir, path); 145 } catch (IOException ioe){ 146 ioe.printStackTrace(); 147 } 148 } 149 }); 150 151 try { 152 task.waitFinished(1000); 153 } catch (InterruptedException e) { 154 } 156 } 157 158 public static void sleepForWhile() { 159 try { 160 Thread.sleep(321); 161 } catch (InterruptedException ex) { 162 } 164 } 165 166 public static void consumeAllMemory() { 167 ArrayList list = new ArrayList (); 168 long size = 0; 169 try { 170 for(int i = 0; i < 1000000; i++) { 171 byte [] padding = new byte[100000]; 172 list.add(padding); 173 size += padding.length; 174 } 175 throw new IllegalStateException ("Can't run out of memory! The VM's heap size is too big."); 176 } catch (OutOfMemoryError e) { 177 list = null; 180 System.out.println("OutOfMemory after allocating " + size + " bytes."); 181 } 182 } 183 184 public static void gc() { 185 for (int i = 0; i < 10; i++) { 186 System.gc(); 187 try { 188 Thread.sleep(123); 189 } catch (InterruptedException ex) { 190 } 192 } 193 } 194 } 195 | Popular Tags |