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.wizardapi;
011:
012: import junit.framework.Test;
013: import junit.framework.TestCase;
014: import junit.framework.TestSuite;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IWorkspaceRoot;
018: import org.eclipse.core.resources.ResourcesPlugin;
019: import org.eclipse.core.runtime.IPath;
020:
021: import org.eclipse.jface.operation.IRunnableWithProgress;
022:
023: import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
024:
025: import org.eclipse.jdt.core.IClasspathEntry;
026: import org.eclipse.jdt.core.IJavaProject;
027: import org.eclipse.jdt.core.JavaCore;
028:
029: import org.eclipse.jdt.launching.JavaRuntime;
030:
031: import org.eclipse.jdt.ui.PreferenceConstants;
032: import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPage;
033:
034: public class NewJavaProjectWizardTest extends TestCase {
035:
036: public static Test suite() {
037: return new TestSuite(NewJavaProjectWizardTest.class);
038: }
039:
040: private class TestNewJavaProjectWizardPage extends
041: NewJavaProjectWizardPage {
042:
043: private IProject fNewProject;
044:
045: public TestNewJavaProjectWizardPage(IWorkspaceRoot root) {
046: super (root, null);
047: }
048:
049: public void setProjectHandle(IProject newProject) {
050: fNewProject = newProject;
051: }
052:
053: /**
054: * @see NewJavaProjectWizardPage#getLocationPath()
055: */
056: protected IPath getLocationPath() {
057: return null;
058: }
059:
060: /**
061: * @see NewJavaProjectWizardPage#getProjectHandle()
062: */
063: protected IProject getProjectHandle() {
064: return fNewProject;
065: }
066:
067: public void initBuildPath() {
068: super .initBuildPaths();
069: }
070:
071: }
072:
073: private static final String PROJECT_NAME = "DummyProject";
074: private static final String OTHER_PROJECT_NAME = "OtherProject";
075:
076: private TestNewJavaProjectWizardPage fWizardPage;
077:
078: public NewJavaProjectWizardTest(String name) {
079: super (name);
080: }
081:
082: /**
083: * @see TestCase#setUp()
084: */
085: protected void setUp() throws Exception {
086: super .setUp();
087:
088: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
089:
090: IProject project = root.getProject(PROJECT_NAME);
091:
092: fWizardPage = new TestNewJavaProjectWizardPage(root);
093: fWizardPage.setProjectHandle(project);
094: }
095:
096: /**
097: * @see TestCase#tearDown()
098: */
099: protected void tearDown() throws Exception {
100: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
101: IProject project = root.getProject(PROJECT_NAME);
102: if (project.exists()) {
103: project.delete(true, null);
104: }
105:
106: project = root.getProject(OTHER_PROJECT_NAME);
107: if (project.exists()) {
108: project.delete(true, null);
109: }
110:
111: super .tearDown();
112: }
113:
114: private IPath getJREEntryPath() {
115: return JavaRuntime.getDefaultJREContainerEntry().getPath();
116: }
117:
118: private void assertBasicBuildPath(IProject project,
119: IPath outputLocation, IClasspathEntry[] classpath) {
120: assertNotNull("a", outputLocation);
121: assertNotNull("b", classpath);
122: assertTrue("c", classpath.length == 2);
123:
124: if (PreferenceConstants.getPreferenceStore().getBoolean(
125: PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ)) {
126: assertEquals("d", outputLocation, project.getFolder("bin")
127: .getFullPath());
128: assertEquals("e", classpath[0].getPath(), project
129: .getFolder("src").getFullPath());
130: } else {
131: assertEquals("f", outputLocation, project.getFullPath());
132: assertEquals("g", classpath[0].getPath(), project
133: .getFullPath());
134: }
135: assertEquals("h", classpath[1].getPath(), getJREEntryPath());
136: }
137:
138: public void testBasicSet() throws Exception {
139: fWizardPage.initBuildPath();
140: IProject project = fWizardPage.getProjectHandle();
141:
142: IPath outputLocation = fWizardPage.getOutputLocation();
143: IClasspathEntry[] classpath = fWizardPage.getRawClassPath();
144: assertBasicBuildPath(project, outputLocation, classpath);
145: }
146:
147: public void testBasicCreate() throws Exception {
148: IProject project = fWizardPage.getProjectHandle();
149:
150: IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(
151: fWizardPage.getRunnable());
152: op.run(null);
153:
154: IJavaProject jproj = fWizardPage.getNewJavaProject();
155:
156: assertEquals("a", jproj.getProject(), project);
157:
158: IPath outputLocation = jproj.getOutputLocation();
159: IClasspathEntry[] classpath = jproj.getRawClasspath();
160: assertBasicBuildPath(jproj.getProject(), outputLocation,
161: classpath);
162: }
163:
164: public void testProjectChange() throws Exception {
165: fWizardPage.initBuildPath();
166: IProject project = fWizardPage.getProjectHandle();
167:
168: IPath outputLocation = fWizardPage.getOutputLocation();
169: IClasspathEntry[] classpath = fWizardPage.getRawClassPath();
170: assertBasicBuildPath(project, outputLocation, classpath);
171:
172: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
173: IProject otherProject = root.getProject(OTHER_PROJECT_NAME);
174:
175: // change the project before create
176: fWizardPage.setProjectHandle(otherProject);
177:
178: IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(
179: fWizardPage.getRunnable());
180: op.run(null);
181:
182: IJavaProject jproj = fWizardPage.getNewJavaProject();
183:
184: assertEquals("a", jproj.getProject(), otherProject);
185:
186: IPath outputLocation1 = fWizardPage.getOutputLocation();
187: IClasspathEntry[] classpath1 = fWizardPage.getRawClassPath();
188: assertBasicBuildPath(otherProject, outputLocation1, classpath1);
189: }
190:
191: private void assertUserBuildPath(IProject project,
192: IPath outputLocation, IClasspathEntry[] classpath) {
193: assertNotNull("a", outputLocation);
194: assertNotNull("b", classpath);
195: assertTrue("c", classpath.length == 3);
196:
197: assertEquals("d", outputLocation, project.getFolder("dbin")
198: .getFullPath());
199: assertEquals("e", classpath[0].getPath(), project.getFolder(
200: "dsrc1").getFullPath());
201: assertEquals("f", classpath[1].getPath(), project.getFolder(
202: "dsrc2").getFullPath());
203: assertEquals("g", classpath[2].getPath(), getJREEntryPath());
204: }
205:
206: public void testUserSet() throws Exception {
207: IProject project = fWizardPage.getProjectHandle();
208:
209: IPath folderPath = project.getFolder("dbin").getFullPath();
210:
211: IClasspathEntry[] entries = new IClasspathEntry[] {
212: JavaCore.newSourceEntry(project.getFolder("dsrc1")
213: .getFullPath()),
214: JavaCore.newSourceEntry(project.getFolder("dsrc2")
215: .getFullPath()) };
216:
217: fWizardPage.setDefaultOutputFolder(folderPath);
218: fWizardPage.setDefaultClassPath(entries, true);
219: fWizardPage.initBuildPath();
220:
221: IPath outputLocation = fWizardPage.getOutputLocation();
222: IClasspathEntry[] classpath = fWizardPage.getRawClassPath();
223: assertUserBuildPath(project, outputLocation, classpath);
224:
225: fWizardPage.setDefaultOutputFolder(null);
226: fWizardPage.setDefaultClassPath(null, false);
227: fWizardPage.initBuildPath();
228:
229: IPath outputLocation1 = fWizardPage.getOutputLocation();
230: IClasspathEntry[] classpath1 = fWizardPage.getRawClassPath();
231: assertBasicBuildPath(project, outputLocation1, classpath1);
232: }
233:
234: public void testUserCreate() throws Exception {
235: IProject project = fWizardPage.getProjectHandle();
236:
237: IPath folderPath = project.getFolder("dbin").getFullPath();
238:
239: IClasspathEntry[] entries = new IClasspathEntry[] {
240: JavaCore.newSourceEntry(project.getFolder("dsrc1")
241: .getFullPath()),
242: JavaCore.newSourceEntry(project.getFolder("dsrc2")
243: .getFullPath()) };
244:
245: fWizardPage.setDefaultOutputFolder(folderPath);
246: fWizardPage.setDefaultClassPath(entries, true);
247:
248: IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(
249: fWizardPage.getRunnable());
250: op.run(null);
251:
252: IJavaProject jproj = fWizardPage.getNewJavaProject();
253:
254: assertEquals("a", jproj.getProject(), project);
255:
256: IPath outputLocation = jproj.getOutputLocation();
257: IClasspathEntry[] classpath = jproj.getRawClasspath();
258: assertUserBuildPath(jproj.getProject(), outputLocation,
259: classpath);
260: }
261:
262: public void testReadExisting() throws Exception {
263: IProject project = fWizardPage.getProjectHandle();
264:
265: IPath folderPath = project.getFolder("dbin").getFullPath();
266: IClasspathEntry[] entries = new IClasspathEntry[] {
267: JavaCore.newSourceEntry(project.getFolder("dsrc1")
268: .getFullPath()),
269: JavaCore.newSourceEntry(project.getFolder("dsrc2")
270: .getFullPath()) };
271:
272: fWizardPage.setDefaultOutputFolder(folderPath);
273: fWizardPage.setDefaultClassPath(entries, true);
274:
275: IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(
276: fWizardPage.getRunnable());
277: op.run(null);
278:
279: IProject proj = fWizardPage.getNewJavaProject().getProject();
280:
281: fWizardPage.setDefaultClassPath(null, false);
282: fWizardPage.setDefaultOutputFolder(null);
283: fWizardPage.setProjectHandle(proj);
284:
285: // reads from existing
286: fWizardPage.initBuildPath();
287:
288: IPath outputLocation1 = fWizardPage.getOutputLocation();
289: IClasspathEntry[] classpath1 = fWizardPage.getRawClassPath();
290: assertUserBuildPath(project, outputLocation1, classpath1);
291: }
292:
293: public void testExistingOverwrite() throws Exception {
294: IProject project = fWizardPage.getProjectHandle();
295:
296: IRunnableWithProgress op = new WorkspaceModifyDelegatingOperation(
297: fWizardPage.getRunnable());
298: op.run(null);
299:
300: IPath folderPath = project.getFolder("dbin").getFullPath();
301: IClasspathEntry[] entries = new IClasspathEntry[] {
302: JavaCore.newSourceEntry(project.getFolder("dsrc1")
303: .getFullPath()),
304: JavaCore.newSourceEntry(project.getFolder("dsrc2")
305: .getFullPath()) };
306:
307: fWizardPage.setDefaultOutputFolder(folderPath);
308: fWizardPage.setDefaultClassPath(entries, true);
309:
310: // should overwrite existing
311: IRunnableWithProgress op1 = new WorkspaceModifyDelegatingOperation(
312: fWizardPage.getRunnable());
313: op1.run(null);
314:
315: IJavaProject jproj = fWizardPage.getNewJavaProject();
316:
317: IPath outputLocation1 = jproj.getOutputLocation();
318: IClasspathEntry[] classpath1 = jproj.getRawClasspath();
319: assertUserBuildPath(project, outputLocation1, classpath1);
320: }
321: }
|