KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > Utilities


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 /*
21  * Utilities.java
22  *
23  * Created on September 30, 2002, 4:34 PM
24  */

25
26 package org.netbeans.test.java;
27
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FilenameFilter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.Vector JavaDoc;
37 import javax.swing.text.StyledDocument JavaDoc;
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 /**
48  *
49  * @author jb105785
50  */

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 JavaDoc file) {
58         try {
59             DataObject.find(FileUtil.toFileObject(file)).delete();
60         } catch (IOException JavaDoc e) {
61         }
62     }
63     
64     public static File JavaDoc copyTo(File JavaDoc file, File JavaDoc 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 JavaDoc(destination, file.getName());
70         } catch (IOException JavaDoc e) {
71             throw new AssertionFailedErrorException(e);
72         }
73     }
74     
75     public static String JavaDoc getAsString(File JavaDoc file) {
76         String JavaDoc 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 JavaDoc doc=ec.openDocument();
83             result=doc.getText(0, doc.getLength());
84         } catch (Exception JavaDoc e){
85             throw new AssertionFailedErrorException(e);
86         }
87         return result;
88     }
89     
90     private static String JavaDoc[] allFileNames(final File JavaDoc f, final Vector JavaDoc v, boolean comp, final boolean recurse, final boolean fullName, final FilenameFilter JavaDoc filter) {
91         String JavaDoc[] files;
92         if(filter != null) {
93             files = f.list(filter);
94         }
95         else {
96             files = f.list();
97         }
98         
99         String JavaDoc 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 JavaDoc 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 JavaDoc[] dirs = f.list(new FilenameFilter JavaDoc() {
116                 public boolean accept(File JavaDoc f, String JavaDoc name){
117                     return (new File JavaDoc(f.getPath() + File.separatorChar + name).isDirectory());
118                 }
119             });
120             for(int i = 0; i < dirs.length; i++) {
121                 File JavaDoc newF = new File JavaDoc(path + dirs[i]);
122                 allFileNames(newF, v, false, true, fullName, filter);
123             }
124         }
125         else {
126             comp = true;
127         }
128         if(comp) {
129             String JavaDoc[] strs = new String JavaDoc[v.size()];
130             v.copyInto(strs);
131             return strs;
132         }
133         return null;
134     }
135     
136     public static String JavaDoc[] getAllFilenames(File JavaDoc initialDirectory, boolean recurse, final String JavaDoc filter) {
137         FilenameFilter JavaDoc f = new FilenameFilter JavaDoc() {
138             public boolean accept(File JavaDoc f, String JavaDoc name) {
139                 return (name.indexOf(filter) > 0);
140             }};
141             
142             if(!initialDirectory.isDirectory()) {
143                 return new String JavaDoc[0];
144             }
145             return allFileNames(initialDirectory, new Vector JavaDoc(), true, recurse, true, f);
146     }
147     
148     /**
149      * Deletes a directory recursively
150      * @param path path to directory for deletion
151      * @return was the direcotory deleted?
152      */

153     public static boolean deleteDirectory(File JavaDoc path) {
154         if (path.exists()) {
155             File JavaDoc[] 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      /**
168      * Sleeps for waitTimeout miliseconds to avoid incorrect test failures.
169      */

170     public static void takeANap(int waitTimeout) {
171         new org.netbeans.jemmy.EventTool().waitNoEvent(waitTimeout);
172     }
173         
174 }
175
Popular Tags