| /******************************************************************************* |
| * Copyright (c) 2005, 2007 Oracle. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0, which accompanies this distribution |
| * and is available at http://www.eclipse.org/legal/epl-v10.html. |
| * |
| * Contributors: |
| * Oracle - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.jpt.core.tests.internal.projects; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.InputStream; |
| 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.IWorkspaceDescription; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.core.runtime.jobs.Job; |
| import org.eclipse.jpt.utility.internal.CollectionTools; |
| |
| /** |
| * This builds and holds a "general" project. |
| * Support for adding natures, folders, and files. |
| */ |
| public class TestPlatformProject { |
| |
| private final IProject project; |
| |
| /** carriage return */ |
| public static final String CR = System.getProperty("line.separator"); |
| |
| |
| // ********** constructors/initialization ********** |
| |
| public TestPlatformProject() throws CoreException { |
| this(false); |
| } |
| |
| public TestPlatformProject(boolean autoBuild) throws CoreException { |
| this("TestProject", autoBuild); |
| } |
| |
| public TestPlatformProject(String projectName, boolean autoBuild) throws CoreException { |
| super(); |
| this.setAutoBuild(autoBuild); // workspace-wide setting |
| this.project = this.createPlatformProject(projectName); |
| } |
| |
| private void setAutoBuild(boolean autoBuild) throws CoreException { |
| IWorkspaceDescription description = ResourcesPlugin.getWorkspace().getDescription(); |
| description.setAutoBuilding(autoBuild); |
| ResourcesPlugin.getWorkspace().setDescription(description); |
| } |
| |
| private IProject createPlatformProject(String projectName) throws CoreException { |
| IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
| p.create(null); |
| p.open(null); |
| return p; |
| } |
| |
| |
| // ********** public methods ********** |
| |
| public IProject getProject() { |
| return this.project; |
| } |
| |
| public void addProjectNature(String natureID) throws CoreException { |
| IProjectDescription description = this.project.getDescription(); |
| description.setNatureIds((String[]) CollectionTools.add(description.getNatureIds(), natureID)); |
| this.project.setDescription(description, null); |
| } |
| |
| public void removeProjectNature(String natureID) throws CoreException { |
| IProjectDescription description = this.project.getDescription(); |
| description.setNatureIds((String[]) CollectionTools.removeAllOccurrences(description.getNatureIds(), natureID)); |
| this.project.setDescription(description, null); |
| } |
| |
| /** |
| * Create a folder with the specified name directly under the project. |
| */ |
| public IFolder createFolder(String folderName) throws CoreException { |
| return this.createFolder(this.project, new Path(folderName)); |
| } |
| |
| /** |
| * Create a folder in the specified container with the specified name. |
| */ |
| public IFolder createFolder(IContainer container, String folderName) throws CoreException { |
| return this.createFolder(container, new Path(folderName)); |
| } |
| |
| /** |
| * Create a folder in the specified container with the specified path. |
| */ |
| public IFolder createFolder(IContainer container, IPath folderPath) throws CoreException { |
| IFolder folder = container.getFolder(folderPath); |
| if ( ! folder.exists()) { |
| folder.create(false, true, null); // false = "no force"; true = "local" |
| } |
| return folder; |
| } |
| |
| /** |
| * Create a file with the specified name and content directly under the project. |
| */ |
| public IFile createFile(String fileName, String content) throws CoreException { |
| return this.createFile(this.project, fileName, content); |
| } |
| |
| /** |
| * Create a file in the specified container with the specified name and content. |
| */ |
| public IFile createFile(IContainer container, String fileName, String content) throws CoreException { |
| return createFile(container, new Path(fileName), content); |
| } |
| |
| /** |
| * Create a file in the project with the specified [relative] path |
| * and content. |
| */ |
| public IFile createFile(IPath filePath, String content) throws CoreException { |
| return this.createFile(this.project, filePath, content); |
| } |
| |
| /** |
| * Create a file in the specified container with the specified path and content. |
| */ |
| public IFile createFile(IContainer container, IPath filePath, String content) throws CoreException { |
| return this.createFile(container, filePath, new ByteArrayInputStream(content.getBytes())); |
| } |
| |
| /** |
| * Create a file in the specified container with the specified path and contents. |
| */ |
| public IFile createFile(IContainer container, IPath filePath, InputStream content) throws CoreException { |
| int len = filePath.segmentCount() - 1; |
| for (int i = 0; i < len; i++) { |
| container = container.getFolder(new Path(filePath.segment(i))); |
| if ( ! container.exists()) { |
| ((IFolder) container).create(true, true, null); // true = "force"; true = "local" |
| } |
| } |
| |
| IFile file = container.getFile(new Path(filePath.lastSegment())); |
| if (file.exists()) { |
| file.delete(true, null); // true = "force" |
| } |
| file.create(content, true, null); // true = "force" |
| return file; |
| } |
| |
| public void open() throws CoreException { |
| this.project.open(null); |
| waitForJobs(); |
| } |
| |
| public void close() throws CoreException { |
| this.project.close(null); |
| waitForJobs(); |
| } |
| |
| public void dispose() throws CoreException { |
| this.project.delete(true, true, null); // true = "delete content"; true = "force" |
| for (int i = 1; this.project.exists(); i++) { |
| waitForJobs(); |
| System.out.println("Project still exists: " + i); |
| } |
| } |
| |
| |
| // ********** static methods ********** |
| |
| /** |
| * Wait until all background tasks are complete. |
| */ |
| public static void waitForJobs() { |
| while (Job.getJobManager().currentJob() != null) { |
| try { |
| Thread.sleep(100); // let other threads get something done |
| } catch (InterruptedException ex) { |
| throw new RuntimeException(ex); |
| } |
| } |
| } |
| |
| } |