KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > java > project > support > ui > PackageViewTest


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.spi.java.project.support.ui;
21
22 import java.awt.datatransfer.Transferable JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36 import javax.swing.Icon JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import javax.swing.event.ChangeListener JavaDoc;
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 JavaDoc name ) {
61         super( name );
62     }
63     
64     private FileObject root;
65     
66     protected void setUp() throws Exception JavaDoc {
67         super.setUp();
68         // XXX *sometimes* (maybe 2/3 of the time) fails when you do this; no idea why:
69
//TestUtil.setLookup(new Object[] {new VQImpl()});
70
// Get "Wrong # or names of nodes expected:<[c.d, c.f, p.me.toolsx]> but was:<[c.d, c.f, p.me.tools, p.me.toolsx]>"
71
// from testRename after call to n.setName("p.me.toolsx")
72
// This however seems to work all the time:
73
TestUtil.setLookup( Lookups.fixed( new Object JavaDoc[] { new VQImpl(), PackageViewTest.class.getClassLoader() } ) );
74         root = TestUtil.makeScratchDir(this);
75     }
76     
77     public void testFolders() throws Exception JavaDoc {
78         assertNull( "source folder should not exist yet", root.getFileObject( "src" ) );
79         
80         
81     // Create children
82
SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) );
83         Children ch = PackageView.createPackageView( group ).getChildren();
84         
85         
86         // Create folder
87
FileUtil.createFolder( root, "src/a/b/c" );
88         assertNodes( ch,
89                      new String JavaDoc[] { "a.b.c", },
90                      new int[] { 0, },
91                      true ); // Needs to compute the nodes first
92

93         // Testing files/folders in ignored folders
94

95         // Create ignored folder
96
FileUtil.createFolder( root, "src/KRTEK.folder" );
97         assertNodes( ch,
98                      new String JavaDoc[] { "a.b.c", },
99                      new int[] { 0, } );
100         
101         // Create file in ignored folder
102
FileUtil.createData( root, "src/KRTEK.folder/nonignored.file" );
103         assertNodes( ch,
104                      new String JavaDoc[] { "a.b.c", },
105                      new int[] { 0, } );
106                      
107         // Create folder in ignored folder
108
FileObject nonignoredFolder = FileUtil.createFolder( root, "src/KRTEK.folder/nonignored.folder" );
109         assertNodes( ch,
110                      new String JavaDoc[] { "a.b.c", },
111                      new int[] { 0, } );
112                      
113         // Create file in NONignored folder which is under ignored folder
114
FileObject nonignoredFile = FileUtil.createData( root, "src/KRTEK.folder/nonignored.folder/nonignored.file" );
115         assertNodes( ch,
116                      new String JavaDoc[] { "a.b.c", },
117                      new int[] { 0, } );
118              
119         // Rename the file
120
FileLock nfLock = nonignoredFile.lock();
121         nonignoredFile.rename( nfLock, "othername.file", null );
122         nfLock.releaseLock();
123         assertNodes( ch,
124                      new String JavaDoc[] { "a.b.c", },
125                      new int[] { 0, } );
126
127         // Delete the file and folder
128
nonignoredFile.delete();
129         assertNodes( ch,
130                      new String JavaDoc[] { "a.b.c", },
131                      new int[] { 0, } );
132         nonignoredFolder.delete();
133         assertNodes( ch,
134                      new String JavaDoc[] { "a.b.c", },
135                      new int[] { 0, } );
136
137                      
138                      
139         // Create some other folder
140
FileUtil.createFolder( root, "src/e/f/g" );
141         assertNodes( ch,
142                      new String JavaDoc[] { "a.b.c", "e.f.g", },
143                      new int[] { 0, 0 } );
144         
145         
146         // Add some ignored files/folders
147
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 JavaDoc[] { "a.b.c", "e.f.g", },
153                      new int[] { 0, 0 } );
154                                           
155                     
156         // Create file
157
FileUtil.createData( root, "src/e/f/g/Some.java" );
158         assertNodes( ch,
159                      new String JavaDoc[] { "a.b.c", "e.f.g", },
160                      new int[] { 0, 1 } );
161
162         // Create ignored file
163
FileUtil.createData( root, "src/e/f/g/KRTEK.file" );
164         assertNodes( ch,
165                      new String JavaDoc[] { "a.b.c", "e.f.g", },
166                      new int[] { 0, 1 } );
167                      
168         // Create file in ignored folder
169
FileUtil.createData( root, "src/e/f/g/KRTEK.folder/Tag" );
170         assertNodes( ch,
171                      new String JavaDoc[] { "a.b.c", "e.f.g", },
172                      new int[] { 0, 1 } );
173                      
174                                   
175         // Add empty package and ignored package
176
FileUtil.createFolder( root, "src/x/y/z/KRTEK" );
177         assertNodes( ch,
178                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y.z" },
179                      new int[] { 0, 1, 0 } );
180                      
181         // Add file to folder
182
FileObject x_y_z_some = FileUtil.createData( root, "src/x/y/z/Some.java" );
183         assertNodes( ch,
184                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y.z" },
185                      new int[] { 0, 1, 1 } );
186                      
187         // Remove file from folder
188
x_y_z_some.delete();
189         assertNodes( ch,
190                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y.z" },
191                      new int[] { 0, 1, 0 } );
192                    
193                      
194         // Add file to super folder
195
FileObject x_y_some = FileUtil.createData( root, "src/x/y/Some.java" );
196         assertNodes( ch,
197                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y", "x.y.z" },
198                      new int[] { 0, 1, 1, 0 } );
199                      
200         // Remove file from superfolder
201
x_y_some.delete();
202         assertNodes( ch,
203                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y.z" },
204                      new int[] { 0, 1, 0 } );
205         
206         
207         // Add subfolder
208
FileObject x_y_z_w = FileUtil.createFolder( root, "src/x/y/z/w" );
209         assertNodes( ch,
210                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y.z.w" },
211                      new int[] { 0, 1, 0 } );
212                      
213         // Remove subfolder
214
x_y_z_w.delete();
215         assertNodes( ch,
216                      new String JavaDoc[] { "a.b.c", "e.f.g", "x.y.z" },
217                      new int[] { 0, 1, 0 } );
218                      
219         // Remove super folder
220
FileObject x_y = root.getFileObject( "src/x/y" );
221         x_y.delete();
222         assertNodes( ch,
223                      new String JavaDoc[] { "a.b.c", "e.f.g", "x" },
224                      new int[] { 0, 1, 0 } );
225         
226         // Remove root folder
227
FileUtil.createFolder( root, "src/x/v/w" );
228                 assertNodes( ch,
229                      new String JavaDoc[] { "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 JavaDoc[] { "a.b.c", "e.f.g" },
235                      new int[] { 0, 1 } );
236
237         /*
238          * Sometime fails in Jarda's DataObject container test
239          *
240         // Rename ignored file to unignored
241         FileObject e_f_g_krtekFile = root.getFileObject( "src/e/f/g/KRTEK.file" );
242         FileLock krtekLock = e_f_g_krtekFile.lock();
243         e_f_g_krtekFile.rename( krtekLock, "ZIZALA.file", null );
244         krtekLock.releaseLock();
245         assertNodes( ch,
246                      new String[] { "a.b.c", "e.f.g" },
247                      new int[] { 0, 2 } );
248         
249                              
250         // Rename unignored to ignored file
251         e_f_g_krtekFile = root.getFileObject( "src/e/f/g/ZIZALA.file" );
252         krtekLock = e_f_g_krtekFile.lock();
253         e_f_g_krtekFile.rename( krtekLock, "KRTEK.file", null );
254         krtekLock.releaseLock();
255         assertNodes( ch,
256                      new String[] { "a.b.c", "e.f.g" },
257                      new int[] { 0, 1 } );
258         */

259                      
260         // Rename leaf folder
261
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 JavaDoc[] { "a.b.c", "e.f.h" },
267                      new int[] { 0, 1 } );
268         
269                      
270         // Rename ignored folder to unignored folder
271
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 JavaDoc[] { "a.b.c", "e.f.h", "e.f.h.ZIZALA" },
277                      new int[] { 0, 1, 1 } );
278                      
279         // Rename unignored folder back to ignored folder
280
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 JavaDoc[] { "a.b.c", "e.f.h" },
286                      new int[] { 0, 1 } );
287                      
288                                           
289         // Rename super folder
290
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 JavaDoc[] { "a.b.c", "e.r.g", "e.r.h", "e.r.i" },
298                      new int[] { 0, 0, 1, 0 } );
299                      
300         // Rename rootfolder
301
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 JavaDoc[] { "a.b.c", "t.r.g", "t.r.h", "t.r.i" },
307                      new int[] { 0, 0, 1, 0 } );
308                      
309         // Test truncated package names
310
FileUtil.createFolder(root, "src/org/foo/something/whatever");
311         assertNodes( ch,
312                      new String JavaDoc[] { "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 JavaDoc {
320     // Create children
321
SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) );
322         Children ch = PackageView.createPackageView( group ).getChildren();
323         
324         // Default package should be there
325
assertNodes( ch,
326                      new String JavaDoc[] { "<default package>" },
327                      new int[] { 0 },
328                      true ); // Needs to compute the nodes first
329

330         // Default package should disappear
331
FileObject a = FileUtil.createFolder( root, "src/a" );
332         assertNodes( ch,
333                      new String JavaDoc[] { "a", },
334                      new int[] { 0, } );
335                      
336         // Default package should appear again
337
FileObject someJava = FileUtil.createData( root, "src/Some.java" );
338         assertNodes( ch,
339                      new String JavaDoc[] { "<default package>", "a", },
340                      new int[] { 1, 0, } );
341                      
342         // Disappear again
343
someJava.delete();
344         assertNodes( ch,
345                      new String JavaDoc[] { "a", },
346                      new int[] { 0, } );
347                      
348         // And appear again
349
a.delete();
350         assertNodes( ch,
351                      new String JavaDoc[] { "<default package>" },
352                      new int[] { 0 } );
353         
354     }
355     
356     public void testNodeDestroy() throws Exception JavaDoc {
357     FileObject srcRoot;
358         FileObject toDelete;
359         SourceGroup group;
360         Node rootNode;
361         Node n;
362         
363         // Empty parent
364
srcRoot = FileUtil.createFolder( root, "ep" );
365         toDelete = FileUtil.createFolder( srcRoot, "a/aa" );
366         group = new SimpleSourceGroup( srcRoot );
367         rootNode = PackageView.createPackageView( group );
368
369         // Compute the nodes
370
assertNodes( rootNode.getChildren(),
371                      new String JavaDoc[] { "a.aa", },
372                      new int[] { 0, },
373                      true ); // Needs to compute the nodes first
374

375         
376         n = PackageView.findPath( rootNode, toDelete );
377         n.destroy();
378         assertFileObjects( srcRoot, new String JavaDoc[0] );
379         
380         // Non-Empty parent
381
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         // Compute the nodes
387
assertNodes( rootNode.getChildren(),
388                      new String JavaDoc[] { "a", "a.aa" },
389                      new int[] { 1, 0 },
390                      true ); // Needs to compute the nodes first
391

392         
393         n = PackageView.findPath( rootNode, toDelete );
394         n.destroy();
395         assertFileObjects( srcRoot, new String JavaDoc[]{ "a" } );
396         
397                
398         // Non empty siblings
399
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         // Compute the nodes
407
assertNodes( rootNode.getChildren(),
408                      new String JavaDoc[] { "a.aa", "a.b" },
409                      new int[] { 1, 0 },
410                      true ); // Needs to compute the nodes first
411

412         
413         n = PackageView.findPath( rootNode, toDelete );
414         n.destroy();
415         assertFileObjects( srcRoot, new String JavaDoc[]{ "a" } );
416         assertFileObjects( a, new String JavaDoc[]{ "aa" } );
417         
418         // Empty siblings
419
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         // Compute the nodes
426
assertNodes( rootNode.getChildren(),
427                      new String JavaDoc[] { "a.aa", "a.b" },
428                      new int[] { 0, 0 },
429                      true ); // Needs to compute the nodes first
430

431         n = PackageView.findPath( rootNode, toDelete );
432         n.destroy();
433         assertFileObjects( srcRoot, new String JavaDoc[]{ "a" } );
434         assertFileObjects( a, new String JavaDoc[]{ "aa" } );
435         
436         
437         // Do not delete subfoders
438
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         // Compute the nodes
447
assertNodes( rootNode.getChildren(),
448                      new String JavaDoc[] { "a", "a.aa", },
449                      new int[] { 1, 1 },
450                      true ); // Needs to compute the nodes first
451

452         n = PackageView.findPath( rootNode, toDelete );
453         n.destroy();
454         assertFileObjects( srcRoot, new String JavaDoc[]{ "a" } );
455         assertFileObjects( a, new String JavaDoc[]{ "aa" } );
456
457         //Issue #49075
458
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 JavaDoc[] { "a.b.c" },true );
465         File JavaDoc cFile = FileUtil.toFile(c);
466         File JavaDoc bFile = FileUtil.toFile(b);
467         cFile.delete();
468         bFile.delete();
469         a.getFileSystem().refresh(false);
470         assertNodes(rootNode.getChildren(), new String JavaDoc[] { "a" },true );
471     }
472     
473     public void testFindPath() throws Exception JavaDoc {
474     // Create children
475
SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) );
476         Node sourceRoot = PackageView.createPackageView( group );
477         // Compute the nodes
478
assertNodes( sourceRoot.getChildren(),
479                      new String JavaDoc[] { "<default package>" },
480                      new int[] { 0 },
481                      true ); // Needs to compute the nodes first
482

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         // Try to find standard files
493
Node n;
494         n = PackageView.findPath( sourceRoot, a_b_c_java );
495         assertNode( n, "ABC" );
496         // Check also DataObject:
497
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         // Try to find folders
507
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         // Try file in default package
517
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(); // Dp will disapear should return root node
524
waitForAWT();
525         n = PackageView.findPath( sourceRoot, group.getRootFolder() );
526         assertNode( n, group.getName() );
527         
528         
529     }
530     
531     public void testCopyPaste () throws Exception JavaDoc {
532         //Setup 2 sourcegroups
533
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         //Single package into same source root
546
Transferable JavaDoc t = nodes[0].clipboardCopy();
547         PasteType[] pts = rn1.getPasteTypes(t);
548         assertEquals ("Single package into same source root",0, pts.length);
549         
550         //Multiple packages into same source root
551
t = new ExTransferable.Multi (new Transferable JavaDoc[] {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         //Single file into package
557
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         //Multiple files into package
568
t = new ExTransferable.Multi (new Transferable JavaDoc[] {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         //After change - requires optimalResults
574
assertNodes (nodes[1].getChildren(), new String JavaDoc[] {
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         //Single file into source root
585
t = fileNodes[0].clipboardCopy();
586         pts = rn1.getPasteTypes(t);
587         assertEquals ("Single file into package",1, pts.length);
588         pts[0].paste();
589         String JavaDoc defaultPackageName = ResourceBundle.getBundle("org/netbeans/modules/java/project/Bundle").getString("LBL_DefaultPackage");
590         assertNodes(rn1.getChildren(), new String JavaDoc[] {
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 JavaDoc[] {
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         //Multiple files into source root
610
//Verify preconditions
611
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 JavaDoc[] {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         //Verify that the files was added, the used PasteType is DataFolder's PasteType
620
files = rn1.getCookie(DataObject.class).getPrimaryFile().getChildren();
621         assertEquals("Invalid final file count",4,files.length);
622         Set JavaDoc<String JavaDoc> s = new HashSet JavaDoc<String JavaDoc>();
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 JavaDoc[] {
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 JavaDoc[] {
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         //Single package into different source root
651
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 JavaDoc[] {"src1test1"}, true);
656         rn2.getChildren().getNodes(true)[0].getCookie(DataObject.class).delete();
657         
658         //Multiple packages into different source root
659
t = new ExTransferable.Multi (new Transferable JavaDoc[] {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 JavaDoc[] {"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         //One more case (Issue #48246), Copy default pkg test
672
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 JavaDoc[] {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 JavaDoc {
699         assertNull( "source folder should not exist yet", root.getFileObject( "src" ) );
700         
701         
702     // Create children
703
SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) );
704         Children ch = PackageView.createPackageView( group ).getChildren();
705         
706         // Create folder
707
FileUtil.createFolder( root, "src/a" );
708         assertNodes( ch,
709                      new String JavaDoc[] { "a", },
710                      new int[] { 0, },
711                      true );
712         
713         Node n = ch.findChild( "a" );
714         n.setName( "b" );
715         assertNodes( ch,
716                      new String JavaDoc[] { "b", },
717                      new int[] { 0, } );
718         
719         FileUtil.createFolder( root, "src/b/c" );
720         assertNodes( ch,
721                      new String JavaDoc[] { "b.c", },
722                      new int[] { 0, } );
723         
724         n = ch.findChild( "b.c" );
725         n.setName( "b.d" );
726         assertNodes( ch,
727                      new String JavaDoc[] { "b.d", },
728                      new int[] { 0, } );
729         
730         n = ch.findChild( "b.d" );
731         n.setName( "a.d" );
732         assertNodes( ch,
733                      new String JavaDoc[] { "a.d", },
734                      new int[] { 0, } );
735         
736         FileUtil.createFolder( root, "src/a/e" );
737         assertNodes( ch,
738                      new String JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc[] { "c.d", "c.f", "p.me.tools" },
795                      new int[] { 0, 0, 0 } );
796     }
797
798     private static void assertNodes( Children children, String JavaDoc[] nodeNames, boolean optimalResult ) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
799         assertNodes( children, nodeNames, null, optimalResult );
800     }
801
802     private static void assertNodes( Children children, String JavaDoc[] nodeNames ) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
803         assertNodes( children, nodeNames, null, false );
804     }
805
806     private static void assertNodes (Children children, String JavaDoc[] nodeNames, int[] childCount) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
807         assertNodes(children, nodeNames, childCount, false);
808     }
809
810     private static void assertNodes( Children children, String JavaDoc[] nodeNames, int[] childCount, boolean optimalResult ) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
811         waitForAWT();
812         Node[] nodes = children.getNodes (optimalResult);
813         String JavaDoc[] actualNodeNames = new String JavaDoc[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 JavaDoc 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 JavaDoc[] names ) {
867         
868         assertTrue( "Has to be a folder ", folder.isFolder() );
869         
870         FileObject[] children = folder.getChildren();
871         String JavaDoc[] chNames = new String JavaDoc[ 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 JavaDoc pkg, String JavaDoc name) throws IOException JavaDoc {
884         FileObject fo = parent.createData (name,"java");
885         FileLock lock = fo.lock();
886         try {
887             PrintWriter JavaDoc out = new PrintWriter JavaDoc (new OutputStreamWriter JavaDoc (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 JavaDoc, InvocationTargetException JavaDoc {
905         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc getName() {
924             return "TestGroup";
925         }
926         
927         public String JavaDoc getDisplayName() {
928             return getName();
929         }
930         
931         public Icon JavaDoc getIcon(boolean opened) {
932             return null;
933         }
934
935         public boolean contains(FileObject file) throws IllegalArgumentException JavaDoc {
936             return FileUtil.isParentOf( root, file );
937         }
938     
939         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {}
940
941         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {}
942         
943     }
944     
945     private static class VQImpl implements VisibilityQueryImplementation {
946         
947         public static String JavaDoc IGNORED = "KRTEK";
948         
949         public boolean isVisible(FileObject file) {
950             return !file.getNameExt().startsWith( IGNORED );
951         }
952
953         public void addChangeListener(ChangeListener JavaDoc l) {}
954
955         public void removeChangeListener(ChangeListener JavaDoc l) {}
956         
957     }
958
959     public void testFilteredViews() throws Exception JavaDoc {
960         final FileObject r = FileUtil.createMemoryFileSystem().getRoot();
961         class Grp implements SourceGroup {
962             public FileObject getRootFolder() {
963                 return r;
964             }
965             public String JavaDoc getName() {
966                 return "test";
967             }
968             public String JavaDoc getDisplayName() {
969                 return "Test";
970             }
971             public Icon JavaDoc getIcon(boolean opened) {
972                 return null;
973             }
974             boolean sense = true;
975             public boolean contains(FileObject file) throws IllegalArgumentException JavaDoc {
976                 String JavaDoc path = FileUtil.getRelativePath(r, file);
977                 if (path == null) {
978                     throw new IllegalArgumentException JavaDoc();
979                 }
980                 return !path.matches(".+/(" + (sense ? "bad" : "contemporary") + "(/|$)|" + (sense ? "Ugly" : "Pretty") + ".*\\.java$)");
981             }
982             PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
983             public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
984                 pcs.addPropertyChangeListener(listener);
985             }
986             public void removePropertyChangeListener(PropertyChangeListener JavaDoc 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 JavaDoc expected, Node n) {
1011        assertEquals(expected, printTree(n).replace('[', '{').replace(']', '}'));
1012    }
1013    private static String JavaDoc printTree(Node n) {
1014        String JavaDoc name = n.getDisplayName();
1015        if (n.isLeaf()) {
1016            return name;
1017        } else {
1018            List JavaDoc<String JavaDoc> kidNames = new ArrayList JavaDoc<String JavaDoc>();
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