KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > CompoundFolderChildrenTest


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.modules.editor.mimelookup.impl;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.Repository;
29
30 /**
31  *
32  * @author vita
33  */

34 public class CompoundFolderChildrenTest extends NbTestCase {
35
36     /** Creates a new instance of FolderChildrenTest */
37     public CompoundFolderChildrenTest(String JavaDoc name) {
38         super(name);
39     }
40
41     protected void setUp() throws java.lang.Exception JavaDoc {
42         // Set up the default lookup, repository, etc.
43
EditorTestLookup.setLookup(
44             new String JavaDoc[] {
45                 "Tmp/"
46             },
47             getWorkDir(), new Object JavaDoc[] {},
48             getClass().getClassLoader(),
49             null
50         );
51     }
52
53     // test collecting files on different layers
54

55     public void testCollecting() throws Exception JavaDoc {
56         String JavaDoc fileName1 = "file-on-layer-1.instance";
57         String JavaDoc fileName2 = "file-on-layer-2.instance";
58         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1);
59         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName2);
60
61         CompoundFolderChildren cfch = new CompoundFolderChildren(new String JavaDoc [] { "Tmp/A/B/C/D", "Tmp/A/B" }, false);
62         List JavaDoc files = cfch.getChildren();
63         
64         assertEquals("Wrong number of files", 2, files.size());
65         assertNotNull("Files do not contain " + fileName1, findFileByName(files, fileName1));
66         assertNotNull("Files do not contain " + fileName2, findFileByName(files, fileName2));
67         
68         cfch = new CompoundFolderChildren(new String JavaDoc [] { "Tmp/X/Y/Z" });
69         files = cfch.getChildren();
70
71         assertEquals("Wrong number of files", 0, files.size());
72     }
73     
74     // test hiding files on lower layer by files on higher layers
75

76     public void testHidingSameFilesOnLowerLayers() throws Exception JavaDoc {
77         String JavaDoc fileName = "some-file.instance";
78         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName);
79         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName);
80
81         CompoundFolderChildren cfch = new CompoundFolderChildren(new String JavaDoc [] { "Tmp/A/B/C/D", "Tmp/A/B" }, false);
82         List JavaDoc files = cfch.getChildren();
83         
84         assertEquals("Wrong number of files", 1, files.size());
85         assertEquals("Wrong layerA file", fileName, ((FileObject) files.get(0)).getNameExt());
86     }
87     
88     // test hidden files
89

90 // This one's failing, because the filesystem doesn't show files with the _hidden suffix
91
// public void testFilesHiddenBySuffix() throws Exception {
92
// String fileName1 = "file-on-layer-A.instance";
93
// String fileName2 = "file-on-layer-B.instance";
94
// EditorTestLookup.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1);
95
// EditorTestLookup.createFile(getWorkDir(), "Tmp/A/B/" + fileName2);
96
// EditorTestLookup.createFile(getWorkDir(), "Tmp/A/" + fileName2);
97
//
98
// File markerFile = new File(getWorkDir(), "Tmp/A/B/C/D/" + fileName2 + "_hidden");
99
// markerFile.createNewFile();
100
//
101
// // Check precondition
102
// FileObject f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/C/D/");
103
// f.refresh();
104
//
105
// f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/C/D/" + fileName2 + "_hidden");
106
// assertNotNull("The _hidden file does not exist", f);
107
//
108
// f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/" + fileName2);
109
// assertNotNull("The original file on the second layer that should be hidden does not exist", f);
110
//
111
// f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/" + fileName2);
112
// assertNotNull("The original file on the third layer that should be hidden does not exist", f);
113
//
114
// // Test compound children
115
// CompoundFolderChildren cfch = new CompoundFolderChildren(new String [] { "Tmp/A/B/C/D", "Tmp/A/B", "Tmp/A" }, false);
116
// List files = cfch.getChildren();
117
//
118
// assertEquals("Wrong number of files", 1, files.size());
119
// assertEquals("Wrong layerA file", fileName1, ((FileObject) files.get(0)).getNameExt());
120
// }
121

122     public void testFilesHiddenByAttribute() throws Exception JavaDoc {
123         String JavaDoc fileName1 = "file-on-layer-A.instance";
124         String JavaDoc fileName2 = "file-on-layer-B.instance";
125         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1);
126         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName2);
127         TestUtilities.createFile(getWorkDir(), "Tmp/A/" + fileName2);
128
129         // Check precondition
130
FileObject f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/" + fileName2);
131         assertNotNull("The hidden file on the second layer does not exist", f);
132
133         // Mark the file as hidden, which should hide both this file and
134
// the same one on the third layer.
135
f.setAttribute("hidden", Boolean.TRUE);
136         
137         f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/" + fileName2);
138         assertNotNull("The original file on the third layer that should be hidden does not exist", f);
139         
140         // Test compound children
141
CompoundFolderChildren cfch = new CompoundFolderChildren(new String JavaDoc [] { "Tmp/A/B/C/D", "Tmp/A/B", "Tmp/A" }, false);
142         List JavaDoc files = cfch.getChildren();
143         
144         assertEquals("Wrong number of files", 1, files.size());
145         assertEquals("Wrong layerA file", fileName1, ((FileObject) files.get(0)).getNameExt());
146     }
147     
148     // test sorting using attributes on different layers
149

150     public void testSorting() throws Exception JavaDoc {
151         // Create files
152
String JavaDoc fileName1 = "file-1.instance";
153         String JavaDoc fileName2 = "file-2.instance";
154         String JavaDoc fileName3 = "file-3.instance";
155         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1);
156         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName2);
157         TestUtilities.createFile(getWorkDir(), "Tmp/A/" + fileName3);
158
159         // Set the sorting attributes
160
FileObject layer1 = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/C/D");
161         FileObject layer2 = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B");
162         FileObject layer3 = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A");
163         
164         layer1.setAttribute("file-3.instance/file-1.instance", Boolean.TRUE);
165         layer2.setAttribute("file-2.instance/file-3.instance", Boolean.TRUE);
166         
167         // Create compound children
168
CompoundFolderChildren cfch = new CompoundFolderChildren(new String JavaDoc [] { "Tmp/A/B/C/D", "Tmp/A/B", "Tmp/A" }, false);
169         List JavaDoc files = cfch.getChildren();
170
171         assertEquals("Wrong number of files", 3, files.size());
172         assertEquals("Wrong first file", fileName2, ((FileObject) files.get(0)).getNameExt());
173         assertEquals("Wrong second file", fileName3, ((FileObject) files.get(1)).getNameExt());
174         assertEquals("Wrong third file", fileName1, ((FileObject) files.get(2)).getNameExt());
175     }
176     
177     // test events
178

179     private FileObject findFileByName(List JavaDoc files, String JavaDoc nameExt) {
180         for (Iterator JavaDoc i = files.iterator(); i.hasNext(); ) {
181             FileObject f = (FileObject) i.next();
182             if (nameExt.equals(f.getNameExt())) {
183                 return f;
184             }
185         }
186         return null;
187     }
188     
189     private static class L implements PropertyChangeListener JavaDoc {
190         public int changeEventsCnt = 0;
191         public PropertyChangeEvent JavaDoc lastEvent = null;
192         
193         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
194             changeEventsCnt++;
195             lastEvent = evt;
196         }
197         
198         public void reset() {
199             changeEventsCnt = 0;
200             lastEvent = null;
201         }
202     } // End of L class
203
}
204
Popular Tags