1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.io.IOException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import javax.swing.text.BadLocationException ; 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 ; 49 import org.w3c.dom.NodeList ; 50 51 55 public class ProjectUtilitiesTest extends NbTestCase { 56 57 private static final String 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 <DataObject> openFilesSet = new HashSet <DataObject>(); 63 TopComponent tc1_1, tc1_2, tc2_1, tc1_1_navigator; 64 65 public ProjectUtilitiesTest (String testName) { 66 super (testName); 67 } 68 69 protected boolean runInEQ () { 70 return true; 71 } 72 73 protected void setUp () throws Exception { 74 super.setUp (); 75 MockServices.setServices(TestSupport.TestProjectFactory.class); 76 77 clearWorkDir (); 78 79 FileObject workDir = FileUtil.toFileObject (getWorkDir ()); 80 81 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 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 ("deprecation") 117 private static void createMode(String 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 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 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 url = list.item (i).getChildNodes ().item (0).getNodeValue (); 150 FileObject fo = null; 151 try { 152 fo = URLMapper.findFileObject (new URL (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 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 , IOException { 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 , IOException { 175 AuxiliaryConfiguration aux = project1.getLookup().lookup(AuxiliaryConfiguration.class); 176 assertNotNull ("AuxiliaryConfiguration must be present if project's lookup", aux); 177 178 Element 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 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 url = list.item (i).getChildNodes ().item (0).getNodeValue (); 203 FileObject fo = null; 204 try { 205 fo = URLMapper.findFileObject (new URL (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 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 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 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 { 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 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 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 assertNull("can create directory subtree", ProjectUtilities.canUseFileName(d, null, "a/b/c", null, true)); 267 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 { 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 content; 282 private String modeToDockInto; 283 public SimpleTopComponent (Object obj, String 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 content; 301 private String modeToDockInto; 302 public SimpleTopComponent2 (Object obj, String 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 |