001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.project.uiapi;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.List;
048: import javax.swing.JComponent;
049: import org.netbeans.api.progress.ProgressHandle;
050: import org.netbeans.api.progress.ProgressHandleFactory;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.api.project.TestUtil;
054: import org.netbeans.api.project.ui.OpenProjects;
055: import org.netbeans.junit.NbTestCase;
056: import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.Executor;
057: import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.UserInputHandler;
058: import org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation;
059: import org.netbeans.spi.project.DeleteOperationImplementation;
060: import org.netbeans.spi.project.ProjectFactory;
061: import org.netbeans.spi.project.ProjectState;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.util.Lookup;
065: import org.openide.util.lookup.Lookups;
066:
067: /**
068: * @author Jan Lahoda
069: */
070: public class DefaultProjectOperationsImplementationTest extends
071: NbTestCase {
072:
073: public DefaultProjectOperationsImplementationTest(String testName) {
074: super (testName);
075: }
076:
077: private FileObject scratch;
078: private FileObject projdir;
079: private Project prj;
080: private File projectDirectory;
081:
082: private void createProject(FileObject projdir) throws Exception {
083: TestUtil.createFileFromContent(
084: DefaultProjectOperationsImplementationTest.class
085: .getResource("data/test.txt"), projdir,
086: "nbproject/test.txt");
087: TestUtil.createFileFromContent(
088: DefaultProjectOperationsImplementationTest.class
089: .getResource("data/test.txt"), projdir,
090: "src/test/test.txt");
091: }
092:
093: protected void setUp() throws Exception {
094: scratch = TestUtil.makeScratchDir(this );
095: projdir = scratch.createFolder("proj");
096:
097: createProject(projdir);
098:
099: TestUtil.setLookup(new Object[] { new TestProjectFactory(),
100: new SimpleFileOwnerQueryImplementation(), });
101:
102: prj = ProjectManager.getDefault().findProject(projdir);
103:
104: assertNotNull(prj);
105:
106: projectDirectory = FileUtil.toFile(projdir);
107:
108: assertNotNull(projectDirectory);
109: }
110:
111: @Override
112: protected boolean runInEQ() {
113: return true;
114: }
115:
116: //<editor-fold defaultstate="collapsed" desc="Delete Operation">
117: public void testDeleteProjectDeleteAll() throws Exception {
118: TestUserInputHandler handler = new TestUserInputHandler(
119: TestUserInputHandler.USER_OK_ALL);
120:
121: DefaultProjectOperationsImplementation.deleteProject(prj,
122: handler);
123:
124: assertTrue(handler.confirmationDialogCalled);
125:
126: assertFalse(projectDirectory.exists());
127: }
128:
129: public void testDeleteProjectDeleteMetadata() throws Exception {
130: TestUserInputHandler handler = new TestUserInputHandler(
131: TestUserInputHandler.USER_OK_METADATA);
132:
133: DefaultProjectOperationsImplementation.deleteProject(prj,
134: handler);
135:
136: assertTrue(handler.confirmationDialogCalled);
137:
138: assertTrue(projectDirectory.exists());
139: assertTrue(Arrays.equals(new String[] { "src" },
140: projectDirectory.list()));
141: }
142:
143: public void testDeleteProjectDoNotDelete() throws Exception {
144: TestUserInputHandler handler = new TestUserInputHandler(
145: TestUserInputHandler.USER_CANCEL);
146:
147: DefaultProjectOperationsImplementation.deleteProject(prj,
148: handler);
149:
150: assertTrue(handler.confirmationDialogCalled);
151:
152: assertTrue(projectDirectory.exists());
153: List<String> items = Arrays.asList(projectDirectory.list());
154: Collections.sort(items);
155: assertEquals(Arrays.asList("nbproject", "src"), items);
156: }
157:
158: public void testDeleteProjectNestedProject() throws Exception {
159: FileObject projdir2 = projdir.createFolder("proj2");
160:
161: createProject(projdir2);
162:
163: TestUserInputHandler handler = new TestUserInputHandler(
164: TestUserInputHandler.USER_OK_ALL);
165:
166: DefaultProjectOperationsImplementation.deleteProject(prj,
167: handler);
168:
169: assertTrue(handler.confirmationDialogCalled);
170:
171: assertTrue(projectDirectory.exists());
172: assertTrue(Arrays.equals(new String[] { "proj2" },
173: projectDirectory.list()));
174: }
175:
176: public void testDeleteProjectNestedLibrary() throws Exception {
177: FileObject library = projdir.createFolder("lib");
178:
179: TestUserInputHandler handler = new TestUserInputHandler(
180: TestUserInputHandler.USER_OK_ALL);
181:
182: DefaultProjectOperationsImplementation.deleteProject(prj,
183: handler);
184:
185: assertTrue(handler.confirmationDialogCalled);
186:
187: assertTrue(projectDirectory.exists());
188: assertTrue(Arrays.equals(new String[] { "lib" },
189: projectDirectory.list()));
190: }
191:
192: public void testDeleteProjectExternalSources() throws Exception {
193: FileObject extDir = scratch.createFolder("external");
194: File extDirFile = FileUtil.toFile(extDir);
195:
196: assertNotNull(extDirFile);
197:
198: DeleteProjectOperationImpl dpoi = prj.getLookup().lookup(
199: DeleteProjectOperationImpl.class);
200:
201: assertNotNull(dpoi);
202:
203: dpoi.setExternalFile(extDir);
204:
205: TestUserInputHandler handler = new TestUserInputHandler(
206: TestUserInputHandler.USER_OK_ALL);
207:
208: DefaultProjectOperationsImplementation.deleteProject(prj,
209: handler);
210:
211: assertTrue(handler.confirmationDialogCalled);
212:
213: assertFalse(projectDirectory.exists());
214:
215: assertTrue(extDirFile.exists());
216: }
217:
218: private static final class TestUserInputHandler implements
219: UserInputHandler {
220:
221: public static final int USER_CANCEL = 1;
222: public static final int USER_OK_METADATA = 2;
223: public static final int USER_OK_ALL = 3;
224:
225: private int answer;
226: private Exception exception;
227:
228: private boolean confirmationDialogCalled;
229:
230: public TestUserInputHandler(int answer) {
231: this .answer = answer;
232: this .confirmationDialogCalled = false;
233: this .exception = null;
234: }
235:
236: public void showConfirmationDialog(final JComponent panel,
237: Project project, String caption, String confirmButton,
238: String cancelButton, boolean doSetMessageType,
239: final Executor executor) {
240: confirmationDialogCalled = true;
241:
242: if (answer == USER_CANCEL) {
243: return;
244: }
245:
246: if (answer == USER_OK_ALL) {
247: ((DefaultProjectDeletePanel) panel)
248: .setDeleteSources(true);
249: } else {
250: ((DefaultProjectDeletePanel) panel)
251: .setDeleteSources(false);
252: }
253:
254: try {
255: executor.execute();
256: } catch (Exception e) {
257: exception = e;
258: }
259: }
260:
261: }
262:
263: //</editor-fold>
264:
265: //<editor-fold defaultstate="collapsed" desc="Copy Operation">
266: public void testCopyWithLib() throws Exception {
267: TestUtil.createFileFromContent(
268: DefaultProjectOperationsImplementationTest.class
269: .getResource("data/test.txt"), projdir,
270: "lib/test.txt");
271: ProgressHandle handle = ProgressHandleFactory
272: .createHandle("test-handle");
273: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
274: FileObject newTarget = prj.getProjectDirectory().getParent();
275:
276: DefaultProjectOperationsImplementation.doCopyProject(handle,
277: prj, "projCopy", newTarget);
278:
279: File newProject = new File(FileUtil.toFile(newTarget),
280: "projCopy");
281:
282: assertTrue(newProject.isDirectory());
283: assertTrue(new File(newProject, "nbproject").isDirectory());
284: assertTrue(new File(newProject, "src").isDirectory());
285: assertTrue(new File(newProject, "lib").isDirectory());
286: }
287:
288: public void testCopyWithInnerProjectSimple() throws Exception {
289: TestUtil.createFileFromContent(
290: DefaultProjectOperationsImplementationTest.class
291: .getResource("data/test.txt"), projdir,
292: "lib/test.txt");
293: FileObject projdir2 = projdir.createFolder("proj2");
294:
295: createProject(projdir2);
296:
297: ProgressHandle handle = ProgressHandleFactory
298: .createHandle("test-handle");
299: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
300: FileObject newTarget = prj.getProjectDirectory().getParent();
301:
302: DefaultProjectOperationsImplementation.doCopyProject(handle,
303: prj, "projCopy", newTarget);
304:
305: File newProject = new File(FileUtil.toFile(newTarget),
306: "projCopy");
307:
308: assertTrue(newProject.isDirectory());
309: assertTrue(new File(newProject, "nbproject").isDirectory());
310: assertTrue(new File(newProject, "src").isDirectory());
311: assertTrue(new File(newProject, "lib").isDirectory());
312: assertFalse(new File(newProject, "proj2").exists());
313: }
314:
315: public void testCopyWithInnerProjectComplex() throws Exception {
316: TestUtil.createFileFromContent(
317: DefaultProjectOperationsImplementationTest.class
318: .getResource("data/test.txt"), projdir,
319: "lib/test.txt");
320: FileObject projdir2 = projdir.getFileObject("lib")
321: .createFolder("proj2");
322:
323: createProject(projdir2);
324:
325: ProgressHandle handle = ProgressHandleFactory
326: .createHandle("test-handle");
327: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
328:
329: FileObject newTarget = prj.getProjectDirectory().getParent();
330:
331: DefaultProjectOperationsImplementation.doCopyProject(handle,
332: prj, "projCopy", newTarget);
333:
334: File newProject = new File(FileUtil.toFile(newTarget),
335: "projCopy");
336:
337: assertTrue(newProject.isDirectory());
338: assertTrue(new File(newProject, "nbproject").isDirectory());
339: assertTrue(new File(newProject, "src").isDirectory());
340: assertTrue(new File(newProject, "lib").isDirectory());
341: assertFalse(new File(new File(newProject, "lib"), "proj2")
342: .exists());
343: }
344:
345: public void testMainProjectFlagNotMovedWhenCopying()
346: throws Exception {
347: OpenProjects.getDefault().open(new Project[] { prj }, false);
348:
349: Project main = OpenProjects.getDefault().getMainProject();
350:
351: assertTrue(main == null
352: || !prj.getProjectDirectory().equals(
353: main.getProjectDirectory()));
354:
355: ProgressHandle handle = ProgressHandleFactory
356: .createHandle("test-handle");
357: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
358: FileObject oldProject = prj.getProjectDirectory();
359: File oldProjectFile = FileUtil.toFile(oldProject);
360: FileObject newTarget = oldProject.getParent();
361:
362: DefaultProjectOperationsImplementation.doCopyProject(handle,
363: prj, "projCopy", newTarget);
364:
365: assertTrue(main == null
366: || OpenProjects.getDefault().getMainProject().equals(
367: main.getProjectDirectory()));
368: }
369:
370: //</editor-fold>
371:
372: //<editor-fold defaultstate="collapsed" desc="Move Operation">
373: public void testMoveWithLib() throws Exception {
374: TestUtil.createFileFromContent(
375: DefaultProjectOperationsImplementationTest.class
376: .getResource("data/test.txt"), projdir,
377: "lib/test.txt");
378: ProgressHandle handle = ProgressHandleFactory
379: .createHandle("test-handle");
380: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
381: FileObject oldProject = prj.getProjectDirectory();
382: File oldProjectFile = FileUtil.toFile(oldProject);
383: FileObject newTarget = oldProject.getParent();
384:
385: DefaultProjectOperationsImplementation.doMoveProject(handle,
386: prj, "projMove", "projMove", newTarget,
387: "ERR_Cannot_Move");
388:
389: File newProject = new File(FileUtil.toFile(newTarget),
390: "projMove");
391:
392: assertTrue(newProject.isDirectory());
393: assertTrue(new File(newProject, "nbproject").isDirectory());
394: assertTrue(new File(newProject, "src").isDirectory());
395: assertTrue(new File(newProject, "lib").isDirectory());
396:
397: assertFalse(oldProjectFile.exists());
398: }
399:
400: public void testMoveWithInnerProjectSimple() throws Exception {
401: TestUtil.createFileFromContent(
402: DefaultProjectOperationsImplementationTest.class
403: .getResource("data/test.txt"), projdir,
404: "lib/test.txt");
405: FileObject projdir2 = projdir.createFolder("proj2");
406:
407: createProject(projdir2);
408:
409: ProgressHandle handle = ProgressHandleFactory
410: .createHandle("test-handle");
411: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
412: FileObject oldProject = prj.getProjectDirectory();
413: File oldProjectFile = FileUtil.toFile(oldProject);
414: FileObject newTarget = oldProject.getParent();
415:
416: DefaultProjectOperationsImplementation.doMoveProject(handle,
417: prj, "projMove", "projMove", newTarget,
418: "ERR_Cannot_Move");
419:
420: File newProject = new File(FileUtil.toFile(newTarget),
421: "projMove");
422:
423: assertTrue(newProject.isDirectory());
424: assertTrue(new File(newProject, "nbproject").isDirectory());
425: assertTrue(new File(newProject, "src").isDirectory());
426: assertTrue(new File(newProject, "lib").isDirectory());
427: assertFalse(new File(newProject, "proj2").exists());
428:
429: assertTrue(new File(oldProjectFile, "proj2").exists());
430: assertTrue(new File(new File(oldProjectFile, "proj2"),
431: "nbproject").exists());
432: }
433:
434: public void testMoveWithInnerProjectComplex() throws Exception {
435: TestUtil.createFileFromContent(
436: DefaultProjectOperationsImplementationTest.class
437: .getResource("data/test.txt"), projdir,
438: "lib/test.txt");
439: FileObject projdir2 = projdir.getFileObject("lib")
440: .createFolder("proj2");
441:
442: createProject(projdir2);
443:
444: ProgressHandle handle = ProgressHandleFactory
445: .createHandle("test-handle");
446: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
447:
448: FileObject oldProject = prj.getProjectDirectory();
449: File oldProjectFile = FileUtil.toFile(oldProject);
450: FileObject newTarget = oldProject.getParent();
451:
452: DefaultProjectOperationsImplementation.doMoveProject(handle,
453: prj, "projMove", "projMove", newTarget,
454: "ERR_Cannot_Move");
455:
456: File newProject = new File(FileUtil.toFile(newTarget),
457: "projMove");
458:
459: assertTrue(newProject.isDirectory());
460: assertTrue(new File(newProject, "nbproject").isDirectory());
461: assertTrue(new File(newProject, "src").isDirectory());
462: assertTrue(new File(newProject, "lib").isDirectory());
463: assertFalse(new File(new File(newProject, "lib"), "proj2")
464: .exists());
465:
466: assertTrue(new File(new File(oldProjectFile, "lib"), "proj2")
467: .exists());
468: assertTrue(new File(new File(new File(oldProjectFile, "lib"),
469: "proj2"), "nbproject").exists());
470: }
471:
472: public void testMainProjectFlagMovedForMainProject()
473: throws Exception {
474: OpenProjects.getDefault().open(new Project[] { prj }, false);
475: OpenProjects.getDefault().setMainProject(prj);
476: assertEquals(prj, OpenProjects.getDefault().getMainProject());
477: ProgressHandle handle = ProgressHandleFactory
478: .createHandle("test-handle");
479: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
480: FileObject oldProject = prj.getProjectDirectory();
481: File oldProjectFile = FileUtil.toFile(oldProject);
482: FileObject newTarget = oldProject.getParent();
483:
484: DefaultProjectOperationsImplementation.doMoveProject(handle,
485: prj, "projMove", "projMove", newTarget,
486: "ERR_Cannot_Move");
487:
488: Project newProject = ProjectManager.getDefault().findProject(
489: newTarget.getFileObject("projMove"));
490:
491: assertEquals(OpenProjects.getDefault().getMainProject(),
492: newProject);
493: }
494:
495: public void testMainProjectFlagNotMovedForNonMainProject()
496: throws Exception {
497: OpenProjects.getDefault().open(new Project[] { prj }, false);
498:
499: Project main = OpenProjects.getDefault().getMainProject();
500:
501: assertTrue(main == null
502: || !prj.getProjectDirectory().equals(
503: main.getProjectDirectory()));
504:
505: ProgressHandle handle = ProgressHandleFactory
506: .createHandle("test-handle");
507: handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
508: FileObject oldProject = prj.getProjectDirectory();
509: File oldProjectFile = FileUtil.toFile(oldProject);
510: FileObject newTarget = oldProject.getParent();
511:
512: DefaultProjectOperationsImplementation.doMoveProject(handle,
513: prj, "projMove", "projMove", newTarget,
514: "ERR_Cannot_Move");
515:
516: Project newProject = ProjectManager.getDefault().findProject(
517: newTarget.getFileObject("projMove"));
518:
519: main = OpenProjects.getDefault().getMainProject();
520:
521: assertTrue(main == null
522: || (!prj.getProjectDirectory().equals(
523: main.getProjectDirectory()) && !newProject
524: .getProjectDirectory().equals(
525: main.getProjectDirectory())));
526: }
527:
528: //</editor-fold>
529:
530: //<editor-fold defaultstate="collapsed" desc="Utilities">
531: private static final class TestProject implements Project {
532:
533: private final Lookup l;
534: private final FileObject projectDirectory;
535:
536: TestProject(FileObject projectDirectory) throws IOException {
537: l = Lookups.fixed(new DeleteProjectOperationImpl(this ));
538: this .projectDirectory = projectDirectory;
539: }
540:
541: public FileObject getProjectDirectory() {
542: return projectDirectory;
543: }
544:
545: public Lookup getLookup() {
546: return l;
547: }
548:
549: public String toString() {
550: return "TestAntBasedProject[" + getProjectDirectory() + "]";
551: }
552:
553: }
554:
555: public static class TestProjectFactory implements ProjectFactory {
556:
557: public boolean isProject(FileObject projectDirectory) {
558: return projectDirectory.getFileObject("nbproject") != null;
559: }
560:
561: public Project loadProject(FileObject projectDirectory,
562: ProjectState state) throws IOException {
563: if (isProject(projectDirectory))
564: return new TestProject(projectDirectory);
565:
566: return null;
567: }
568:
569: public void saveProject(Project project) throws IOException,
570: ClassCastException {
571: }
572:
573: }
574:
575: public static final class DeleteProjectOperationImpl implements
576: DeleteOperationImplementation {
577:
578: private boolean wasCleaned = false;
579: private boolean wasNotified = false;
580:
581: private FileObject externalFile = null;
582:
583: private TestProject project;
584:
585: public DeleteProjectOperationImpl(TestProject project) {
586: this .project = project;
587: }
588:
589: public List<FileObject> getMetadataFiles() {
590: return Collections.singletonList(project
591: .getProjectDirectory().getFileObject("nbproject"));
592: }
593:
594: public List<FileObject> getDataFiles() {
595: if (externalFile == null) {
596: return Collections.singletonList(project
597: .getProjectDirectory().getFileObject("src"));
598: } else {
599: return Arrays.asList(project.getProjectDirectory()
600: .getFileObject("src"), externalFile);
601: }
602: }
603:
604: public void setExternalFile(FileObject externalFile) {
605: this .externalFile = externalFile;
606: }
607:
608: public synchronized boolean getWasCleaned() {
609: return wasCleaned;
610: }
611:
612: public synchronized void notifyDeleting() throws IOException {
613: wasCleaned = true;
614: }
615:
616: public synchronized boolean getWasNotified() {
617: return wasNotified;
618: }
619:
620: public synchronized void notifyDeleted() throws IOException {
621: wasNotified = true;
622: }
623:
624: }
625: //</editor-fold>
626:
627: }
|