Lines 10-22
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.util; |
11 |
package org.eclipse.jdt.core.tests.util; |
12 |
|
12 |
|
13 |
import java.io.*; |
13 |
import java.io.File; |
|
|
14 |
import java.io.FileInputStream; |
15 |
import java.io.FileNotFoundException; |
16 |
import java.io.FileOutputStream; |
17 |
import java.io.IOException; |
18 |
import java.io.PrintWriter; |
14 |
import java.net.ServerSocket; |
19 |
import java.net.ServerSocket; |
15 |
import java.util.Locale; |
20 |
import java.util.Locale; |
16 |
import java.util.Map; |
21 |
import java.util.Map; |
17 |
import java.util.zip.ZipEntry; |
22 |
import java.util.zip.ZipEntry; |
18 |
import java.util.zip.ZipOutputStream; |
23 |
import java.util.zip.ZipOutputStream; |
19 |
|
24 |
|
|
|
25 |
import org.eclipse.core.resources.IContainer; |
26 |
import org.eclipse.core.resources.IResource; |
27 |
import org.eclipse.core.runtime.CoreException; |
20 |
import org.eclipse.jdt.core.tests.compiler.regression.Requestor; |
28 |
import org.eclipse.jdt.core.tests.compiler.regression.Requestor; |
21 |
import org.eclipse.jdt.internal.compiler.Compiler; |
29 |
import org.eclipse.jdt.internal.compiler.Compiler; |
22 |
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; |
30 |
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; |
Lines 26-32
Link Here
|
26 |
import org.eclipse.jdt.internal.compiler.env.INameEnvironment; |
34 |
import org.eclipse.jdt.internal.compiler.env.INameEnvironment; |
27 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; |
35 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; |
28 |
public class Util { |
36 |
public class Util { |
29 |
public static String OUTPUT_DIRECTORY = "comptest"; |
37 |
// Trace for delete operation |
|
|
38 |
/* |
39 |
* Maximum time wasted repeating delete operations while running JDT/Core tests. |
40 |
*/ |
41 |
private static int DELETE_MAX_TIME = 0; |
42 |
/** |
43 |
* Trace deletion operations while running JDT/Core tests. |
44 |
*/ |
45 |
public static boolean DELETE_DEBUG = false; |
46 |
/** |
47 |
* Maximum of time in ms to wait in deletion operation while running JDT/Core tests. |
48 |
* Default is 10 seconds. This number cannot exceed 1 minute (ie. 60000). |
49 |
* <br> |
50 |
* To avoid too many loops while waiting, the ten first ones are done waiting |
51 |
* 10ms before repeating, the ten loops after are done waiting 100ms and |
52 |
* the other loops are done waiting 1s... |
53 |
*/ |
54 |
public static int DELETE_MAX_WAIT = 10000; |
55 |
|
56 |
// Output directory initialization |
57 |
/** |
58 |
* Initially, output directory was located in System.getProperty("user.home")+"\comptest". |
59 |
* To allow user to run several compiler tests at the same time, main output directory |
60 |
* is now located in a sub-directory of "comptest" which name is "run."+<code>System.currentMilliseconds</code>. |
61 |
* |
62 |
* @see #DELAY_BEFORE_CLEAN_PREVIOUS |
63 |
*/ |
64 |
private final static String OUTPUT_DIRECTORY; |
65 |
/** |
66 |
* Let user specify the delay in hours before output directories are removed from file system |
67 |
* while starting a new test run. Default value is 2 hours. |
68 |
* <p> |
69 |
* Note that this value may be a float and so have time less than one hour. |
70 |
* If value is 0 or negative, then all previous run directories will be removed... |
71 |
* |
72 |
* @see #OUTPUT_DIRECTORY |
73 |
*/ |
74 |
private final static String DELAY_BEFORE_CLEAN_PREVIOUS = System.getProperty("delay"); |
75 |
/* |
76 |
* Static initializer to clean directories created while running previous test suites. |
77 |
*/ |
78 |
static { |
79 |
// Get delay for cleaning sub-directories |
80 |
long millisecondsPerHour = 1000L * 3600L; |
81 |
long delay = millisecondsPerHour * 2; // default is to keep previous run directories for 2 hours |
82 |
try { |
83 |
if (DELAY_BEFORE_CLEAN_PREVIOUS != null) { |
84 |
float hours = Float.parseFloat(DELAY_BEFORE_CLEAN_PREVIOUS); |
85 |
delay = (int) (millisecondsPerHour * hours); |
86 |
} |
87 |
} |
88 |
catch (NumberFormatException nfe) { |
89 |
// use default |
90 |
} |
91 |
|
92 |
// Get output directory root from system properties |
93 |
String container = System.getProperty("jdt.test.output_directory"); |
94 |
if (container == null){ |
95 |
container = System.getProperty("user.home"); |
96 |
} |
97 |
if (container == null) { |
98 |
container = "."; // use current directory |
99 |
} |
100 |
|
101 |
// Get file for root directory |
102 |
if (Character.isLowerCase(container.charAt(0)) && container.charAt(1) == ':') { |
103 |
container = Character.toUpperCase(container.charAt(0)) + container.substring(1); |
104 |
} |
105 |
File dir = new File(new File(container), "comptest"); |
106 |
|
107 |
// If root directory already exists, clean it |
108 |
if (dir.exists()) { |
109 |
long now = System.currentTimeMillis(); |
110 |
if ((now - dir.lastModified()) > delay) { |
111 |
// remove all directory content |
112 |
flushDirectoryContent(dir); |
113 |
} else { |
114 |
// remove only old sub-dirs |
115 |
File[] testDirs = dir.listFiles(); |
116 |
for (int i=0,l=testDirs.length; i<l; i++) { |
117 |
if (testDirs[i].isDirectory()) { |
118 |
if ((now - testDirs[i].lastModified()) > delay) { |
119 |
delete(testDirs[i]); |
120 |
} |
121 |
} |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
// Computed test run directory name based on current time |
127 |
File dateDir = new File(dir, "run."+System.currentTimeMillis()); |
128 |
OUTPUT_DIRECTORY = dateDir.getPath(); |
129 |
} |
30 |
|
130 |
|
31 |
public static CompilationUnit[] compilationUnits(String[] testFiles) { |
131 |
public static CompilationUnit[] compilationUnits(String[] testFiles) { |
32 |
int length = testFiles.length / 2; |
132 |
int length = testFiles.length / 2; |
Lines 124-131
Link Here
|
124 |
try { |
224 |
try { |
125 |
in = new FileInputStream(source); |
225 |
in = new FileInputStream(source); |
126 |
File destFile = new File(dest, source.getName()); |
226 |
File destFile = new File(dest, source.getName()); |
127 |
if (destFile.exists() && !destFile.delete()) { |
227 |
if (destFile.exists()) { |
128 |
throw new IOException(destFile + " is in use"); |
228 |
if (!delete(destFile)) { |
|
|
229 |
throw new IOException(destFile + " is in use"); |
230 |
} |
129 |
} |
231 |
} |
130 |
out = new FileOutputStream(destFile); |
232 |
out = new FileOutputStream(destFile); |
131 |
int bufferLength = 1024; |
233 |
int bufferLength = 1024; |
Lines 163-168
Link Here
|
163 |
zip(classesDir, jarPath); |
265 |
zip(classesDir, jarPath); |
164 |
} |
266 |
} |
165 |
/** |
267 |
/** |
|
|
268 |
* Delete a file or directory and insure that the file is no longer present |
269 |
* on file system. In case of directory, delete all the hierarchy underneath. |
270 |
* |
271 |
* @param file The file or directory to delete |
272 |
* @return true iff the file was really delete, false otherwise |
273 |
*/ |
274 |
public static boolean delete(File file) { |
275 |
// flush all directory content |
276 |
if (file.isDirectory()) { |
277 |
flushDirectoryContent(file); |
278 |
} |
279 |
// remove file |
280 |
file.delete(); |
281 |
if (isFileDeleted(file)) { |
282 |
return true; |
283 |
} |
284 |
return waitUntilFileDeleted(file); |
285 |
} |
286 |
/** |
287 |
* Delete a file or directory and insure that the file is no longer present |
288 |
* on file system. In case of directory, delete all the hierarchy underneath. |
289 |
* |
290 |
* @param resource The resource to delete |
291 |
* @return true iff the file was really delete, false otherwise |
292 |
*/ |
293 |
public static boolean delete(IResource resource) { |
294 |
try { |
295 |
resource.delete(true, null); |
296 |
if (isResourceDeleted(resource)) { |
297 |
return true; |
298 |
} |
299 |
} |
300 |
catch (CoreException e) { |
301 |
// skip |
302 |
} |
303 |
return waitUntilResourceDeleted(resource); |
304 |
} |
305 |
/** |
306 |
* Delete a file or directory and insure that the file is no longer present |
307 |
* on file system. In case of directory, delete all the hierarchy underneath. |
308 |
* |
309 |
* @param path The path of the file or directory to delete |
310 |
* @return true iff the file was really delete, false otherwise |
311 |
*/ |
312 |
public static boolean delete(String path) { |
313 |
return delete(new File(path)); |
314 |
} |
315 |
/** |
166 |
* Generate a display string from the given String. |
316 |
* Generate a display string from the given String. |
167 |
* @param indent number of tabs are added at the begining of each line. |
317 |
* @param indent number of tabs are added at the begining of each line. |
168 |
* |
318 |
* |
Lines 362-377
Link Here
|
362 |
* no-op if not a directory. |
512 |
* no-op if not a directory. |
363 |
*/ |
513 |
*/ |
364 |
public static void flushDirectoryContent(File dir) { |
514 |
public static void flushDirectoryContent(File dir) { |
365 |
if (dir.isDirectory()) { |
515 |
File[] files = dir.listFiles(); |
366 |
String[] files = dir.list(); |
516 |
if (files == null) return; |
367 |
if (files == null) return; |
517 |
for (int i = 0, max = files.length; i < max; i++) { |
368 |
for (int i = 0, max = files.length; i < max; i++) { |
518 |
delete(files[i]); |
369 |
File current = new File(dir, files[i]); |
|
|
370 |
if (current.isDirectory()) { |
371 |
flushDirectoryContent(current); |
372 |
} |
373 |
current.delete(); |
374 |
} |
375 |
} |
519 |
} |
376 |
} |
520 |
} |
377 |
/** |
521 |
/** |
Lines 428-438
Link Here
|
428 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getOutputDirectory()] |
572 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getOutputDirectory()] |
429 |
*/ |
573 |
*/ |
430 |
public static String getOutputDirectory() { |
574 |
public static String getOutputDirectory() { |
431 |
String container = System.getProperty("user.home"); |
575 |
return OUTPUT_DIRECTORY; |
432 |
if (container == null){ |
576 |
} |
433 |
return null; |
577 |
/** |
|
|
578 |
* Returns the parent's child file matching the given file or null if not found. |
579 |
* |
580 |
* @param file The searched file in parent |
581 |
* @return The parent's child matching the given file or null if not found. |
582 |
*/ |
583 |
private static File getParentChildFile(File file) { |
584 |
File parent = file.getParentFile(); |
585 |
if (parent == null || !parent.exists()) return null; |
586 |
File[] files = parent.listFiles(); |
587 |
int length = files==null ? 0 : files.length; |
588 |
if (length > 0) { |
589 |
for (int i=0; i<length; i++) { |
590 |
if (files[i] == file) { |
591 |
return files[i]; |
592 |
} else if (files[i].equals(file)) { |
593 |
return files[i]; |
594 |
} else if (files[i].getPath().equals(file.getPath())) { |
595 |
return files[i]; |
596 |
} |
597 |
} |
598 |
} |
599 |
return null; |
600 |
} |
601 |
/** |
602 |
* Returns parent's child resource matching the given resource or null if not found. |
603 |
* |
604 |
* @param resource The searched file in parent |
605 |
* @return The parent's child matching the given file or null if not found. |
606 |
*/ |
607 |
private static IResource getParentChildResource(IResource resource) { |
608 |
IContainer parent = resource.getParent(); |
609 |
if (parent == null || !parent.exists()) return null; |
610 |
try { |
611 |
IResource[] members = parent.members(); |
612 |
int length = members ==null ? 0 : members.length; |
613 |
if (length > 0) { |
614 |
for (int i=0; i<length; i++) { |
615 |
if (members[i] == resource) { |
616 |
return members[i]; |
617 |
} else if (members[i].equals(resource)) { |
618 |
return members[i]; |
619 |
} else if (members[i].getFullPath().equals(resource.getFullPath())) { |
620 |
return members[i]; |
621 |
} |
622 |
} |
623 |
} |
624 |
} |
625 |
catch (CoreException ce) { |
626 |
// skip |
627 |
} |
628 |
return null; |
629 |
} |
630 |
/** |
631 |
* Returns the test name from stack elements info. |
632 |
* |
633 |
* @return The name of the test currently running |
634 |
*/ |
635 |
private static String getTestName() { |
636 |
StackTraceElement[] elements = new Exception().getStackTrace(); |
637 |
int idx = 0, length=elements.length; |
638 |
while (idx<length && !elements[idx++].getClassName().startsWith("org.eclipse.jdt")) { |
639 |
// loop until JDT/Core class appears in the stack |
640 |
} |
641 |
if (idx<length) { |
642 |
StackTraceElement testElement = null; |
643 |
while (idx<length && elements[idx].getClassName().startsWith("org.eclipse.jdt")) { |
644 |
testElement = elements[idx++]; |
645 |
} |
646 |
if (testElement != null) { |
647 |
return testElement.getClassName() + " - " + testElement.getMethodName(); |
648 |
} |
434 |
} |
649 |
} |
435 |
return toNativePath(container) + File.separator + OUTPUT_DIRECTORY; |
650 |
return "?"; |
436 |
} |
651 |
} |
437 |
/** |
652 |
/** |
438 |
* Returns the next available port number on the local host. |
653 |
* Returns the next available port number on the local host. |
Lines 456-461
Link Here
|
456 |
return -1; |
671 |
return -1; |
457 |
} |
672 |
} |
458 |
/** |
673 |
/** |
|
|
674 |
* Returns whether a file is really deleted or not. |
675 |
* Does not only rely on {@link File#exists()} method but also |
676 |
* look if it's not in its parent children {@link #getParentChildFile(File)}. |
677 |
* |
678 |
* @param file The file to test if deleted |
679 |
* @return true if the file does not exist and was not found in its parent children. |
680 |
*/ |
681 |
public static boolean isFileDeleted(File file) { |
682 |
return !file.exists() && getParentChildFile(file) == null; |
683 |
} |
684 |
public static boolean isMacOS() { |
685 |
return System.getProperty("os.name").indexOf("Mac") != -1; |
686 |
} |
687 |
/** |
688 |
* Returns whether a resource is really deleted or not. |
689 |
* Does not only rely on {@link IResource#isAccessible()} method but also |
690 |
* look if it's not in its parent children {@link #getParentChildResource(IResource)}. |
691 |
* |
692 |
* @param resource The resource to test if deleted |
693 |
* @return true if the resource is not accessible and was not found in its parent children. |
694 |
*/ |
695 |
public static boolean isResourceDeleted(IResource resource) { |
696 |
return !resource.isAccessible() && getParentChildResource(resource) == null; |
697 |
} |
698 |
/** |
699 |
* Print given file information with specified indentation. |
700 |
* These information are:<ul> |
701 |
* <li>read {@link File#canRead()}</li> |
702 |
* <li>write {@link File#canWrite()}</li> |
703 |
* <li>exists {@link File#exists()}</li> |
704 |
* <li>is file {@link File#isFile()}</li> |
705 |
* <li>is directory {@link File#isDirectory()}</li> |
706 |
* <li>is hidden {@link File#isHidden()}</li> |
707 |
* </ul> |
708 |
* May recurse several level in parents hierarchy. |
709 |
* May also display children, but then will not recusre in parent |
710 |
* hierarchy to avoid infinite loop... |
711 |
* |
712 |
* @param file The file to display information |
713 |
* @param indent Number of tab to print before the information |
714 |
* @param recurse Display also information on <code>recurse</code>th parents in hierarchy. |
715 |
* If negative then display children information instead. |
716 |
*/ |
717 |
private static void printFileInfo(File file, int indent, int recurse) { |
718 |
String tab = ""; |
719 |
for (int i=0; i<indent; i++) tab+="\t"; |
720 |
System.out.print(tab+"- "+file.getName()+" file info: "); |
721 |
String sep = ""; |
722 |
if (file.canRead()) { |
723 |
System.out.print("read"); |
724 |
sep = ", "; |
725 |
} |
726 |
if (file.canWrite()) { |
727 |
System.out.print(sep+"write"); |
728 |
sep = ", "; |
729 |
} |
730 |
if (file.exists()) { |
731 |
System.out.print(sep+"exist"); |
732 |
sep = ", "; |
733 |
} |
734 |
if (file.isDirectory()) { |
735 |
System.out.print(sep+"dir"); |
736 |
sep = ", "; |
737 |
} |
738 |
if (file.isFile()) { |
739 |
System.out.print(sep+"file"); |
740 |
sep = ", "; |
741 |
} |
742 |
if (file.isHidden()) { |
743 |
System.out.print(sep+"hidden"); |
744 |
sep = ", "; |
745 |
} |
746 |
System.out.println(); |
747 |
File[] files = file.listFiles(); |
748 |
int length = files==null ? 0 : files.length; |
749 |
if (length > 0) { |
750 |
boolean children = recurse < 0; |
751 |
System.out.print(tab+" + children: "); |
752 |
if (children) System.out.println(); |
753 |
for (int i=0; i<length; i++) { |
754 |
if (children) { // display children |
755 |
printFileInfo(files[i], indent+2, -1); |
756 |
} else { |
757 |
if (i>0) System.out.print(", "); |
758 |
System.out.print(files[i].getName()); |
759 |
if (files[i].isDirectory()) System.out.print("[dir]"); |
760 |
else if (files[i].isFile()) System.out.print("[file]"); |
761 |
else System.out.print("[?]"); |
762 |
} |
763 |
} |
764 |
if (!children) System.out.println(); |
765 |
} |
766 |
if (recurse > 0) { |
767 |
File parent = file.getParentFile(); |
768 |
if (parent != null) printFileInfo(parent, indent+1, recurse-1); |
769 |
} |
770 |
} |
771 |
/** |
772 |
* Print stack trace with only JDT/Core elements. |
773 |
* |
774 |
* @param exception Exception of the stack trace. May be null, then a fake exception is used. |
775 |
* @param indent Number of tab to display before the stack elements to display. |
776 |
*/ |
777 |
private static void printJdtCoreStackTrace(Exception exception, int indent) { |
778 |
String tab = ""; |
779 |
for (int i=0; i<indent; i++) tab+="\t"; |
780 |
StackTraceElement[] elements = (exception==null?new Exception():exception).getStackTrace(); |
781 |
int idx = 0, length=elements.length; |
782 |
while (idx<length && !elements[idx++].getClassName().startsWith("org.eclipse.jdt")) { |
783 |
// loop until JDT/Core class appears in the stack |
784 |
} |
785 |
if (idx<length) { |
786 |
System.out.print(tab+"- stack trace"); |
787 |
if (exception == null) |
788 |
System.out.println(":"); |
789 |
else |
790 |
System.out.println(" for exception "+exception+":"); |
791 |
while (idx<length && elements[idx].getClassName().startsWith("org.eclipse.jdt")) { |
792 |
StackTraceElement testElement = elements[idx++]; |
793 |
System.out.println(tab+" -> "+testElement); |
794 |
} |
795 |
} else { |
796 |
exception.printStackTrace(System.out); |
797 |
} |
798 |
} |
799 |
/** |
459 |
* Makes the given path a path using native path separators as returned by File.getPath() |
800 |
* Makes the given path a path using native path separators as returned by File.getPath() |
460 |
* and trimming any extra slash. |
801 |
* and trimming any extra slash. |
461 |
*/ |
802 |
*/ |
Lines 466-471
Link Here
|
466 |
nativePath.substring(0, nativePath.length() - 1) : |
807 |
nativePath.substring(0, nativePath.length() - 1) : |
467 |
nativePath; |
808 |
nativePath; |
468 |
} |
809 |
} |
|
|
810 |
/** |
811 |
* Wait until the file is _really_ deleted on file system. |
812 |
* |
813 |
* @param file Deleted file |
814 |
* @return true if the file was finally deleted, false otherwise |
815 |
*/ |
816 |
private static boolean waitUntilFileDeleted(File file) { |
817 |
if (DELETE_DEBUG) { |
818 |
System.out.println(); |
819 |
System.out.println("WARNING in test: "+getTestName()); |
820 |
System.out.println(" - problems occured while deleting "+file); |
821 |
printJdtCoreStackTrace(null, 1); |
822 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
823 |
System.out.print(" - wait for ("+DELETE_MAX_WAIT+"ms max): "); |
824 |
} |
825 |
int count = 0; |
826 |
int delay = 10; // ms |
827 |
int maxRetry = DELETE_MAX_WAIT / delay; |
828 |
int time = 0; |
829 |
while (count < maxRetry) { |
830 |
try { |
831 |
count++; |
832 |
Thread.sleep(delay); |
833 |
time += delay; |
834 |
if (time > DELETE_MAX_TIME) DELETE_MAX_TIME = time; |
835 |
if (DELETE_DEBUG) System.out.print('.'); |
836 |
if (file.exists()) { |
837 |
if (file.delete()) { |
838 |
// SUCCESS |
839 |
if (DELETE_DEBUG) { |
840 |
System.out.println(); |
841 |
System.out.println(" => file really removed after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
842 |
System.out.println(); |
843 |
} |
844 |
return true; |
845 |
} |
846 |
} |
847 |
if (isFileDeleted(file)) { |
848 |
// SUCCESS |
849 |
if (DELETE_DEBUG) { |
850 |
System.out.println(); |
851 |
System.out.println(" => file disappeared after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
852 |
System.out.println(); |
853 |
} |
854 |
return true; |
855 |
} |
856 |
// Increment waiting delay exponentially |
857 |
if (count >= 10 && delay <= 100) { |
858 |
count = 1; |
859 |
delay *= 10; |
860 |
maxRetry = DELETE_MAX_WAIT / delay; |
861 |
if ((DELETE_MAX_WAIT%delay) != 0) { |
862 |
maxRetry++; |
863 |
} |
864 |
} |
865 |
} |
866 |
catch (InterruptedException ie) { |
867 |
break; // end loop |
868 |
} |
869 |
} |
870 |
if (!DELETE_DEBUG) { |
871 |
System.out.println(); |
872 |
System.out.println("WARNING in test: "+getTestName()); |
873 |
System.out.println(" - problems occured while deleting "+file); |
874 |
printJdtCoreStackTrace(null, 1); |
875 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
876 |
} |
877 |
System.out.println(); |
878 |
System.out.println(" !!! ERROR: "+file+" was never deleted even after having waited "+DELETE_MAX_TIME+"ms!!!"); |
879 |
System.out.println(); |
880 |
return false; |
881 |
} |
882 |
/** |
883 |
* Wait until a resource is _really_ deleted on file system. |
884 |
* |
885 |
* @param resource Deleted resource |
886 |
* @return true if the file was finally deleted, false otherwise |
887 |
*/ |
888 |
private static boolean waitUntilResourceDeleted(IResource resource) { |
889 |
File file = resource.getLocation().toFile(); |
890 |
if (DELETE_DEBUG) { |
891 |
System.out.println(); |
892 |
System.out.println("WARNING in test: "+getTestName()); |
893 |
System.out.println(" - problems occured while deleting resource "+resource); |
894 |
printJdtCoreStackTrace(null, 1); |
895 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
896 |
System.out.print(" - wait for ("+DELETE_MAX_WAIT+"ms max): "); |
897 |
} |
898 |
int count = 0; |
899 |
int delay = 10; // ms |
900 |
int maxRetry = DELETE_MAX_WAIT / delay; |
901 |
int time = 0; |
902 |
while (count < maxRetry) { |
903 |
try { |
904 |
count++; |
905 |
Thread.sleep(delay); |
906 |
time += delay; |
907 |
if (time > DELETE_MAX_TIME) DELETE_MAX_TIME = time; |
908 |
if (DELETE_DEBUG) System.out.print('.'); |
909 |
if (resource.isAccessible()) { |
910 |
try { |
911 |
resource.delete(true, null); |
912 |
if (isResourceDeleted(resource) && isFileDeleted(file)) { |
913 |
// SUCCESS |
914 |
if (DELETE_DEBUG) { |
915 |
System.out.println(); |
916 |
System.out.println(" => resource really removed after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
917 |
System.out.println(); |
918 |
} |
919 |
return true; |
920 |
} |
921 |
} |
922 |
catch (CoreException e) { |
923 |
// skip |
924 |
} |
925 |
} |
926 |
if (isResourceDeleted(resource) && isFileDeleted(file)) { |
927 |
// SUCCESS |
928 |
if (DELETE_DEBUG) { |
929 |
System.out.println(); |
930 |
System.out.println(" => resource disappeared after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
931 |
System.out.println(); |
932 |
} |
933 |
return true; |
934 |
} |
935 |
// Increment waiting delay exponentially |
936 |
if (count >= 10 && delay <= 100) { |
937 |
count = 1; |
938 |
delay *= 10; |
939 |
maxRetry = DELETE_MAX_WAIT / delay; |
940 |
if ((DELETE_MAX_WAIT%delay) != 0) { |
941 |
maxRetry++; |
942 |
} |
943 |
} |
944 |
} |
945 |
catch (InterruptedException ie) { |
946 |
break; // end loop |
947 |
} |
948 |
} |
949 |
if (!DELETE_DEBUG) { |
950 |
System.out.println(); |
951 |
System.out.println("WARNING in test: "+getTestName()); |
952 |
System.out.println(" - problems occured while deleting resource "+resource); |
953 |
printJdtCoreStackTrace(null, 1); |
954 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
955 |
} |
956 |
System.out.println(); |
957 |
System.out.println(" !!! ERROR: "+resource+" was never deleted even after having waited "+DELETE_MAX_TIME+"ms!!!"); |
958 |
System.out.println(); |
959 |
return false; |
960 |
} |
469 |
public static void writeToFile(String contents, String destinationFilePath) { |
961 |
public static void writeToFile(String contents, String destinationFilePath) { |
470 |
File destFile = new File(destinationFilePath); |
962 |
File destFile = new File(destinationFilePath); |
471 |
FileOutputStream output = null; |
963 |
FileOutputStream output = null; |
Lines 489-495
Link Here
|
489 |
public static void zip(File rootDir, String zipPath) throws IOException { |
981 |
public static void zip(File rootDir, String zipPath) throws IOException { |
490 |
ZipOutputStream zip = null; |
982 |
ZipOutputStream zip = null; |
491 |
try { |
983 |
try { |
492 |
zip = new ZipOutputStream(new FileOutputStream(zipPath)); |
984 |
File zipFile = new File(zipPath); |
|
|
985 |
if (zipFile.exists()) delete(zipFile); |
986 |
zip = new ZipOutputStream(new FileOutputStream(zipFile)); |
493 |
zip(rootDir, zip, rootDir.getPath().length()+1); // 1 for last slash |
987 |
zip(rootDir, zip, rootDir.getPath().length()+1); // 1 for last slash |
494 |
} finally { |
988 |
} finally { |
495 |
if (zip != null) { |
989 |
if (zip != null) { |
Lines 498-517
Link Here
|
498 |
} |
992 |
} |
499 |
} |
993 |
} |
500 |
private static void zip(File dir, ZipOutputStream zip, int rootPathLength) throws IOException { |
994 |
private static void zip(File dir, ZipOutputStream zip, int rootPathLength) throws IOException { |
501 |
String[] list = dir.list(); |
995 |
File[] files = dir.listFiles(); |
502 |
if (list != null) { |
996 |
if (files != null) { |
503 |
for (int i = 0, length = list.length; i < length; i++) { |
997 |
for (int i = 0, length = files.length; i < length; i++) { |
504 |
String name = list[i]; |
998 |
File file = files[i]; |
505 |
File file = new File(dir, name); |
999 |
if (file.isFile()) { |
506 |
if (file.isDirectory()) { |
|
|
507 |
zip(file, zip, rootPathLength); |
508 |
} else { |
509 |
String path = file.getPath(); |
1000 |
String path = file.getPath(); |
510 |
path = path.substring(rootPathLength); |
1001 |
path = path.substring(rootPathLength); |
511 |
ZipEntry entry = new ZipEntry(path.replace('\\', '/')); |
1002 |
ZipEntry entry = new ZipEntry(path.replace('\\', '/')); |
512 |
zip.putNextEntry(entry); |
1003 |
zip.putNextEntry(entry); |
513 |
zip.write(org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file)); |
1004 |
zip.write(org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file)); |
514 |
zip.closeEntry(); |
1005 |
zip.closeEntry(); |
|
|
1006 |
} else { |
1007 |
zip(file, zip, rootPathLength); |
515 |
} |
1008 |
} |
516 |
} |
1009 |
} |
517 |
} |
1010 |
} |