KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > TestUtilities


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 package org.netbeans.modules.editor.mimelookup.impl;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
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 /**
33  *
34  * @author Martin Roskanin
35  */

36 public class TestUtilities {
37
38
39     private TestUtilities(){
40     }
41
42     /** Method will wait max. <code> maxMiliSeconds </code> miliseconds for the <code> requiredValue </code>
43      * gathered by <code> resolver </code>.
44      *
45      * @param maxMiliSeconds maximum time to wait for requiredValue
46      * @param resolver resolver, which is gathering an actual value
47      * @param requiredValue if resolver value equals requiredValue the wait cycle is finished
48      *
49      * @return false if the given maxMiliSeconds time elapsed and the requiredValue wasn't obtained
50      */

51     public static boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object JavaDoc requiredValue){
52         int time = (int) maxMiliSeconds / 100;
53         while (time > 0) {
54             Object JavaDoc 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 JavaDoc ex) {
64                 time=0;
65             }
66             time--;
67         }
68         return false;
69     }
70     
71     /** Interface for value resolver needed for i.e. waitMaxMilisForValue method.
72      * For more details, please look at {@link #waitMaxMilisForValue()}.
73      */

74     public static interface ValueResolver{
75         /** Returns checked value */
76         Object JavaDoc getValue();
77     }
78     
79     private static void deleteFileImpl(File JavaDoc workDir, String JavaDoc path) throws IOException JavaDoc{
80         FileObject fo = FileUtil.toFileObject(new File JavaDoc(workDir, path));
81         if (fo == null) {
82             fo = Repository.getDefault().getDefaultFileSystem().findResource(path); // NOI18N
83
if (fo == null){
84                 return;
85             }
86         }
87         fo.delete();
88     }
89     
90     public static void deleteFile(final File JavaDoc workDir, final String JavaDoc path) {
91         // delete a file from a different thread
92
RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable JavaDoc(){
93             public void run(){
94                 try {
95                     deleteFileImpl(workDir, path);
96                 } catch (IOException JavaDoc ioe){
97                     ioe.printStackTrace();
98                 }
99             }
100         });
101         
102         try {
103             task.waitFinished(1000);
104         } catch (InterruptedException JavaDoc e) {
105             // ignore
106
}
107     }
108
109     private static void createFileImpl(File JavaDoc workDir, String JavaDoc path) throws IOException JavaDoc{
110         FileObject fo = FileUtil.toFileObject(workDir);
111         if (fo == null) {
112             throw new IOException JavaDoc("Can't map '" + workDir.getAbsolutePath() + "' to the filesystem repository.");
113         }
114
115         String JavaDoc [] pathElements = path.split("/", -1);
116         for (int i = 0; i < pathElements.length; i++ ) {
117             String JavaDoc 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                     // The last element in the path
131
fo = fo.createData(elementName);
132                 }
133             }
134             
135             fo.refresh();
136         }
137     }
138     
139     public static void createFile(final File JavaDoc workDir, final String JavaDoc path) {
140         // create a file from a different thread
141
RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable JavaDoc(){
142             public void run(){
143                 try {
144                     createFileImpl(workDir, path);
145                 } catch (IOException JavaDoc ioe){
146                     ioe.printStackTrace();
147                 }
148             }
149         });
150         
151         try {
152             task.waitFinished(1000);
153         } catch (InterruptedException JavaDoc e) {
154             // ignore
155
}
156     }
157
158     public static void sleepForWhile() {
159         try {
160             Thread.sleep(321);
161         } catch (InterruptedException JavaDoc ex) {
162             // ignore
163
}
164     }
165
166     public static void consumeAllMemory() {
167         ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc("Can't run out of memory! The VM's heap size is too big.");
176         } catch (OutOfMemoryError JavaDoc e) {
177             // ok the VM's just run out of memory
178
// release everything we've allocated
179
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 JavaDoc ex) {
190                 // ignore
191
}
192         }
193     }
194 }
195
Popular Tags