1 /*******************************************************************************
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *******************************************************************************/
10 package org
.eclipse
.egit
.core
.test
;
14 import org
.eclipse
.core
.resources
.IFolder
;
15 import org
.eclipse
.core
.resources
.IProject
;
16 import org
.eclipse
.core
.resources
.IProjectDescription
;
17 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
18 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.core
.runtime
.IPath
;
21 import org
.eclipse
.core
.runtime
.Path
;
22 import org
.eclipse
.core
.runtime
.Platform
;
23 import org
.eclipse
.jdt
.core
.IClasspathEntry
;
24 import org
.eclipse
.jdt
.core
.ICompilationUnit
;
25 import org
.eclipse
.jdt
.core
.IJavaProject
;
26 import org
.eclipse
.jdt
.core
.IPackageFragment
;
27 import org
.eclipse
.jdt
.core
.IPackageFragmentRoot
;
28 import org
.eclipse
.jdt
.core
.IType
;
29 import org
.eclipse
.jdt
.core
.JavaCore
;
30 import org
.eclipse
.jdt
.core
.JavaModelException
;
31 import org
.eclipse
.jdt
.launching
.JavaRuntime
;
32 import org
.osgi
.framework
.Bundle
;
34 public class TestProject
{
35 public IProject project
;
37 public IJavaProject javaProject
;
39 private IPackageFragmentRoot sourceFolder
;
42 * @throws CoreException
43 * If project already exists
45 public TestProject() throws CoreException
{
51 * should project be removed if already exists
52 * @throws CoreException
54 public TestProject(final boolean remove
) throws CoreException
{
55 IWorkspaceRoot root
= ResourcesPlugin
.getWorkspace().getRoot();
56 project
= root
.getProject("Project-1");
58 project
.delete(true, null);
61 javaProject
= JavaCore
.create(project
);
62 IFolder binFolder
= createBinFolder();
64 javaProject
.setRawClasspath(new IClasspathEntry
[0], null);
65 createOutputFolder(binFolder
);
69 public IProject
getProject() {
73 public IJavaProject
getJavaProject() {
77 public void addJar(String plugin
, String jar
) throws JavaModelException
{
78 Path result
= findFileInPlugin(plugin
, jar
);
79 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
80 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
81 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
82 newEntries
[oldEntries
.length
] = JavaCore
.newLibraryEntry(result
, null,
84 javaProject
.setRawClasspath(newEntries
, null);
87 public IPackageFragment
createPackage(String name
) throws CoreException
{
88 if (sourceFolder
== null)
89 sourceFolder
= createSourceFolder();
90 return sourceFolder
.createPackageFragment(name
, false, null);
93 public IType
createType(IPackageFragment pack
, String cuName
, String source
)
94 throws JavaModelException
{
95 StringBuffer buf
= new StringBuffer();
96 buf
.append("package " + pack
.getElementName() + ";\n");
99 ICompilationUnit cu
= pack
.createCompilationUnit(cuName
,
100 buf
.toString(), false, null);
101 return cu
.getTypes()[0];
104 public void dispose() throws CoreException
{
106 project
.delete(true, true, null);
109 private IFolder
createBinFolder() throws CoreException
{
110 IFolder binFolder
= project
.getFolder("bin");
111 binFolder
.create(false, true, null);
115 private void setJavaNature() throws CoreException
{
116 IProjectDescription description
= project
.getDescription();
117 description
.setNatureIds(new String
[] { JavaCore
.NATURE_ID
});
118 project
.setDescription(description
, null);
121 private void createOutputFolder(IFolder binFolder
)
122 throws JavaModelException
{
123 IPath outputLocation
= binFolder
.getFullPath();
124 javaProject
.setOutputLocation(outputLocation
, null);
127 public IPackageFragmentRoot
createSourceFolder() throws CoreException
{
128 IFolder folder
= project
.getFolder("src");
129 folder
.create(false, true, null);
130 IPackageFragmentRoot root
= javaProject
.getPackageFragmentRoot(folder
);
131 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
132 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
133 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
134 newEntries
[oldEntries
.length
] = JavaCore
.newSourceEntry(root
.getPath());
135 javaProject
.setRawClasspath(newEntries
, null);
139 private void addSystemLibraries() throws JavaModelException
{
140 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
141 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
142 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
143 newEntries
[oldEntries
.length
] = JavaRuntime
144 .getDefaultJREContainerEntry();
145 javaProject
.setRawClasspath(newEntries
, null);
148 private Path
findFileInPlugin(String plugin
, String file
) {
149 Bundle bundle
= Platform
.getBundle(plugin
);
150 URL resource
= bundle
.getResource(file
);
151 return new Path(resource
.getPath());
154 public void waitForIndexer() {
155 // new SearchEngine().searchAllTypeNames(ResourcesPlugin.getWorkspace(),
156 // null, null, IJavaSearchConstants.EXACT_MATCH,
157 // IJavaSearchConstants.CASE_SENSITIVE,
158 // IJavaSearchConstants.CLASS, SearchEngine
159 // .createJavaSearchScope(new IJavaElement[0]),
160 // new ITypeNameRequestor() {
161 // public void acceptClass(char[] packageName,
162 // char[] simpleTypeName, char[][] enclosingTypeNames,
165 // public void acceptInterface(char[] packageName,
166 // char[] simpleTypeName, char[][] enclosingTypeNames,
169 // }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
173 * @return Returns the sourceFolder.
175 public IPackageFragmentRoot
getSourceFolder() {
180 * @param sourceFolder The sourceFolder to set.
182 public void setSourceFolder(IPackageFragmentRoot sourceFolder
) {
183 this.sourceFolder
= sourceFolder
;