KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > OpenProjectListTest


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
20 package org.netbeans.modules.project.ui;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import org.netbeans.api.project.FileOwnerQuery;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.ProjectManager;
34 import org.netbeans.junit.MockServices;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.project.ui.actions.TestSupport;
37 import org.netbeans.spi.project.SubprojectProvider;
38 import org.netbeans.spi.project.ui.ProjectOpenedHook;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileStateInvalidException;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.filesystems.URLMapper;
43 import org.openide.loaders.DataObject;
44 import org.openide.loaders.DataObjectNotFoundException;
45 import org.openide.util.lookup.Lookups;
46
47 /** Tests fix of issue 56454.
48  *
49  * @author Jiri Rechtacek
50  */

51 public class OpenProjectListTest extends NbTestCase {
52     FileObject f1_1_open, f1_2_open, f1_3_close;
53     FileObject f2_1_open;
54
55     Project project1, project2;
56     TestOpenCloseProjectDocument handler = new OpenProjectListTest.TestOpenCloseProjectDocument ();
57
58     public OpenProjectListTest (String JavaDoc testName) {
59         super (testName);
60     }
61
62     protected void setUp () throws Exception JavaDoc {
63         super.setUp ();
64         MockServices.setServices(TestSupport.TestProjectFactory.class);
65         clearWorkDir ();
66         
67         ProjectUtilities.OPEN_CLOSE_PROJECT_DOCUMENT_IMPL = handler;
68         
69         FileObject workDir = FileUtil.toFileObject (getWorkDir ());
70     
71         FileObject p1 = TestSupport.createTestProject (workDir, "project1");
72         f1_1_open = p1.createData("f1_1.java");
73         f1_2_open = p1.createData("f1_2.java");
74         f1_3_close = p1.createData("f1_3.java");
75
76         project1 = ProjectManager.getDefault ().findProject (p1);
77         ((TestSupport.TestProject) project1).setLookup (Lookups.singleton (TestSupport.createAuxiliaryConfiguration ()));
78         
79         FileObject p2 = TestSupport.createTestProject (workDir, "project2");
80         f2_1_open = p2.createData ("f2_1.java");
81
82         // project2 depends on projects1
83
project2 = ProjectManager.getDefault ().findProject (p2);
84         ((TestSupport.TestProject) project2).setLookup(Lookups.fixed(TestSupport.createAuxiliaryConfiguration(), new MySubprojectProvider(project1)));
85         
86         // prepare set of open documents for both projects
87
ProjectUtilities.OPEN_CLOSE_PROJECT_DOCUMENT_IMPL.open (f1_1_open);
88         ProjectUtilities.OPEN_CLOSE_PROJECT_DOCUMENT_IMPL.open (f1_2_open);
89         ProjectUtilities.OPEN_CLOSE_PROJECT_DOCUMENT_IMPL.open (f2_1_open);
90         
91         // close both projects with own open files
92
OpenProjectList.getDefault().close(new Project[] {project1, project2}, false);
93     }
94     
95     protected void tearDown () {
96         OpenProjectList.getDefault().close(new Project[] {project1, project2}, false);
97     }
98
99     public void testOpen () throws Exception JavaDoc {
100         assertTrue ("No project is open.", OpenProjectList.getDefault ().getOpenProjects ().length == 0);
101         OpenProjectList.getDefault ().open (project1, true);
102         assertTrue ("Project1 is opened.", OpenProjectList.getDefault ().isOpen (project1));
103         
104         assertTrue ("Document f1_1_open is loaded.", handler.openFiles.contains (f1_1_open.getURL ().toExternalForm ()));
105         assertTrue ("Document f1_2_open is loaded.", handler.openFiles.contains (f1_2_open.getURL ().toExternalForm ()));
106         assertFalse ("Document f2_1_open isn't loaded.", handler.openFiles.contains (f2_1_open.getURL ().toExternalForm ()));
107     }
108     
109     public void testClose () throws Exception JavaDoc {
110         testOpen ();
111         
112         OpenProjectList.getDefault().close(new Project[] {project1}, false);
113         assertFalse ("Document f1_1_open isn't loaded.", handler.openFiles.contains (f1_1_open.getURL ().toExternalForm ()));
114         assertFalse ("Document f1_2_open isn't loaded.", handler.openFiles.contains (f1_2_open.getURL ().toExternalForm ()));
115         assertFalse ("Document f2_1_open isn't loaded.", handler.openFiles.contains (f2_1_open.getURL ().toExternalForm ()));
116         
117         OpenProjectList.getDefault ().open (project1);
118         OpenProjectList.getDefault ().open (project2);
119         
120         // close all project1's documents
121
handler.openFiles.remove (f1_1_open.getURL ().toExternalForm ());
122         handler.openFiles.remove (f1_2_open.getURL ().toExternalForm ());
123         
124         ProjectUtilities.closeAllDocuments(new Project[] {project1}, false);
125         OpenProjectList.getDefault().close(new Project[] {project1}, false);
126
127         OpenProjectList.getDefault ().open (project1);
128         assertFalse ("Document f1_1_open isn't loaded.", handler.openFiles.contains (f1_1_open.getURL ().toExternalForm ()));
129         assertFalse ("Document f1_2_open isn't loaded.", handler.openFiles.contains (f1_2_open.getURL ().toExternalForm ()));
130         assertTrue ("Document f2_1_open is still loaded.", handler.openFiles.contains (f2_1_open.getURL ().toExternalForm ()));
131     }
132     
133     public void testOpenDependingProject () throws Exception JavaDoc {
134         assertTrue ("No project is open.", OpenProjectList.getDefault ().getOpenProjects ().length == 0);
135         OpenProjectList.getDefault ().open (project2, true);
136         assertTrue ("Project1 is opened.", OpenProjectList.getDefault ().isOpen (project1));
137         assertTrue ("Project2 is opened.", OpenProjectList.getDefault ().isOpen (project2));
138         
139         assertTrue ("Document f1_1_open is loaded.", handler.openFiles.contains (f1_1_open.getURL ().toExternalForm ()));
140         assertTrue ("Document f1_2_open is loaded.", handler.openFiles.contains (f1_2_open.getURL ().toExternalForm ()));
141         assertTrue ("Document f2_1_open is loaded.", handler.openFiles.contains (f2_1_open.getURL ().toExternalForm ()));
142     }
143     
144     public void testCloseProjectWithoutOpenDocuments () throws Exception JavaDoc {
145         assertTrue ("No project is open.", OpenProjectList.getDefault ().getOpenProjects ().length == 0);
146         OpenProjectList.getDefault ().open (project2, false);
147         assertFalse ("Project1 isn't opened.", OpenProjectList.getDefault ().isOpen (project1));
148         assertTrue ("Project2 is opened.", OpenProjectList.getDefault ().isOpen (project2));
149         
150         handler.openFiles.remove (f2_1_open.getURL ().toExternalForm ());
151         
152         assertFalse ("Document f2_1_open isn't loaded.", handler.openFiles.contains (f2_1_open.getURL ().toExternalForm ()));
153         
154         ProjectUtilities.closeAllDocuments(new Project[] {project2}, false);
155         OpenProjectList.getDefault().close(new Project[] {project2}, false);
156
157         assertFalse ("Project2 is closed.", OpenProjectList.getDefault ().isOpen (project2));
158     }
159     
160     public void testProjectOpenedClosed() throws Exception JavaDoc {
161         ((TestSupport.TestProject) project1).setLookup(Lookups.fixed(new Object JavaDoc[] {
162             new TestProjectOpenedHookImpl(),
163             new TestProjectOpenedHookImpl(),
164         }));
165         
166         TestProjectOpenedHookImpl.opened = 0;
167         TestProjectOpenedHookImpl.closed = 0;
168         
169         OpenProjectList.getDefault().open(project1);
170         
171         assertEquals("both open hooks were called", 2, TestProjectOpenedHookImpl.opened);
172         assertEquals("no close hook was called", 0, TestProjectOpenedHookImpl.closed);
173         
174         OpenProjectList.getDefault().close(new Project[] {project1}, false);
175         
176         assertEquals("both open hooks were called", 2, TestProjectOpenedHookImpl.opened);
177         assertEquals("both close hooks were called", 2, TestProjectOpenedHookImpl.closed);
178     }
179     
180     public void testNotifyDeleted() throws Exception JavaDoc {
181         FileObject workDir = FileUtil.toFileObject (getWorkDir ());
182         
183         FileObject p1 = workDir.createFolder("p1");
184         FileObject p1TestProject = p1.createFolder("testproject");
185         
186         Project project1 = ProjectManager.getDefault().findProject(p1);
187         
188         assertNotNull("project1 is recognized", project1);
189         
190         OpenProjectList.getDefault().open(project1);
191         
192         OpenProjectList.getDefault().close(new Project[] {project1}, false);
193         
194         p1TestProject.delete();
195         TestSupport.notifyDeleted(project1);
196         
197         assertNull("project1 is deleted", ProjectManager.getDefault().findProject(p1));
198         
199         assertFalse("project1 is not in recent projects list", OpenProjectList.getDefault().getRecentProjects().contains(project1));
200         
201         FileObject p2 = workDir.createFolder("p2");
202         FileObject p2TestProject = p2.createFolder("testproject");
203         
204         Project project2 = ProjectManager.getDefault().findProject(p2);
205         
206         assertNotNull("project2 is recognized", project2);
207         OpenProjectList.getDefault().open(project2);
208         
209         OpenProjectList.getDefault().close(new Project[] {project2}, false);
210         
211         TestSupport.notifyDeleted(project2);
212         
213         assertFalse("project2 is not in recent projects list", OpenProjectList.getDefault().getRecentProjects().contains(project2));
214     }
215     
216     public void testMainProject() throws Exception JavaDoc {
217         FileObject workDir = FileUtil.toFileObject (getWorkDir ());
218         
219         FileObject p1 = workDir.createFolder("p1");
220         FileObject p1TestProject = p1.createFolder("testproject");
221         
222         Project project1 = ProjectManager.getDefault().findProject(p1);
223         
224         assertNotNull("project1 is recognized", project1);
225         
226         FileObject p2 = workDir.createFolder("p2");
227         FileObject p2TestProject = p2.createFolder("testproject");
228         
229         Project project2 = ProjectManager.getDefault().findProject(p2);
230         
231         assertNotNull("project2 is recognized", project2);
232         
233         FileObject p3 = workDir.createFolder("p3");
234         FileObject p3TestProject = p3.createFolder("testproject");
235         
236         Project project3 = ProjectManager.getDefault().findProject(p3);
237         
238         assertNotNull("project3 is recognized", project3);
239         
240         assertNull("no main project set when OPL is empty", OpenProjectList.getDefault().getMainProject());
241         
242         OpenProjectList.getDefault().open(project1);
243         
244         assertNull("open project does not change main project", OpenProjectList.getDefault().getMainProject());
245         
246         OpenProjectList.getDefault().setMainProject(project1);
247         
248         assertTrue("main project correctly set", OpenProjectList.getDefault().getMainProject() == project1);
249         
250         OpenProjectList.getDefault().open(project2);
251         
252         assertTrue("open project does not change main project", OpenProjectList.getDefault().getMainProject() == project1);
253         
254         OpenProjectList.getDefault().close(new Project[] {project1}, false);
255         
256         assertNull("no main project set when main project is closed", OpenProjectList.getDefault().getMainProject());
257         
258         boolean exceptionThrown = false;
259         
260         try {
261             OpenProjectList.getDefault().setMainProject(project3);
262         } catch (IllegalArgumentException JavaDoc e) {
263             exceptionThrown = true;
264         }
265         
266         assertTrue("IAE thrown when trying to set main project that is not opened", exceptionThrown);
267         
268         //the same for a previously opened project:
269
exceptionThrown = false;
270         
271         try {
272             OpenProjectList.getDefault().setMainProject(project1);
273         } catch (IllegalArgumentException JavaDoc e) {
274             exceptionThrown = true;
275         }
276         
277         assertTrue("IAE thrown when trying to set main project that is not opened", exceptionThrown);
278     }
279     // helper code
280

281     private static class MySubprojectProvider implements SubprojectProvider {
282         Project p;
283         public MySubprojectProvider (final Project project) {
284             p = project;
285         }
286         public Set JavaDoc<Project> getSubprojects() {
287             return Collections.singleton (p);
288         }
289         
290         public void removeChangeListener (javax.swing.event.ChangeListener JavaDoc changeListener) {}
291         public void addChangeListener (javax.swing.event.ChangeListener JavaDoc changeListener) {}
292
293     }
294     
295     private static class TestOpenCloseProjectDocument implements ProjectUtilities.OpenCloseProjectDocument {
296         public Set JavaDoc<String JavaDoc> openFiles = new HashSet JavaDoc<String JavaDoc>();
297         public Map JavaDoc<Project,SortedSet JavaDoc<String JavaDoc>> urls4project = new HashMap JavaDoc<Project,SortedSet JavaDoc<String JavaDoc>>();
298         
299         public boolean open (FileObject fo) {
300             Project owner = FileOwnerQuery.getOwner (fo);
301             if (!urls4project.containsKey (owner)) {
302               // add project
303
urls4project.put(owner, new TreeSet JavaDoc<String JavaDoc>());
304             }
305             URL JavaDoc url = null;
306             DataObject dobj = null;
307             try {
308                 dobj = DataObject.find (fo);
309                 url = dobj.getPrimaryFile ().getURL ();
310                 urls4project.get(owner).add(url.toExternalForm());
311                 openFiles.add (fo.getURL ().toExternalForm ());
312             } catch (FileStateInvalidException fsie) {
313                 fail ("FileStateInvalidException in " + dobj.getPrimaryFile ());
314             } catch (DataObjectNotFoundException donfe) {
315                 fail ("DataObjectNotFoundException on " + fo);
316             }
317             return true;
318         }
319         
320         public Map JavaDoc<Project,SortedSet JavaDoc<String JavaDoc>> close(Project[] projects, boolean notifyUI) {
321             
322             for (int i = 0; i < projects.length; i++) {
323                 SortedSet JavaDoc<String JavaDoc> projectOpenFiles = urls4project.get(projects [i]);
324                 if (projectOpenFiles != null) {
325                     projectOpenFiles.retainAll (openFiles);
326                     urls4project.put (projects [i], projectOpenFiles);
327                     for (String JavaDoc url : projectOpenFiles) {
328                         FileObject fo = null;
329                         try {
330                             fo = URLMapper.findFileObject (new URL JavaDoc (url));
331                             openFiles.remove (fo.getURL ().toExternalForm ());
332                         } catch (MalformedURLException JavaDoc mue) {
333                             fail ("MalformedURLException in " + url);
334                         } catch (FileStateInvalidException fsie) {
335                             fail ("FileStateInvalidException in " + fo);
336                         }
337                     }
338                 }
339             }
340             
341             return urls4project;
342         }
343     }
344     
345     private static class TestProjectOpenedHookImpl extends ProjectOpenedHook {
346         
347         public static int opened = 0;
348         public static int closed = 0;
349         
350         protected void projectClosed() {
351             closed++;
352         }
353         
354         protected void projectOpened() {
355             opened++;
356         }
357         
358     }
359 }
360
Popular Tags