blob: 848f65f9bf3a54ec1b62eb7d1fe4356c15aa54ca [file] [log] [blame]
/**
* <copyright>
******************************************************************************
* Copyright (c) 2002, 2003 Eteration Bilisim A.S., Naci Dai and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License (LGPL)
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* Contributors:
* Eteration Bilisim A.S. - initial API and implementation
* Naci M. Dai
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Eteration Bilisim A.S and Naci Dai (http://www.eteration.com/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Lomboz", "ObjectLearn" and "Eteration" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact info@eteration.com.
*
* 5. Products derived from this software may not be called "Lomboz"
* nor may "Lomboz" appear in their names without prior written
* permission of the Eteration Bilisim A.S.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Eteration Bilisim A.S. For more
* information on eteration, please see
* <http://www.eteration.com/>.
****************************************************************************
*</copyright>
*
* $Id$
*/
package org.eclipse.jst.j2ee.ejb.generation.tests.common;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
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.Platform;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.TypeNameRequestor;
import org.eclipse.jdt.launching.JavaRuntime;
public class TestProject {
public IProject project;
public IJavaProject javaProject;
private IPackageFragmentRoot sourceFolder;
public TestProject() throws CoreException {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject("Project-1");
project.create(null);
project.open(null);
javaProject = JavaCore.create(project);
IFolder binFolder = createBinFolder();
setJavaNature();
javaProject.setRawClasspath(new IClasspathEntry[0], null);
createOutputFolder(binFolder);
addSystemLibraries();
}
public IProject getProject() {
return project;
}
public IJavaProject getJavaProject() {
return javaProject;
}
public void addJar(String plugin, String jar) throws MalformedURLException,
IOException, JavaModelException {
Path result = findFileInPlugin(plugin, jar);
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
newEntries[oldEntries.length] = JavaCore.newLibraryEntry(result, null,
null);
javaProject.setRawClasspath(newEntries, null);
}
public IPackageFragment createPackage(String name) throws CoreException {
if (sourceFolder == null)
sourceFolder = createSourceFolder();
return sourceFolder.createPackageFragment(name, false, null);
}
public IType createType(IPackageFragment pack, String cuName, String source)
throws JavaModelException {
StringBuffer buf = new StringBuffer();
buf.append("package " + pack.getElementName() + ";\n");
buf.append("\n");
buf.append(source);
ICompilationUnit cu = pack.createCompilationUnit(cuName,
buf.toString(), false, null);
return cu.getTypes()[0];
}
public void dispose() throws CoreException {
waitForIndexer();
project.delete(true, true, null);
}
private IFolder createBinFolder() throws CoreException {
IFolder binFolder = project.getFolder("bin");
binFolder.create(false, true, null);
return binFolder;
}
private void setJavaNature() throws CoreException {
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[]{JavaCore.NATURE_ID});
project.setDescription(description, null);
}
private void createOutputFolder(IFolder binFolder)
throws JavaModelException {
IPath outputLocation = binFolder.getFullPath();
javaProject.setOutputLocation(outputLocation, null);
}
public IPackageFragmentRoot createSourceFolder() throws CoreException {
IFolder folder = project.getFolder("src");
folder.create(false, true, null);
IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
javaProject.setRawClasspath(newEntries, null);
return root;
}
private void addSystemLibraries() throws JavaModelException {
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
newEntries[oldEntries.length] = JavaRuntime.getDefaultJREContainerEntry();
javaProject.setRawClasspath(newEntries, null);
}
private Path findFileInPlugin(String plugin, String file)
throws MalformedURLException, IOException {
URL jarURL = Platform.getBundle(plugin).getEntry(file);
URL localJarURL = Platform.asLocalURL(jarURL);
return new Path(localJarURL.getPath());
}
private void waitForIndexer() throws JavaModelException {
int matchMode = SearchPattern.R_EXACT_MATCH;
int matchRule = SearchPattern.R_CASE_SENSITIVE;
new SearchEngine().searchAllTypeNames(
null,
null,
matchRule,
IJavaSearchConstants.TYPE,
SearchEngine.createJavaSearchScope(new IJavaElement[0], false),
new TypeNameRequestor() {
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {}
},
IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
null);
}
/**
* @return Returns the sourceFolder.
*/
public IPackageFragmentRoot getSourceFolder() {
return sourceFolder;
}
/**
* @param sourceFolder The sourceFolder to set.
*/
public void setSourceFolder(IPackageFragmentRoot sourceFolder) {
this.sourceFolder = sourceFolder;
}
}