1 19 20 package org.openide.filesystems; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.net.URL ; 27 import java.text.DecimalFormat ; 28 import java.text.NumberFormat ; 29 import java.util.Arrays ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.StringTokenizer ; 35 36 39 public class TestUtilHid { 40 private static int cnt = 0; 41 private static NumberFormat cntFormat = new DecimalFormat ("000"); 42 public static final File locationOfTempFolder (String name) throws IOException { 43 name += cntFormat.format(cnt++); 44 String property = System.getProperty("workdir"); 45 46 File workdir = (property == null) ? null: new File (property); 47 File tmpdir = (workdir != null) ? workdir : new File (System.getProperty("java.io.tmpdir"), "fstests"); 48 tmpdir.mkdirs(); 49 if (!tmpdir.isDirectory()) throw new IOException ("Could not make: " + tmpdir); 50 File tmp = File.createTempFile(name,null, tmpdir); 51 tmp.delete(); 52 tmp = new File (tmp.getParent(),name); 53 return tmp; 54 } 55 56 57 public final static URL getResourceContext () { 58 return FileSystemFactoryHid.class.getResource("../data/"); 60 } 61 62 63 public final static boolean deleteFolder (File file) throws IOException { 64 boolean ret = file.delete(); 65 66 if (ret) { 67 return true; 68 } 69 70 if (! file.exists()) { 71 return false; 72 } 73 74 if (file.isDirectory()) { 75 File [] arr = file.listFiles(); 77 for (int i = 0; i < arr.length; i++) { 78 if (deleteFolder (arr[i]) != true) { 79 throw new IOException ("Cannot delete: "+ arr[i]); 80 } 82 } 83 } 84 85 return (file.delete() ? true : false); 86 } 87 88 94 public final static FileSystem createLocalFileSystem(String testName, String [] resources) throws IOException { 95 File mountPoint = locationOfLFSTempFolder(testName); 96 return createLocalFileSystem(mountPoint, resources); 97 98 } 99 100 public static FileSystem createLocalFileSystem(File mountPoint, String [] resources) throws IOException { 101 mountPoint.mkdir(); 102 103 for (int i = 0; i < resources.length; i++) { 104 File f = new File (mountPoint,resources[i]); 105 if (f.isDirectory() || resources[i].endsWith("/")) { 106 f.mkdirs(); 107 } 108 else { 109 f.getParentFile().mkdirs(); 110 try { 111 f.createNewFile(); 112 } catch (IOException iex) { 113 throw new IOException ("While creating " + resources[i] + " in " + mountPoint.getAbsolutePath() + ": " + iex.toString() + ": " + f.getAbsolutePath() + " with resource list: " + Arrays.asList(resources)); 114 } 115 } 116 } 117 118 LocalFileSystem lfs = new StatusFileSystem(); 119 try { 120 lfs.setRootDirectory(mountPoint); 121 } catch (Exception ex) {} 122 123 return lfs; 124 } 125 126 public static File locationOfLFSTempFolder(String testName) throws IOException { 127 File mountPoint = TestUtilHid.locationOfTempFolder("lfstest"); 128 return mountPoint; 129 } 130 131 public final static void destroyLocalFileSystem (String testName) throws IOException { 132 File mountPoint = TestUtilHid.locationOfTempFolder("lfstest"); 133 134 if (mountPoint.exists()) { 135 if (TestUtilHid.deleteFolder(mountPoint) == false) 136 throw new IOException ("Cannot delete test folder: " + mountPoint.toString()); 137 } 138 139 } 140 141 public final static void destroyXMLFileSystem(String testName) throws IOException { 142 File tempFile = TestUtilHid.locationOfTempFolder("xfstest"); 143 File xmlFile = new File (tempFile,"xfstest.xml"); 144 if (xmlFile.exists()) 145 xmlFile.delete(); 146 } 147 148 149 public final static FileSystem createXMLFileSystem(String testName, String [] resources) throws IOException { 150 File xmlFile = createXMLLayer(testName, resources); 151 152 XMLFileSystem xfs = new XMLFileSystem (); 153 try { 154 xfs.setXmlUrl(xmlFile.toURI().toURL()); 155 } catch (Exception ex) {} 156 157 return xfs; 158 } 159 160 public static File createXMLLayer(String testName, String [] resources) throws IOException { 161 File tempFile = TestUtilHid.locationOfTempFolder("xfstest"); 162 tempFile.mkdir(); 163 164 File xmlFile = new File (tempFile,"xfstest.xml"); 165 if (!xmlFile.exists()) { 166 xmlFile.getParentFile().mkdirs(); 167 xmlFile.createNewFile(); 168 } 169 FileOutputStream xos = new FileOutputStream (xmlFile); 170 ResourceElement root = new ResourceElement (""); 171 172 for (int i = 0; i < resources.length; i++) 173 root.add (resources[i]); 174 175 PrintWriter pw = new PrintWriter (xos); 176 pw.println("<filesystem>"); 177 testStructure (pw,root.getChildren () ," "); 178 pw.println("</filesystem>"); 179 pw.close(); 180 return xmlFile; 181 } 182 183 private static void testStructure (PrintWriter pw,ResourceElement[] childern,String tab) { 184 for (int i = 0; i < childern.length;i++) { 185 ResourceElement[] sub = childern[i].getChildren (); 186 if (sub.length != 0) 187 pw.println(tab+"<folder name=\""+childern[i].getName ()+"\">" ); 188 else 189 pw.println(tab+"<file name=\""+childern[i].getName ()+"\">" ); 190 191 testStructure (pw,sub, tab+" "); 192 193 if (sub.length != 0) 194 pw.println(tab+"</folder>" ); 195 else 196 pw.println(tab+"</file>" ); 197 } 198 } 199 200 private static class ResourceElement { 201 String element; 202 ResourceElement (String element) { 203 this.element = element; 205 } 206 Map <String ,ResourceElement> children = new HashMap <String ,ResourceElement> (); 207 void add (String resource) { 208 add (new StringTokenizer (resource,"/")); 209 } 210 private void add (Enumeration en) { 211 if (en.hasMoreElements()) { 213 String chldElem = (String )en.nextElement(); 214 ResourceElement child = (ResourceElement)children.get(chldElem); 215 if (child == null) 216 child = new ResourceElement(chldElem); 217 children.put (chldElem,child); 218 child.add (en); 219 } 220 } 221 ResourceElement[] getChildren () { 222 int i = 0; 223 ResourceElement[] retVal = new ResourceElement[children.entrySet().size()]; 224 Iterator it = children.entrySet().iterator(); 225 while (it.hasNext()) { 226 retVal[i++] = (ResourceElement)((Map.Entry )it.next()).getValue(); 227 } 228 229 return retVal; 230 } 231 232 String getName () { 233 return element; 234 } 235 } 236 237 static class StatusFileSystem extends LocalFileSystem { 238 Status status = new Status () { 239 public String annotateName (String name, java.util.Set files) { 240 return name; 241 } 242 243 public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files) { 244 return icon; 245 } 246 }; 247 248 public org.openide.filesystems.FileSystem.Status getStatus() { 249 return status; 250 } 251 252 } 253 } 254 | Popular Tags |