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
;
12 import java
.io
.IOException
;
13 import java
.net
.MalformedURLException
;
16 import org
.eclipse
.core
.resources
.IFolder
;
17 import org
.eclipse
.core
.resources
.IProject
;
18 import org
.eclipse
.core
.resources
.IProjectDescription
;
19 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
20 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IPath
;
23 import org
.eclipse
.core
.runtime
.IPluginDescriptor
;
24 import org
.eclipse
.core
.runtime
.IPluginRegistry
;
25 import org
.eclipse
.core
.runtime
.Path
;
26 import org
.eclipse
.core
.runtime
.Platform
;
27 import org
.eclipse
.jdt
.core
.IClasspathEntry
;
28 import org
.eclipse
.jdt
.core
.ICompilationUnit
;
29 import org
.eclipse
.jdt
.core
.IJavaProject
;
30 import org
.eclipse
.jdt
.core
.IPackageFragment
;
31 import org
.eclipse
.jdt
.core
.IPackageFragmentRoot
;
32 import org
.eclipse
.jdt
.core
.IType
;
33 import org
.eclipse
.jdt
.core
.JavaCore
;
34 import org
.eclipse
.jdt
.core
.JavaModelException
;
35 import org
.eclipse
.jdt
.launching
.JavaRuntime
;
37 public class TestProject
{
38 public IProject project
;
40 public IJavaProject javaProject
;
42 private IPackageFragmentRoot sourceFolder
;
44 public TestProject() throws CoreException
{
45 IWorkspaceRoot root
= ResourcesPlugin
.getWorkspace().getRoot();
46 project
= root
.getProject("Project-1");
49 javaProject
= JavaCore
.create(project
);
50 IFolder binFolder
= createBinFolder();
52 javaProject
.setRawClasspath(new IClasspathEntry
[0], null);
53 createOutputFolder(binFolder
);
57 public IProject
getProject() {
61 public IJavaProject
getJavaProject() {
65 public void addJar(String plugin
, String jar
) throws MalformedURLException
,
66 IOException
, JavaModelException
{
67 Path result
= findFileInPlugin(plugin
, jar
);
68 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
69 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
70 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
71 newEntries
[oldEntries
.length
] = JavaCore
.newLibraryEntry(result
, null,
73 javaProject
.setRawClasspath(newEntries
, null);
76 public IPackageFragment
createPackage(String name
) throws CoreException
{
77 if (sourceFolder
== null)
78 sourceFolder
= createSourceFolder();
79 return sourceFolder
.createPackageFragment(name
, false, null);
82 public IType
createType(IPackageFragment pack
, String cuName
, String source
)
83 throws JavaModelException
{
84 StringBuffer buf
= new StringBuffer();
85 buf
.append("package " + pack
.getElementName() + ";\n");
88 ICompilationUnit cu
= pack
.createCompilationUnit(cuName
,
89 buf
.toString(), false, null);
90 return cu
.getTypes()[0];
93 public void dispose() throws CoreException
{
95 project
.delete(true, true, null);
98 private IFolder
createBinFolder() throws CoreException
{
99 IFolder binFolder
= project
.getFolder("bin");
100 binFolder
.create(false, true, null);
104 private void setJavaNature() throws CoreException
{
105 IProjectDescription description
= project
.getDescription();
106 description
.setNatureIds(new String
[] { JavaCore
.NATURE_ID
});
107 project
.setDescription(description
, null);
110 private void createOutputFolder(IFolder binFolder
)
111 throws JavaModelException
{
112 IPath outputLocation
= binFolder
.getFullPath();
113 javaProject
.setOutputLocation(outputLocation
, null);
116 public IPackageFragmentRoot
createSourceFolder() throws CoreException
{
117 IFolder folder
= project
.getFolder("src");
118 folder
.create(false, true, null);
119 IPackageFragmentRoot root
= javaProject
.getPackageFragmentRoot(folder
);
120 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
121 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
122 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
123 newEntries
[oldEntries
.length
] = JavaCore
.newSourceEntry(root
.getPath());
124 javaProject
.setRawClasspath(newEntries
, null);
128 private void addSystemLibraries() throws JavaModelException
{
129 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
130 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
131 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
132 newEntries
[oldEntries
.length
] = JavaRuntime
133 .getDefaultJREContainerEntry();
134 javaProject
.setRawClasspath(newEntries
, null);
137 private Path
findFileInPlugin(String plugin
, String file
)
138 throws MalformedURLException
, IOException
{
139 IPluginRegistry registry
= Platform
.getPluginRegistry();
140 IPluginDescriptor descriptor
= registry
.getPluginDescriptor(plugin
);
141 URL pluginURL
= descriptor
.getInstallURL();
142 URL jarURL
= new URL(pluginURL
, file
);
143 URL localJarURL
= Platform
.asLocalURL(jarURL
);
144 return new Path(localJarURL
.getPath());
147 public void waitForIndexer() {
148 // new SearchEngine().searchAllTypeNames(ResourcesPlugin.getWorkspace(),
149 // null, null, IJavaSearchConstants.EXACT_MATCH,
150 // IJavaSearchConstants.CASE_SENSITIVE,
151 // IJavaSearchConstants.CLASS, SearchEngine
152 // .createJavaSearchScope(new IJavaElement[0]),
153 // new ITypeNameRequestor() {
154 // public void acceptClass(char[] packageName,
155 // char[] simpleTypeName, char[][] enclosingTypeNames,
158 // public void acceptInterface(char[] packageName,
159 // char[] simpleTypeName, char[][] enclosingTypeNames,
162 // }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
166 * @return Returns the sourceFolder.
168 public IPackageFragmentRoot
getSourceFolder() {
173 * @param sourceFolder The sourceFolder to set.
175 public void setSourceFolder(IPackageFragmentRoot sourceFolder
) {
176 this.sourceFolder
= sourceFolder
;