Lines 30-391
Link Here
|
30 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem; |
30 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem; |
31 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; |
31 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; |
32 |
public class Util { |
32 |
public class Util { |
33 |
// Trace for delete operation |
33 |
// Trace for delete operation |
34 |
/* |
34 |
/* |
35 |
* Maximum time wasted repeating delete operations while running JDT/Core tests. |
35 |
* Maximum time wasted repeating delete operations while running JDT/Core tests. |
36 |
*/ |
36 |
*/ |
37 |
private static int DELETE_MAX_TIME = 0; |
37 |
private static int DELETE_MAX_TIME = 0; |
38 |
/** |
38 |
/** |
39 |
* Trace deletion operations while running JDT/Core tests. |
39 |
* Trace deletion operations while running JDT/Core tests. |
40 |
*/ |
40 |
*/ |
41 |
public static boolean DELETE_DEBUG = false; |
41 |
public static boolean DELETE_DEBUG = false; |
42 |
/** |
42 |
/** |
43 |
* Maximum of time in ms to wait in deletion operation while running JDT/Core tests. |
43 |
* Maximum of time in ms to wait in deletion operation while running JDT/Core tests. |
44 |
* Default is 10 seconds. This number cannot exceed 1 minute (ie. 60000). |
44 |
* Default is 10 seconds. This number cannot exceed 1 minute (ie. 60000). |
45 |
* <br> |
45 |
* <br> |
46 |
* To avoid too many loops while waiting, the ten first ones are done waiting |
46 |
* To avoid too many loops while waiting, the ten first ones are done waiting |
47 |
* 10ms before repeating, the ten loops after are done waiting 100ms and |
47 |
* 10ms before repeating, the ten loops after are done waiting 100ms and |
48 |
* the other loops are done waiting 1s... |
48 |
* the other loops are done waiting 1s... |
49 |
*/ |
49 |
*/ |
50 |
public static int DELETE_MAX_WAIT = 10000; |
50 |
public static int DELETE_MAX_WAIT = 10000; |
51 |
|
51 |
|
52 |
private static final boolean DEBUG = false; |
52 |
private static final boolean DEBUG = false; |
53 |
/** |
53 |
/** |
54 |
* Initially, output directory was located in System.getProperty("user.home")+"\comptest". |
54 |
* Initially, output directory was located in System.getProperty("user.home")+"\comptest". |
55 |
* To allow user to run several compiler tests at the same time, main output directory |
55 |
* To allow user to run several compiler tests at the same time, main output directory |
56 |
* is now located in a sub-directory of "comptest" which name is "run."+<code>System.currentMilliseconds</code>. |
56 |
* is now located in a sub-directory of "comptest" which name is "run."+<code>System.currentMilliseconds</code>. |
57 |
* |
57 |
* |
58 |
* @see #DELAY_BEFORE_CLEAN_PREVIOUS |
58 |
* @see #DELAY_BEFORE_CLEAN_PREVIOUS |
59 |
*/ |
59 |
*/ |
60 |
private final static String OUTPUT_DIRECTORY; |
60 |
private final static String OUTPUT_DIRECTORY; |
61 |
/** |
61 |
/** |
62 |
* Let user specify the delay in hours before output directories are removed from file system |
62 |
* Let user specify the delay in hours before output directories are removed from file system |
63 |
* while starting a new test run. Default value is 2 hours. |
63 |
* while starting a new test run. Default value is 2 hours. |
64 |
* <p> |
64 |
* <p> |
65 |
* Note that this value may be a float and so have time less than one hour. |
65 |
* Note that this value may be a float and so have time less than one hour. |
66 |
* If value is 0 or negative, then all previous run directories will be removed... |
66 |
* If value is 0 or negative, then all previous run directories will be removed... |
67 |
* |
67 |
* |
68 |
* @see #OUTPUT_DIRECTORY |
68 |
* @see #OUTPUT_DIRECTORY |
69 |
*/ |
69 |
*/ |
70 |
private final static String DELAY_BEFORE_CLEAN_PREVIOUS = System.getProperty("delay"); |
70 |
private final static String DELAY_BEFORE_CLEAN_PREVIOUS = System.getProperty("delay"); |
71 |
/* |
71 |
/* |
72 |
* Static initializer to clean directories created while running previous test suites. |
72 |
* Static initializer to clean directories created while running previous test suites. |
73 |
*/ |
73 |
*/ |
74 |
static { |
74 |
static { |
75 |
// Get delay for cleaning sub-directories |
75 |
// Get delay for cleaning sub-directories |
76 |
long millisecondsPerHour = 1000L * 3600L; |
76 |
long millisecondsPerHour = 1000L * 3600L; |
77 |
long delay = millisecondsPerHour * 2; // default is to keep previous run directories for 2 hours |
77 |
long delay = millisecondsPerHour * 2; // default is to keep previous run directories for 2 hours |
78 |
try { |
78 |
try { |
79 |
if (DELAY_BEFORE_CLEAN_PREVIOUS != null) { |
79 |
if (DELAY_BEFORE_CLEAN_PREVIOUS != null) { |
80 |
float hours = Float.parseFloat(DELAY_BEFORE_CLEAN_PREVIOUS); |
80 |
float hours = Float.parseFloat(DELAY_BEFORE_CLEAN_PREVIOUS); |
81 |
delay = (int) (millisecondsPerHour * hours); |
81 |
delay = (int) (millisecondsPerHour * hours); |
82 |
} |
82 |
} |
83 |
} |
83 |
} |
84 |
catch (NumberFormatException nfe) { |
84 |
catch (NumberFormatException nfe) { |
85 |
// use default |
85 |
// use default |
86 |
} |
86 |
} |
87 |
|
87 |
|
88 |
// Get output directory root from system properties |
88 |
// Get output directory root from system properties |
89 |
String container = System.getProperty("jdt.test.output_directory"); |
89 |
String container = System.getProperty("jdt.test.output_directory"); |
90 |
if (container == null){ |
90 |
if (container == null){ |
91 |
container = System.getProperty("user.home"); |
91 |
container = System.getProperty("user.home"); |
92 |
} |
92 |
} |
93 |
if (container == null) { |
93 |
if (container == null) { |
94 |
container = "."; // use current directory |
94 |
container = "."; // use current directory |
95 |
} |
95 |
} |
96 |
|
|
|
97 |
// Get file for root directory |
98 |
if (Character.isLowerCase(container.charAt(0)) && container.charAt(1) == ':') { |
99 |
container = Character.toUpperCase(container.charAt(0)) + container.substring(1); |
100 |
} |
101 |
File dir = new File(new File(container), "comptest"); |
102 |
|
96 |
|
103 |
// If root directory already exists, clean it |
97 |
// Get file for root directory |
104 |
if (dir.exists()) { |
98 |
if (Character.isLowerCase(container.charAt(0)) && container.charAt(1) == ':') { |
105 |
long now = System.currentTimeMillis(); |
99 |
container = Character.toUpperCase(container.charAt(0)) + container.substring(1); |
106 |
if ((now - dir.lastModified()) > delay) { |
100 |
} |
107 |
// remove all directory content |
101 |
File dir = new File(new File(container), "comptest"); |
108 |
flushDirectoryContent(dir); |
|
|
109 |
} else { |
110 |
// remove only old sub-dirs |
111 |
File[] testDirs = dir.listFiles(); |
112 |
for (int i=0,l=testDirs.length; i<l; i++) { |
113 |
if (testDirs[i].isDirectory()) { |
114 |
if ((now - testDirs[i].lastModified()) > delay) { |
115 |
delete(testDirs[i]); |
116 |
} |
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
|
102 |
|
122 |
// Computed test run directory name based on current time |
103 |
// If root directory already exists, clean it |
123 |
File dateDir = new File(dir, "run."+System.currentTimeMillis()); |
104 |
if (dir.exists()) { |
124 |
OUTPUT_DIRECTORY = dateDir.getPath(); |
105 |
long now = System.currentTimeMillis(); |
125 |
} |
106 |
if ((now - dir.lastModified()) > delay) { |
|
|
107 |
// remove all directory content |
108 |
flushDirectoryContent(dir); |
109 |
} else { |
110 |
// remove only old sub-dirs |
111 |
File[] testDirs = dir.listFiles(); |
112 |
for (int i=0,l=testDirs.length; i<l; i++) { |
113 |
if (testDirs[i].isDirectory()) { |
114 |
if ((now - testDirs[i].lastModified()) > delay) { |
115 |
delete(testDirs[i]); |
116 |
} |
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
// Computed test run directory name based on current time |
123 |
File dateDir = new File(dir, "run."+System.currentTimeMillis()); |
124 |
OUTPUT_DIRECTORY = dateDir.getPath(); |
125 |
} |
126 |
|
126 |
|
127 |
public static void appendProblem(StringBuffer problems, IProblem problem, char[] source, int problemCount) { |
127 |
public static void appendProblem(StringBuffer problems, IProblem problem, char[] source, int problemCount) { |
128 |
problems.append(problemCount + (problem.isError() ? ". ERROR" : ". WARNING")); |
128 |
problems.append(problemCount + (problem.isError() ? ". ERROR" : ". WARNING")); |
129 |
problems.append(" in " + new String(problem.getOriginatingFileName())); |
129 |
problems.append(" in " + new String(problem.getOriginatingFileName())); |
130 |
if (source != null) { |
130 |
if (source != null) { |
131 |
problems.append(((DefaultProblem)problem).errorReportSource(source)); |
131 |
problems.append(((DefaultProblem)problem).errorReportSource(source)); |
132 |
} |
132 |
} |
133 |
problems.append("\n"); |
133 |
problems.append("\n"); |
134 |
problems.append(problem.getMessage()); |
134 |
problems.append(problem.getMessage()); |
135 |
problems.append("\n"); |
135 |
problems.append("\n"); |
136 |
} |
136 |
} |
137 |
|
137 |
|
138 |
public static CompilationUnit[] compilationUnits(String[] testFiles) { |
138 |
public static CompilationUnit[] compilationUnits(String[] testFiles) { |
139 |
int length = testFiles.length / 2; |
139 |
int length = testFiles.length / 2; |
140 |
CompilationUnit[] result = new CompilationUnit[length]; |
140 |
CompilationUnit[] result = new CompilationUnit[length]; |
141 |
int index = 0; |
141 |
int index = 0; |
142 |
for (int i = 0; i < length; i++) { |
142 |
for (int i = 0; i < length; i++) { |
143 |
result[i] = new CompilationUnit(testFiles[index + 1].toCharArray(), testFiles[index], null); |
143 |
result[i] = new CompilationUnit(testFiles[index + 1].toCharArray(), testFiles[index], null); |
144 |
index += 2; |
144 |
index += 2; |
145 |
} |
145 |
} |
146 |
return result; |
146 |
return result; |
147 |
} |
147 |
} |
148 |
public static void compile(String[] pathsAndContents, Map options, String outputPath) { |
148 |
public static void compile(String[] pathsAndContents, Map options, String outputPath) { |
149 |
IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); |
149 |
IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); |
150 |
Requestor requestor = |
150 |
Requestor requestor = |
151 |
new Requestor( |
151 |
new Requestor( |
152 |
problemFactory, |
152 |
problemFactory, |
153 |
outputPath.endsWith(File.separator) ? outputPath : outputPath + File.separator, |
153 |
outputPath.endsWith(File.separator) ? outputPath : outputPath + File.separator, |
154 |
false, |
154 |
false, |
155 |
null/*no custom requestor*/, |
155 |
null/*no custom requestor*/, |
156 |
false, /* show category */ |
156 |
false, /* show category */ |
157 |
false /* show warning token*/); |
157 |
false /* show warning token*/); |
158 |
|
158 |
|
159 |
INameEnvironment nameEnvironment = new FileSystem(getJavaClassLibs(), new String[] {}, null); |
159 |
INameEnvironment nameEnvironment = new FileSystem(getJavaClassLibs(), new String[] {}, null); |
160 |
IErrorHandlingPolicy errorHandlingPolicy = |
160 |
IErrorHandlingPolicy errorHandlingPolicy = |
161 |
new IErrorHandlingPolicy() { |
161 |
new IErrorHandlingPolicy() { |
162 |
public boolean proceedOnErrors() { |
162 |
public boolean proceedOnErrors() { |
163 |
return true; |
163 |
return true; |
164 |
} |
164 |
} |
165 |
public boolean stopOnFirstError() { |
165 |
public boolean stopOnFirstError() { |
166 |
return false; |
166 |
return false; |
167 |
} |
167 |
} |
168 |
}; |
168 |
}; |
169 |
CompilerOptions compilerOptions = new CompilerOptions(options); |
169 |
CompilerOptions compilerOptions = new CompilerOptions(options); |
170 |
compilerOptions.performMethodsFullRecovery = false; |
170 |
compilerOptions.performMethodsFullRecovery = false; |
171 |
compilerOptions.performStatementsRecovery = false; |
171 |
compilerOptions.performStatementsRecovery = false; |
172 |
Compiler batchCompiler = |
172 |
Compiler batchCompiler = |
173 |
new Compiler( |
173 |
new Compiler( |
174 |
nameEnvironment, |
174 |
nameEnvironment, |
175 |
errorHandlingPolicy, |
175 |
errorHandlingPolicy, |
176 |
compilerOptions, |
176 |
compilerOptions, |
177 |
requestor, |
177 |
requestor, |
178 |
problemFactory); |
178 |
problemFactory); |
179 |
batchCompiler.options.produceReferenceInfo = true; |
179 |
batchCompiler.options.produceReferenceInfo = true; |
180 |
batchCompiler.compile(compilationUnits(pathsAndContents)); // compile all files together |
180 |
batchCompiler.compile(compilationUnits(pathsAndContents)); // compile all files together |
181 |
System.err.print(requestor.problemLog); // problem log empty if no problems |
181 |
System.err.print(requestor.problemLog); // problem log empty if no problems |
182 |
} |
182 |
} |
183 |
public static String[] concatWithClassLibs(String[] classpaths, boolean inFront) { |
183 |
public static String[] concatWithClassLibs(String[] classpaths, boolean inFront) { |
184 |
String[] classLibs = getJavaClassLibs(); |
184 |
String[] classLibs = getJavaClassLibs(); |
185 |
if (classpaths == null) return classLibs; |
185 |
if (classpaths == null) return classLibs; |
186 |
final int classLibsLength = classLibs.length; |
186 |
final int classLibsLength = classLibs.length; |
187 |
final int classpathsLength = classpaths.length; |
187 |
final int classpathsLength = classpaths.length; |
188 |
String[] defaultClassPaths = new String[classLibsLength + classpathsLength]; |
188 |
String[] defaultClassPaths = new String[classLibsLength + classpathsLength]; |
189 |
if (inFront) { |
189 |
if (inFront) { |
190 |
System.arraycopy(classLibs, 0, defaultClassPaths, classpathsLength, classLibsLength); |
190 |
System.arraycopy(classLibs, 0, defaultClassPaths, classpathsLength, classLibsLength); |
191 |
System.arraycopy(classpaths, 0, defaultClassPaths, 0, classpathsLength); |
191 |
System.arraycopy(classpaths, 0, defaultClassPaths, 0, classpathsLength); |
192 |
} else { |
192 |
} else { |
193 |
System.arraycopy(classLibs, 0, defaultClassPaths, 0, classLibsLength); |
193 |
System.arraycopy(classLibs, 0, defaultClassPaths, 0, classLibsLength); |
194 |
System.arraycopy(classpaths, 0, defaultClassPaths, classLibsLength, classpathsLength); |
194 |
System.arraycopy(classpaths, 0, defaultClassPaths, classLibsLength, classpathsLength); |
195 |
} |
195 |
} |
196 |
for (int i = 0; i < classpathsLength; i++) { |
196 |
for (int i = 0; i < classpathsLength; i++) { |
197 |
File file = new File(classpaths[i]); |
197 |
File file = new File(classpaths[i]); |
198 |
if (!file.exists()) { |
198 |
if (!file.exists()) { |
199 |
file.mkdirs(); |
199 |
file.mkdirs(); |
200 |
} |
200 |
} |
201 |
} |
201 |
} |
202 |
return defaultClassPaths; |
202 |
return defaultClassPaths; |
203 |
} |
203 |
} |
204 |
public static String[] concatWithClassLibs(String classpath, boolean inFront) { |
204 |
public static String[] concatWithClassLibs(String classpath, boolean inFront) { |
205 |
String[] classLibs = getJavaClassLibs(); |
205 |
String[] classLibs = getJavaClassLibs(); |
206 |
final int length = classLibs.length; |
206 |
final int length = classLibs.length; |
207 |
File dir = new File(classpath); |
207 |
File dir = new File(classpath); |
208 |
if (!dir.exists()) |
208 |
if (!dir.exists()) |
209 |
dir.mkdirs(); |
209 |
dir.mkdirs(); |
210 |
String[] defaultClassPaths = new String[length + 1]; |
210 |
String[] defaultClassPaths = new String[length + 1]; |
211 |
if (inFront) { |
211 |
if (inFront) { |
212 |
System.arraycopy(classLibs, 0, defaultClassPaths, 1, length); |
212 |
System.arraycopy(classLibs, 0, defaultClassPaths, 1, length); |
213 |
defaultClassPaths[0] = classpath; |
213 |
defaultClassPaths[0] = classpath; |
214 |
} else { |
214 |
} else { |
215 |
System.arraycopy(classLibs, 0, defaultClassPaths, 0, length); |
215 |
System.arraycopy(classLibs, 0, defaultClassPaths, 0, length); |
216 |
defaultClassPaths[length] = classpath; |
216 |
defaultClassPaths[length] = classpath; |
217 |
} |
217 |
} |
218 |
return defaultClassPaths; |
218 |
return defaultClassPaths; |
219 |
} |
219 |
} |
220 |
public static String convertToIndependantLineDelimiter(String source) { |
220 |
public static String convertToIndependantLineDelimiter(String source) { |
221 |
if (source.indexOf('\n') == -1 && source.indexOf('\r') == -1) return source; |
221 |
if (source.indexOf('\n') == -1 && source.indexOf('\r') == -1) return source; |
222 |
StringBuffer buffer = new StringBuffer(); |
222 |
StringBuffer buffer = new StringBuffer(); |
223 |
for (int i = 0, length = source.length(); i < length; i++) { |
223 |
for (int i = 0, length = source.length(); i < length; i++) { |
224 |
char car = source.charAt(i); |
224 |
char car = source.charAt(i); |
225 |
if (car == '\r') { |
225 |
if (car == '\r') { |
226 |
buffer.append('\n'); |
226 |
buffer.append('\n'); |
227 |
if (i < length-1 && source.charAt(i+1) == '\n') { |
227 |
if (i < length-1 && source.charAt(i+1) == '\n') { |
228 |
i++; // skip \n after \r |
228 |
i++; // skip \n after \r |
229 |
} |
229 |
} |
230 |
} else { |
230 |
} else { |
231 |
buffer.append(car); |
231 |
buffer.append(car); |
232 |
} |
232 |
} |
233 |
} |
233 |
} |
234 |
return buffer.toString(); |
234 |
return buffer.toString(); |
235 |
} |
235 |
} |
236 |
/** |
236 |
/** |
237 |
* Copy the given source (a file or a directory that must exists) to the given destination (a directory that must exists). |
237 |
* Copy the given source (a file or a directory that must exists) to the given destination (a directory that must exists). |
238 |
*/ |
238 |
*/ |
239 |
public static void copy(String sourcePath, String destPath) { |
239 |
public static void copy(String sourcePath, String destPath) { |
240 |
sourcePath = toNativePath(sourcePath); |
240 |
sourcePath = toNativePath(sourcePath); |
241 |
destPath = toNativePath(destPath); |
241 |
destPath = toNativePath(destPath); |
242 |
File source = new File(sourcePath); |
242 |
File source = new File(sourcePath); |
243 |
if (!source.exists()) return; |
243 |
if (!source.exists()) return; |
244 |
File dest = new File(destPath); |
244 |
File dest = new File(destPath); |
245 |
if (!dest.exists()) return; |
245 |
if (!dest.exists()) return; |
246 |
if (source.isDirectory()) { |
246 |
if (source.isDirectory()) { |
247 |
String[] files = source.list(); |
247 |
String[] files = source.list(); |
248 |
if (files != null) { |
248 |
if (files != null) { |
249 |
for (int i = 0; i < files.length; i++) { |
249 |
for (int i = 0; i < files.length; i++) { |
250 |
String file = files[i]; |
250 |
String file = files[i]; |
251 |
File sourceFile = new File(source, file); |
251 |
File sourceFile = new File(source, file); |
252 |
if (sourceFile.isDirectory()) { |
252 |
if (sourceFile.isDirectory()) { |
253 |
File destSubDir = new File(dest, file); |
253 |
File destSubDir = new File(dest, file); |
254 |
destSubDir.mkdir(); |
254 |
destSubDir.mkdir(); |
255 |
copy(sourceFile.getPath(), destSubDir.getPath()); |
255 |
copy(sourceFile.getPath(), destSubDir.getPath()); |
256 |
} else { |
256 |
} else { |
257 |
copy(sourceFile.getPath(), dest.getPath()); |
257 |
copy(sourceFile.getPath(), dest.getPath()); |
258 |
} |
258 |
} |
259 |
} |
259 |
} |
260 |
} |
260 |
} |
261 |
} else { |
261 |
} else { |
262 |
FileInputStream in = null; |
262 |
FileInputStream in = null; |
263 |
FileOutputStream out = null; |
263 |
FileOutputStream out = null; |
264 |
try { |
264 |
try { |
265 |
in = new FileInputStream(source); |
265 |
in = new FileInputStream(source); |
266 |
File destFile = new File(dest, source.getName()); |
266 |
File destFile = new File(dest, source.getName()); |
267 |
if (destFile.exists()) { |
267 |
if (destFile.exists()) { |
268 |
if (!Util.delete(destFile)) { |
268 |
if (!Util.delete(destFile)) { |
269 |
throw new IOException(destFile + " is in use"); |
269 |
throw new IOException(destFile + " is in use"); |
270 |
} |
270 |
} |
271 |
} |
271 |
} |
272 |
out = new FileOutputStream(destFile); |
272 |
out = new FileOutputStream(destFile); |
273 |
int bufferLength = 1024; |
273 |
int bufferLength = 1024; |
274 |
byte[] buffer = new byte[bufferLength]; |
274 |
byte[] buffer = new byte[bufferLength]; |
275 |
int read = 0; |
275 |
int read = 0; |
276 |
while (read != -1) { |
276 |
while (read != -1) { |
277 |
read = in.read(buffer, 0, bufferLength); |
277 |
read = in.read(buffer, 0, bufferLength); |
278 |
if (read != -1) { |
278 |
if (read != -1) { |
279 |
out.write(buffer, 0, read); |
279 |
out.write(buffer, 0, read); |
280 |
} |
280 |
} |
281 |
} |
281 |
} |
282 |
} catch (IOException e) { |
282 |
} catch (IOException e) { |
283 |
throw new Error(e.toString()); |
283 |
throw new Error(e.toString()); |
284 |
} finally { |
284 |
} finally { |
285 |
if (in != null) { |
285 |
if (in != null) { |
286 |
try { |
286 |
try { |
287 |
in.close(); |
287 |
in.close(); |
288 |
} catch (IOException e) { |
288 |
} catch (IOException e) { |
289 |
} |
289 |
} |
290 |
} |
290 |
} |
291 |
if (out != null) { |
291 |
if (out != null) { |
292 |
try { |
292 |
try { |
293 |
out.close(); |
293 |
out.close(); |
294 |
} catch (IOException e) { |
294 |
} catch (IOException e) { |
295 |
} |
295 |
} |
296 |
} |
296 |
} |
297 |
} |
297 |
} |
298 |
} |
298 |
} |
299 |
} |
299 |
} |
300 |
public static void createFile(String path, String contents) throws IOException { |
300 |
public static void createFile(String path, String contents) throws IOException { |
301 |
FileOutputStream output = new FileOutputStream(path); |
301 |
FileOutputStream output = new FileOutputStream(path); |
302 |
try { |
302 |
try { |
303 |
output.write(contents.getBytes()); |
303 |
output.write(contents.getBytes()); |
304 |
} finally { |
304 |
} finally { |
305 |
output.close(); |
305 |
output.close(); |
306 |
} |
306 |
} |
307 |
} |
307 |
} |
308 |
public static void createJar(String[] pathsAndContents, Map options, String jarPath) throws IOException { |
308 |
public static void createJar(String[] pathsAndContents, Map options, String jarPath) throws IOException { |
309 |
String classesPath = getOutputDirectory() + File.separator + "classes"; |
309 |
String classesPath = getOutputDirectory() + File.separator + "classes"; |
310 |
File classesDir = new File(classesPath); |
310 |
File classesDir = new File(classesPath); |
311 |
flushDirectoryContent(classesDir); |
311 |
flushDirectoryContent(classesDir); |
312 |
compile(pathsAndContents, options, classesPath); |
312 |
compile(pathsAndContents, options, classesPath); |
313 |
zip(classesDir, jarPath); |
313 |
zip(classesDir, jarPath); |
314 |
} |
314 |
} |
315 |
public static void createJar(String[] pathsAndContents, String jarPath, String compliance) throws IOException { |
315 |
public static void createJar(String[] pathsAndContents, String jarPath, String compliance) throws IOException { |
316 |
Map options = new HashMap(); |
316 |
Map options = new HashMap(); |
317 |
options.put(CompilerOptions.OPTION_Compliance, compliance); |
317 |
options.put(CompilerOptions.OPTION_Compliance, compliance); |
318 |
options.put(CompilerOptions.OPTION_Source, compliance); |
318 |
options.put(CompilerOptions.OPTION_Source, compliance); |
319 |
options.put(CompilerOptions.OPTION_TargetPlatform, compliance); |
319 |
options.put(CompilerOptions.OPTION_TargetPlatform, compliance); |
320 |
// Ignore options with new defaults (since bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=76530) |
320 |
// Ignore options with new defaults (since bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=76530) |
321 |
options.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE); |
321 |
options.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE); |
322 |
options.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE); |
322 |
options.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE); |
323 |
options.put(CompilerOptions.OPTION_ReportFieldHiding, CompilerOptions.IGNORE); |
323 |
options.put(CompilerOptions.OPTION_ReportFieldHiding, CompilerOptions.IGNORE); |
324 |
options.put(CompilerOptions.OPTION_ReportLocalVariableHiding, CompilerOptions.IGNORE); |
324 |
options.put(CompilerOptions.OPTION_ReportLocalVariableHiding, CompilerOptions.IGNORE); |
325 |
options.put(CompilerOptions.OPTION_ReportTypeParameterHiding, CompilerOptions.IGNORE); |
325 |
options.put(CompilerOptions.OPTION_ReportTypeParameterHiding, CompilerOptions.IGNORE); |
326 |
options.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE); |
326 |
options.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE); |
327 |
options.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE); |
327 |
options.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE); |
328 |
createJar(pathsAndContents, options, jarPath); |
328 |
createJar(pathsAndContents, options, jarPath); |
329 |
} |
329 |
} |
330 |
public static void createSourceZip(String[] pathsAndContents, String zipPath) throws IOException { |
330 |
public static void createSourceZip(String[] pathsAndContents, String zipPath) throws IOException { |
331 |
String sourcesPath = getOutputDirectory() + File.separator + "sources"; |
331 |
String sourcesPath = getOutputDirectory() + File.separator + "sources"; |
332 |
File sourcesDir = new File(sourcesPath); |
332 |
File sourcesDir = new File(sourcesPath); |
333 |
flushDirectoryContent(sourcesDir); |
333 |
flushDirectoryContent(sourcesDir); |
334 |
for (int i = 0, length = pathsAndContents.length; i < length; i+=2) { |
334 |
for (int i = 0, length = pathsAndContents.length; i < length; i+=2) { |
335 |
String sourcePath = sourcesPath + File.separator + pathsAndContents[i]; |
335 |
String sourcePath = sourcesPath + File.separator + pathsAndContents[i]; |
336 |
File sourceFile = new File(sourcePath); |
336 |
File sourceFile = new File(sourcePath); |
337 |
sourceFile.getParentFile().mkdirs(); |
337 |
sourceFile.getParentFile().mkdirs(); |
338 |
createFile(sourcePath, pathsAndContents[i+1]); |
338 |
createFile(sourcePath, pathsAndContents[i+1]); |
339 |
} |
339 |
} |
340 |
zip(sourcesDir, zipPath); |
340 |
zip(sourcesDir, zipPath); |
341 |
} |
341 |
} |
342 |
/** |
342 |
/** |
343 |
* Delete a file or directory and insure that the file is no longer present |
343 |
* Delete a file or directory and insure that the file is no longer present |
344 |
* on file system. In case of directory, delete all the hierarchy underneath. |
344 |
* on file system. In case of directory, delete all the hierarchy underneath. |
345 |
* |
345 |
* |
346 |
* @param file The file or directory to delete |
346 |
* @param file The file or directory to delete |
347 |
* @return true iff the file was really delete, false otherwise |
347 |
* @return true iff the file was really delete, false otherwise |
348 |
*/ |
348 |
*/ |
349 |
public static boolean delete(File file) { |
349 |
public static boolean delete(File file) { |
350 |
// flush all directory content |
350 |
// flush all directory content |
351 |
if (file.isDirectory()) { |
351 |
if (file.isDirectory()) { |
352 |
flushDirectoryContent(file); |
352 |
flushDirectoryContent(file); |
353 |
} |
353 |
} |
354 |
// remove file |
354 |
// remove file |
355 |
file.delete(); |
355 |
file.delete(); |
356 |
if (isFileDeleted(file)) { |
356 |
if (isFileDeleted(file)) { |
357 |
return true; |
357 |
return true; |
358 |
} |
358 |
} |
359 |
return waitUntilFileDeleted(file); |
359 |
return waitUntilFileDeleted(file); |
360 |
} |
360 |
} |
361 |
/** |
361 |
/** |
362 |
* Delete a file or directory and insure that the file is no longer present |
362 |
* Delete a file or directory and insure that the file is no longer present |
363 |
* on file system. In case of directory, delete all the hierarchy underneath. |
363 |
* on file system. In case of directory, delete all the hierarchy underneath. |
364 |
* |
364 |
* |
365 |
* @param resource The resource to delete |
365 |
* @param resource The resource to delete |
366 |
* @return true iff the file was really delete, false otherwise |
366 |
* @return true iff the file was really delete, false otherwise |
367 |
*/ |
367 |
*/ |
368 |
public static boolean delete(IResource resource) { |
368 |
public static boolean delete(IResource resource) { |
369 |
try { |
369 |
try { |
370 |
resource.delete(true, null); |
370 |
resource.delete(true, null); |
371 |
if (isResourceDeleted(resource)) { |
371 |
if (isResourceDeleted(resource)) { |
372 |
return true; |
372 |
return true; |
373 |
} |
373 |
} |
374 |
} |
374 |
} |
375 |
catch (CoreException e) { |
375 |
catch (CoreException e) { |
376 |
// skip |
376 |
// skip |
377 |
} |
377 |
} |
378 |
return waitUntilResourceDeleted(resource); |
378 |
return waitUntilResourceDeleted(resource); |
379 |
} |
379 |
} |
380 |
/** |
380 |
/** |
381 |
* Delete a file or directory and insure that the file is no longer present |
381 |
* Delete a file or directory and insure that the file is no longer present |
382 |
* on file system. In case of directory, delete all the hierarchy underneath. |
382 |
* on file system. In case of directory, delete all the hierarchy underneath. |
383 |
* |
383 |
* |
384 |
* @param path The path of the file or directory to delete |
384 |
* @param path The path of the file or directory to delete |
385 |
* @return true iff the file was really delete, false otherwise |
385 |
* @return true iff the file was really delete, false otherwise |
386 |
*/ |
386 |
*/ |
387 |
public static boolean delete(String path) { |
387 |
public static boolean delete(String path) { |
388 |
return delete(new File(path)); |
388 |
return delete(new File(path)); |
389 |
} |
389 |
} |
390 |
/** |
390 |
/** |
391 |
* Generate a display string from the given String. |
391 |
* Generate a display string from the given String. |
Lines 394-400
Link Here
|
394 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.displayString("abc\ndef\tghi")] |
394 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.displayString("abc\ndef\tghi")] |
395 |
*/ |
395 |
*/ |
396 |
public static String displayString(String inputString){ |
396 |
public static String displayString(String inputString){ |
397 |
return displayString(inputString, 0); |
397 |
return displayString(inputString, 0); |
398 |
} |
398 |
} |
399 |
/** |
399 |
/** |
400 |
* Generate a display string from the given String. |
400 |
* Generate a display string from the given String. |
Lines 410-416
Link Here
|
410 |
* <li>\\ to \\\\</li> |
410 |
* <li>\\ to \\\\</li> |
411 |
* <li>All other characters are unchanged.</li> |
411 |
* <li>All other characters are unchanged.</li> |
412 |
* </ul> |
412 |
* </ul> |
413 |
* This method doesn't convert \r\n to \n. |
413 |
* This method doesn't convert \r\n to \n. |
414 |
* <p> |
414 |
* <p> |
415 |
* Example of use: |
415 |
* Example of use: |
416 |
* <o> |
416 |
* <o> |
Lines 440-531
Link Here
|
440 |
* </li> |
440 |
* </li> |
441 |
* </ol> |
441 |
* </ol> |
442 |
* </p> |
442 |
* </p> |
443 |
* |
443 |
* |
444 |
* @param inputString the given input string |
444 |
* @param inputString the given input string |
445 |
* @param indent number of tabs are added at the begining of each line. |
445 |
* @param indent number of tabs are added at the begining of each line. |
446 |
* |
446 |
* |
447 |
* @return the displayed string |
447 |
* @return the displayed string |
448 |
*/ |
448 |
*/ |
449 |
public static String displayString(String inputString, int indent) { |
449 |
public static String displayString(String inputString, int indent) { |
450 |
return displayString(inputString, indent, false); |
450 |
return displayString(inputString, indent, false); |
451 |
} |
451 |
} |
452 |
public static String displayString(String inputString, int indent, boolean shift) { |
452 |
public static String displayString(String inputString, int indent, boolean shift) { |
453 |
if (inputString == null) |
453 |
if (inputString == null) |
454 |
return "null"; |
454 |
return "null"; |
455 |
int length = inputString.length(); |
455 |
int length = inputString.length(); |
456 |
StringBuffer buffer = new StringBuffer(length); |
456 |
StringBuffer buffer = new StringBuffer(length); |
457 |
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(inputString, "\n\r", true); |
457 |
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(inputString, "\n\r", true); |
458 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
458 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
459 |
if (shift) indent++; |
459 |
if (shift) indent++; |
460 |
buffer.append("\""); |
460 |
buffer.append("\""); |
461 |
while (tokenizer.hasMoreTokens()){ |
461 |
while (tokenizer.hasMoreTokens()){ |
462 |
|
462 |
|
463 |
String token = tokenizer.nextToken(); |
463 |
String token = tokenizer.nextToken(); |
464 |
if (token.equals("\r")) { |
464 |
if (token.equals("\r")) { |
465 |
buffer.append("\\r"); |
465 |
buffer.append("\\r"); |
466 |
if (tokenizer.hasMoreTokens()) { |
466 |
if (tokenizer.hasMoreTokens()) { |
467 |
token = tokenizer.nextToken(); |
467 |
token = tokenizer.nextToken(); |
468 |
if (token.equals("\n")) { |
468 |
if (token.equals("\n")) { |
469 |
buffer.append("\\n"); |
469 |
buffer.append("\\n"); |
470 |
if (tokenizer.hasMoreTokens()) { |
470 |
if (tokenizer.hasMoreTokens()) { |
471 |
buffer.append("\" + \n"); |
471 |
buffer.append("\" + \n"); |
472 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
472 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
473 |
buffer.append("\""); |
473 |
buffer.append("\""); |
474 |
} |
474 |
} |
475 |
continue; |
475 |
continue; |
476 |
} |
476 |
} |
477 |
buffer.append("\" + \n"); |
477 |
buffer.append("\" + \n"); |
478 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
478 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
479 |
buffer.append("\""); |
479 |
buffer.append("\""); |
480 |
} else { |
480 |
} else { |
481 |
continue; |
481 |
continue; |
482 |
} |
482 |
} |
483 |
} else if (token.equals("\n")) { |
483 |
} else if (token.equals("\n")) { |
484 |
buffer.append("\\n"); |
484 |
buffer.append("\\n"); |
485 |
if (tokenizer.hasMoreTokens()) { |
485 |
if (tokenizer.hasMoreTokens()) { |
486 |
buffer.append("\" + \n"); |
486 |
buffer.append("\" + \n"); |
487 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
487 |
for (int i = 0; i < indent; i++) buffer.append("\t"); |
488 |
buffer.append("\""); |
488 |
buffer.append("\""); |
489 |
} |
489 |
} |
490 |
continue; |
490 |
continue; |
491 |
} |
491 |
} |
492 |
|
492 |
|
493 |
StringBuffer tokenBuffer = new StringBuffer(); |
493 |
StringBuffer tokenBuffer = new StringBuffer(); |
494 |
for (int i = 0; i < token.length(); i++){ |
494 |
for (int i = 0; i < token.length(); i++){ |
495 |
char c = token.charAt(i); |
495 |
char c = token.charAt(i); |
496 |
switch (c) { |
496 |
switch (c) { |
497 |
case '\r' : |
497 |
case '\r' : |
498 |
tokenBuffer.append("\\r"); |
498 |
tokenBuffer.append("\\r"); |
499 |
break; |
499 |
break; |
500 |
case '\n' : |
500 |
case '\n' : |
501 |
tokenBuffer.append("\\n"); |
501 |
tokenBuffer.append("\\n"); |
502 |
break; |
502 |
break; |
503 |
case '\b' : |
503 |
case '\b' : |
504 |
tokenBuffer.append("\\b"); |
504 |
tokenBuffer.append("\\b"); |
505 |
break; |
505 |
break; |
506 |
case '\t' : |
506 |
case '\t' : |
507 |
tokenBuffer.append("\t"); |
507 |
tokenBuffer.append("\t"); |
508 |
break; |
508 |
break; |
509 |
case '\f' : |
509 |
case '\f' : |
510 |
tokenBuffer.append("\\f"); |
510 |
tokenBuffer.append("\\f"); |
511 |
break; |
511 |
break; |
512 |
case '\"' : |
512 |
case '\"' : |
513 |
tokenBuffer.append("\\\""); |
513 |
tokenBuffer.append("\\\""); |
514 |
break; |
514 |
break; |
515 |
case '\'' : |
515 |
case '\'' : |
516 |
tokenBuffer.append("\\'"); |
516 |
tokenBuffer.append("\\'"); |
517 |
break; |
517 |
break; |
518 |
case '\\' : |
518 |
case '\\' : |
519 |
tokenBuffer.append("\\\\"); |
519 |
tokenBuffer.append("\\\\"); |
520 |
break; |
520 |
break; |
521 |
default : |
521 |
default : |
522 |
tokenBuffer.append(c); |
522 |
tokenBuffer.append(c); |
523 |
} |
523 |
} |
524 |
} |
524 |
} |
525 |
buffer.append(tokenBuffer.toString()); |
525 |
buffer.append(tokenBuffer.toString()); |
526 |
} |
526 |
} |
527 |
buffer.append("\""); |
527 |
buffer.append("\""); |
528 |
return buffer.toString(); |
528 |
return buffer.toString(); |
529 |
} |
529 |
} |
530 |
/** |
530 |
/** |
531 |
* Reads the content of the given source file. |
531 |
* Reads the content of the given source file. |
Lines 534-574
Link Here
|
534 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.fileContent("c:/temp/X.java")] |
534 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.fileContent("c:/temp/X.java")] |
535 |
*/ |
535 |
*/ |
536 |
public static String fileContent(String sourceFilePath) { |
536 |
public static String fileContent(String sourceFilePath) { |
537 |
File sourceFile = new File(sourceFilePath); |
537 |
File sourceFile = new File(sourceFilePath); |
538 |
if (!sourceFile.exists()) { |
538 |
if (!sourceFile.exists()) { |
539 |
if (DEBUG) System.out.println("File " + sourceFilePath + " does not exists."); |
539 |
if (DEBUG) System.out.println("File " + sourceFilePath + " does not exists."); |
540 |
return null; |
540 |
return null; |
541 |
} |
541 |
} |
542 |
if (!sourceFile.isFile()) { |
542 |
if (!sourceFile.isFile()) { |
543 |
if (DEBUG) System.out.println(sourceFilePath + " is not a file."); |
543 |
if (DEBUG) System.out.println(sourceFilePath + " is not a file."); |
544 |
return null; |
544 |
return null; |
545 |
} |
545 |
} |
546 |
StringBuffer sourceContentBuffer = new StringBuffer(); |
546 |
StringBuffer sourceContentBuffer = new StringBuffer(); |
547 |
FileInputStream input = null; |
547 |
FileInputStream input = null; |
548 |
try { |
548 |
try { |
549 |
input = new FileInputStream(sourceFile); |
549 |
input = new FileInputStream(sourceFile); |
550 |
} catch (FileNotFoundException e) { |
550 |
} catch (FileNotFoundException e) { |
551 |
return null; |
551 |
return null; |
552 |
} |
552 |
} |
553 |
try { |
553 |
try { |
554 |
int read; |
554 |
int read; |
555 |
do { |
555 |
do { |
556 |
read = input.read(); |
556 |
read = input.read(); |
557 |
if (read != -1) { |
557 |
if (read != -1) { |
558 |
sourceContentBuffer.append((char)read); |
558 |
sourceContentBuffer.append((char)read); |
559 |
} |
559 |
} |
560 |
} while (read != -1); |
560 |
} while (read != -1); |
561 |
input.close(); |
561 |
input.close(); |
562 |
} catch (IOException e) { |
562 |
} catch (IOException e) { |
563 |
e.printStackTrace(); |
563 |
e.printStackTrace(); |
564 |
return null; |
564 |
return null; |
565 |
} finally { |
565 |
} finally { |
566 |
try { |
566 |
try { |
567 |
input.close(); |
567 |
input.close(); |
568 |
} catch (IOException e2) { |
568 |
} catch (IOException e2) { |
569 |
} |
569 |
} |
570 |
} |
570 |
} |
571 |
return sourceContentBuffer.toString(); |
571 |
return sourceContentBuffer.toString(); |
572 |
} |
572 |
} |
573 |
|
573 |
|
574 |
/** |
574 |
/** |
Lines 577-587
Link Here
|
577 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java", 0)] |
577 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java", 0)] |
578 |
*/ |
578 |
*/ |
579 |
public static String fileContentToDisplayString(String sourceFilePath, int indent, boolean independantLineDelimiter) { |
579 |
public static String fileContentToDisplayString(String sourceFilePath, int indent, boolean independantLineDelimiter) { |
580 |
String sourceString = fileContent(sourceFilePath); |
580 |
String sourceString = fileContent(sourceFilePath); |
581 |
if (independantLineDelimiter) { |
581 |
if (independantLineDelimiter) { |
582 |
sourceString = convertToIndependantLineDelimiter(sourceString); |
582 |
sourceString = convertToIndependantLineDelimiter(sourceString); |
583 |
} |
583 |
} |
584 |
return displayString(sourceString, indent); |
584 |
return displayString(sourceString, indent); |
585 |
} |
585 |
} |
586 |
/** |
586 |
/** |
587 |
* Reads the content of the given source file, converts it to a display string. |
587 |
* Reads the content of the given source file, converts it to a display string. |
Lines 591-634
Link Here
|
591 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java", 0, null)] |
591 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java", 0, null)] |
592 |
*/ |
592 |
*/ |
593 |
public static void fileContentToDisplayString(String sourceFilePath, int indent, String destinationFilePath, boolean independantLineDelimiter) { |
593 |
public static void fileContentToDisplayString(String sourceFilePath, int indent, String destinationFilePath, boolean independantLineDelimiter) { |
594 |
String displayString = fileContentToDisplayString(sourceFilePath, indent, independantLineDelimiter); |
594 |
String displayString = fileContentToDisplayString(sourceFilePath, indent, independantLineDelimiter); |
595 |
if (destinationFilePath == null) { |
595 |
if (destinationFilePath == null) { |
596 |
System.out.println(displayString); |
596 |
System.out.println(displayString); |
597 |
return; |
597 |
return; |
598 |
} |
598 |
} |
599 |
writeToFile(displayString, destinationFilePath); |
599 |
writeToFile(displayString, destinationFilePath); |
600 |
} |
600 |
} |
601 |
/** |
601 |
/** |
602 |
* Flush content of a given directory (leaving it empty), |
602 |
* Flush content of a given directory (leaving it empty), |
603 |
* no-op if not a directory. |
603 |
* no-op if not a directory. |
604 |
*/ |
604 |
*/ |
605 |
public static void flushDirectoryContent(File dir) { |
605 |
public static void flushDirectoryContent(File dir) { |
606 |
File[] files = dir.listFiles(); |
606 |
File[] files = dir.listFiles(); |
607 |
if (files == null) return; |
607 |
if (files == null) return; |
608 |
for (int i = 0, max = files.length; i < max; i++) { |
608 |
for (int i = 0, max = files.length; i < max; i++) { |
609 |
delete(files[i]); |
609 |
delete(files[i]); |
610 |
} |
610 |
} |
611 |
} |
611 |
} |
612 |
/** |
612 |
/** |
613 |
* Returns the next available port number on the local host. |
613 |
* Returns the next available port number on the local host. |
614 |
*/ |
614 |
*/ |
615 |
public static int getFreePort() { |
615 |
public static int getFreePort() { |
616 |
ServerSocket socket = null; |
616 |
ServerSocket socket = null; |
617 |
try { |
617 |
try { |
618 |
socket = new ServerSocket(0); |
618 |
socket = new ServerSocket(0); |
619 |
return socket.getLocalPort(); |
619 |
return socket.getLocalPort(); |
620 |
} catch (IOException e) { |
620 |
} catch (IOException e) { |
621 |
// ignore |
621 |
// ignore |
622 |
} finally { |
622 |
} finally { |
623 |
if (socket != null) { |
623 |
if (socket != null) { |
624 |
try { |
624 |
try { |
625 |
socket.close(); |
625 |
socket.close(); |
626 |
} catch (IOException e) { |
626 |
} catch (IOException e) { |
627 |
// ignore |
627 |
// ignore |
628 |
} |
628 |
} |
629 |
} |
629 |
} |
630 |
} |
630 |
} |
631 |
return -1; |
631 |
return -1; |
632 |
} |
632 |
} |
633 |
/** |
633 |
/** |
634 |
* Search the user hard-drive for a Java class library. |
634 |
* Search the user hard-drive for a Java class library. |
Lines 637-821
Link Here
|
637 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getJavaClassLib()] |
637 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getJavaClassLib()] |
638 |
*/ |
638 |
*/ |
639 |
public static String[] getJavaClassLibs() { |
639 |
public static String[] getJavaClassLibs() { |
640 |
String jreDir = getJREDirectory(); |
640 |
String jreDir = getJREDirectory(); |
641 |
final String osName = System.getProperty("os.name"); |
641 |
final String osName = System.getProperty("os.name"); |
642 |
if (jreDir == null) { |
642 |
if (jreDir == null) { |
643 |
return new String[] {}; |
643 |
return new String[] {}; |
644 |
} |
644 |
} |
645 |
if (osName.startsWith("Mac")) { |
645 |
if (osName.startsWith("Mac")) { |
646 |
return new String[] { toNativePath(jreDir + "/../Classes/classes.jar") }; |
646 |
return new String[] { |
647 |
} |
647 |
toNativePath(jreDir + "/../Classes/classes.jar") |
648 |
final String vmName = System.getProperty("java.vm.name"); |
648 |
}; |
649 |
if ("J9".equals(vmName)) { |
649 |
} |
650 |
return new String[] { toNativePath(jreDir + "/lib/jclMax/classes.zip") }; |
650 |
final String vmName = System.getProperty("java.vm.name"); |
651 |
} |
651 |
if ("J9".equals(vmName)) { |
652 |
File file = new File(jreDir + "/lib/rt.jar"); |
652 |
return new String[] { |
653 |
if (file.exists()) { |
653 |
toNativePath(jreDir + "/lib/jclMax/classes.zip") |
654 |
return new String[] { |
654 |
}; |
655 |
toNativePath(jreDir + "/lib/rt.jar") |
655 |
} |
656 |
}; |
656 |
ArrayList paths = new ArrayList(); |
657 |
} |
657 |
String[] jarsNames = new String[] { |
658 |
file = new File(jreDir + "/lib/vm.jar"); |
658 |
"/lib/vm.jar", |
659 |
if (file.exists()) { |
659 |
"/lib/rt.jar", |
660 |
// The IBM J2SE 5.0 has put the java.lang classes in vm.jar. |
660 |
"/lib/core.jar", |
661 |
return new String[] { |
661 |
"/lib/security.jar", |
662 |
toNativePath(jreDir + "/lib/vm.jar"), |
662 |
"/lib/xml.jar", |
663 |
toNativePath(jreDir + "/lib/core.jar"), |
663 |
"/lib/graphics.jar" |
664 |
toNativePath(jreDir + "/lib/security.jar"), |
664 |
}; |
665 |
toNativePath(jreDir + "/lib/graphics.jar") }; |
665 |
addJarEntries(jreDir, jarsNames, paths); |
|
|
666 |
String[] result = new String[paths.size()]; |
667 |
paths.toArray(result); |
668 |
return result; |
669 |
} |
670 |
private static void addJarEntries(String jreDir, String[] jarNames, ArrayList paths) { |
671 |
for (int i = 0, max = jarNames.length; i < max; i++) { |
672 |
final String currentName = jreDir + jarNames[i]; |
673 |
File f = new File(currentName); |
674 |
if (f.exists()) { |
675 |
paths.add(toNativePath(currentName)); |
676 |
} |
666 |
} |
677 |
} |
667 |
return new String[] { |
|
|
668 |
toNativePath(jreDir + "/lib/core.jar"), |
669 |
toNativePath(jreDir + "/lib/security.jar"), |
670 |
toNativePath(jreDir + "/lib/graphics.jar") |
671 |
}; |
672 |
} |
678 |
} |
673 |
public static String getJavaClassLibsAsString() { |
679 |
public static String getJavaClassLibsAsString() { |
674 |
String[] classLibs = getJavaClassLibs(); |
680 |
String[] classLibs = getJavaClassLibs(); |
675 |
StringBuffer buffer = new StringBuffer(); |
681 |
StringBuffer buffer = new StringBuffer(); |
676 |
for (int i = 0, max = classLibs.length; i < max; i++) { |
682 |
for (int i = 0, max = classLibs.length; i < max; i++) { |
677 |
buffer |
683 |
buffer |
678 |
.append(classLibs[i]) |
684 |
.append(classLibs[i]) |
679 |
.append(File.pathSeparatorChar); |
685 |
.append(File.pathSeparatorChar); |
680 |
|
686 |
|
681 |
} |
687 |
} |
682 |
return buffer.toString(); |
688 |
return buffer.toString(); |
683 |
} |
689 |
} |
684 |
/** |
690 |
/** |
685 |
* Returns the JRE directory this tests are running on. |
691 |
* Returns the JRE directory this tests are running on. |
686 |
* Returns null if none could be found. |
692 |
* Returns null if none could be found. |
687 |
* |
693 |
* |
688 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getJREDirectory()] |
694 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getJREDirectory()] |
689 |
*/ |
695 |
*/ |
690 |
public static String getJREDirectory() { |
696 |
public static String getJREDirectory() { |
691 |
return System.getProperty("java.home"); |
697 |
return System.getProperty("java.home"); |
692 |
} |
698 |
} |
693 |
/** |
699 |
/** |
694 |
* Search the user hard-drive for a possible output directory. |
700 |
* Search the user hard-drive for a possible output directory. |
695 |
* Returns null if none could be found. |
701 |
* Returns null if none could be found. |
696 |
* |
702 |
* |
697 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getOutputDirectory()] |
703 |
* Example of use: [org.eclipse.jdt.core.tests.util.Util.getOutputDirectory()] |
698 |
*/ |
704 |
*/ |
699 |
public static String getOutputDirectory() { |
705 |
public static String getOutputDirectory() { |
700 |
return OUTPUT_DIRECTORY; |
706 |
return OUTPUT_DIRECTORY; |
701 |
} |
707 |
} |
702 |
/** |
708 |
/** |
703 |
* Returns the parent's child file matching the given file or null if not found. |
709 |
* Returns the parent's child file matching the given file or null if not found. |
704 |
* |
710 |
* |
705 |
* @param file The searched file in parent |
711 |
* @param file The searched file in parent |
706 |
* @return The parent's child matching the given file or null if not found. |
712 |
* @return The parent's child matching the given file or null if not found. |
707 |
*/ |
713 |
*/ |
708 |
private static File getParentChildFile(File file) { |
714 |
private static File getParentChildFile(File file) { |
709 |
File parent = file.getParentFile(); |
715 |
File parent = file.getParentFile(); |
710 |
if (parent == null || !parent.exists()) return null; |
716 |
if (parent == null || !parent.exists()) return null; |
711 |
File[] files = parent.listFiles(); |
717 |
File[] files = parent.listFiles(); |
712 |
int length = files==null ? 0 : files.length; |
718 |
int length = files==null ? 0 : files.length; |
713 |
if (length > 0) { |
719 |
if (length > 0) { |
714 |
for (int i=0; i<length; i++) { |
720 |
for (int i=0; i<length; i++) { |
715 |
if (files[i] == file) { |
721 |
if (files[i] == file) { |
716 |
return files[i]; |
722 |
return files[i]; |
717 |
} else if (files[i].equals(file)) { |
723 |
} else if (files[i].equals(file)) { |
718 |
return files[i]; |
724 |
return files[i]; |
719 |
} else if (files[i].getPath().equals(file.getPath())) { |
725 |
} else if (files[i].getPath().equals(file.getPath())) { |
720 |
return files[i]; |
726 |
return files[i]; |
721 |
} |
727 |
} |
722 |
} |
728 |
} |
723 |
} |
729 |
} |
724 |
return null; |
730 |
return null; |
725 |
} |
731 |
} |
726 |
/** |
732 |
/** |
727 |
* Returns parent's child resource matching the given resource or null if not found. |
733 |
* Returns parent's child resource matching the given resource or null if not found. |
728 |
* |
734 |
* |
729 |
* @param resource The searched file in parent |
735 |
* @param resource The searched file in parent |
730 |
* @return The parent's child matching the given file or null if not found. |
736 |
* @return The parent's child matching the given file or null if not found. |
731 |
*/ |
737 |
*/ |
732 |
private static IResource getParentChildResource(IResource resource) { |
738 |
private static IResource getParentChildResource(IResource resource) { |
733 |
IContainer parent = resource.getParent(); |
739 |
IContainer parent = resource.getParent(); |
734 |
if (parent == null || !parent.exists()) return null; |
740 |
if (parent == null || !parent.exists()) return null; |
735 |
try { |
741 |
try { |
736 |
IResource[] members = parent.members(); |
742 |
IResource[] members = parent.members(); |
737 |
int length = members ==null ? 0 : members.length; |
743 |
int length = members ==null ? 0 : members.length; |
738 |
if (length > 0) { |
744 |
if (length > 0) { |
739 |
for (int i=0; i<length; i++) { |
745 |
for (int i=0; i<length; i++) { |
740 |
if (members[i] == resource) { |
746 |
if (members[i] == resource) { |
741 |
return members[i]; |
747 |
return members[i]; |
742 |
} else if (members[i].equals(resource)) { |
748 |
} else if (members[i].equals(resource)) { |
743 |
return members[i]; |
749 |
return members[i]; |
744 |
} else if (members[i].getFullPath().equals(resource.getFullPath())) { |
750 |
} else if (members[i].getFullPath().equals(resource.getFullPath())) { |
745 |
return members[i]; |
751 |
return members[i]; |
746 |
} |
752 |
} |
747 |
} |
753 |
} |
748 |
} |
754 |
} |
749 |
} |
755 |
} |
750 |
catch (CoreException ce) { |
756 |
catch (CoreException ce) { |
751 |
// skip |
757 |
// skip |
752 |
} |
758 |
} |
753 |
return null; |
759 |
return null; |
754 |
} |
760 |
} |
755 |
/** |
761 |
/** |
756 |
* Returns the test name from stack elements info. |
762 |
* Returns the test name from stack elements info. |
757 |
* |
763 |
* |
758 |
* @return The name of the test currently running |
764 |
* @return The name of the test currently running |
759 |
*/ |
765 |
*/ |
760 |
private static String getTestName() { |
766 |
private static String getTestName() { |
761 |
StackTraceElement[] elements = new Exception().getStackTrace(); |
767 |
StackTraceElement[] elements = new Exception().getStackTrace(); |
762 |
int idx = 0, length=elements.length; |
768 |
int idx = 0, length=elements.length; |
763 |
while (idx<length && !elements[idx++].getClassName().startsWith("org.eclipse.jdt")) { |
769 |
while (idx<length && !elements[idx++].getClassName().startsWith("org.eclipse.jdt")) { |
764 |
// loop until JDT/Core class appears in the stack |
770 |
// loop until JDT/Core class appears in the stack |
765 |
} |
771 |
} |
766 |
if (idx<length) { |
772 |
if (idx<length) { |
767 |
StackTraceElement testElement = null; |
773 |
StackTraceElement testElement = null; |
768 |
while (idx<length && elements[idx].getClassName().startsWith("org.eclipse.jdt")) { |
774 |
while (idx<length && elements[idx].getClassName().startsWith("org.eclipse.jdt")) { |
769 |
testElement = elements[idx++]; |
775 |
testElement = elements[idx++]; |
770 |
} |
776 |
} |
771 |
if (testElement != null) { |
777 |
if (testElement != null) { |
772 |
return testElement.getClassName() + " - " + testElement.getMethodName(); |
778 |
return testElement.getClassName() + " - " + testElement.getMethodName(); |
773 |
} |
779 |
} |
774 |
} |
780 |
} |
775 |
return "?"; |
781 |
return "?"; |
776 |
} |
782 |
} |
777 |
public static String indentString(String inputString, int indent) { |
783 |
public static String indentString(String inputString, int indent) { |
778 |
if (inputString == null) |
784 |
if (inputString == null) |
779 |
return ""; |
785 |
return ""; |
780 |
int length = inputString.length(); |
786 |
int length = inputString.length(); |
781 |
StringBuffer buffer = new StringBuffer(length); |
787 |
StringBuffer buffer = new StringBuffer(length); |
782 |
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(inputString, "\n\r", true); |
788 |
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(inputString, "\n\r", true); |
783 |
StringBuffer indentStr = new StringBuffer(indent); |
789 |
StringBuffer indentStr = new StringBuffer(indent); |
784 |
for (int i = 0; i < indent; i++) indentStr.append("\t"); |
790 |
for (int i = 0; i < indent; i++) indentStr.append("\t"); |
785 |
buffer.append(indentStr); |
791 |
buffer.append(indentStr); |
786 |
while (tokenizer.hasMoreTokens()){ |
792 |
while (tokenizer.hasMoreTokens()){ |
787 |
String token = tokenizer.nextToken(); |
793 |
String token = tokenizer.nextToken(); |
788 |
buffer.append(token); |
794 |
buffer.append(token); |
789 |
if (token.equals("\r") || token.equals("\n")) { |
795 |
if (token.equals("\r") || token.equals("\n")) { |
790 |
buffer.append(indentStr); |
796 |
buffer.append(indentStr); |
791 |
} |
797 |
} |
792 |
} |
798 |
} |
793 |
return buffer.toString(); |
799 |
return buffer.toString(); |
794 |
} |
800 |
} |
795 |
/** |
801 |
/** |
796 |
* Returns whether a file is really deleted or not. |
802 |
* Returns whether a file is really deleted or not. |
797 |
* Does not only rely on {@link File#exists()} method but also |
803 |
* Does not only rely on {@link File#exists()} method but also |
798 |
* look if it's not in its parent children {@link #getParentChildFile(File)}. |
804 |
* look if it's not in its parent children {@link #getParentChildFile(File)}. |
799 |
* |
805 |
* |
800 |
* @param file The file to test if deleted |
806 |
* @param file The file to test if deleted |
801 |
* @return true if the file does not exist and was not found in its parent children. |
807 |
* @return true if the file does not exist and was not found in its parent children. |
802 |
*/ |
808 |
*/ |
803 |
public static boolean isFileDeleted(File file) { |
809 |
public static boolean isFileDeleted(File file) { |
804 |
return !file.exists() && getParentChildFile(file) == null; |
810 |
return !file.exists() && getParentChildFile(file) == null; |
805 |
} |
811 |
} |
806 |
public static boolean isMacOS() { |
812 |
public static boolean isMacOS() { |
807 |
return System.getProperty("os.name").indexOf("Mac") != -1; |
813 |
return System.getProperty("os.name").indexOf("Mac") != -1; |
808 |
} |
814 |
} |
809 |
/** |
815 |
/** |
810 |
* Returns whether a resource is really deleted or not. |
816 |
* Returns whether a resource is really deleted or not. |
811 |
* Does not only rely on {@link IResource#isAccessible()} method but also |
817 |
* Does not only rely on {@link IResource#isAccessible()} method but also |
812 |
* look if it's not in its parent children {@link #getParentChildResource(IResource)}. |
818 |
* look if it's not in its parent children {@link #getParentChildResource(IResource)}. |
813 |
* |
819 |
* |
814 |
* @param resource The resource to test if deleted |
820 |
* @param resource The resource to test if deleted |
815 |
* @return true if the resource is not accessible and was not found in its parent children. |
821 |
* @return true if the resource is not accessible and was not found in its parent children. |
816 |
*/ |
822 |
*/ |
817 |
public static boolean isResourceDeleted(IResource resource) { |
823 |
public static boolean isResourceDeleted(IResource resource) { |
818 |
return !resource.isAccessible() && getParentChildResource(resource) == null; |
824 |
return !resource.isAccessible() && getParentChildResource(resource) == null; |
819 |
} |
825 |
} |
820 |
/** |
826 |
/** |
821 |
* Print given file information with specified indentation. |
827 |
* Print given file information with specified indentation. |
Lines 830-1186
Link Here
|
830 |
* May recurse several level in parents hierarchy. |
836 |
* May recurse several level in parents hierarchy. |
831 |
* May also display children, but then will not recusre in parent |
837 |
* May also display children, but then will not recusre in parent |
832 |
* hierarchy to avoid infinite loop... |
838 |
* hierarchy to avoid infinite loop... |
833 |
* |
839 |
* |
834 |
* @param file The file to display information |
840 |
* @param file The file to display information |
835 |
* @param indent Number of tab to print before the information |
841 |
* @param indent Number of tab to print before the information |
836 |
* @param recurse Display also information on <code>recurse</code>th parents in hierarchy. |
842 |
* @param recurse Display also information on <code>recurse</code>th parents in hierarchy. |
837 |
* If negative then display children information instead. |
843 |
* If negative then display children information instead. |
838 |
*/ |
844 |
*/ |
839 |
private static void printFileInfo(File file, int indent, int recurse) { |
845 |
private static void printFileInfo(File file, int indent, int recurse) { |
840 |
String tab = ""; |
846 |
String tab = ""; |
841 |
for (int i=0; i<indent; i++) tab+="\t"; |
847 |
for (int i=0; i<indent; i++) tab+="\t"; |
842 |
System.out.print(tab+"- "+file.getName()+" file info: "); |
848 |
System.out.print(tab+"- "+file.getName()+" file info: "); |
843 |
String sep = ""; |
849 |
String sep = ""; |
844 |
if (file.canRead()) { |
850 |
if (file.canRead()) { |
845 |
System.out.print("read"); |
851 |
System.out.print("read"); |
846 |
sep = ", "; |
852 |
sep = ", "; |
847 |
} |
853 |
} |
848 |
if (file.canWrite()) { |
854 |
if (file.canWrite()) { |
849 |
System.out.print(sep+"write"); |
855 |
System.out.print(sep+"write"); |
850 |
sep = ", "; |
856 |
sep = ", "; |
851 |
} |
857 |
} |
852 |
if (file.exists()) { |
858 |
if (file.exists()) { |
853 |
System.out.print(sep+"exist"); |
859 |
System.out.print(sep+"exist"); |
854 |
sep = ", "; |
860 |
sep = ", "; |
855 |
} |
861 |
} |
856 |
if (file.isDirectory()) { |
862 |
if (file.isDirectory()) { |
857 |
System.out.print(sep+"dir"); |
863 |
System.out.print(sep+"dir"); |
858 |
sep = ", "; |
864 |
sep = ", "; |
859 |
} |
865 |
} |
860 |
if (file.isFile()) { |
866 |
if (file.isFile()) { |
861 |
System.out.print(sep+"file"); |
867 |
System.out.print(sep+"file"); |
862 |
sep = ", "; |
868 |
sep = ", "; |
863 |
} |
869 |
} |
864 |
if (file.isHidden()) { |
870 |
if (file.isHidden()) { |
865 |
System.out.print(sep+"hidden"); |
871 |
System.out.print(sep+"hidden"); |
866 |
sep = ", "; |
872 |
sep = ", "; |
867 |
} |
873 |
} |
868 |
System.out.println(); |
874 |
System.out.println(); |
869 |
File[] files = file.listFiles(); |
875 |
File[] files = file.listFiles(); |
870 |
int length = files==null ? 0 : files.length; |
876 |
int length = files==null ? 0 : files.length; |
871 |
if (length > 0) { |
877 |
if (length > 0) { |
872 |
boolean children = recurse < 0; |
878 |
boolean children = recurse < 0; |
873 |
System.out.print(tab+" + children: "); |
879 |
System.out.print(tab+" + children: "); |
874 |
if (children) System.out.println(); |
880 |
if (children) System.out.println(); |
875 |
for (int i=0; i<length; i++) { |
881 |
for (int i=0; i<length; i++) { |
876 |
if (children) { // display children |
882 |
if (children) { // display children |
877 |
printFileInfo(files[i], indent+2, -1); |
883 |
printFileInfo(files[i], indent+2, -1); |
878 |
} else { |
884 |
} else { |
879 |
if (i>0) System.out.print(", "); |
885 |
if (i>0) System.out.print(", "); |
880 |
System.out.print(files[i].getName()); |
886 |
System.out.print(files[i].getName()); |
881 |
if (files[i].isDirectory()) System.out.print("[dir]"); |
887 |
if (files[i].isDirectory()) System.out.print("[dir]"); |
882 |
else if (files[i].isFile()) System.out.print("[file]"); |
888 |
else if (files[i].isFile()) System.out.print("[file]"); |
883 |
else System.out.print("[?]"); |
889 |
else System.out.print("[?]"); |
884 |
} |
890 |
} |
885 |
} |
891 |
} |
886 |
if (!children) System.out.println(); |
892 |
if (!children) System.out.println(); |
887 |
} |
893 |
} |
888 |
if (recurse > 0) { |
894 |
if (recurse > 0) { |
889 |
File parent = file.getParentFile(); |
895 |
File parent = file.getParentFile(); |
890 |
if (parent != null) printFileInfo(parent, indent+1, recurse-1); |
896 |
if (parent != null) printFileInfo(parent, indent+1, recurse-1); |
891 |
} |
897 |
} |
892 |
} |
898 |
} |
893 |
/** |
899 |
/** |
894 |
* Print stack trace with only JDT/Core elements. |
900 |
* Print stack trace with only JDT/Core elements. |
895 |
* |
901 |
* |
896 |
* @param exception Exception of the stack trace. May be null, then a fake exception is used. |
902 |
* @param exception Exception of the stack trace. May be null, then a fake exception is used. |
897 |
* @param indent Number of tab to display before the stack elements to display. |
903 |
* @param indent Number of tab to display before the stack elements to display. |
898 |
*/ |
904 |
*/ |
899 |
private static void printJdtCoreStackTrace(Exception exception, int indent) { |
905 |
private static void printJdtCoreStackTrace(Exception exception, int indent) { |
900 |
String tab = ""; |
906 |
String tab = ""; |
901 |
for (int i=0; i<indent; i++) tab+="\t"; |
907 |
for (int i=0; i<indent; i++) tab+="\t"; |
902 |
StackTraceElement[] elements = (exception==null?new Exception():exception).getStackTrace(); |
908 |
StackTraceElement[] elements = (exception==null?new Exception():exception).getStackTrace(); |
903 |
int idx = 0, length=elements.length; |
909 |
int idx = 0, length=elements.length; |
904 |
while (idx<length && !elements[idx++].getClassName().startsWith("org.eclipse.jdt")) { |
910 |
while (idx<length && !elements[idx++].getClassName().startsWith("org.eclipse.jdt")) { |
905 |
// loop until JDT/Core class appears in the stack |
911 |
// loop until JDT/Core class appears in the stack |
906 |
} |
912 |
} |
907 |
if (idx<length) { |
913 |
if (idx<length) { |
908 |
System.out.print(tab+"- stack trace"); |
914 |
System.out.print(tab+"- stack trace"); |
909 |
if (exception == null) |
915 |
if (exception == null) |
910 |
System.out.println(":"); |
916 |
System.out.println(":"); |
911 |
else |
917 |
else |
912 |
System.out.println(" for exception "+exception+":"); |
918 |
System.out.println(" for exception "+exception+":"); |
913 |
while (idx<length && elements[idx].getClassName().startsWith("org.eclipse.jdt")) { |
919 |
while (idx<length && elements[idx].getClassName().startsWith("org.eclipse.jdt")) { |
914 |
StackTraceElement testElement = elements[idx++]; |
920 |
StackTraceElement testElement = elements[idx++]; |
915 |
System.out.println(tab+" -> "+testElement); |
921 |
System.out.println(tab+" -> "+testElement); |
916 |
} |
922 |
} |
917 |
} else { |
923 |
} else { |
918 |
exception.printStackTrace(System.out); |
924 |
exception.printStackTrace(System.out); |
919 |
} |
925 |
} |
920 |
} |
926 |
} |
921 |
/** |
927 |
/** |
922 |
* Makes the given path a path using native path separators as returned by File.getPath() |
928 |
* Makes the given path a path using native path separators as returned by File.getPath() |
923 |
* and trimming any extra slash. |
929 |
* and trimming any extra slash. |
924 |
*/ |
930 |
*/ |
925 |
public static String toNativePath(String path) { |
931 |
public static String toNativePath(String path) { |
926 |
String nativePath = path.replace('\\', File.separatorChar).replace('/', File.separatorChar); |
932 |
String nativePath = path.replace('\\', File.separatorChar).replace('/', File.separatorChar); |
927 |
return |
933 |
return |
928 |
nativePath.endsWith("/") || nativePath.endsWith("\\") ? |
934 |
nativePath.endsWith("/") || nativePath.endsWith("\\") ? |
929 |
nativePath.substring(0, nativePath.length() - 1) : |
935 |
nativePath.substring(0, nativePath.length() - 1) : |
930 |
nativePath; |
936 |
nativePath; |
931 |
} |
937 |
} |
932 |
/** |
938 |
/** |
933 |
* Unzip the contents of the given zip in the given directory (create it if it doesn't exist) |
939 |
* Unzip the contents of the given zip in the given directory (create it if it doesn't exist) |
934 |
*/ |
940 |
*/ |
935 |
public static void unzip(String zipPath, String destDirPath) throws IOException { |
941 |
public static void unzip(String zipPath, String destDirPath) throws IOException { |
936 |
|
942 |
|
937 |
InputStream zipIn = new FileInputStream(zipPath); |
943 |
InputStream zipIn = new FileInputStream(zipPath); |
938 |
byte[] buf = new byte[8192]; |
944 |
byte[] buf = new byte[8192]; |
939 |
File destDir = new File(destDirPath); |
945 |
File destDir = new File(destDirPath); |
940 |
ZipInputStream zis = new ZipInputStream(zipIn); |
946 |
ZipInputStream zis = new ZipInputStream(zipIn); |
941 |
FileOutputStream fos = null; |
947 |
FileOutputStream fos = null; |
942 |
try { |
948 |
try { |
943 |
ZipEntry zEntry; |
949 |
ZipEntry zEntry; |
944 |
while ((zEntry = zis.getNextEntry()) != null) { |
950 |
while ((zEntry = zis.getNextEntry()) != null) { |
945 |
// if it is empty directory, create it |
951 |
// if it is empty directory, create it |
946 |
if (zEntry.isDirectory()) { |
952 |
if (zEntry.isDirectory()) { |
947 |
new File(destDir, zEntry.getName()).mkdirs(); |
953 |
new File(destDir, zEntry.getName()).mkdirs(); |
948 |
continue; |
954 |
continue; |
949 |
} |
955 |
} |
950 |
// if it is a file, extract it |
956 |
// if it is a file, extract it |
951 |
String filePath = zEntry.getName(); |
957 |
String filePath = zEntry.getName(); |
952 |
int lastSeparator = filePath.lastIndexOf("/"); //$NON-NLS-1$ |
958 |
int lastSeparator = filePath.lastIndexOf("/"); //$NON-NLS-1$ |
953 |
String fileDir = ""; //$NON-NLS-1$ |
959 |
String fileDir = ""; //$NON-NLS-1$ |
954 |
if (lastSeparator >= 0) { |
960 |
if (lastSeparator >= 0) { |
955 |
fileDir = filePath.substring(0, lastSeparator); |
961 |
fileDir = filePath.substring(0, lastSeparator); |
956 |
} |
962 |
} |
957 |
//create directory for a file |
963 |
//create directory for a file |
958 |
new File(destDir, fileDir).mkdirs(); |
964 |
new File(destDir, fileDir).mkdirs(); |
959 |
//write file |
965 |
//write file |
960 |
File outFile = new File(destDir, filePath); |
966 |
File outFile = new File(destDir, filePath); |
961 |
fos = new FileOutputStream(outFile); |
967 |
fos = new FileOutputStream(outFile); |
962 |
int n = 0; |
968 |
int n = 0; |
963 |
while ((n = zis.read(buf)) >= 0) { |
969 |
while ((n = zis.read(buf)) >= 0) { |
964 |
fos.write(buf, 0, n); |
970 |
fos.write(buf, 0, n); |
965 |
} |
971 |
} |
966 |
fos.close(); |
972 |
fos.close(); |
967 |
} |
973 |
} |
968 |
} catch (IOException ioe) { |
974 |
} catch (IOException ioe) { |
969 |
if (fos != null) { |
975 |
if (fos != null) { |
970 |
try { |
976 |
try { |
971 |
fos.close(); |
977 |
fos.close(); |
972 |
} catch (IOException ioe2) { |
978 |
} catch (IOException ioe2) { |
973 |
} |
979 |
} |
974 |
} |
980 |
} |
975 |
} finally { |
981 |
} finally { |
976 |
try { |
982 |
try { |
977 |
zipIn.close(); |
983 |
zipIn.close(); |
978 |
if (zis != null) |
984 |
if (zis != null) |
979 |
zis.close(); |
985 |
zis.close(); |
980 |
} catch (IOException ioe) { |
986 |
} catch (IOException ioe) { |
981 |
} |
987 |
} |
982 |
} |
988 |
} |
983 |
} |
989 |
} |
984 |
/** |
990 |
/** |
985 |
* Wait until the file is _really_ deleted on file system. |
991 |
* Wait until the file is _really_ deleted on file system. |
986 |
* |
992 |
* |
987 |
* @param file Deleted file |
993 |
* @param file Deleted file |
988 |
* @return true if the file was finally deleted, false otherwise |
994 |
* @return true if the file was finally deleted, false otherwise |
989 |
*/ |
995 |
*/ |
990 |
private static boolean waitUntilFileDeleted(File file) { |
996 |
private static boolean waitUntilFileDeleted(File file) { |
991 |
if (DELETE_DEBUG) { |
997 |
if (DELETE_DEBUG) { |
992 |
System.out.println(); |
998 |
System.out.println(); |
993 |
System.out.println("WARNING in test: "+getTestName()); |
999 |
System.out.println("WARNING in test: "+getTestName()); |
994 |
System.out.println(" - problems occured while deleting "+file); |
1000 |
System.out.println(" - problems occured while deleting "+file); |
995 |
printJdtCoreStackTrace(null, 1); |
1001 |
printJdtCoreStackTrace(null, 1); |
996 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1002 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
997 |
System.out.print(" - wait for ("+DELETE_MAX_WAIT+"ms max): "); |
1003 |
System.out.print(" - wait for ("+DELETE_MAX_WAIT+"ms max): "); |
998 |
} |
1004 |
} |
999 |
int count = 0; |
1005 |
int count = 0; |
1000 |
int delay = 10; // ms |
1006 |
int delay = 10; // ms |
1001 |
int maxRetry = DELETE_MAX_WAIT / delay; |
1007 |
int maxRetry = DELETE_MAX_WAIT / delay; |
1002 |
int time = 0; |
1008 |
int time = 0; |
1003 |
while (count < maxRetry) { |
1009 |
while (count < maxRetry) { |
1004 |
try { |
1010 |
try { |
1005 |
count++; |
1011 |
count++; |
1006 |
Thread.sleep(delay); |
1012 |
Thread.sleep(delay); |
1007 |
time += delay; |
1013 |
time += delay; |
1008 |
if (time > DELETE_MAX_TIME) DELETE_MAX_TIME = time; |
1014 |
if (time > DELETE_MAX_TIME) DELETE_MAX_TIME = time; |
1009 |
if (DELETE_DEBUG) System.out.print('.'); |
1015 |
if (DELETE_DEBUG) System.out.print('.'); |
1010 |
if (file.exists()) { |
1016 |
if (file.exists()) { |
1011 |
if (file.delete()) { |
1017 |
if (file.delete()) { |
1012 |
// SUCCESS |
1018 |
// SUCCESS |
1013 |
if (DELETE_DEBUG) { |
1019 |
if (DELETE_DEBUG) { |
1014 |
System.out.println(); |
1020 |
System.out.println(); |
1015 |
System.out.println(" => file really removed after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1021 |
System.out.println(" => file really removed after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1016 |
System.out.println(); |
1022 |
System.out.println(); |
1017 |
} |
1023 |
} |
1018 |
return true; |
1024 |
return true; |
1019 |
} |
1025 |
} |
1020 |
} |
1026 |
} |
1021 |
if (isFileDeleted(file)) { |
1027 |
if (isFileDeleted(file)) { |
1022 |
// SUCCESS |
1028 |
// SUCCESS |
1023 |
if (DELETE_DEBUG) { |
1029 |
if (DELETE_DEBUG) { |
1024 |
System.out.println(); |
1030 |
System.out.println(); |
1025 |
System.out.println(" => file disappeared after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1031 |
System.out.println(" => file disappeared after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1026 |
System.out.println(); |
1032 |
System.out.println(); |
1027 |
} |
1033 |
} |
1028 |
return true; |
1034 |
return true; |
1029 |
} |
1035 |
} |
1030 |
// Increment waiting delay exponentially |
1036 |
// Increment waiting delay exponentially |
1031 |
if (count >= 10 && delay <= 100) { |
1037 |
if (count >= 10 && delay <= 100) { |
1032 |
count = 1; |
1038 |
count = 1; |
1033 |
delay *= 10; |
1039 |
delay *= 10; |
1034 |
maxRetry = DELETE_MAX_WAIT / delay; |
1040 |
maxRetry = DELETE_MAX_WAIT / delay; |
1035 |
if ((DELETE_MAX_WAIT%delay) != 0) { |
1041 |
if ((DELETE_MAX_WAIT%delay) != 0) { |
1036 |
maxRetry++; |
1042 |
maxRetry++; |
1037 |
} |
1043 |
} |
1038 |
} |
1044 |
} |
1039 |
} |
1045 |
} |
1040 |
catch (InterruptedException ie) { |
1046 |
catch (InterruptedException ie) { |
1041 |
break; // end loop |
1047 |
break; // end loop |
1042 |
} |
1048 |
} |
1043 |
} |
1049 |
} |
1044 |
if (!DELETE_DEBUG) { |
1050 |
if (!DELETE_DEBUG) { |
1045 |
System.out.println(); |
1051 |
System.out.println(); |
1046 |
System.out.println("WARNING in test: "+getTestName()); |
1052 |
System.out.println("WARNING in test: "+getTestName()); |
1047 |
System.out.println(" - problems occured while deleting "+file); |
1053 |
System.out.println(" - problems occured while deleting "+file); |
1048 |
printJdtCoreStackTrace(null, 1); |
1054 |
printJdtCoreStackTrace(null, 1); |
1049 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1055 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1050 |
} |
1056 |
} |
1051 |
System.out.println(); |
1057 |
System.out.println(); |
1052 |
System.out.println(" !!! ERROR: "+file+" was never deleted even after having waited "+DELETE_MAX_TIME+"ms!!!"); |
1058 |
System.out.println(" !!! ERROR: "+file+" was never deleted even after having waited "+DELETE_MAX_TIME+"ms!!!"); |
1053 |
System.out.println(); |
1059 |
System.out.println(); |
1054 |
return false; |
1060 |
return false; |
1055 |
} |
1061 |
} |
1056 |
/** |
1062 |
/** |
1057 |
* Wait until a resource is _really_ deleted on file system. |
1063 |
* Wait until a resource is _really_ deleted on file system. |
1058 |
* |
1064 |
* |
1059 |
* @param resource Deleted resource |
1065 |
* @param resource Deleted resource |
1060 |
* @return true if the file was finally deleted, false otherwise |
1066 |
* @return true if the file was finally deleted, false otherwise |
1061 |
*/ |
1067 |
*/ |
1062 |
private static boolean waitUntilResourceDeleted(IResource resource) { |
1068 |
private static boolean waitUntilResourceDeleted(IResource resource) { |
1063 |
File file = resource.getLocation().toFile(); |
1069 |
File file = resource.getLocation().toFile(); |
1064 |
if (DELETE_DEBUG) { |
1070 |
if (DELETE_DEBUG) { |
1065 |
System.out.println(); |
1071 |
System.out.println(); |
1066 |
System.out.println("WARNING in test: "+getTestName()); |
1072 |
System.out.println("WARNING in test: "+getTestName()); |
1067 |
System.out.println(" - problems occured while deleting resource "+resource); |
1073 |
System.out.println(" - problems occured while deleting resource "+resource); |
1068 |
printJdtCoreStackTrace(null, 1); |
1074 |
printJdtCoreStackTrace(null, 1); |
1069 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1075 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1070 |
System.out.print(" - wait for ("+DELETE_MAX_WAIT+"ms max): "); |
1076 |
System.out.print(" - wait for ("+DELETE_MAX_WAIT+"ms max): "); |
1071 |
} |
1077 |
} |
1072 |
int count = 0; |
1078 |
int count = 0; |
1073 |
int delay = 10; // ms |
1079 |
int delay = 10; // ms |
1074 |
int maxRetry = DELETE_MAX_WAIT / delay; |
1080 |
int maxRetry = DELETE_MAX_WAIT / delay; |
1075 |
int time = 0; |
1081 |
int time = 0; |
1076 |
while (count < maxRetry) { |
1082 |
while (count < maxRetry) { |
1077 |
try { |
1083 |
try { |
1078 |
count++; |
1084 |
count++; |
1079 |
Thread.sleep(delay); |
1085 |
Thread.sleep(delay); |
1080 |
time += delay; |
1086 |
time += delay; |
1081 |
if (time > DELETE_MAX_TIME) DELETE_MAX_TIME = time; |
1087 |
if (time > DELETE_MAX_TIME) DELETE_MAX_TIME = time; |
1082 |
if (DELETE_DEBUG) System.out.print('.'); |
1088 |
if (DELETE_DEBUG) System.out.print('.'); |
1083 |
if (resource.isAccessible()) { |
1089 |
if (resource.isAccessible()) { |
1084 |
try { |
1090 |
try { |
1085 |
resource.delete(true, null); |
1091 |
resource.delete(true, null); |
1086 |
if (isResourceDeleted(resource) && isFileDeleted(file)) { |
1092 |
if (isResourceDeleted(resource) && isFileDeleted(file)) { |
1087 |
// SUCCESS |
1093 |
// SUCCESS |
1088 |
if (DELETE_DEBUG) { |
1094 |
if (DELETE_DEBUG) { |
1089 |
System.out.println(); |
1095 |
System.out.println(); |
1090 |
System.out.println(" => resource really removed after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1096 |
System.out.println(" => resource really removed after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1091 |
System.out.println(); |
1097 |
System.out.println(); |
1092 |
} |
1098 |
} |
1093 |
return true; |
1099 |
return true; |
1094 |
} |
1100 |
} |
1095 |
} |
1101 |
} |
1096 |
catch (CoreException e) { |
1102 |
catch (CoreException e) { |
1097 |
// skip |
1103 |
// skip |
1098 |
} |
1104 |
} |
1099 |
} |
1105 |
} |
1100 |
if (isResourceDeleted(resource) && isFileDeleted(file)) { |
1106 |
if (isResourceDeleted(resource) && isFileDeleted(file)) { |
1101 |
// SUCCESS |
1107 |
// SUCCESS |
1102 |
if (DELETE_DEBUG) { |
1108 |
if (DELETE_DEBUG) { |
1103 |
System.out.println(); |
1109 |
System.out.println(); |
1104 |
System.out.println(" => resource disappeared after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1110 |
System.out.println(" => resource disappeared after "+time+"ms (max="+DELETE_MAX_TIME+"ms)"); |
1105 |
System.out.println(); |
1111 |
System.out.println(); |
1106 |
} |
1112 |
} |
1107 |
return true; |
1113 |
return true; |
1108 |
} |
1114 |
} |
1109 |
// Increment waiting delay exponentially |
1115 |
// Increment waiting delay exponentially |
1110 |
if (count >= 10 && delay <= 100) { |
1116 |
if (count >= 10 && delay <= 100) { |
1111 |
count = 1; |
1117 |
count = 1; |
1112 |
delay *= 10; |
1118 |
delay *= 10; |
1113 |
maxRetry = DELETE_MAX_WAIT / delay; |
1119 |
maxRetry = DELETE_MAX_WAIT / delay; |
1114 |
if ((DELETE_MAX_WAIT%delay) != 0) { |
1120 |
if ((DELETE_MAX_WAIT%delay) != 0) { |
1115 |
maxRetry++; |
1121 |
maxRetry++; |
1116 |
} |
1122 |
} |
1117 |
} |
1123 |
} |
1118 |
} |
1124 |
} |
1119 |
catch (InterruptedException ie) { |
1125 |
catch (InterruptedException ie) { |
1120 |
break; // end loop |
1126 |
break; // end loop |
1121 |
} |
1127 |
} |
1122 |
} |
1128 |
} |
1123 |
if (!DELETE_DEBUG) { |
1129 |
if (!DELETE_DEBUG) { |
1124 |
System.out.println(); |
1130 |
System.out.println(); |
1125 |
System.out.println("WARNING in test: "+getTestName()); |
1131 |
System.out.println("WARNING in test: "+getTestName()); |
1126 |
System.out.println(" - problems occured while deleting resource "+resource); |
1132 |
System.out.println(" - problems occured while deleting resource "+resource); |
1127 |
printJdtCoreStackTrace(null, 1); |
1133 |
printJdtCoreStackTrace(null, 1); |
1128 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1134 |
printFileInfo(file.getParentFile(), 1, -1); // display parent with its children |
1129 |
} |
1135 |
} |
1130 |
System.out.println(); |
1136 |
System.out.println(); |
1131 |
System.out.println(" !!! ERROR: "+resource+" was never deleted even after having waited "+DELETE_MAX_TIME+"ms!!!"); |
1137 |
System.out.println(" !!! ERROR: "+resource+" was never deleted even after having waited "+DELETE_MAX_TIME+"ms!!!"); |
1132 |
System.out.println(); |
1138 |
System.out.println(); |
1133 |
return false; |
1139 |
return false; |
1134 |
} |
1140 |
} |
1135 |
public static void writeToFile(String contents, String destinationFilePath) { |
1141 |
public static void writeToFile(String contents, String destinationFilePath) { |
1136 |
File destFile = new File(destinationFilePath); |
1142 |
File destFile = new File(destinationFilePath); |
1137 |
FileOutputStream output = null; |
1143 |
FileOutputStream output = null; |
1138 |
try { |
1144 |
try { |
1139 |
output = new FileOutputStream(destFile); |
1145 |
output = new FileOutputStream(destFile); |
1140 |
PrintWriter writer = new PrintWriter(output); |
1146 |
PrintWriter writer = new PrintWriter(output); |
1141 |
writer.print(contents); |
1147 |
writer.print(contents); |
1142 |
writer.flush(); |
1148 |
writer.flush(); |
1143 |
} catch (IOException e) { |
1149 |
} catch (IOException e) { |
1144 |
e.printStackTrace(); |
1150 |
e.printStackTrace(); |
1145 |
return; |
1151 |
return; |
1146 |
} finally { |
1152 |
} finally { |
1147 |
if (output != null) { |
1153 |
if (output != null) { |
1148 |
try { |
1154 |
try { |
1149 |
output.close(); |
1155 |
output.close(); |
1150 |
} catch (IOException e2) { |
1156 |
} catch (IOException e2) { |
1151 |
} |
1157 |
} |
1152 |
} |
1158 |
} |
1153 |
} |
1159 |
} |
1154 |
} |
1160 |
} |
1155 |
public static void zip(File rootDir, String zipPath) throws IOException { |
1161 |
public static void zip(File rootDir, String zipPath) throws IOException { |
1156 |
ZipOutputStream zip = null; |
1162 |
ZipOutputStream zip = null; |
1157 |
try { |
1163 |
try { |
1158 |
File zipFile = new File(zipPath); |
1164 |
File zipFile = new File(zipPath); |
1159 |
if (zipFile.exists()) delete(zipFile); |
1165 |
if (zipFile.exists()) delete(zipFile); |
1160 |
zip = new ZipOutputStream(new FileOutputStream(zipFile)); |
1166 |
zip = new ZipOutputStream(new FileOutputStream(zipFile)); |
1161 |
zip(rootDir, zip, rootDir.getPath().length()+1); // 1 for last slash |
1167 |
zip(rootDir, zip, rootDir.getPath().length()+1); // 1 for last slash |
1162 |
} finally { |
1168 |
} finally { |
1163 |
if (zip != null) { |
1169 |
if (zip != null) { |
1164 |
zip.close(); |
1170 |
zip.close(); |
1165 |
} |
1171 |
} |
1166 |
} |
1172 |
} |
1167 |
} |
1173 |
} |
1168 |
private static void zip(File dir, ZipOutputStream zip, int rootPathLength) throws IOException { |
1174 |
private static void zip(File dir, ZipOutputStream zip, int rootPathLength) throws IOException { |
1169 |
File[] files = dir.listFiles(); |
1175 |
File[] files = dir.listFiles(); |
1170 |
if (files != null) { |
1176 |
if (files != null) { |
1171 |
for (int i = 0, length = files.length; i < length; i++) { |
1177 |
for (int i = 0, length = files.length; i < length; i++) { |
1172 |
File file = files[i]; |
1178 |
File file = files[i]; |
1173 |
if (file.isFile()) { |
1179 |
if (file.isFile()) { |
1174 |
String path = file.getPath(); |
1180 |
String path = file.getPath(); |
1175 |
path = path.substring(rootPathLength); |
1181 |
path = path.substring(rootPathLength); |
1176 |
ZipEntry entry = new ZipEntry(path.replace('\\', '/')); |
1182 |
ZipEntry entry = new ZipEntry(path.replace('\\', '/')); |
1177 |
zip.putNextEntry(entry); |
1183 |
zip.putNextEntry(entry); |
1178 |
zip.write(org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file)); |
1184 |
zip.write(org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file)); |
1179 |
zip.closeEntry(); |
1185 |
zip.closeEntry(); |
1180 |
} else { |
1186 |
} else { |
1181 |
zip(file, zip, rootPathLength); |
1187 |
zip(file, zip, rootPathLength); |
1182 |
} |
1188 |
} |
1183 |
} |
1189 |
} |
1184 |
} |
1190 |
} |
1185 |
} |
1191 |
} |
1186 |
} |
1192 |
} |