KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > uiapi > DefaultProjectOperationsImplementationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.project.uiapi;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import org.netbeans.api.progress.ProgressHandle;
28 import org.netbeans.api.progress.ProgressHandleFactory;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.project.TestUtil;
32 import org.netbeans.api.project.ui.OpenProjects;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.Executor;
35 import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.UserInputHandler;
36 import org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation;
37 import org.netbeans.spi.project.DeleteOperationImplementation;
38 import org.netbeans.spi.project.ProjectFactory;
39 import org.netbeans.spi.project.ProjectState;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.util.Lookup;
43 import org.openide.util.lookup.Lookups;
44
45 /**
46  * @author Jan Lahoda
47  */

48 public class DefaultProjectOperationsImplementationTest extends NbTestCase {
49     
50     public DefaultProjectOperationsImplementationTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     private FileObject scratch;
55     private FileObject projdir;
56     private Project prj;
57     private File JavaDoc projectDirectory;
58     
59     private void createProject(FileObject projdir) throws Exception JavaDoc {
60         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "nbproject/test.txt");
61         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "src/test/test.txt");
62     }
63     
64     protected void setUp() throws Exception JavaDoc {
65         scratch = TestUtil.makeScratchDir(this);
66         projdir = scratch.createFolder("proj");
67         
68         createProject(projdir);
69
70         TestUtil.setLookup(new Object JavaDoc[] {
71             new TestProjectFactory(),
72             new SimpleFileOwnerQueryImplementation(),
73         });
74         
75         prj = ProjectManager.getDefault().findProject(projdir);
76         
77         assertNotNull(prj);
78         
79         projectDirectory = FileUtil.toFile(projdir);
80         
81         assertNotNull(projectDirectory);
82     }
83     
84     //<editor-fold defaultstate="collapsed" desc="Delete Operation">
85
public void testDeleteProjectDeleteAll() throws Exception JavaDoc {
86         TestUserInputHandler handler = new TestUserInputHandler(TestUserInputHandler.USER_OK_ALL);
87         
88         DefaultProjectOperationsImplementation.deleteProject(prj, handler);
89         
90         assertTrue(handler.confirmationDialogCalled);
91         
92         assertFalse(projectDirectory.exists());
93     }
94     
95     public void testDeleteProjectDeleteMetadata() throws Exception JavaDoc {
96         TestUserInputHandler handler = new TestUserInputHandler(TestUserInputHandler.USER_OK_METADATA);
97         
98         DefaultProjectOperationsImplementation.deleteProject(prj, handler);
99         
100         assertTrue(handler.confirmationDialogCalled);
101         
102         assertTrue(projectDirectory.exists());
103         assertTrue(Arrays.equals(new String JavaDoc[] {"src"}, projectDirectory.list()));
104     }
105     
106     public void testDeleteProjectDoNotDelete() throws Exception JavaDoc {
107         TestUserInputHandler handler = new TestUserInputHandler(TestUserInputHandler.USER_CANCEL);
108         
109         DefaultProjectOperationsImplementation.deleteProject(prj, handler);
110         
111         assertTrue(handler.confirmationDialogCalled);
112         
113         assertTrue(projectDirectory.exists());
114         List JavaDoc<String JavaDoc> items = Arrays.asList(projectDirectory.list());
115         Collections.sort(items);
116         assertEquals(Arrays.asList("nbproject", "src"), items);
117     }
118     
119     public void testDeleteProjectNestedProject() throws Exception JavaDoc {
120         FileObject projdir2 = projdir.createFolder("proj2");
121         
122         createProject(projdir2);
123         
124         TestUserInputHandler handler = new TestUserInputHandler(TestUserInputHandler.USER_OK_ALL);
125         
126         DefaultProjectOperationsImplementation.deleteProject(prj, handler);
127         
128         assertTrue(handler.confirmationDialogCalled);
129         
130         assertTrue(projectDirectory.exists());
131         assertTrue(Arrays.equals(new String JavaDoc[] {"proj2"}, projectDirectory.list()));
132     }
133     
134     public void testDeleteProjectNestedLibrary() throws Exception JavaDoc {
135         FileObject library = projdir.createFolder("lib");
136         
137         TestUserInputHandler handler = new TestUserInputHandler(TestUserInputHandler.USER_OK_ALL);
138         
139         DefaultProjectOperationsImplementation.deleteProject(prj, handler);
140         
141         assertTrue(handler.confirmationDialogCalled);
142         
143         assertTrue(projectDirectory.exists());
144         assertTrue(Arrays.equals(new String JavaDoc[] {"lib"}, projectDirectory.list()));
145     }
146     
147     public void testDeleteProjectExternalSources() throws Exception JavaDoc {
148         FileObject extDir = scratch.createFolder("external");
149         File JavaDoc extDirFile = FileUtil.toFile(extDir);
150         
151         assertNotNull(extDirFile);
152         
153         DeleteProjectOperationImpl dpoi = prj.getLookup().lookup(DeleteProjectOperationImpl.class);
154         
155         assertNotNull(dpoi);
156         
157         dpoi.setExternalFile(extDir);
158         
159         TestUserInputHandler handler = new TestUserInputHandler(TestUserInputHandler.USER_OK_ALL);
160         
161         DefaultProjectOperationsImplementation.deleteProject(prj, handler);
162         
163         assertTrue(handler.confirmationDialogCalled);
164         
165         assertFalse(projectDirectory.exists());
166         
167         assertTrue(extDirFile.exists());
168     }
169     
170     private static final class TestUserInputHandler implements UserInputHandler {
171         
172         public static final int USER_CANCEL = 1;
173         public static final int USER_OK_METADATA = 2;
174         public static final int USER_OK_ALL = 3;
175
176         private int answer;
177         private Exception JavaDoc exception;
178         
179         private boolean confirmationDialogCalled;
180         
181         public TestUserInputHandler(int answer) {
182             this.answer = answer;
183             this.confirmationDialogCalled = false;
184             this.exception = null;
185         }
186
187         public void showConfirmationDialog(final JComponent JavaDoc panel, Project project, String JavaDoc caption, String JavaDoc confirmButton, String JavaDoc cancelButton, boolean doSetMessageType, final Executor executor) {
188             confirmationDialogCalled = true;
189             
190             if (answer == USER_CANCEL) {
191                 return ;
192             }
193             
194             if (answer == USER_OK_ALL) {
195                 ((DefaultProjectDeletePanel) panel).setDeleteSources(true);
196             } else {
197                 ((DefaultProjectDeletePanel) panel).setDeleteSources(false);
198             }
199             
200             try {
201                 executor.execute();
202             } catch (Exception JavaDoc e) {
203                 exception = e;
204             }
205         }
206         
207     }
208     //</editor-fold>
209

210     //<editor-fold defaultstate="collapsed" desc="Copy Operation">
211
public void testCopyWithLib() throws Exception JavaDoc {
212         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "lib/test.txt");
213         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
214         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
215         FileObject newTarget = prj.getProjectDirectory().getParent();
216         
217         DefaultProjectOperationsImplementation.doCopyProject(handle, prj, "projCopy", newTarget);
218         
219         File JavaDoc newProject = new File JavaDoc(FileUtil.toFile(newTarget), "projCopy");
220         
221         assertTrue(newProject.isDirectory());
222         assertTrue(new File JavaDoc(newProject, "nbproject").isDirectory());
223         assertTrue(new File JavaDoc(newProject, "src").isDirectory());
224         assertTrue(new File JavaDoc(newProject, "lib").isDirectory());
225     }
226     
227     public void testCopyWithInnerProjectSimple() throws Exception JavaDoc {
228         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "lib/test.txt");
229         FileObject projdir2 = projdir.createFolder("proj2");
230         
231         createProject(projdir2);
232         
233         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
234         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
235         FileObject newTarget = prj.getProjectDirectory().getParent();
236         
237         DefaultProjectOperationsImplementation.doCopyProject(handle, prj, "projCopy", newTarget);
238         
239         File JavaDoc newProject = new File JavaDoc(FileUtil.toFile(newTarget), "projCopy");
240         
241         assertTrue(newProject.isDirectory());
242         assertTrue(new File JavaDoc(newProject, "nbproject").isDirectory());
243         assertTrue(new File JavaDoc(newProject, "src").isDirectory());
244         assertTrue(new File JavaDoc(newProject, "lib").isDirectory());
245         assertFalse(new File JavaDoc(newProject, "proj2").exists());
246     }
247     
248     public void testCopyWithInnerProjectComplex() throws Exception JavaDoc {
249         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "lib/test.txt");
250         FileObject projdir2 = projdir.getFileObject("lib").createFolder("proj2");
251         
252         createProject(projdir2);
253         
254         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
255         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
256         
257         FileObject newTarget = prj.getProjectDirectory().getParent();
258         
259         DefaultProjectOperationsImplementation.doCopyProject(handle, prj, "projCopy", newTarget);
260         
261         File JavaDoc newProject = new File JavaDoc(FileUtil.toFile(newTarget), "projCopy");
262         
263         assertTrue(newProject.isDirectory());
264         assertTrue(new File JavaDoc(newProject, "nbproject").isDirectory());
265         assertTrue(new File JavaDoc(newProject, "src").isDirectory());
266         assertTrue(new File JavaDoc(newProject, "lib").isDirectory());
267         assertFalse(new File JavaDoc(new File JavaDoc(newProject, "lib"), "proj2").exists());
268     }
269
270     public void testMainProjectFlagNotMovedWhenCopying() throws Exception JavaDoc {
271         OpenProjects.getDefault().open(new Project[] {prj}, false);
272         
273         Project main = OpenProjects.getDefault().getMainProject();
274         
275         assertTrue(main == null || !prj.getProjectDirectory().equals(main.getProjectDirectory()));
276         
277         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
278         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
279         FileObject oldProject = prj.getProjectDirectory();
280         File JavaDoc oldProjectFile = FileUtil.toFile(oldProject);
281         FileObject newTarget = oldProject.getParent();
282         
283         DefaultProjectOperationsImplementation.doCopyProject(handle, prj, "projCopy", newTarget);
284         
285         assertTrue(main == null || OpenProjects.getDefault().getMainProject().equals(main.getProjectDirectory()));
286     }
287     //</editor-fold>
288

289     //<editor-fold defaultstate="collapsed" desc="Move Operation">
290
public void testMoveWithLib() throws Exception JavaDoc {
291         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "lib/test.txt");
292         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
293         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
294         FileObject oldProject = prj.getProjectDirectory();
295         File JavaDoc oldProjectFile = FileUtil.toFile(oldProject);
296         FileObject newTarget = oldProject.getParent();
297         
298         DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove","projMove", newTarget, "ERR_Cannot_Move");
299         
300         File JavaDoc newProject = new File JavaDoc(FileUtil.toFile(newTarget), "projMove");
301         
302         assertTrue(newProject.isDirectory());
303         assertTrue(new File JavaDoc(newProject, "nbproject").isDirectory());
304         assertTrue(new File JavaDoc(newProject, "src").isDirectory());
305         assertTrue(new File JavaDoc(newProject, "lib").isDirectory());
306         
307         assertFalse(oldProjectFile.exists());
308     }
309     
310     public void testMoveWithInnerProjectSimple() throws Exception JavaDoc {
311         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "lib/test.txt");
312         FileObject projdir2 = projdir.createFolder("proj2");
313         
314         createProject(projdir2);
315         
316         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
317         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
318         FileObject oldProject = prj.getProjectDirectory();
319         File JavaDoc oldProjectFile = FileUtil.toFile(oldProject);
320         FileObject newTarget = oldProject.getParent();
321         
322         DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove", "projMove", newTarget, "ERR_Cannot_Move");
323         
324         File JavaDoc newProject = new File JavaDoc(FileUtil.toFile(newTarget), "projMove");
325         
326         assertTrue(newProject.isDirectory());
327         assertTrue(new File JavaDoc(newProject, "nbproject").isDirectory());
328         assertTrue(new File JavaDoc(newProject, "src").isDirectory());
329         assertTrue(new File JavaDoc(newProject, "lib").isDirectory());
330         assertFalse(new File JavaDoc(newProject, "proj2").exists());
331         
332         assertTrue(new File JavaDoc(oldProjectFile, "proj2").exists());
333         assertTrue(new File JavaDoc(new File JavaDoc(oldProjectFile, "proj2"), "nbproject").exists());
334     }
335     
336     public void testMoveWithInnerProjectComplex() throws Exception JavaDoc {
337         TestUtil.createFileFromContent(DefaultProjectOperationsImplementationTest.class.getResource("data/test.txt"), projdir, "lib/test.txt");
338         FileObject projdir2 = projdir.getFileObject("lib").createFolder("proj2");
339         
340         createProject(projdir2);
341         
342         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
343         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
344         
345         FileObject oldProject = prj.getProjectDirectory();
346         File JavaDoc oldProjectFile = FileUtil.toFile(oldProject);
347         FileObject newTarget = oldProject.getParent();
348         
349         DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove", "projMove", newTarget, "ERR_Cannot_Move");
350         
351         File JavaDoc newProject = new File JavaDoc(FileUtil.toFile(newTarget), "projMove");
352         
353         assertTrue(newProject.isDirectory());
354         assertTrue(new File JavaDoc(newProject, "nbproject").isDirectory());
355         assertTrue(new File JavaDoc(newProject, "src").isDirectory());
356         assertTrue(new File JavaDoc(newProject, "lib").isDirectory());
357         assertFalse(new File JavaDoc(new File JavaDoc(newProject, "lib"), "proj2").exists());
358
359         assertTrue(new File JavaDoc(new File JavaDoc(oldProjectFile, "lib"), "proj2").exists());
360         assertTrue(new File JavaDoc(new File JavaDoc(new File JavaDoc(oldProjectFile, "lib"), "proj2"), "nbproject").exists());
361     }
362     
363     public void testMainProjectFlagMovedForMainProject() throws Exception JavaDoc {
364         OpenProjects.getDefault().open(new Project[] {prj}, false);
365         OpenProjects.getDefault().setMainProject(prj);
366         assertEquals(prj, OpenProjects.getDefault().getMainProject());
367         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
368         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
369         FileObject oldProject = prj.getProjectDirectory();
370         File JavaDoc oldProjectFile = FileUtil.toFile(oldProject);
371         FileObject newTarget = oldProject.getParent();
372         
373         DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove", "projMove", newTarget, "ERR_Cannot_Move");
374         
375         Project newProject = ProjectManager.getDefault().findProject(newTarget.getFileObject("projMove"));
376         
377         assertEquals(OpenProjects.getDefault().getMainProject(), newProject);
378     }
379     
380     public void testMainProjectFlagNotMovedForNonMainProject() throws Exception JavaDoc {
381         OpenProjects.getDefault().open(new Project[] {prj}, false);
382         
383         Project main = OpenProjects.getDefault().getMainProject();
384         
385         assertTrue(main == null || !prj.getProjectDirectory().equals(main.getProjectDirectory()));
386         
387         ProgressHandle handle = ProgressHandleFactory.createHandle("test-handle");
388         handle.start(DefaultProjectOperationsImplementation.MAX_WORK);
389         FileObject oldProject = prj.getProjectDirectory();
390         File JavaDoc oldProjectFile = FileUtil.toFile(oldProject);
391         FileObject newTarget = oldProject.getParent();
392         
393         DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove", "projMove", newTarget, "ERR_Cannot_Move");
394         
395         Project newProject = ProjectManager.getDefault().findProject(newTarget.getFileObject("projMove"));
396         
397         main = OpenProjects.getDefault().getMainProject();
398         
399         assertTrue( main == null
400                     || ( !prj.getProjectDirectory().equals(main.getProjectDirectory())
401                          && !newProject.getProjectDirectory().equals(main.getProjectDirectory())));
402     }
403     //</editor-fold>
404

405     //<editor-fold defaultstate="collapsed" desc="Utilities">
406
private static final class TestProject implements Project {
407         
408         private final Lookup l;
409         private final FileObject projectDirectory;
410         
411         TestProject(FileObject projectDirectory) throws IOException JavaDoc {
412             l = Lookups.fixed(new DeleteProjectOperationImpl(this));
413             this.projectDirectory = projectDirectory;
414         }
415         
416         public FileObject getProjectDirectory() {
417             return projectDirectory;
418         }
419         
420         public Lookup getLookup() {
421             return l;
422         }
423         
424         public String JavaDoc toString() {
425             return "TestAntBasedProject[" + getProjectDirectory() + "]";
426         }
427         
428     }
429     
430     public static class TestProjectFactory implements ProjectFactory {
431         
432         public boolean isProject(FileObject projectDirectory) {
433             return projectDirectory.getFileObject("nbproject") != null;
434         }
435         
436         public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException JavaDoc {
437             if (isProject(projectDirectory))
438                 return new TestProject(projectDirectory);
439             
440             return null;
441         }
442         
443         public void saveProject(Project project) throws IOException JavaDoc, ClassCastException JavaDoc {
444         }
445         
446     }
447     
448     public static final class DeleteProjectOperationImpl implements DeleteOperationImplementation {
449         
450         private boolean wasCleaned = false;
451         private boolean wasNotified = false;
452         
453         private FileObject externalFile = null;
454         
455         private TestProject project;
456         
457         public DeleteProjectOperationImpl(TestProject project) {
458             this.project = project;
459         }
460         
461         public List JavaDoc<FileObject> getMetadataFiles() {
462             return Collections.singletonList(project.getProjectDirectory().getFileObject("nbproject"));
463         }
464         
465         public List JavaDoc<FileObject> getDataFiles() {
466             if (externalFile == null) {
467                 return Collections.singletonList(project.getProjectDirectory().getFileObject("src"));
468             } else {
469                 return Arrays.asList(project.getProjectDirectory().getFileObject("src"), externalFile);
470             }
471         }
472         
473         public void setExternalFile(FileObject externalFile) {
474             this.externalFile = externalFile;
475         }
476         
477         public synchronized boolean getWasCleaned() {
478             return wasCleaned;
479         }
480         
481         public synchronized void notifyDeleting() throws IOException JavaDoc {
482             wasCleaned = true;
483         }
484         
485         public synchronized boolean getWasNotified() {
486             return wasNotified;
487         }
488         
489         public synchronized void notifyDeleted() throws IOException JavaDoc {
490             wasNotified = true;
491         }
492         
493     }
494     //</editor-fold>
495

496 }
497
Popular Tags