1 19 package org.netbeans.modules.project.uiapi; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import javax.swing.JComponent ; 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 48 public class DefaultProjectOperationsImplementationTest extends NbTestCase { 49 50 public DefaultProjectOperationsImplementationTest(String testName) { 51 super(testName); 52 } 53 54 private FileObject scratch; 55 private FileObject projdir; 56 private Project prj; 57 private File projectDirectory; 58 59 private void createProject(FileObject projdir) throws Exception { 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 { 65 scratch = TestUtil.makeScratchDir(this); 66 projdir = scratch.createFolder("proj"); 67 68 createProject(projdir); 69 70 TestUtil.setLookup(new Object [] { 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 public void testDeleteProjectDeleteAll() throws Exception { 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 { 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 [] {"src"}, projectDirectory.list())); 104 } 105 106 public void testDeleteProjectDoNotDelete() throws Exception { 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 <String > items = Arrays.asList(projectDirectory.list()); 115 Collections.sort(items); 116 assertEquals(Arrays.asList("nbproject", "src"), items); 117 } 118 119 public void testDeleteProjectNestedProject() throws Exception { 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 [] {"proj2"}, projectDirectory.list())); 132 } 133 134 public void testDeleteProjectNestedLibrary() throws Exception { 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 [] {"lib"}, projectDirectory.list())); 145 } 146 147 public void testDeleteProjectExternalSources() throws Exception { 148 FileObject extDir = scratch.createFolder("external"); 149 File 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 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 panel, Project project, String caption, String confirmButton, String 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 e) { 203 exception = e; 204 } 205 } 206 207 } 208 210 public void testCopyWithLib() throws Exception { 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 newProject = new File (FileUtil.toFile(newTarget), "projCopy"); 220 221 assertTrue(newProject.isDirectory()); 222 assertTrue(new File (newProject, "nbproject").isDirectory()); 223 assertTrue(new File (newProject, "src").isDirectory()); 224 assertTrue(new File (newProject, "lib").isDirectory()); 225 } 226 227 public void testCopyWithInnerProjectSimple() throws Exception { 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 newProject = new File (FileUtil.toFile(newTarget), "projCopy"); 240 241 assertTrue(newProject.isDirectory()); 242 assertTrue(new File (newProject, "nbproject").isDirectory()); 243 assertTrue(new File (newProject, "src").isDirectory()); 244 assertTrue(new File (newProject, "lib").isDirectory()); 245 assertFalse(new File (newProject, "proj2").exists()); 246 } 247 248 public void testCopyWithInnerProjectComplex() throws Exception { 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 newProject = new File (FileUtil.toFile(newTarget), "projCopy"); 262 263 assertTrue(newProject.isDirectory()); 264 assertTrue(new File (newProject, "nbproject").isDirectory()); 265 assertTrue(new File (newProject, "src").isDirectory()); 266 assertTrue(new File (newProject, "lib").isDirectory()); 267 assertFalse(new File (new File (newProject, "lib"), "proj2").exists()); 268 } 269 270 public void testMainProjectFlagNotMovedWhenCopying() throws Exception { 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 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 289 public void testMoveWithLib() throws Exception { 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 oldProjectFile = FileUtil.toFile(oldProject); 296 FileObject newTarget = oldProject.getParent(); 297 298 DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove","projMove", newTarget, "ERR_Cannot_Move"); 299 300 File newProject = new File (FileUtil.toFile(newTarget), "projMove"); 301 302 assertTrue(newProject.isDirectory()); 303 assertTrue(new File (newProject, "nbproject").isDirectory()); 304 assertTrue(new File (newProject, "src").isDirectory()); 305 assertTrue(new File (newProject, "lib").isDirectory()); 306 307 assertFalse(oldProjectFile.exists()); 308 } 309 310 public void testMoveWithInnerProjectSimple() throws Exception { 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 oldProjectFile = FileUtil.toFile(oldProject); 320 FileObject newTarget = oldProject.getParent(); 321 322 DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove", "projMove", newTarget, "ERR_Cannot_Move"); 323 324 File newProject = new File (FileUtil.toFile(newTarget), "projMove"); 325 326 assertTrue(newProject.isDirectory()); 327 assertTrue(new File (newProject, "nbproject").isDirectory()); 328 assertTrue(new File (newProject, "src").isDirectory()); 329 assertTrue(new File (newProject, "lib").isDirectory()); 330 assertFalse(new File (newProject, "proj2").exists()); 331 332 assertTrue(new File (oldProjectFile, "proj2").exists()); 333 assertTrue(new File (new File (oldProjectFile, "proj2"), "nbproject").exists()); 334 } 335 336 public void testMoveWithInnerProjectComplex() throws Exception { 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 oldProjectFile = FileUtil.toFile(oldProject); 347 FileObject newTarget = oldProject.getParent(); 348 349 DefaultProjectOperationsImplementation.doMoveProject(handle, prj, "projMove", "projMove", newTarget, "ERR_Cannot_Move"); 350 351 File newProject = new File (FileUtil.toFile(newTarget), "projMove"); 352 353 assertTrue(newProject.isDirectory()); 354 assertTrue(new File (newProject, "nbproject").isDirectory()); 355 assertTrue(new File (newProject, "src").isDirectory()); 356 assertTrue(new File (newProject, "lib").isDirectory()); 357 assertFalse(new File (new File (newProject, "lib"), "proj2").exists()); 358 359 assertTrue(new File (new File (oldProjectFile, "lib"), "proj2").exists()); 360 assertTrue(new File (new File (new File (oldProjectFile, "lib"), "proj2"), "nbproject").exists()); 361 } 362 363 public void testMainProjectFlagMovedForMainProject() throws Exception { 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 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 { 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 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 405 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 { 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 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 { 437 if (isProject(projectDirectory)) 438 return new TestProject(projectDirectory); 439 440 return null; 441 } 442 443 public void saveProject(Project project) throws IOException , ClassCastException { 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 <FileObject> getMetadataFiles() { 462 return Collections.singletonList(project.getProjectDirectory().getFileObject("nbproject")); 463 } 464 465 public List <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 { 482 wasCleaned = true; 483 } 484 485 public synchronized boolean getWasNotified() { 486 return wasNotified; 487 } 488 489 public synchronized void notifyDeleted() throws IOException { 490 wasNotified = true; 491 } 492 493 } 494 496 } 497 | Popular Tags |