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 * See LICENSE for the full license text, also available.
8 *******************************************************************************/
9 package org
.spearce
.egit
.core
.test
;
11 import java
.io
.IOException
;
12 import java
.net
.MalformedURLException
;
15 import org
.eclipse
.core
.resources
.IFolder
;
16 import org
.eclipse
.core
.resources
.IProject
;
17 import org
.eclipse
.core
.resources
.IProjectDescription
;
18 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
19 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IPath
;
22 import org
.eclipse
.core
.runtime
.IPluginDescriptor
;
23 import org
.eclipse
.core
.runtime
.IPluginRegistry
;
24 import org
.eclipse
.core
.runtime
.Path
;
25 import org
.eclipse
.core
.runtime
.Platform
;
26 import org
.eclipse
.jdt
.core
.IClasspathEntry
;
27 import org
.eclipse
.jdt
.core
.ICompilationUnit
;
28 import org
.eclipse
.jdt
.core
.IJavaProject
;
29 import org
.eclipse
.jdt
.core
.IPackageFragment
;
30 import org
.eclipse
.jdt
.core
.IPackageFragmentRoot
;
31 import org
.eclipse
.jdt
.core
.IType
;
32 import org
.eclipse
.jdt
.core
.JavaCore
;
33 import org
.eclipse
.jdt
.core
.JavaModelException
;
34 import org
.eclipse
.jdt
.launching
.JavaRuntime
;
36 public class TestProject
{
37 public IProject project
;
39 public IJavaProject javaProject
;
41 private IPackageFragmentRoot sourceFolder
;
43 public TestProject() throws CoreException
{
44 IWorkspaceRoot root
= ResourcesPlugin
.getWorkspace().getRoot();
45 project
= root
.getProject("Project-1");
48 javaProject
= JavaCore
.create(project
);
49 IFolder binFolder
= createBinFolder();
51 javaProject
.setRawClasspath(new IClasspathEntry
[0], null);
52 createOutputFolder(binFolder
);
56 public IProject
getProject() {
60 public IJavaProject
getJavaProject() {
64 public void addJar(String plugin
, String jar
) throws MalformedURLException
,
65 IOException
, JavaModelException
{
66 Path result
= findFileInPlugin(plugin
, jar
);
67 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
68 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
69 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
70 newEntries
[oldEntries
.length
] = JavaCore
.newLibraryEntry(result
, null,
72 javaProject
.setRawClasspath(newEntries
, null);
75 public IPackageFragment
createPackage(String name
) throws CoreException
{
76 if (sourceFolder
== null)
77 sourceFolder
= createSourceFolder();
78 return sourceFolder
.createPackageFragment(name
, false, null);
81 public IType
createType(IPackageFragment pack
, String cuName
, String source
)
82 throws JavaModelException
{
83 StringBuffer buf
= new StringBuffer();
84 buf
.append("package " + pack
.getElementName() + ";\n");
87 ICompilationUnit cu
= pack
.createCompilationUnit(cuName
,
88 buf
.toString(), false, null);
89 return cu
.getTypes()[0];
92 public void dispose() throws CoreException
{
94 project
.delete(true, true, null);
97 private IFolder
createBinFolder() throws CoreException
{
98 IFolder binFolder
= project
.getFolder("bin");
99 binFolder
.create(false, true, null);
103 private void setJavaNature() throws CoreException
{
104 IProjectDescription description
= project
.getDescription();
105 description
.setNatureIds(new String
[] { JavaCore
.NATURE_ID
});
106 project
.setDescription(description
, null);
109 private void createOutputFolder(IFolder binFolder
)
110 throws JavaModelException
{
111 IPath outputLocation
= binFolder
.getFullPath();
112 javaProject
.setOutputLocation(outputLocation
, null);
115 public IPackageFragmentRoot
createSourceFolder() throws CoreException
{
116 IFolder folder
= project
.getFolder("src");
117 folder
.create(false, true, null);
118 IPackageFragmentRoot root
= javaProject
.getPackageFragmentRoot(folder
);
119 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
120 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
121 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
122 newEntries
[oldEntries
.length
] = JavaCore
.newSourceEntry(root
.getPath());
123 javaProject
.setRawClasspath(newEntries
, null);
127 private void addSystemLibraries() throws JavaModelException
{
128 IClasspathEntry
[] oldEntries
= javaProject
.getRawClasspath();
129 IClasspathEntry
[] newEntries
= new IClasspathEntry
[oldEntries
.length
+ 1];
130 System
.arraycopy(oldEntries
, 0, newEntries
, 0, oldEntries
.length
);
131 newEntries
[oldEntries
.length
] = JavaRuntime
132 .getDefaultJREContainerEntry();
133 javaProject
.setRawClasspath(newEntries
, null);
136 private Path
findFileInPlugin(String plugin
, String file
)
137 throws MalformedURLException
, IOException
{
138 IPluginRegistry registry
= Platform
.getPluginRegistry();
139 IPluginDescriptor descriptor
= registry
.getPluginDescriptor(plugin
);
140 URL pluginURL
= descriptor
.getInstallURL();
141 URL jarURL
= new URL(pluginURL
, file
);
142 URL localJarURL
= Platform
.asLocalURL(jarURL
);
143 return new Path(localJarURL
.getPath());
146 public void waitForIndexer() {
147 // new SearchEngine().searchAllTypeNames(ResourcesPlugin.getWorkspace(),
148 // null, null, IJavaSearchConstants.EXACT_MATCH,
149 // IJavaSearchConstants.CASE_SENSITIVE,
150 // IJavaSearchConstants.CLASS, SearchEngine
151 // .createJavaSearchScope(new IJavaElement[0]),
152 // new ITypeNameRequestor() {
153 // public void acceptClass(char[] packageName,
154 // char[] simpleTypeName, char[][] enclosingTypeNames,
157 // public void acceptInterface(char[] packageName,
158 // char[] simpleTypeName, char[][] enclosingTypeNames,
161 // }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
165 * @return Returns the sourceFolder.
167 public IPackageFragmentRoot
getSourceFolder() {
172 * @param sourceFolder The sourceFolder to set.
174 public void setSourceFolder(IPackageFragmentRoot sourceFolder
) {
175 this.sourceFolder
= sourceFolder
;