1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
14 import java.net.ServerSocket;
15 import java.util.Locale;
17 import java.util.zip.ZipEntry;
18 import java.util.zip.ZipOutputStream;
20 // import net.sourceforge.phpdt.core.tests.compiler.regression.Requestor;
21 import net.sourceforge.phpdt.internal.compiler.Compiler;
22 import net.sourceforge.phpdt.internal.compiler.IErrorHandlingPolicy;
23 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
24 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
25 // import net.sourceforge.phpdt.internal.compiler.batch.FileSystem;
26 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
27 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
30 public static String OUTPUT_DIRECTORY = "comptest";
32 public static CompilationUnit[] compilationUnits(String[] testFiles) {
33 int length = testFiles.length / 2;
34 CompilationUnit[] result = new CompilationUnit[length];
36 for (int i = 0; i < length; i++) {
37 result[i] = new CompilationUnit(testFiles[index + 1].toCharArray(), testFiles[index], null);
43 public static String[] concatWithClassLibs(String classpath, boolean inFront) {
44 String[] classLibs = getJavaClassLibs();
45 final int length = classLibs.length;
46 String[] defaultClassPaths = new String[length + 1];
48 System.arraycopy(classLibs, 0, defaultClassPaths, 1, length);
49 defaultClassPaths[0] = classpath;
51 System.arraycopy(classLibs, 0, defaultClassPaths, 0, length);
52 defaultClassPaths[length] = classpath;
54 return defaultClassPaths;
57 public static String convertToIndependantLineDelimiter(String source) {
58 StringBuffer buffer = new StringBuffer();
59 for (int i = 0, length = source.length(); i < length; i++) {
60 char car = source.charAt(i);
63 if (i < length - 1 && source.charAt(i + 1) == '\n') {
64 i++; // skip \n after \r
70 return buffer.toString();
74 * Copy the given source (a file or a directory that must exists) to the given
75 * destination (a directory that must exists).
77 public static void copy(String sourcePath, String destPath) {
78 sourcePath = toNativePath(sourcePath);
79 destPath = toNativePath(destPath);
80 File source = new File(sourcePath);
83 File dest = new File(destPath);
86 if (source.isDirectory()) {
87 String[] files = source.list();
89 for (int i = 0; i < files.length; i++) {
90 String file = files[i];
91 File sourceFile = new File(source, file);
92 if (sourceFile.isDirectory()) {
93 File destSubDir = new File(dest, file);
95 copy(sourceFile.getPath(), destSubDir.getPath());
97 copy(sourceFile.getPath(), dest.getPath());
102 FileInputStream in = null;
103 FileOutputStream out = null;
105 in = new FileInputStream(source);
106 File destFile = new File(dest, source.getName());
107 if (destFile.exists() && !destFile.delete()) {
108 throw new IOException(destFile + " is in use");
110 out = new FileOutputStream(destFile);
111 int bufferLength = 1024;
112 byte[] buffer = new byte[bufferLength];
115 read = in.read(buffer, 0, bufferLength);
117 out.write(buffer, 0, read);
120 } catch (IOException e) {
121 throw new Error(e.toString());
126 } catch (IOException e) {
132 } catch (IOException e) {
139 // public static void createJar(String[] pathsAndContents, Map options, String
140 // jarPath) throws IOException {
141 // String classesPath = getOutputDirectory() + File.separator + "classes";
142 // File classesDir = new File(classesPath);
143 // flushDirectoryContent(classesDir);
144 // compile(pathsAndContents, options, classesPath);
145 // zip(classesDir, jarPath);
148 * Generate a display string from the given String.
151 * number of tabs are added at the begining of each line.
154 * [org.eclipse.jdt.core.tests.util.Util.displayString("abc\ndef\tghi")]
156 public static String displayString(String inputString) {
157 return displayString(inputString, 0);
161 * Generate a display string from the given String. It converts:
168 * <li>\" to \\\"</li>
170 * <li>\\ to \\\\</li>
171 * <li>All other characters are unchanged.</li>
173 * This method doesn't convert \r\n to \n.
175 * Example of use: <o>
179 * input string = "abc\ndef\tghi",
181 * result = "\"\t\t\tabc\\n" +
182 * "\t\t\tdef\tghi\""
189 * input string = "abc\ndef\tghi\n",
191 * result = "\"\t\t\tabc\\n" +
192 * "\t\t\tdef\tghi\\n\""
199 * input string = "abc\r\ndef\tghi\r\n",
201 * result = "\"\t\t\tabc\\r\\n" +
202 * "\t\t\tdef\tghi\\r\\n\""
210 * the given input string
212 * number of tabs are added at the begining of each line.
214 * @return the displayed string
216 public static String displayString(String inputString, int indent) {
217 int length = inputString.length();
218 StringBuffer buffer = new StringBuffer(length);
219 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(inputString, "\n\r", true);
220 for (int i = 0; i < indent; i++)
223 while (tokenizer.hasMoreTokens()) {
225 String token = tokenizer.nextToken();
226 if (token.equals("\r")) {
227 buffer.append("\\r");
228 if (tokenizer.hasMoreTokens()) {
229 token = tokenizer.nextToken();
230 if (token.equals("\n")) {
231 buffer.append("\\n");
232 if (tokenizer.hasMoreTokens()) {
233 buffer.append("\" + \n");
234 for (int i = 0; i < indent; i++)
240 buffer.append("\" + \n");
241 for (int i = 0; i < indent; i++)
248 } else if (token.equals("\n")) {
249 buffer.append("\\n");
250 if (tokenizer.hasMoreTokens()) {
251 buffer.append("\" + \n");
252 for (int i = 0; i < indent; i++)
259 StringBuffer tokenBuffer = new StringBuffer();
260 for (int i = 0; i < token.length(); i++) {
261 char c = token.charAt(i);
264 tokenBuffer.append("\\r");
267 tokenBuffer.append("\\n");
270 tokenBuffer.append("\\b");
273 tokenBuffer.append("\t");
276 tokenBuffer.append("\\f");
279 tokenBuffer.append("\\\"");
282 tokenBuffer.append("\\'");
285 tokenBuffer.append("\\\\");
288 tokenBuffer.append(c);
291 buffer.append(tokenBuffer.toString());
294 return buffer.toString();
298 * Reads the content of the given source file and converts it to a display
302 * [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java",
305 public static String fileContentToDisplayString(String sourceFilePath, int indent, boolean independantLineDelimiter) {
306 File sourceFile = new File(sourceFilePath);
307 if (!sourceFile.exists()) {
308 System.out.println("File " + sourceFilePath + " does not exists.");
311 if (!sourceFile.isFile()) {
312 System.out.println(sourceFilePath + " is not a file.");
315 StringBuffer sourceContentBuffer = new StringBuffer();
316 FileInputStream input = null;
318 input = new FileInputStream(sourceFile);
319 } catch (FileNotFoundException e) {
327 sourceContentBuffer.append((char) read);
329 } while (read != -1);
331 } catch (IOException e) {
337 } catch (IOException e2) {
340 String sourceString = sourceContentBuffer.toString();
341 if (independantLineDelimiter) {
342 sourceString = convertToIndependantLineDelimiter(sourceString);
344 return displayString(sourceString, indent);
348 * Reads the content of the given source file, converts it to a display
349 * string. If the destination file path is not null, writes the result to this
350 * file. Otherwise writes it to the console.
353 * [org.eclipse.jdt.core.tests.util.Util.fileContentToDisplayString("c:/temp/X.java",
356 public static void fileContentToDisplayString(String sourceFilePath, int indent, String destinationFilePath,
357 boolean independantLineDelimiter) {
358 String displayString = fileContentToDisplayString(sourceFilePath, indent, independantLineDelimiter);
359 if (destinationFilePath == null) {
360 System.out.println(displayString);
363 writeToFile(displayString, destinationFilePath);
367 * Flush content of a given directory (leaving it empty), no-op if not a
370 public static void flushDirectoryContent(File dir) {
371 if (dir.isDirectory()) {
372 String[] files = dir.list();
375 for (int i = 0, max = files.length; i < max; i++) {
376 File current = new File(dir, files[i]);
377 if (current.isDirectory()) {
378 flushDirectoryContent(current);
386 * Search the user hard-drive for a Java class library. Returns null if none
389 * Example of use: [org.eclipse.jdt.core.tests.util.Util.getJavaClassLib()]
391 public static String[] getJavaClassLibs() {
392 String jreDir = getJREDirectory();
393 if (jreDir == null) {
394 return new String[] {};
396 final String vmName = System.getProperty("java.vm.name");
397 if ("J9".equals(vmName)) {
398 return new String[] { toNativePath(jreDir + "/lib/jclMax/classes.zip") };
400 File file = new File(jreDir + "/lib/rt.jar");
402 return new String[] { toNativePath(jreDir + "/lib/rt.jar") };
404 return new String[] { toNativePath(jreDir + "/lib/core.jar"), toNativePath(jreDir + "/lib/security.jar"),
405 toNativePath(jreDir + "/lib/graphics.jar") };
411 public static String getJavaClassLibsAsString() {
412 String[] classLibs = getJavaClassLibs();
413 StringBuffer buffer = new StringBuffer();
414 for (int i = 0, max = classLibs.length; i < max; i++) {
415 buffer.append(classLibs[i]).append(File.pathSeparatorChar);
418 return buffer.toString();
422 * Returns the JRE directory this tests are running on. Returns null if none
425 * Example of use: [org.eclipse.jdt.core.tests.util.Util.getJREDirectory()]
427 public static String getJREDirectory() {
428 return System.getProperty("java.home");
432 * Search the user hard-drive for a possible output directory. Returns null if
433 * none could be found.
435 * Example of use: [org.eclipse.jdt.core.tests.util.Util.getOutputDirectory()]
437 public static String getOutputDirectory() {
438 String container = System.getProperty("user.home");
439 if (container == null) {
442 return toNativePath(container) + File.separator + OUTPUT_DIRECTORY;
447 * Returns the next available port number on the local host.
449 public static int getFreePort() {
450 ServerSocket socket = null;
452 socket = new ServerSocket(0);
453 return socket.getLocalPort();
454 } catch (IOException e) {
457 if (socket != null) {
460 } catch (IOException e) {
469 * Makes the given path a path using native path separators as returned by
470 * File.getPath() and trimming any extra slash.
472 public static String toNativePath(String path) {
473 String nativePath = path.replace('\\', File.separatorChar).replace('/', File.separatorChar);
474 return nativePath.endsWith("/") || nativePath.endsWith("\\") ? nativePath.substring(0, nativePath.length() - 1) : nativePath;
477 public static void writeToFile(String contents, String destinationFilePath) {
478 File destFile = new File(destinationFilePath);
479 FileOutputStream output = null;
481 output = new FileOutputStream(destFile);
482 PrintWriter writer = new PrintWriter(output);
483 writer.print(contents);
485 } catch (IOException e) {
489 if (output != null) {
492 } catch (IOException e2) {