1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.SortedSet ; 30 import java.util.TreeSet ; 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 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 testName) { 59 super (testName); 60 } 61 62 protected void setUp () throws Exception { 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 = ProjectManager.getDefault ().findProject (p2); 84 ((TestSupport.TestProject) project2).setLookup(Lookups.fixed(TestSupport.createAuxiliaryConfiguration(), new MySubprojectProvider(project1))); 85 86 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 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 { 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 { 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 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 { 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 { 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 { 161 ((TestSupport.TestProject) project1).setLookup(Lookups.fixed(new Object [] { 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 { 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 { 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 e) { 263 exceptionThrown = true; 264 } 265 266 assertTrue("IAE thrown when trying to set main project that is not opened", exceptionThrown); 267 268 exceptionThrown = false; 270 271 try { 272 OpenProjectList.getDefault().setMainProject(project1); 273 } catch (IllegalArgumentException e) { 274 exceptionThrown = true; 275 } 276 277 assertTrue("IAE thrown when trying to set main project that is not opened", exceptionThrown); 278 } 279 281 private static class MySubprojectProvider implements SubprojectProvider { 282 Project p; 283 public MySubprojectProvider (final Project project) { 284 p = project; 285 } 286 public Set <Project> getSubprojects() { 287 return Collections.singleton (p); 288 } 289 290 public void removeChangeListener (javax.swing.event.ChangeListener changeListener) {} 291 public void addChangeListener (javax.swing.event.ChangeListener changeListener) {} 292 293 } 294 295 private static class TestOpenCloseProjectDocument implements ProjectUtilities.OpenCloseProjectDocument { 296 public Set <String > openFiles = new HashSet <String >(); 297 public Map <Project,SortedSet <String >> urls4project = new HashMap <Project,SortedSet <String >>(); 298 299 public boolean open (FileObject fo) { 300 Project owner = FileOwnerQuery.getOwner (fo); 301 if (!urls4project.containsKey (owner)) { 302 urls4project.put(owner, new TreeSet <String >()); 304 } 305 URL 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 <Project,SortedSet <String >> close(Project[] projects, boolean notifyUI) { 321 322 for (int i = 0; i < projects.length; i++) { 323 SortedSet <String > projectOpenFiles = urls4project.get(projects [i]); 324 if (projectOpenFiles != null) { 325 projectOpenFiles.retainAll (openFiles); 326 urls4project.put (projects [i], projectOpenFiles); 327 for (String url : projectOpenFiles) { 328 FileObject fo = null; 329 try { 330 fo = URLMapper.findFileObject (new URL (url)); 331 openFiles.remove (fo.getURL ().toExternalForm ()); 332 } catch (MalformedURLException 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 |