1 19 20 package org.netbeans.spi.java.project.support.ui; 21 22 import java.awt.datatransfer.Transferable ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.OutputStreamWriter ; 28 import java.io.PrintWriter ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Set ; 35 import java.util.ResourceBundle ; 36 import javax.swing.Icon ; 37 import javax.swing.SwingUtilities ; 38 import javax.swing.event.ChangeListener ; 39 import org.netbeans.api.project.SourceGroup; 40 import org.netbeans.api.project.TestUtil; 41 import org.netbeans.api.queries.VisibilityQuery; 42 import org.netbeans.junit.NbTestCase; 43 import org.netbeans.spi.queries.VisibilityQueryImplementation; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.loaders.DataObject; 48 import org.openide.nodes.Children; 49 import org.openide.nodes.Node; 50 import org.openide.util.datatransfer.ExTransferable; 51 import org.openide.util.datatransfer.PasteType; 52 import org.openide.util.lookup.Lookups; 53 54 public class PackageViewTest extends NbTestCase { 55 56 static { 57 System.setProperty("org.netbeans.spi.java.project.support.ui.packageView.TRUNCATE_PACKAGE_NAMES", "true"); 58 } 59 60 public PackageViewTest( String name ) { 61 super( name ); 62 } 63 64 private FileObject root; 65 66 protected void setUp() throws Exception { 67 super.setUp(); 68 TestUtil.setLookup( Lookups.fixed( new Object [] { new VQImpl(), PackageViewTest.class.getClassLoader() } ) ); 74 root = TestUtil.makeScratchDir(this); 75 } 76 77 public void testFolders() throws Exception { 78 assertNull( "source folder should not exist yet", root.getFileObject( "src" ) ); 79 80 81 SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) ); 83 Children ch = PackageView.createPackageView( group ).getChildren(); 84 85 86 FileUtil.createFolder( root, "src/a/b/c" ); 88 assertNodes( ch, 89 new String [] { "a.b.c", }, 90 new int[] { 0, }, 91 true ); 93 95 FileUtil.createFolder( root, "src/KRTEK.folder" ); 97 assertNodes( ch, 98 new String [] { "a.b.c", }, 99 new int[] { 0, } ); 100 101 FileUtil.createData( root, "src/KRTEK.folder/nonignored.file" ); 103 assertNodes( ch, 104 new String [] { "a.b.c", }, 105 new int[] { 0, } ); 106 107 FileObject nonignoredFolder = FileUtil.createFolder( root, "src/KRTEK.folder/nonignored.folder" ); 109 assertNodes( ch, 110 new String [] { "a.b.c", }, 111 new int[] { 0, } ); 112 113 FileObject nonignoredFile = FileUtil.createData( root, "src/KRTEK.folder/nonignored.folder/nonignored.file" ); 115 assertNodes( ch, 116 new String [] { "a.b.c", }, 117 new int[] { 0, } ); 118 119 FileLock nfLock = nonignoredFile.lock(); 121 nonignoredFile.rename( nfLock, "othername.file", null ); 122 nfLock.releaseLock(); 123 assertNodes( ch, 124 new String [] { "a.b.c", }, 125 new int[] { 0, } ); 126 127 nonignoredFile.delete(); 129 assertNodes( ch, 130 new String [] { "a.b.c", }, 131 new int[] { 0, } ); 132 nonignoredFolder.delete(); 133 assertNodes( ch, 134 new String [] { "a.b.c", }, 135 new int[] { 0, } ); 136 137 138 139 FileUtil.createFolder( root, "src/e/f/g" ); 141 assertNodes( ch, 142 new String [] { "a.b.c", "e.f.g", }, 143 new int[] { 0, 0 } ); 144 145 146 FileUtil.createFolder( root, "src/e/KRTEK" ); 148 FileUtil.createFolder( root, "src/e/f/KRTEK.folder" ); 149 FileUtil.createData( root, "src/e/f/KRTEK.file" ); 150 FileUtil.createFolder( root, "src/e/f/g/KRTEK.folder" ); 151 assertNodes( ch, 152 new String [] { "a.b.c", "e.f.g", }, 153 new int[] { 0, 0 } ); 154 155 156 FileUtil.createData( root, "src/e/f/g/Some.java" ); 158 assertNodes( ch, 159 new String [] { "a.b.c", "e.f.g", }, 160 new int[] { 0, 1 } ); 161 162 FileUtil.createData( root, "src/e/f/g/KRTEK.file" ); 164 assertNodes( ch, 165 new String [] { "a.b.c", "e.f.g", }, 166 new int[] { 0, 1 } ); 167 168 FileUtil.createData( root, "src/e/f/g/KRTEK.folder/Tag" ); 170 assertNodes( ch, 171 new String [] { "a.b.c", "e.f.g", }, 172 new int[] { 0, 1 } ); 173 174 175 FileUtil.createFolder( root, "src/x/y/z/KRTEK" ); 177 assertNodes( ch, 178 new String [] { "a.b.c", "e.f.g", "x.y.z" }, 179 new int[] { 0, 1, 0 } ); 180 181 FileObject x_y_z_some = FileUtil.createData( root, "src/x/y/z/Some.java" ); 183 assertNodes( ch, 184 new String [] { "a.b.c", "e.f.g", "x.y.z" }, 185 new int[] { 0, 1, 1 } ); 186 187 x_y_z_some.delete(); 189 assertNodes( ch, 190 new String [] { "a.b.c", "e.f.g", "x.y.z" }, 191 new int[] { 0, 1, 0 } ); 192 193 194 FileObject x_y_some = FileUtil.createData( root, "src/x/y/Some.java" ); 196 assertNodes( ch, 197 new String [] { "a.b.c", "e.f.g", "x.y", "x.y.z" }, 198 new int[] { 0, 1, 1, 0 } ); 199 200 x_y_some.delete(); 202 assertNodes( ch, 203 new String [] { "a.b.c", "e.f.g", "x.y.z" }, 204 new int[] { 0, 1, 0 } ); 205 206 207 FileObject x_y_z_w = FileUtil.createFolder( root, "src/x/y/z/w" ); 209 assertNodes( ch, 210 new String [] { "a.b.c", "e.f.g", "x.y.z.w" }, 211 new int[] { 0, 1, 0 } ); 212 213 x_y_z_w.delete(); 215 assertNodes( ch, 216 new String [] { "a.b.c", "e.f.g", "x.y.z" }, 217 new int[] { 0, 1, 0 } ); 218 219 FileObject x_y = root.getFileObject( "src/x/y" ); 221 x_y.delete(); 222 assertNodes( ch, 223 new String [] { "a.b.c", "e.f.g", "x" }, 224 new int[] { 0, 1, 0 } ); 225 226 FileUtil.createFolder( root, "src/x/v/w" ); 228 assertNodes( ch, 229 new String [] { "a.b.c", "e.f.g", "x.v.w" }, 230 new int[] { 0, 1, 0 } ); 231 FileObject x = root.getFileObject( "src/x" ); 232 x.delete(); 233 assertNodes( ch, 234 new String [] { "a.b.c", "e.f.g" }, 235 new int[] { 0, 1 } ); 236 237 259 260 FileObject e_f_g = root.getFileObject( "src/e/f/g" ); 262 FileLock lock = e_f_g.lock(); 263 e_f_g.rename( lock, "h", null ); 264 lock.releaseLock(); 265 assertNodes( ch, 266 new String [] { "a.b.c", "e.f.h" }, 267 new int[] { 0, 1 } ); 268 269 270 FileObject e_f_h_krtekFolder = root.getFileObject( "src/e/f/h/KRTEK.folder" ); 272 lock = e_f_h_krtekFolder.lock(); 273 e_f_h_krtekFolder.rename( lock, "ZIZALA", null ); 274 lock.releaseLock(); 275 assertNodes( ch, 276 new String [] { "a.b.c", "e.f.h", "e.f.h.ZIZALA" }, 277 new int[] { 0, 1, 1 } ); 278 279 e_f_h_krtekFolder = root.getFileObject( "src/e/f/h/ZIZALA" ); 281 lock = e_f_h_krtekFolder.lock(); 282 e_f_h_krtekFolder.rename( lock, "KRTEK.folder", null ); 283 lock.releaseLock(); 284 assertNodes( ch, 285 new String [] { "a.b.c", "e.f.h" }, 286 new int[] { 0, 1 } ); 287 288 289 FileUtil.createFolder( root, "src/e/f/g" ); 291 FileUtil.createFolder( root, "src/e/f/i" ); 292 FileObject e_f = root.getFileObject( "src/e/f" ); 293 lock = e_f.lock(); 294 e_f.rename( lock, "r", null ); 295 lock.releaseLock(); 296 assertNodes( ch, 297 new String [] { "a.b.c", "e.r.g", "e.r.h", "e.r.i" }, 298 new int[] { 0, 0, 1, 0 } ); 299 300 FileObject e = root.getFileObject( "src/e/" ); 302 lock = e.lock(); 303 e.rename( lock, "t", null ); 304 lock.releaseLock(); 305 assertNodes( ch, 306 new String [] { "a.b.c", "t.r.g", "t.r.h", "t.r.i" }, 307 new int[] { 0, 0, 1, 0 } ); 308 309 FileUtil.createFolder(root, "src/org/foo/something/whatever"); 311 assertNodes( ch, 312 new String [] { "a.b.c", "o.foo.som.whatever", "t.r.g", "t.r.h", "t.r.i" }, 313 new int[] { 0, 0, 0, 1, 0 } ); 314 315 316 317 } 318 319 public void testDefaultPackage() throws Exception { 320 SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) ); 322 Children ch = PackageView.createPackageView( group ).getChildren(); 323 324 assertNodes( ch, 326 new String [] { "<default package>" }, 327 new int[] { 0 }, 328 true ); 330 FileObject a = FileUtil.createFolder( root, "src/a" ); 332 assertNodes( ch, 333 new String [] { "a", }, 334 new int[] { 0, } ); 335 336 FileObject someJava = FileUtil.createData( root, "src/Some.java" ); 338 assertNodes( ch, 339 new String [] { "<default package>", "a", }, 340 new int[] { 1, 0, } ); 341 342 someJava.delete(); 344 assertNodes( ch, 345 new String [] { "a", }, 346 new int[] { 0, } ); 347 348 a.delete(); 350 assertNodes( ch, 351 new String [] { "<default package>" }, 352 new int[] { 0 } ); 353 354 } 355 356 public void testNodeDestroy() throws Exception { 357 FileObject srcRoot; 358 FileObject toDelete; 359 SourceGroup group; 360 Node rootNode; 361 Node n; 362 363 srcRoot = FileUtil.createFolder( root, "ep" ); 365 toDelete = FileUtil.createFolder( srcRoot, "a/aa" ); 366 group = new SimpleSourceGroup( srcRoot ); 367 rootNode = PackageView.createPackageView( group ); 368 369 assertNodes( rootNode.getChildren(), 371 new String [] { "a.aa", }, 372 new int[] { 0, }, 373 true ); 375 376 n = PackageView.findPath( rootNode, toDelete ); 377 n.destroy(); 378 assertFileObjects( srcRoot, new String [0] ); 379 380 srcRoot = FileUtil.createFolder( root, "nep" ); 382 toDelete = FileUtil.createFolder( srcRoot, "a/aa" ); 383 FileUtil.createData( srcRoot, "a/some.java" ); 384 group = new SimpleSourceGroup( srcRoot ); 385 rootNode = PackageView.createPackageView( group ); 386 assertNodes( rootNode.getChildren(), 388 new String [] { "a", "a.aa" }, 389 new int[] { 1, 0 }, 390 true ); 392 393 n = PackageView.findPath( rootNode, toDelete ); 394 n.destroy(); 395 assertFileObjects( srcRoot, new String []{ "a" } ); 396 397 398 srcRoot = FileUtil.createFolder( root, "es" ); 400 FileObject a = FileUtil.createFolder( srcRoot, "a" ); 401 FileUtil.createFolder( a, "aa" ); 402 FileUtil.createData( srcRoot, "a/aa/some.java" ); 403 toDelete = FileUtil.createFolder( srcRoot, "a/b" ); 404 group = new SimpleSourceGroup( srcRoot ); 405 rootNode = PackageView.createPackageView( group ); 406 assertNodes( rootNode.getChildren(), 408 new String [] { "a.aa", "a.b" }, 409 new int[] { 1, 0 }, 410 true ); 412 413 n = PackageView.findPath( rootNode, toDelete ); 414 n.destroy(); 415 assertFileObjects( srcRoot, new String []{ "a" } ); 416 assertFileObjects( a, new String []{ "aa" } ); 417 418 srcRoot = FileUtil.createFolder( root, "nes" ); 420 a = FileUtil.createFolder( srcRoot, "a" ); 421 FileUtil.createFolder( a, "aa" ); 422 toDelete = FileUtil.createFolder( srcRoot, "a/b" ); 423 group = new SimpleSourceGroup( srcRoot ); 424 rootNode = PackageView.createPackageView( group ); 425 assertNodes( rootNode.getChildren(), 427 new String [] { "a.aa", "a.b" }, 428 new int[] { 0, 0 }, 429 true ); 431 n = PackageView.findPath( rootNode, toDelete ); 432 n.destroy(); 433 assertFileObjects( srcRoot, new String []{ "a" } ); 434 assertFileObjects( a, new String []{ "aa" } ); 435 436 437 srcRoot = FileUtil.createFolder( root, "dds" ); 439 a = FileUtil.createFolder( srcRoot, "a" ); 440 FileUtil.createData( srcRoot, "a/some.java" ); 441 FileObject aa = FileUtil.createFolder( a, "aa" ); 442 FileUtil.createData( srcRoot, "a/aa/some.java" ); 443 toDelete = a; 444 group = new SimpleSourceGroup( srcRoot ); 445 rootNode = PackageView.createPackageView( group ); 446 assertNodes( rootNode.getChildren(), 448 new String [] { "a", "a.aa", }, 449 new int[] { 1, 1 }, 450 true ); 452 n = PackageView.findPath( rootNode, toDelete ); 453 n.destroy(); 454 assertFileObjects( srcRoot, new String []{ "a" } ); 455 assertFileObjects( a, new String []{ "aa" } ); 456 457 srcRoot = FileUtil.createFolder(root, "issue49075"); 459 a = srcRoot.createFolder("a"); 460 FileObject b = FileUtil.createFolder( a, "b" ); 461 FileObject c = FileUtil.createFolder( b, "c" ); 462 group = new SimpleSourceGroup( srcRoot ); 463 rootNode = PackageView.createPackageView( group ); 464 assertNodes(rootNode.getChildren(), new String [] { "a.b.c" },true ); 465 File cFile = FileUtil.toFile(c); 466 File bFile = FileUtil.toFile(b); 467 cFile.delete(); 468 bFile.delete(); 469 a.getFileSystem().refresh(false); 470 assertNodes(rootNode.getChildren(), new String [] { "a" },true ); 471 } 472 473 public void testFindPath() throws Exception { 474 SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) ); 476 Node sourceRoot = PackageView.createPackageView( group ); 477 assertNodes( sourceRoot.getChildren(), 479 new String [] { "<default package>" }, 480 new int[] { 0 }, 481 true ); 483 FileObject a_b_c = FileUtil.createFolder( root, "src/a/b/c" ); 484 FileObject a_b = root.getFileObject( "src/a/b" ); 485 FileObject e_f_g = FileUtil.createFolder( root, "src/e/f/g" ); 486 487 FileObject dp_java = FileUtil.createData( root, "src/DP" ); 488 FileObject a_b_c_java = FileUtil.createData( root, "src/a/b/c/ABC" ); 489 FileObject a_b_java = FileUtil.createData( root, "src/a/b/AB" ); 490 FileObject e_f_g_java = FileUtil.createData( root, "src/e/f/g/EFG.someext" ); 491 492 Node n; 494 n = PackageView.findPath( sourceRoot, a_b_c_java ); 495 assertNode( n, "ABC" ); 496 n = PackageView.findPath(sourceRoot, DataObject.find(a_b_c_java)); 498 assertNode(n, "ABC"); 499 500 n = PackageView.findPath( sourceRoot, a_b_java ); 501 assertNode( n, "AB" ); 502 503 n = PackageView.findPath( sourceRoot, e_f_g_java ); 504 assertNode( n, "EFG.someext" ); 505 506 n = PackageView.findPath( sourceRoot, a_b_c ); 508 assertNode( n, "a.b.c" ); 509 510 n = PackageView.findPath( sourceRoot, a_b ); 511 assertNode( n, "a.b" ); 512 513 n = PackageView.findPath( sourceRoot, e_f_g ); 514 assertNode( n, "e.f.g" ); 515 516 n = PackageView.findPath( sourceRoot, dp_java ); 518 assertNode( n, "DP" ); 519 520 n = PackageView.findPath( sourceRoot, group.getRootFolder() ); 521 assertNode( n, "" ); 522 523 dp_java.delete(); waitForAWT(); 525 n = PackageView.findPath( sourceRoot, group.getRootFolder() ); 526 assertNode( n, group.getName() ); 527 528 529 } 530 531 public void testCopyPaste () throws Exception { 532 FileObject root1 = root.createFolder("src1"); 534 FileObject tmp = root1.createFolder ("src1test1"); 535 root1.createFolder ("src1test2"); 536 createFile(tmp, "src1test1", "File1"); 537 createFile(tmp, "src1test1", "File2"); 538 FileObject root2 = root.createFolder("src2"); 539 SourceGroup group1 = new SimpleSourceGroup(root1); 540 SourceGroup group2 = new SimpleSourceGroup(root2); 541 Node rn1 = PackageView.createPackageView( group1 ); 542 Node rn2 = PackageView.createPackageView( group2 ); 543 Node[] nodes = rn1.getChildren().getNodes(true); 544 545 Transferable t = nodes[0].clipboardCopy(); 547 PasteType[] pts = rn1.getPasteTypes(t); 548 assertEquals ("Single package into same source root",0, pts.length); 549 550 t = new ExTransferable.Multi (new Transferable [] {nodes[0].clipboardCopy(), 552 nodes[1].clipboardCopy()}); 553 pts = rn1.getPasteTypes(t); 554 assertEquals ("Multiple packages into same source root",0,pts.length); 555 556 Node[] fileNodes = nodes[0].getChildren().getNodes(true); 558 t = fileNodes[0].clipboardCopy(); 559 pts = nodes[1].getPasteTypes(t); 560 assertEquals ("Single file into package",1, pts.length); 561 pts[0].paste(); 562 Node[] resultNodes = nodes[1].getChildren().getNodes(true); 563 assertEquals ("Wrong paste result",1, resultNodes.length); 564 assertEquals ("Wrong paste result",fileNodes[0].getDisplayName(), resultNodes[0].getDisplayName()); 565 resultNodes[0].getCookie(DataObject.class).delete(); 566 567 t = new ExTransferable.Multi (new Transferable [] {fileNodes[0].clipboardCopy(), 569 fileNodes[1].clipboardCopy()}); 570 pts = nodes[1].getPasteTypes(t); 571 assertEquals ("Multiple files into package",1, pts.length); 572 pts[0].paste(); 573 assertNodes (nodes[1].getChildren(), new String [] { 575 fileNodes[0].getDisplayName(), 576 fileNodes[1].getDisplayName(), 577 }, true); 578 for (Node n : nodes[1].getChildren().getNodes(true)) { 579 DataObject dobj = n.getCookie(DataObject.class); 580 if (dobj != null) 581 dobj.delete (); 582 } 583 584 t = fileNodes[0].clipboardCopy(); 586 pts = rn1.getPasteTypes(t); 587 assertEquals ("Single file into package",1, pts.length); 588 pts[0].paste(); 589 String defaultPackageName = ResourceBundle.getBundle("org/netbeans/modules/java/project/Bundle").getString("LBL_DefaultPackage"); 590 assertNodes(rn1.getChildren(), new String [] { 591 defaultPackageName, 592 "src1test1", 593 "src1test2", 594 }, true); 595 for (Node n : rn1.getChildren().getNodes(true)) { 596 if (defaultPackageName.equals(n.getDisplayName())) { 597 assertNodes(n.getChildren(), new String [] { 598 fileNodes[0].getDisplayName(), 599 }, true); 600 for (Node n2 : n.getChildren().getNodes(true)) { 601 DataObject dobj = n2.getCookie(DataObject.class); 602 if (dobj != null) { 603 dobj.delete (); 604 } 605 } 606 break; 607 } 608 } 609 FileObject[] files = rn1.getCookie(DataObject.class).getPrimaryFile().getChildren(); 612 assertEquals("Invalid initial file count",2,files.length); 613 614 t = new ExTransferable.Multi (new Transferable [] {fileNodes[0].clipboardCopy(), 615 fileNodes[1].clipboardCopy()}); 616 pts = rn1.getPasteTypes(t); 617 assertEquals ("Multiple files into source root",1, pts.length); 618 pts[0].paste(); 619 files = rn1.getCookie(DataObject.class).getPrimaryFile().getChildren(); 621 assertEquals("Invalid final file count",4,files.length); 622 Set <String > s = new HashSet <String >(); 623 s.add(fileNodes[0].getCookie(DataObject.class).getPrimaryFile().getNameExt()); 624 s.add(fileNodes[1].getCookie(DataObject.class).getPrimaryFile().getNameExt()); 625 for (FileObject f : files) { 626 s.remove(f.getNameExt()); 627 } 628 assertTrue("The following files were not created: "+s.toString(),s.size()==0); 629 assertNodes(rn1.getChildren(), new String [] { 630 defaultPackageName, 631 "src1test1", 632 "src1test2", 633 }, true); 634 for (Node n : rn1.getChildren().getNodes(true)) { 635 if (defaultPackageName.equals(n.getDisplayName())) { 636 assertNodes(n.getChildren(), new String [] { 637 fileNodes[0].getDisplayName(), 638 fileNodes[1].getDisplayName() 639 }, true); 640 for (Node n2 : n.getChildren().getNodes(true)) { 641 DataObject dobj = n2.getCookie(DataObject.class); 642 if (dobj != null) { 643 dobj.delete (); 644 } 645 } 646 break; 647 } 648 } 649 650 t = nodes[0].clipboardCopy(); 652 pts = rn2.getPasteTypes(t); 653 assertEquals ("Single package into different source root",1,pts.length); 654 pts[0].paste (); 655 assertNodes (rn2.getChildren(), new String [] {"src1test1"}, true); 656 rn2.getChildren().getNodes(true)[0].getCookie(DataObject.class).delete(); 657 658 t = new ExTransferable.Multi (new Transferable [] {nodes[0].clipboardCopy(), 660 nodes[1].clipboardCopy()}); 661 pts = rn2.getPasteTypes(t); 662 assertEquals ("Multiple packages into different source root",1,pts.length); 663 pts[0].paste (); 664 assertNodes (rn2.getChildren(), new String [] {"src1test1","src1test2"}, true); 665 for (Node n : rn2.getChildren().getNodes(true)) { 666 DataObject dobj = n.getCookie(DataObject.class); 667 if (dobj != null) 668 dobj.delete (); 669 } 670 671 FileObject defPkgFileRoot1 = createFile(root1, null, "TestDP1"); 673 nodes = rn1.getChildren().getNodes(true); 674 FileObject defPkgFileRoot2 = createFile(root2, null, "TestDP2"); 675 676 Node defPkgNode = null; 677 for (Node n : nodes) { 678 if (n.getDisplayName().equals (defaultPackageName)) { 679 defPkgNode = n; 680 break; 681 } 682 } 683 assertNotNull("Default package exists",defPkgNode); 684 t = defPkgNode.clipboardCopy(); 685 pts = rn2.getPasteTypes(t); 686 assertEquals ("Multiple packages into different source root",1,pts.length); 687 pts[0].paste(); 688 assertNodes (rn2.getChildren(), new String [] {defaultPackageName}, true); 689 defPkgFileRoot1.delete(); 690 for (Node n : rn2.getChildren().getNodes(true)[0].getChildren().getNodes(true)) { 691 DataObject dobj = n.getCookie(DataObject.class); 692 if (dobj != null) { 693 dobj.delete(); 694 } 695 } 696 } 697 698 public void testRename() throws Exception { 699 assertNull( "source folder should not exist yet", root.getFileObject( "src" ) ); 700 701 702 SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) ); 704 Children ch = PackageView.createPackageView( group ).getChildren(); 705 706 FileUtil.createFolder( root, "src/a" ); 708 assertNodes( ch, 709 new String [] { "a", }, 710 new int[] { 0, }, 711 true ); 712 713 Node n = ch.findChild( "a" ); 714 n.setName( "b" ); 715 assertNodes( ch, 716 new String [] { "b", }, 717 new int[] { 0, } ); 718 719 FileUtil.createFolder( root, "src/b/c" ); 720 assertNodes( ch, 721 new String [] { "b.c", }, 722 new int[] { 0, } ); 723 724 n = ch.findChild( "b.c" ); 725 n.setName( "b.d" ); 726 assertNodes( ch, 727 new String [] { "b.d", }, 728 new int[] { 0, } ); 729 730 n = ch.findChild( "b.d" ); 731 n.setName( "a.d" ); 732 assertNodes( ch, 733 new String [] { "a.d", }, 734 new int[] { 0, } ); 735 736 FileUtil.createFolder( root, "src/a/e" ); 737 assertNodes( ch, 738 new String [] { "a.d", "a.e" }, 739 new int[] { 0, 0 } ); 740 741 n = ch.findChild( "a.e" ); 742 n.setName( "a.f" ); 743 assertNodes( ch, 744 new String [] { "a.d", "a.f" }, 745 new int[] { 0, 0 } ); 746 747 748 n = ch.findChild( "a.d" ); 749 n.setName( "c.d" ); 750 assertNodes( ch, 751 new String [] { "a.f", "c.d"}, 752 new int[] { 0, 0 } ); 753 754 n = ch.findChild( "a.f" ); 755 n.setName( "c.f" ); 756 assertNodes( ch, 757 new String [] { "c.d", "c.f" }, 758 new int[] { 0, 0 } ); 759 760 761 FileUtil.createFolder( root, "src/x/y/z" ); 762 assertNodes( ch, 763 new String [] { "c.d", "c.f", "x.y.z" }, 764 new int[] { 0, 0, 0 } ); 765 n = ch.findChild( "x.y.z" ); 766 n.setName( "x.y" ); 767 assertNodes( ch, 768 new String [] { "c.d", "c.f", "x.y" }, 769 new int[] { 0, 0, 0 } ); 770 n = ch.findChild( "x.y" ); 771 n.setName( "p.me.tools" ); 772 assertNodes( ch, 773 new String [] { "c.d", "c.f", "p.me.tools" }, 774 new int[] { 0, 0, 0 } ); 775 n = ch.findChild( "p.me.tools" ); 776 n.setName( "p.metools" ); 777 assertNodes( ch, 778 new String [] { "c.d", "c.f", "p.metools" }, 779 new int[] { 0, 0, 0 } ); 780 n = ch.findChild( "p.metools" ); 781 n.setName( "p.me.tools" ); 782 assertNodes( ch, 783 new String [] { "c.d", "c.f", "p.me.tools" }, 784 new int[] { 0, 0, 0 } ); 785 n = ch.findChild( "p.me.tools" ); 786 n.setName( "p.me.toolsx" ); 787 assertNodes( ch, 788 new String [] { "c.d", "c.f", "p.me.toolsx" }, 789 new int[] { 0, 0, 0 }, 790 true); 791 n = ch.findChild( "p.me.toolsx" ); 792 n.setName( "p.me.tools" ); 793 assertNodes( ch, 794 new String [] { "c.d", "c.f", "p.me.tools" }, 795 new int[] { 0, 0, 0 } ); 796 } 797 798 private static void assertNodes( Children children, String [] nodeNames, boolean optimalResult ) throws InterruptedException , InvocationTargetException { 799 assertNodes( children, nodeNames, null, optimalResult ); 800 } 801 802 private static void assertNodes( Children children, String [] nodeNames ) throws InterruptedException , InvocationTargetException { 803 assertNodes( children, nodeNames, null, false ); 804 } 805 806 private static void assertNodes (Children children, String [] nodeNames, int[] childCount) throws InterruptedException , InvocationTargetException { 807 assertNodes(children, nodeNames, childCount, false); 808 } 809 810 private static void assertNodes( Children children, String [] nodeNames, int[] childCount, boolean optimalResult ) throws InterruptedException , InvocationTargetException { 811 waitForAWT(); 812 Node[] nodes = children.getNodes (optimalResult); 813 String [] actualNodeNames = new String [nodes.length]; 814 for (int i = 0; i < nodes.length; i++) { 815 actualNodeNames[i] = nodes[i].getDisplayName(); 816 } 817 assertEquals("Wrong # or names of nodes", Arrays.asList(nodeNames), Arrays.asList(actualNodeNames)); 818 819 for( int i = 0; i < nodeNames.length; i++ ) { 820 if ( childCount != null ) { 821 if ( childCount[i] == 0 ) { 822 assertEquals( "Node should be leaf", true, nodes[i].isLeaf() ); 823 } 824 else { 825 assertEquals( "Node should not be leaf", false, nodes[i].isLeaf() ); 826 } 827 828 assertEquals( "Wrong number of children. Node: " + nodeNames[i] +".", childCount[i], nodes[i].getChildren().getNodes( true ).length ); 829 830 831 DataObject.Container cont = nodes[i].getCookie (DataObject.Container.class); 832 if (cont != null) { 833 Node[] arr = nodes[i].getChildren ().getNodes ( true ); 834 DataObject[] child = cont.getChildren (); 835 for (int k = 0, l = 0; k < arr.length; k++) { 836 if ( !VisibilityQuery.getDefault().isVisible( child[k].getPrimaryFile() ) ) { 837 continue; 838 } 839 DataObject myObj = arr[l].getCookie(DataObject.class); 840 assertNotNull ("Data object should be found for " + arr[k], myObj); 841 if (child.length <= k) { 842 fail ("But there is no object for node: " + arr[k]); 843 } else { 844 assertEquals ("child objects are the same", child[k], myObj); 845 } 846 l++; 847 } 848 } 849 850 } 851 } 852 } 853 854 private static void assertNode( Node n, String name ) { 855 856 if ( name != null ) { 857 assertNotNull( "Node " + name +" not found", n ); 858 assertEquals( "Wrong name", name, n.getName() ); 859 } 860 else { 861 assertNull( "No node should be found", n ); 862 } 863 864 } 865 866 private static void assertFileObjects( FileObject folder, String [] names ) { 867 868 assertTrue( "Has to be a folder ", folder.isFolder() ); 869 870 FileObject[] children = folder.getChildren(); 871 String [] chNames = new String [ children.length ]; 872 for( int i = 0; i < children.length; i++ ) { 873 chNames[i] = children[i].getNameExt(); 874 } 875 876 Arrays.sort( names ); 877 Arrays.sort( chNames ); 878 879 assertTrue( "Arrays have to be equal ", Arrays.equals( names, chNames ) ); 880 881 } 882 883 private static FileObject createFile (FileObject parent, String pkg, String name) throws IOException { 884 FileObject fo = parent.createData (name,"java"); 885 FileLock lock = fo.lock(); 886 try { 887 PrintWriter out = new PrintWriter (new OutputStreamWriter (fo.getOutputStream(lock))); 888 try { 889 if (pkg != null) { 890 out.println ("package "+pkg+";"); 891 } 892 out.println("public class "+name+" {"); 893 out.println("}"); 894 } finally { 895 out.close (); 896 } 897 } finally { 898 lock.releaseLock(); 899 } 900 return fo; 901 } 902 903 904 private static void waitForAWT () throws InterruptedException , InvocationTargetException { 905 SwingUtilities.invokeAndWait(new Runnable () { 906 public void run() { 907 } 908 } ); 909 } 910 911 private static class SimpleSourceGroup implements SourceGroup { 912 913 private FileObject root; 914 915 public SimpleSourceGroup( FileObject root ) { 916 this.root = root; 917 } 918 919 public FileObject getRootFolder() { 920 return root; 921 } 922 923 public String getName() { 924 return "TestGroup"; 925 } 926 927 public String getDisplayName() { 928 return getName(); 929 } 930 931 public Icon getIcon(boolean opened) { 932 return null; 933 } 934 935 public boolean contains(FileObject file) throws IllegalArgumentException { 936 return FileUtil.isParentOf( root, file ); 937 } 938 939 public void addPropertyChangeListener(PropertyChangeListener listener) {} 940 941 public void removePropertyChangeListener(PropertyChangeListener listener) {} 942 943 } 944 945 private static class VQImpl implements VisibilityQueryImplementation { 946 947 public static String IGNORED = "KRTEK"; 948 949 public boolean isVisible(FileObject file) { 950 return !file.getNameExt().startsWith( IGNORED ); 951 } 952 953 public void addChangeListener(ChangeListener l) {} 954 955 public void removeChangeListener(ChangeListener l) {} 956 957 } 958 959 public void testFilteredViews() throws Exception { 960 final FileObject r = FileUtil.createMemoryFileSystem().getRoot(); 961 class Grp implements SourceGroup { 962 public FileObject getRootFolder() { 963 return r; 964 } 965 public String getName() { 966 return "test"; 967 } 968 public String getDisplayName() { 969 return "Test"; 970 } 971 public Icon getIcon(boolean opened) { 972 return null; 973 } 974 boolean sense = true; 975 public boolean contains(FileObject file) throws IllegalArgumentException { 976 String path = FileUtil.getRelativePath(r, file); 977 if (path == null) { 978 throw new IllegalArgumentException (); 979 } 980 return !path.matches(".+/(" + (sense ? "bad" : "contemporary") + "(/|$)|" + (sense ? "Ugly" : "Pretty") + ".*\\.java$)"); 981 } 982 PropertyChangeSupport pcs = new PropertyChangeSupport (this); 983 public void addPropertyChangeListener(PropertyChangeListener listener) { 984 pcs.addPropertyChangeListener(listener); 985 } 986 public void removePropertyChangeListener(PropertyChangeListener listener) { 987 pcs.removePropertyChangeListener(listener); 988 } 989 void nonsense() { 990 sense = !sense; 991 pcs.firePropertyChange(PROP_CONTAINERSHIP, null, null); 992 } 993 }; 994 FileUtil.createData(r, "a/good/man/is/hard/to/Find.java"); 995 FileUtil.createData(r, "museum/of/contemporary/Art.java"); 996 FileUtil.createData(r, "museum/of/bad/Art.java"); 997 FileUtil.createData(r, "net/pond/aquafowl/PrettyDuckling.java"); 998 FileUtil.createData(r, "net/pond/aquafowl/UglyDuckling.java"); 999 Grp g = new Grp(); 1000 Node n = PackageView.createPackageView(g); 1001 assertTree("Test{a.goo.man.is.har.to{Find.java}, m.of.contemporary{Art.java}, n.pon.aquafowl{PrettyDuckling.java}}", n); 1002 g.nonsense(); 1003 assertTree("Test{a.goo.man.is.har.to{Find.java}, m.of.bad{Art.java}, n.pon.aquafowl{UglyDuckling.java}}", n); 1004 g = new Grp(); 1005 n = new TreeRootNode(g); 1006 assertTree("Test{a{good{man{is{hard{to{Find.java}}}}}}, museum{of{contemporary{Art.java}}}, net{pond{aquafowl{PrettyDuckling.java}}}}", n); 1007 g.nonsense(); 1008 assertTree("Test{a{good{man{is{hard{to{Find.java}}}}}}, museum{of{bad{Art.java}}}, net{pond{aquafowl{UglyDuckling.java}}}}", n); 1009 } 1010 private static void assertTree(String expected, Node n) { 1011 assertEquals(expected, printTree(n).replace('[', '{').replace(']', '}')); 1012 } 1013 private static String printTree(Node n) { 1014 String name = n.getDisplayName(); 1015 if (n.isLeaf()) { 1016 return name; 1017 } else { 1018 List <String > kidNames = new ArrayList <String >(); 1019 for (Node kid : n.getChildren().getNodes(true)) { 1020 kidNames.add(printTree(kid)); 1021 } 1022 return name + kidNames; 1023 } 1024 } 1025 1026} 1027 | Popular Tags |