Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8f6f562a6295be9e37b0cf69978075875538ad9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 2005, 2008 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.jpt.utility.internal.CollectionTools;

/**
 * This builds and holds a "general" Eclipse 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");

	
	// ********** builders **********
	
	public static TestPlatformProject buildPlatformProject(String baseProjectName, boolean autoBuild)
			throws CoreException {
		return new TestPlatformProject(uniqueProjectName(baseProjectName), autoBuild);
	}
	
	public static String uniqueProjectName(String baseProjectName) {
		return baseProjectName + String.valueOf(System.currentTimeMillis()).substring(7);
	}
	
	
	// ********** constructors/initialization **********
	
	public TestPlatformProject(String projectName, boolean autoBuild) throws CoreException {
		super();
		this.setAutoBuild(autoBuild);  // workspace-wide setting
		this.project = this.buildPlatformProject(projectName);
	}

	private void setAutoBuild(boolean autoBuild) throws CoreException {
		IWorkspaceDescription description = ResourcesPlugin.getWorkspace().getDescription();
		description.setAutoBuilding(autoBuild);
		ResourcesPlugin.getWorkspace().setDescription(description);
	}

	private IProject buildPlatformProject(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(CollectionTools.add(description.getNatureIds(), natureID));
		this.project.setDescription(description, null);
	}

	public void removeProjectNature(String natureID) throws CoreException {
		IProjectDescription description = this.project.getDescription();
		description.setNatureIds(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 pathCount = filePath.segmentCount() - 1;
		for (int i = 0; i < pathCount; 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;
	}

}

Back to the top