001: /*******************************************************************************
002: * Copyright (c) 2007 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * wharley@bea.com - initial API and implementation
010: *
011: *******************************************************************************/package org.eclipse.jdt.apt.pluggable.tests;
012:
013: import java.io.File;
014: import java.io.FileOutputStream;
015: import java.io.IOException;
016: import java.net.URL;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.runtime.FileLocator;
021: import org.eclipse.core.runtime.Platform;
022:
023: /**
024: * Helper class to support compilation and results checking for tests running in batch mode.
025: * @since 3.3.1
026: */
027: public class IdeTestUtils {
028: /**
029: * Name of the subdirectory within the test bundle where target resources are stored.
030: */
031: public static final String RESOURCES_DIR = "resources";
032:
033: /**
034: * Copy files from a bundle into a project in the target workspace. Newlines will be
035: * converted according to {@link #shouldConvertToIndependentLineDelimiter(File)}.
036: * Directories named "CVS" will be ignored.
037: *
038: * @param proj
039: * the project within which the files will be created.
040: * @param resourceFolderName
041: * the name of the folder within the plug-in that the files will be copied
042: * from, relative to <code>[plugin-root]/resources</code>
043: * @param destFolderName
044: * the name of the folder within the target workspace that the files will
045: * be copied to, relative to the project
046: * @throws Exception
047: * might be an IOException or a CoreException
048: */
049: public static void copyResources(IProject proj,
050: String resourceFolderName, String destFolderName)
051: throws Exception {
052: String destFolderOSName = proj.getFolder(destFolderName)
053: .getLocation().toOSString(); //$NON-NLS-1$
054: File destFolder = new File(destFolderOSName);
055: File resourceFolder = TestUtils.concatPath(
056: getPluginDirectoryPath(), RESOURCES_DIR,
057: resourceFolderName);
058: copyResources(resourceFolder, destFolder);
059: proj.refreshLocal(IResource.DEPTH_INFINITE, null);
060: }
061:
062: /**
063: * @return the absolute filesystem-based path of the root of the bundle filesystem.
064: * This will cause the bundle to be extracted to a temporary directory on the filesystem
065: * if necessary; see {@link FileLocator#toFileURL(URL)}.
066: */
067: public static String getPluginDirectoryPath() {
068: try {
069: URL platformURL = Platform.getBundle(
070: "org.eclipse.jdt.apt.pluggable.tests")
071: .getEntry("/");
072: return new File(FileLocator.toFileURL(platformURL)
073: .getFile()).getAbsolutePath();
074: } catch (IOException e) {
075: e.printStackTrace();
076: }
077: return null;
078: }
079:
080: /**
081: * Copy a file from one location to another, unless the destination file already exists and has
082: * the same timestamp and file size. Create the destination location if necessary. Convert line
083: * delimiters according to {@link #shouldConvertToIndependentLineDelimiter(File)}.
084: *
085: * @param src
086: * the full path to the resource location.
087: * @param destFolder
088: * the full path to the destination location.
089: * @throws IOException
090: */
091: private static void copyResource(File src, File dest)
092: throws IOException {
093: if (dest.exists() && src.lastModified() < dest.lastModified()
094: && src.length() == dest.length()) {
095: return;
096: }
097:
098: // read source bytes
099: byte[] srcBytes = null;
100: srcBytes = read(src);
101:
102: if (shouldConvertToIndependentLineDelimiter(src)) {
103: String contents = new String(srcBytes);
104: contents = TestUtils
105: .convertToIndependentLineDelimiter(contents);
106: srcBytes = contents.getBytes();
107: }
108:
109: File destFolder = dest.getParentFile();
110: if (!destFolder.exists()) {
111: if (!destFolder.mkdirs()) {
112: throw new IOException("Unable to create directory "
113: + destFolder);
114: }
115: }
116: // write bytes to dest
117: FileOutputStream out = null;
118: try {
119: out = new FileOutputStream(dest);
120: out.write(srcBytes);
121: out.flush();
122: } finally {
123: if (out != null) {
124: out.close();
125: }
126: }
127: }
128:
129: private static void copyResources(File resourceFolder,
130: File destFolder) throws IOException {
131: if (resourceFolder == null) {
132: return;
133: }
134: // Copy all resources in this folder
135: String[] children = resourceFolder.list();
136: if (null == children) {
137: return;
138: }
139: // if there are any children, (recursively) copy them
140: for (String child : children) {
141: if ("CVS".equals(child)) {
142: continue;
143: }
144: File childRes = new File(resourceFolder, child);
145: File childDest = new File(destFolder, child);
146: if (childRes.isDirectory()) {
147: copyResources(childRes, childDest);
148: } else {
149: copyResource(childRes, childDest);
150: }
151: }
152: }
153:
154: private static byte[] read(java.io.File file)
155: throws java.io.IOException {
156: int fileLength;
157: byte[] fileBytes = new byte[fileLength = (int) file.length()];
158: java.io.FileInputStream stream = null;
159: try {
160: stream = new java.io.FileInputStream(file);
161: int bytesRead = 0;
162: int lastReadSize = 0;
163: while ((lastReadSize != -1) && (bytesRead != fileLength)) {
164: lastReadSize = stream.read(fileBytes, bytesRead,
165: fileLength - bytesRead);
166: bytesRead += lastReadSize;
167: }
168: } finally {
169: if (stream != null) {
170: stream.close();
171: }
172: }
173: return fileBytes;
174: }
175:
176: /**
177: * @return true if this file's end-of-line delimiters should be replaced with
178: * a platform-independent value, e.g. for compilation.
179: */
180: private static boolean shouldConvertToIndependentLineDelimiter(
181: File file) {
182: return file.getName().endsWith(".java");
183: }
184:
185: }
|