1 19 20 25 26 package org.netbeans.test.java; 27 28 import java.io.BufferedInputStream ; 29 import java.io.BufferedOutputStream ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.FilenameFilter ; 34 import java.io.IOException ; 35 import java.net.URL ; 36 import java.util.Vector ; 37 import javax.swing.text.StyledDocument ; 38 import org.netbeans.junit.AssertionFailedErrorException; 39 import org.openide.actions.SaveAllAction; 40 import org.openide.cookies.EditorCookie; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.loaders.DataFolder; 44 import org.openide.loaders.DataObject; 45 46 47 51 public class Utilities { 52 53 public static void saveAll() { 54 ((SaveAllAction) SaveAllAction.findObject(SaveAllAction.class, true)).performAction(); 55 } 56 57 public static void delete(File file) { 58 try { 59 DataObject.find(FileUtil.toFileObject(file)).delete(); 60 } catch (IOException e) { 61 } 62 } 63 64 public static File copyTo(File file, File destination) { 65 try { 66 FileObject SRC=FileUtil.toFileObject(file); 67 FileObject dest=FileUtil.toFileObject(destination); 68 DataObject.find(src).copy(DataFolder.findFolder(dest)); 69 return new File (destination, file.getName()); 70 } catch (IOException e) { 71 throw new AssertionFailedErrorException(e); 72 } 73 } 74 75 public static String getAsString(File file) { 76 String result; 77 try { 78 FileObject testFile = FileUtil.toFileObject(file); 79 DataObject DO = DataObject.find(testFile); 80 81 EditorCookie ec=(EditorCookie)(DO.getCookie(EditorCookie.class)); 82 StyledDocument doc=ec.openDocument(); 83 result=doc.getText(0, doc.getLength()); 84 } catch (Exception e){ 85 throw new AssertionFailedErrorException(e); 86 } 87 return result; 88 } 89 90 private static String [] allFileNames(final File f, final Vector v, boolean comp, final boolean recurse, final boolean fullName, final FilenameFilter filter) { 91 String [] files; 92 if(filter != null) { 93 files = f.list(filter); 94 } 95 else { 96 files = f.list(); 97 } 98 99 String path = f.getPath(); 100 if(!path.endsWith(File.separator)) { 101 path += File.separatorChar; 102 } 103 for(int i = 0; i < files.length; i++) { 104 String addElement; 105 if(fullName) { 106 addElement = path + files[i]; 107 } 108 else { 109 addElement = files[i]; 110 } 111 112 v.addElement(addElement); 113 } 114 if(recurse) { 115 String [] dirs = f.list(new FilenameFilter () { 116 public boolean accept(File f, String name){ 117 return (new File (f.getPath() + File.separatorChar + name).isDirectory()); 118 } 119 }); 120 for(int i = 0; i < dirs.length; i++) { 121 File newF = new File (path + dirs[i]); 122 allFileNames(newF, v, false, true, fullName, filter); 123 } 124 } 125 else { 126 comp = true; 127 } 128 if(comp) { 129 String [] strs = new String [v.size()]; 130 v.copyInto(strs); 131 return strs; 132 } 133 return null; 134 } 135 136 public static String [] getAllFilenames(File initialDirectory, boolean recurse, final String filter) { 137 FilenameFilter f = new FilenameFilter () { 138 public boolean accept(File f, String name) { 139 return (name.indexOf(filter) > 0); 140 }}; 141 142 if(!initialDirectory.isDirectory()) { 143 return new String [0]; 144 } 145 return allFileNames(initialDirectory, new Vector (), true, recurse, true, f); 146 } 147 148 153 public static boolean deleteDirectory(File path) { 154 if (path.exists()) { 155 File [] files = path.listFiles(); 156 for(int i=0; i<files.length; i++) { 157 if(files[i].isDirectory()) { 158 deleteDirectory(files[i]); 159 } else { 160 files[i].delete(); 161 } 162 } 163 } 164 return (path.delete()); 165 } 166 167 170 public static void takeANap(int waitTimeout) { 171 new org.netbeans.jemmy.EventTool().waitNoEvent(waitTimeout); 172 } 173 174 } 175 | Popular Tags |