Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/FileTool.java241
-rw-r--r--org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/ResourceHelper.java149
2 files changed, 390 insertions, 0 deletions
diff --git a/org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/FileTool.java b/org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/FileTool.java
new file mode 100644
index 000000000..bb92a26a4
--- /dev/null
+++ b/org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/FileTool.java
@@ -0,0 +1,241 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.core.filebuffers.tests;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Plugin;
+
+
+public class FileTool {
+
+ private final static int MAX_RETRY= 5;
+
+ /**
+ * A buffer.
+ */
+ private static byte[] buffer = new byte[8192];
+
+ /**
+ * Unzips the given zip file to the given destination directory
+ * extracting only those entries the pass through the given
+ * filter.
+ *
+ * @param zipFile the zip file to unzip
+ * @param dstDir the destination directory
+ * @throws IOException in case of problem
+ */
+ public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
+ unzip(zipFile, dstDir, dstDir, 0);
+ }
+
+ private static void unzip(ZipFile zipFile, File rootDstDir, File dstDir, int depth) throws IOException {
+
+ Enumeration entries = zipFile.entries();
+
+ try {
+ while(entries.hasMoreElements()){
+ ZipEntry entry = (ZipEntry)entries.nextElement();
+ if(entry.isDirectory()){
+ continue;
+ }
+ String entryName = entry.getName();
+ File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
+ file.getParentFile().mkdirs();
+ InputStream src = null;
+ OutputStream dst = null;
+ try {
+ src = zipFile.getInputStream(entry);
+ dst = new FileOutputStream(file);
+ transferData(src, dst);
+ } finally {
+ if(dst != null){
+ try {
+ dst.close();
+ } catch(IOException e){
+ }
+ }
+ if(src != null){
+ try {
+ src.close();
+ } catch(IOException e){
+ }
+ }
+ }
+ }
+ } finally {
+ try {
+ zipFile.close();
+ } catch(IOException e){
+ }
+ }
+ }
+
+ /**
+ * Returns the given file path with its separator
+ * character changed from the given old separator to the
+ * given new separator.
+ *
+ * @param path a file path
+ * @param oldSeparator a path separator character
+ * @param newSeparator a path separator character
+ * @return the file path with its separator character
+ * changed from the given old separator to the given new
+ * separator
+ */
+ public static String changeSeparator(String path, char oldSeparator, char newSeparator){
+ return path.replace(oldSeparator, newSeparator);
+ }
+
+ /**
+ * Copies all bytes in the given source file to
+ * the given destination file.
+ *
+ * @param source the given source file
+ * @param destination the given destination file
+ * @throws IOException in case of error
+ */
+ public static void transferData(File source, File destination) throws IOException {
+ destination.getParentFile().mkdirs();
+ InputStream is = null;
+ OutputStream os = null;
+ try {
+ is = new FileInputStream(source);
+ os = new FileOutputStream(destination);
+ transferData(is, os);
+ } finally {
+ if(os != null)
+ os.close();
+ if(is != null)
+ is.close();
+ }
+ }
+
+ /**
+ * Copies all bytes in the given source stream to
+ * the given destination stream. Neither streams
+ * are closed.
+ *
+ * @param source the given source stream
+ * @param destination the given destination stream
+ * @throws IOException in case of error
+ */
+ public static void transferData(InputStream source, OutputStream destination) throws IOException {
+ int bytesRead = 0;
+ while(bytesRead != -1){
+ bytesRead = source.read(buffer, 0, buffer.length);
+ if(bytesRead != -1){
+ destination.write(buffer, 0, bytesRead);
+ }
+ }
+ }
+
+ /**
+ * Copies the given source file to the given destination file.
+ *
+ * @param src the given source file
+ * @param dst the given destination file
+ * @throws IOException in case of error
+ */
+ public static void copy(File src, File dst) throws IOException {
+ if(src.isDirectory()){
+ String[] srcChildren = src.list();
+ for(int i = 0; i < srcChildren.length; ++i){
+ File srcChild= new File(src, srcChildren[i]);
+ File dstChild= new File(dst, srcChildren[i]);
+ copy(srcChild, dstChild);
+ }
+ } else
+ transferData(src, dst);
+ }
+
+ public static File getFileInPlugin(Plugin plugin, IPath path) {
+ try {
+ URL installURL= plugin.getBundle().getEntry(path.toString());
+ URL localURL= Platform.asLocalURL(installURL);
+ return new File(localURL.getFile());
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ public static File createTempFileInPlugin(Plugin plugin, IPath path) {
+ IPath stateLocation= plugin.getStateLocation();
+ stateLocation= stateLocation.append(path);
+ return stateLocation.toFile();
+ }
+
+ public static StringBuffer read(String fileName) throws IOException {
+ return read(new FileReader(fileName));
+ }
+
+ public static StringBuffer read(Reader reader) throws IOException {
+ StringBuffer s= new StringBuffer();
+ try {
+ char[] charBuffer= new char[8196];
+ int chars= reader.read(charBuffer);
+ while (chars != -1) {
+ s.append(charBuffer, 0, chars);
+ chars= reader.read(charBuffer);
+ }
+ } finally {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ }
+ }
+ return s;
+ }
+
+ public static void write(String fileName, StringBuffer content) throws IOException {
+ Writer writer= new FileWriter(fileName);
+ try {
+ writer.write(content.toString());
+ } finally {
+ try {
+ writer.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ public static void delete(File file) throws CoreException {
+ if (file.exists()) {
+ for (int i= 0; i < MAX_RETRY; i++) {
+ if (file.delete())
+ i= MAX_RETRY;
+ else {
+ try {
+ Thread.sleep(1000); // sleep a second
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/ResourceHelper.java b/org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/ResourceHelper.java
new file mode 100644
index 000000000..ffc7f1cbb
--- /dev/null
+++ b/org.eclipse.core.filebuffers.tests/src/org/eclipse/core/filebuffers/tests/ResourceHelper.java
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.core.filebuffers.tests;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.StringBufferInputStream;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+
+import org.eclipse.core.filebuffers.manipulation.ContainerCreator;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Plugin;
+
+/**
+ * @since 3.0
+ */
+public class ResourceHelper {
+
+ private final static IProgressMonitor NULL_MONITOR= new NullProgressMonitor();
+ private static final int MAX_RETRY= 5;
+
+ public static IProject createProject(String projectName) throws CoreException {
+
+ IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
+ IProject project= root.getProject(projectName);
+ if (!project.exists())
+ project.create(NULL_MONITOR);
+ else
+ project.refreshLocal(IResource.DEPTH_INFINITE, null);
+
+ if (!project.isOpen())
+ project.open(NULL_MONITOR);
+
+ return project;
+ }
+
+ public static void deleteProject(String projectName) throws CoreException {
+ IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
+ IProject project= root.getProject(projectName);
+ if (project.exists())
+ delete(project);
+ }
+
+ public static void delete(final IProject project) throws CoreException {
+ delete(project, true);
+ }
+
+ public static void delete(final IProject project, boolean deleteContent) throws CoreException {
+ for (int i= 0; i < MAX_RETRY; i++) {
+ try {
+ project.delete(deleteContent, true, NULL_MONITOR);
+ i= MAX_RETRY;
+ } catch (CoreException x) {
+ if (i == MAX_RETRY - 1) {
+ FileBuffersTestPlugin.getDefault().getLog().log(x.getStatus());
+// throw x;
+ }
+ try {
+ Thread.sleep(1000); // sleep a second
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+ }
+
+ public static IFolder createFolder(String portableFolderPath) throws CoreException {
+ ContainerCreator creator= new ContainerCreator(ResourcesPlugin.getWorkspace(), new Path(portableFolderPath));
+ IContainer container= creator.createContainer(NULL_MONITOR);
+ if (container instanceof IFolder)
+ return (IFolder) container;
+ return null;
+ }
+
+ public static IFile createFile(IFolder folder, String name, String contents) throws CoreException {
+ IFile file= folder.getFile(name);
+ if (contents == null)
+ contents= "";
+ InputStream inputStream= new StringBufferInputStream(contents);
+ file.create(inputStream, true, NULL_MONITOR);
+ return file;
+ }
+
+ public static IFile createLinkedFile(IContainer container, IPath linkPath, File linkedFileTarget) throws CoreException {
+ IFile iFile= container.getFile(linkPath);
+ iFile.createLink(new Path(linkedFileTarget.getAbsolutePath()), IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR);
+ return iFile;
+ }
+
+ public static IFile createLinkedFile(IContainer container, IPath linkPath, Plugin plugin, IPath linkedFileTargetPath) throws CoreException {
+ File file= FileTool.getFileInPlugin(plugin, linkedFileTargetPath);
+ IFile iFile= container.getFile(linkPath);
+ iFile.createLink(new Path(file.getAbsolutePath()), IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR);
+ return iFile;
+ }
+
+ public static IFolder createLinkedFolder(IContainer container, IPath linkPath, File linkedFolderTarget) throws CoreException {
+ IFolder folder= container.getFolder(linkPath);
+ folder.createLink(new Path(linkedFolderTarget.getAbsolutePath()), IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR);
+ return folder;
+ }
+
+ public static IFolder createLinkedFolder(IContainer container, IPath linkPath, Plugin plugin, IPath linkedFolderTargetPath) throws CoreException {
+ File file= FileTool.getFileInPlugin(plugin, linkedFolderTargetPath);
+ IFolder iFolder= container.getFolder(linkPath);
+ iFolder.createLink(new Path(file.getAbsolutePath()), IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR);
+ return iFolder;
+ }
+
+ public static IProject createLinkedProject(String projectName, Plugin plugin, IPath linkPath) throws CoreException {
+ IWorkspace workspace= ResourcesPlugin.getWorkspace();
+ IProject project= workspace.getRoot().getProject(projectName);
+
+ IProjectDescription desc= workspace.newProjectDescription(projectName);
+ File file= FileTool.getFileInPlugin(plugin, linkPath);
+ IPath projectLocation= new Path(file.getAbsolutePath());
+ if (Platform.getLocation().equals(projectLocation))
+ projectLocation= null;
+ desc.setLocation(projectLocation);
+
+ project.create(desc, NULL_MONITOR);
+ if (!project.isOpen())
+ project.open(NULL_MONITOR);
+
+ return project;
+ }
+}

Back to the top