KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > TestUtilHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.filesystems;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.text.DecimalFormat JavaDoc;
28 import java.text.NumberFormat JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 /**
37  * @author rm111737
38  */

39 public class TestUtilHid {
40     private static int cnt = 0;
41     private static NumberFormat JavaDoc cntFormat = new DecimalFormat JavaDoc("000");
42     public static final File JavaDoc locationOfTempFolder (String JavaDoc name) throws IOException JavaDoc {
43         name += cntFormat.format(cnt++);
44         String JavaDoc property = System.getProperty("workdir");
45
46         File JavaDoc workdir = (property == null) ? null: new File JavaDoc (property);
47         File JavaDoc tmpdir = (workdir != null) ? workdir : new File JavaDoc(System.getProperty("java.io.tmpdir"), "fstests");
48         tmpdir.mkdirs();
49         if (!tmpdir.isDirectory()) throw new IOException JavaDoc("Could not make: " + tmpdir);
50         File JavaDoc tmp = File.createTempFile(name,null, tmpdir);
51         tmp.delete();
52         tmp = new File JavaDoc(tmp.getParent(),name);
53         return tmp;
54     }
55
56     /** @return URL to folder where should be placed tested data */
57     public final static URL JavaDoc getResourceContext () {
58         //System.out.println("getResourceContext: " + FileSystemFactoryHid.class.getResource("../data"));
59
return FileSystemFactoryHid.class.getResource("../data/");
60     }
61     
62     /** It may be helpful to delete resursively Files */
63     public final static boolean deleteFolder (File JavaDoc file) throws IOException JavaDoc{
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             // first of all delete whole content
76
File JavaDoc[] arr = file.listFiles();
77             for (int i = 0; i < arr.length; i++) {
78                 if (deleteFolder (arr[i]) != true) {
79                     throw new IOException JavaDoc ("Cannot delete: "+ arr[i]);
80                     //return false;
81
}
82             }
83         }
84         
85         return (file.delete() ? true : false);
86     }
87     
88     /**
89      * XXX this method should be package-private: non-FS-testing code should instead use
90      * {@link NbTestCase#getWorkDir} and call either
91      * {@link #createLocalFileSystem(File,String[])}
92      * or use FileUtil.toFileObject with masterfs in CP
93      */

94     public final static FileSystem createLocalFileSystem(String JavaDoc testName, String JavaDoc[] resources) throws IOException JavaDoc {
95         File JavaDoc mountPoint = locationOfLFSTempFolder(testName);
96         return createLocalFileSystem(mountPoint, resources);
97
98     }
99
100     public static FileSystem createLocalFileSystem(File JavaDoc mountPoint, String JavaDoc[] resources) throws IOException JavaDoc {
101         mountPoint.mkdir();
102         
103         for (int i = 0; i < resources.length; i++) {
104             File JavaDoc f = new File JavaDoc (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 JavaDoc iex) {
113                     throw new IOException JavaDoc ("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 JavaDoc ex) {}
122         
123         return lfs;
124     }
125
126     public static File JavaDoc locationOfLFSTempFolder(String JavaDoc testName) throws IOException JavaDoc {
127         File JavaDoc mountPoint = TestUtilHid.locationOfTempFolder("lfstest");
128         return mountPoint;
129     }
130
131     public final static void destroyLocalFileSystem (String JavaDoc testName) throws IOException JavaDoc {
132         File JavaDoc mountPoint = TestUtilHid.locationOfTempFolder("lfstest");
133         
134         if (mountPoint.exists()) {
135                 if (TestUtilHid.deleteFolder(mountPoint) == false)
136                     throw new IOException JavaDoc("Cannot delete test folder: " + mountPoint.toString());
137         }
138         
139     }
140
141     public final static void destroyXMLFileSystem(String JavaDoc testName) throws IOException JavaDoc {
142         File JavaDoc tempFile = TestUtilHid.locationOfTempFolder("xfstest");
143         File JavaDoc xmlFile = new File JavaDoc (tempFile,"xfstest.xml");
144         if (xmlFile.exists())
145             xmlFile.delete();
146     }
147     
148     
149     public final static FileSystem createXMLFileSystem(String JavaDoc testName, String JavaDoc[] resources) throws IOException JavaDoc{
150         File JavaDoc xmlFile = createXMLLayer(testName, resources);
151
152         XMLFileSystem xfs = new XMLFileSystem ();
153         try {
154             xfs.setXmlUrl(xmlFile.toURI().toURL());
155         } catch (Exception JavaDoc ex) {}
156         
157         return xfs;
158     }
159
160     public static File JavaDoc createXMLLayer(String JavaDoc testName, String JavaDoc[] resources) throws IOException JavaDoc {
161         File JavaDoc tempFile = TestUtilHid.locationOfTempFolder("xfstest");
162         tempFile.mkdir();
163         
164         File JavaDoc xmlFile = new File JavaDoc (tempFile,"xfstest.xml");
165         if (!xmlFile.exists()) {
166             xmlFile.getParentFile().mkdirs();
167             xmlFile.createNewFile();
168         }
169         FileOutputStream JavaDoc xos = new FileOutputStream JavaDoc (xmlFile);
170         ResourceElement root = new ResourceElement ("");
171         
172         for (int i = 0; i < resources.length; i++)
173             root.add (resources[i]);
174        
175         PrintWriter JavaDoc pw = new PrintWriter JavaDoc (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 JavaDoc pw,ResourceElement[] childern,String JavaDoc 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 JavaDoc element;
202         ResourceElement (String JavaDoc element) {
203             //System.out.println(element);
204
this.element = element;
205         }
206         Map JavaDoc<String JavaDoc,ResourceElement> children = new HashMap JavaDoc<String JavaDoc,ResourceElement> ();
207         void add (String JavaDoc resource) {
208             add (new StringTokenizer JavaDoc (resource,"/"));
209         }
210         private void add (Enumeration JavaDoc en) {
211             //StringTokenizer tokens = StringTokenizer (resource);
212
if (en.hasMoreElements()) {
213                 String JavaDoc chldElem = (String JavaDoc)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 JavaDoc it = children.entrySet().iterator();
225             while (it.hasNext()) {
226                 retVal[i++] = (ResourceElement)((Map.Entry JavaDoc)it.next()).getValue();
227             }
228                         
229             return retVal;
230         }
231         
232         String JavaDoc getName () {
233             return element;
234         }
235     }
236
237     static class StatusFileSystem extends LocalFileSystem {
238         Status status = new Status () {
239             public String JavaDoc annotateName (String JavaDoc name, java.util.Set JavaDoc files) {
240                 return name;
241             }
242
243             public java.awt.Image JavaDoc annotateIcon (java.awt.Image JavaDoc icon, int iconType, java.util.Set JavaDoc 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