001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.dialogs;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.List;
015:
016: import junit.extensions.TestSetup;
017: import junit.framework.Test;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.core.runtime.NullProgressMonitor;
022: import org.eclipse.core.runtime.Path;
023:
024: import org.eclipse.core.resources.IFolder;
025:
026: import org.eclipse.jdt.core.IClasspathEntry;
027: import org.eclipse.jdt.core.IJavaProject;
028: import org.eclipse.jdt.core.JavaCore;
029: import org.eclipse.jdt.core.JavaModelException;
030:
031: import org.eclipse.jdt.ui.PreferenceConstants;
032:
033: import org.eclipse.jdt.internal.ui.JavaPlugin;
034: import org.eclipse.jdt.internal.ui.util.CoreUtility;
035:
036: import org.eclipse.jdt.testplugin.JavaProjectHelper;
037: import org.eclipse.jdt.testplugin.TestOptions;
038:
039: public class NewProjectTestSetup extends TestSetup {
040:
041: public static final String WORKSPACE_PROJECT = "WorkspaceProject";
042: public static final String WORKSPACE_PROJECT_SRC = "WorkspaceProjectWithSourceAndInFolder";
043:
044: public static IJavaProject getProject(String projectName,
045: String binFolderName) throws CoreException {
046: return JavaProjectHelper.createJavaProject(projectName,
047: binFolderName);
048: }
049:
050: public static IClasspathEntry[] getDefaultClasspath() {
051: return PreferenceConstants.getDefaultJRELibrary();
052: }
053:
054: private IJavaProject fWorkspaceProject;
055: private IJavaProject fWorkspaceProjectWithSrc;
056: private IJavaProject fExternalProject;
057:
058: private boolean fAutobuilding;
059:
060: public NewProjectTestSetup(Test test) {
061: super (test);
062: try {
063: fAutobuilding = JavaProjectHelper.setAutoBuilding(false);
064: } catch (CoreException e) {
065: JavaPlugin.log(e);
066: }
067: }
068:
069: public IJavaProject getProject(IJavaProject currentProject)
070: throws CoreException {
071: String name = currentProject.getElementName();
072: currentProject.getProject().delete(true, null);
073: if (name.equals(WORKSPACE_PROJECT))
074: return getWorkspaceProject();
075: if (name.equals(WORKSPACE_PROJECT_SRC))
076: return getWorkspaceProjectWithSrc();
077: return null;
078: }
079:
080: public IJavaProject getWorkspaceProject() {
081: try {
082: fWorkspaceProject = getProject(WORKSPACE_PROJECT, "");
083: List cpEntries = new ArrayList();
084: IPath projectPath = fWorkspaceProject.getProject()
085: .getFullPath();
086: cpEntries.add(JavaCore.newSourceEntry(projectPath));
087: cpEntries.addAll(Arrays.asList(getDefaultClasspath()));
088: IClasspathEntry[] entries = (IClasspathEntry[]) cpEntries
089: .toArray(new IClasspathEntry[cpEntries.size()]);
090: fWorkspaceProject.setRawClasspath(entries, projectPath,
091: new NullProgressMonitor());
092: } catch (JavaModelException e) {
093: JavaPlugin.log(e);
094: } catch (CoreException e) {
095: JavaPlugin.log(e);
096: }
097: return fWorkspaceProject;
098: }
099:
100: public IJavaProject getWorkspaceProjectWithSrc() {
101: try {
102: fWorkspaceProjectWithSrc = getProject(
103: WORKSPACE_PROJECT_SRC, PreferenceConstants
104: .getPreferenceStore().getString(
105: PreferenceConstants.SRCBIN_BINNAME));
106: createWithSrcAndBinFolder(fWorkspaceProjectWithSrc);
107: } catch (CoreException e) {
108: JavaPlugin.log(e);
109: }
110: return fWorkspaceProjectWithSrc;
111: }
112:
113: public IJavaProject getExternalProject() throws CoreException {
114: return fExternalProject;
115: }
116:
117: /* (non-Javadoc)
118: * @see junit.extensions.TestSetup#setUp()
119: */
120: protected void setUp() throws Exception {
121: super .setUp();
122: JavaCore.setOptions(TestOptions.getDefaultOptions());
123: TestOptions.initializeCodeGenerationOptions();
124: JavaPlugin.getDefault().getCodeTemplateStore().load();
125: }
126:
127: protected void tearDown() throws Exception {
128: if (fWorkspaceProject != null && fWorkspaceProject.exists())
129: JavaProjectHelper.delete(fWorkspaceProject);
130: if (fWorkspaceProjectWithSrc != null
131: && fWorkspaceProjectWithSrc.exists())
132: JavaProjectHelper.delete(fWorkspaceProjectWithSrc);
133: if (fExternalProject != null && fExternalProject.exists())
134: JavaProjectHelper.delete(fExternalProject);
135: JavaProjectHelper.setAutoBuilding(fAutobuilding);
136: }
137:
138: private void createWithSrcAndBinFolder(IJavaProject project) {
139: IPath srcPath = new Path("src");
140: try {
141: if (srcPath.segmentCount() > 0) {
142: IFolder folder = project.getProject()
143: .getFolder(srcPath);
144: CoreUtility.createFolder(folder, true, true, null);
145: }
146:
147: final IPath projectPath = project.getProject()
148: .getFullPath();
149:
150: // configure the classpath entries, including the default jre library.
151: List cpEntries = new ArrayList();
152: cpEntries.add(JavaCore.newSourceEntry(projectPath
153: .append(srcPath)));
154: cpEntries.addAll(Arrays.asList(PreferenceConstants
155: .getDefaultJRELibrary()));
156: IClasspathEntry[] entries = (IClasspathEntry[]) cpEntries
157: .toArray(new IClasspathEntry[cpEntries.size()]);
158:
159: project.setRawClasspath(entries, null);
160: } catch (CoreException e) {
161: JavaPlugin.log(e);
162: }
163: }
164:
165: }
|