KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.junit.MockServices;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.project.ui.actions.TestSupport;
34 import org.netbeans.spi.project.AuxiliaryConfiguration;
35 import org.openide.cookies.EditorCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.filesystems.URLMapper;
39 import org.openide.filesystems.XMLFileSystem;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42 import org.openide.text.CloneableEditorSupport;
43 import org.openide.util.Lookup;
44 import org.openide.util.lookup.Lookups;
45 import org.openide.windows.CloneableTopComponent;
46 import org.openide.windows.TopComponent;
47 import org.openide.windows.WindowManager;
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.NodeList JavaDoc;
50
51 /** Tests storing and reloading project's documents in case of open/close project.
52  *
53  * @author Jiri Rechtacek
54  */

55 public class ProjectUtilitiesTest extends NbTestCase {
56     
57     private static final String JavaDoc NAVIGATOR_MODE = "navigator";
58     
59     DataObject do1_1_open, do1_2_open, do1_3_close, do1_4_close;
60     DataObject do2_1_open;
61     Project project1, project2;
62     Set JavaDoc<DataObject> openFilesSet = new HashSet JavaDoc<DataObject>();
63     TopComponent tc1_1, tc1_2, tc2_1, tc1_1_navigator;
64     
65     public ProjectUtilitiesTest (String JavaDoc testName) {
66         super (testName);
67     }
68     
69     protected boolean runInEQ () {
70         return true;
71     }
72
73     protected void setUp () throws Exception JavaDoc {
74         super.setUp ();
75         MockServices.setServices(TestSupport.TestProjectFactory.class);
76                                 
77         clearWorkDir ();
78         
79         FileObject workDir = FileUtil.toFileObject (getWorkDir ());
80     
81         //Mode mode = WindowManager.getDefault ().createWorkspace ("TestHelper").createMode (CloneableEditorSupport.EDITOR_MODE, CloneableEditorSupport.EDITOR_MODE, null);
82

83         FileObject p1 = TestSupport.createTestProject (workDir, "project1");
84         FileObject f1_1 = p1.createData("f1_1.java");
85         FileObject f1_2 = p1.createData("f1_2.java");
86         FileObject f1_3 = p1.createData("f1_3.java");
87         FileObject f1_4 = p1.createData("f1_4.java");
88         do1_1_open = DataObject.find (f1_1);
89         do1_2_open = DataObject.find (f1_2);
90         do1_3_close = DataObject.find (f1_3);
91         do1_4_close = DataObject.find (f1_4);
92         openFilesSet.add (do1_1_open);
93         openFilesSet.add (do1_2_open);
94
95         project1 = ProjectManager.getDefault ().findProject (p1);
96         ((TestSupport.TestProject) project1).setLookup (Lookups.singleton (TestSupport.createAuxiliaryConfiguration ()));
97         
98         FileObject p2 = TestSupport.createTestProject (workDir, "project2");
99         FileObject f2_1 = p2.createData("f2_1.java");
100         do2_1_open = DataObject.find (f2_1);
101
102         project2 = ProjectManager.getDefault ().findProject (p2);
103         ((TestSupport.TestProject) project2).setLookup (Lookups.singleton (TestSupport.createAuxiliaryConfiguration ()));
104         
105         //it will be necessary to dock the top components into the "editor" and "navigator" modes, so they need to be created:
106
createMode(CloneableEditorSupport.EDITOR_MODE);
107         createMode(NAVIGATOR_MODE);
108         (tc1_1 = new SimpleTopComponent (do1_1_open, CloneableEditorSupport.EDITOR_MODE)).open ();
109         (tc1_2 = new SimpleTopComponent (do1_2_open, CloneableEditorSupport.EDITOR_MODE)).open ();
110         (tc2_1 = new SimpleTopComponent (do2_1_open, CloneableEditorSupport.EDITOR_MODE)).open ();
111         (tc1_1_navigator = new SimpleTopComponent2 (do1_1_open, NAVIGATOR_MODE)).open ();
112         
113         ExitDialog.SAVE_ALL_UNCONDITIONALLY = true;
114     }
115     
116     @SuppressWarnings JavaDoc("deprecation")
117     private static void createMode(String JavaDoc name) {
118         WindowManager.getDefault().getWorkspaces()[0].createMode(name, name, null);
119     }
120
121     public void testCloseAllDocuments () {
122         closeProjectWithOpenedFiles ();
123     }
124     
125     private void closeProjectWithOpenedFiles () {
126         AuxiliaryConfiguration aux = project1.getLookup().lookup(AuxiliaryConfiguration.class);
127         assertNotNull ("AuxiliaryConfiguration must be present if project's lookup", aux);
128
129         Element JavaDoc openFilesEl = aux.getConfigurationFragment (ProjectUtilities.OPEN_FILES_ELEMENT, ProjectUtilities.OPEN_FILES_NS, false);
130         if (openFilesEl != null) {
131             assertEquals ("OpenFiles element is empty or null.", 0, openFilesEl.getChildNodes ().getLength ());
132         }
133         
134         Project[] projects = new Project[] {project1};
135         
136         if (ProjectUtilities.closeAllDocuments(projects, false)) {
137             OpenProjectList.getDefault().close(projects, false);
138         }
139         
140         openFilesEl = aux.getConfigurationFragment (ProjectUtilities.OPEN_FILES_ELEMENT, ProjectUtilities.OPEN_FILES_NS, false);
141         assertNotNull ("OPEN_FILES_ELEMENT found in the private configuration.", openFilesEl);
142         
143         NodeList JavaDoc list = openFilesEl.getElementsByTagName (ProjectUtilities.FILE_ELEMENT);
144         
145         assertNotNull ("FILE_ELEMENT must be present", list);
146         assertTrue ("Same count of FILE_ELEMENTs and open files, elements count " + list.getLength (), openFilesSet.size () == list.getLength ());
147         
148         for (int i = 0; i < list.getLength (); i++) {
149             String JavaDoc url = list.item (i).getChildNodes ().item (0).getNodeValue ();
150             FileObject fo = null;
151             try {
152                 fo = URLMapper.findFileObject (new URL JavaDoc (url));
153                 assertNotNull ("Found file for URL " + url, fo);
154                 DataObject dobj = DataObject.find (fo);
155                 assertTrue (dobj + " is present in the set of open files.", openFilesSet.contains (dobj));
156                 assertNotSame ("The closed file are not present.", do1_3_close, dobj);
157                 assertNotSame ("The open file of other project is not present.", do2_1_open, dobj);
158             } catch (MalformedURLException JavaDoc mue) {
159                 fail ("MalformedURLException in " + url);
160             } catch (DataObjectNotFoundException donfo) {
161                 fail ("DataObject must exist for " + fo);
162             }
163         }
164         
165     }
166     
167     private void modifyDO(DataObject toModify) throws BadLocationException JavaDoc, IOException JavaDoc {
168         System.err.println("toModify = " + toModify );
169         EditorCookie ec = (EditorCookie) toModify.getCookie(EditorCookie.class);
170         
171         ec.openDocument().insertString(0, "test", null);
172     }
173     
174     public void testSavingModifiedNotOpenedFiles67526() throws BadLocationException JavaDoc, IOException JavaDoc {
175         AuxiliaryConfiguration aux = project1.getLookup().lookup(AuxiliaryConfiguration.class);
176         assertNotNull ("AuxiliaryConfiguration must be present if project's lookup", aux);
177         
178         Element JavaDoc openFilesEl = aux.getConfigurationFragment (ProjectUtilities.OPEN_FILES_ELEMENT, ProjectUtilities.OPEN_FILES_NS, false);
179         if (openFilesEl != null) {
180             assertEquals ("OpenFiles element is empty or null.", 0, openFilesEl.getChildNodes ().getLength ());
181         }
182         
183         modifyDO(do1_4_close);
184         
185         Project[] projects = new Project[] {project1};
186         
187         if (ProjectUtilities.closeAllDocuments(projects, true)) {
188             OpenProjectList.getDefault().close(projects, true);
189         }
190         
191         assertFalse("the do1_4_close not modified", do1_4_close.isModified());
192         
193         openFilesEl = aux.getConfigurationFragment (ProjectUtilities.OPEN_FILES_ELEMENT, ProjectUtilities.OPEN_FILES_NS, false);
194         assertNotNull ("OPEN_FILES_ELEMENT found in the private configuration.", openFilesEl);
195         
196         NodeList JavaDoc list = openFilesEl.getElementsByTagName (ProjectUtilities.FILE_ELEMENT);
197         
198         assertNotNull ("FILE_ELEMENT must be present", list);
199         assertTrue ("Same count of FILE_ELEMENTs and open files, elements count " + list.getLength (), openFilesSet.size () == list.getLength ());
200         
201         for (int i = 0; i < list.getLength (); i++) {
202             String JavaDoc url = list.item (i).getChildNodes ().item (0).getNodeValue ();
203             FileObject fo = null;
204             try {
205                 fo = URLMapper.findFileObject (new URL JavaDoc (url));
206                 assertNotNull ("Found file for URL " + url, fo);
207                 DataObject dobj = DataObject.find (fo);
208                 System.err.println("openFilesSet = " + openFilesSet );
209                 assertTrue (dobj + " is present in the set of open files.", openFilesSet.contains (dobj));
210                 assertNotSame ("The closed file are not present.", do1_3_close, dobj);
211                 assertNotSame ("The open file of other project is not present.", do2_1_open, dobj);
212             } catch (MalformedURLException JavaDoc mue) {
213                 fail ("MalformedURLException in " + url);
214             } catch (DataObjectNotFoundException donfo) {
215                 fail ("DataObject must exist for " + fo);
216             }
217         }
218     }
219     
220     public void testCloseAndOpenProjectAndClosedWithoutOpenFiles () {
221         closeProjectWithOpenedFiles ();
222
223         OpenProjectList.getDefault ().open (project1, false);
224
225         Iterator JavaDoc/*<TopComponent>*/ openTCs = WindowManager.getDefault ().getRegistry ().getOpened ().iterator ();
226         while (openTCs.hasNext ()) {
227             assertTrue ("TopComponent has been closed successfully.", ((TopComponent)openTCs.next ()).close ());
228         }
229         
230         if (ProjectUtilities.closeAllDocuments(new Project[] {project1}, false)) {
231             OpenProjectList.getDefault().close(new Project[] {project1}, false);
232         }
233
234         AuxiliaryConfiguration aux = project1.getLookup().lookup(AuxiliaryConfiguration.class);
235         Element JavaDoc openFilesEl = aux.getConfigurationFragment (ProjectUtilities.OPEN_FILES_ELEMENT, ProjectUtilities.OPEN_FILES_NS, false);
236         assertNull ("OPEN_FILES_ELEMENT not found in the private configuration.", openFilesEl);
237         
238         assertFalse ("Project1 must be closed.", OpenProjectList.getDefault ().isOpen (project1));
239     }
240     
241     public void testCanUseFileName() throws Exception JavaDoc {
242         FileObject d = FileUtil.toFileObject(getWorkDir());
243         FileObject p1 = d.getFileObject("project1");
244         assertNotNull(p1);
245         assertNull("normal file addition", ProjectUtilities.canUseFileName(p1, null, "foo", "java", false));
246         assertNull("normal file addition with no extension is OK", ProjectUtilities.canUseFileName(p1, null, "foo", null, false));
247         assertNull("normal file addition in an existing subdir", ProjectUtilities.canUseFileName(d, "project1", "foo", "java", false));
248         assertNull("normal file addition in a new subdir", ProjectUtilities.canUseFileName(d, "dir", "foo", "java", false));
249         //assertNotNull("no target name", ProjectUtilities.canUseFileName(d, "dir", null, "java"));
250
assertNotNull("no target folder", ProjectUtilities.canUseFileName(null, "dir", "foo", "java", false));
251         assertNotNull("file already exists", ProjectUtilities.canUseFileName(p1, null, "f1_1", "java", false));
252         assertNotNull("file already exists in subdir", ProjectUtilities.canUseFileName(d, "project1", "f1_1", "java", false));
253         assertNull("similar file already exists in subdir", ProjectUtilities.canUseFileName(d, "project1", "f1_1", "properties", false));
254         assertNull("similar file already exists in subdir", ProjectUtilities.canUseFileName(d, "project1", "f1_1", null, false));
255         d = new XMLFileSystem().getRoot();
256         assertNotNull("FS is r/o", ProjectUtilities.canUseFileName(d, null, "foo", "java", false));
257         // #59876: deal with non-disk-based filesystems sensibly
258
d = FileUtil.createMemoryFileSystem().getRoot();
259         d.createData("bar.java");
260         FileUtil.createData(d, "sub/dir/foo.java");
261         assertNull("can create file in non-disk FS", ProjectUtilities.canUseFileName(d, null, "foo", "java", false));
262         assertNotNull("file already exists", ProjectUtilities.canUseFileName(d, null, "bar", "java", false));
263         assertNotNull("file already exists in subsubdir", ProjectUtilities.canUseFileName(d, "sub/dir", "foo", "java", false));
264         assertNull("can otherwise create file in subsubdir", ProjectUtilities.canUseFileName(d, "sub/dir", "bar", "java", false));
265         //#66792: allow to create whole directory tree at once using Folder Template:
266
assertNull("can create directory subtree", ProjectUtilities.canUseFileName(d, null, "a/b/c", null, true));
267         //#59654: do not allow slash and backslash for common templates:
268
assertNotNull("cannot create file with slashes", ProjectUtilities.canUseFileName(d, null, "a/b/c", "txt", false));
269         assertNotNull("cannot create file with backslashes", ProjectUtilities.canUseFileName(d, null, "a\\b\\c", "txt", false));
270     }
271     
272     public void testNavigatorIsNotClosed() throws Exception JavaDoc {
273         closeProjectWithOpenedFiles ();
274         
275         assertFalse(tc1_1.isOpened());
276         assertFalse(tc1_2.isOpened());
277         assertTrue(tc1_1_navigator.isOpened());
278     }
279
280     private static class SimpleTopComponent extends CloneableTopComponent {
281         private Object JavaDoc content;
282         private String JavaDoc modeToDockInto;
283         public SimpleTopComponent (Object JavaDoc obj, String JavaDoc modeToDockInto) {
284             this.content = obj;
285             this.modeToDockInto = modeToDockInto;
286             setName (obj.toString ());
287         }
288         
289         public Lookup getLookup () {
290             return Lookups.singleton (content);
291         }
292         
293         public void open() {
294             super.open();
295             WindowManager.getDefault().findMode(modeToDockInto).dockInto(this);
296         }
297     }
298     
299     private static class SimpleTopComponent2 extends TopComponent {
300         private Object JavaDoc content;
301         private String JavaDoc modeToDockInto;
302         public SimpleTopComponent2 (Object JavaDoc obj, String JavaDoc modeToDockInto) {
303             this.content = obj;
304             this.modeToDockInto = modeToDockInto;
305             setName (obj.toString ());
306         }
307         
308         public Lookup getLookup () {
309             return Lookups.singleton (content);
310         }
311         
312         public void open() {
313             super.open();
314             WindowManager.getDefault().findMode(modeToDockInto).dockInto(this);
315         }
316     }
317     
318 }
319
Popular Tags