KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.Repository;
28
29 /**
30  *
31  * @author vita
32  */

33 public class FolderChildrenTest extends NbTestCase {
34
35     /** Creates a new instance of FolderChildrenTest */
36     public FolderChildrenTest(String JavaDoc name) {
37         super(name);
38     }
39
40     protected void setUp() throws java.lang.Exception JavaDoc {
41         // Set up the default lookup, repository, etc.
42
EditorTestLookup.setLookup(
43             new String JavaDoc[] {
44                 "Tmp/"
45             },
46             getWorkDir(), new Object JavaDoc[] {},
47             getClass().getClassLoader(),
48             null
49         );
50     }
51
52     public void testSimple() throws Exception JavaDoc {
53         String JavaDoc fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance";
54         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName);
55
56         FolderChildren fch = new FolderChildren("Tmp/A/B/C/D");
57         List JavaDoc files = fch.getChildren();
58         
59         assertEquals("Wrong number of files", 1, files.size());
60         assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt());
61         
62         fch = new FolderChildren("Tmp/X/Y/Z");
63         files = fch.getChildren();
64
65         assertEquals("Wrong number of files", 0, files.size());
66     }
67
68     public void testAddingFolders() throws Exception JavaDoc {
69         String JavaDoc fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance";
70         FolderChildren fch = new FolderChildren("Tmp/A/B/C/D");
71         List JavaDoc files = fch.getChildren();
72
73         assertEquals("Wrong number of files", 0, files.size());
74         
75         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName);
76         
77         files = fch.getChildren();
78         assertEquals("Wrong number of files", 1, files.size());
79         assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt());
80     }
81
82     public void testRemovingFolders() throws Exception JavaDoc {
83         String JavaDoc fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance";
84         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName);
85
86         FolderChildren fch = new FolderChildren("Tmp/A/B/C/D");
87         List JavaDoc files = fch.getChildren();
88
89         assertEquals("Wrong number of files", 1, files.size());
90         assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt());
91         
92         TestUtilities.deleteFile(getWorkDir(), "Tmp/A/");
93         
94         files = fch.getChildren();
95         assertEquals("Wrong number of files", 0, files.size());
96     }
97     
98     public void testMultipleAddRemove() throws Exception JavaDoc {
99         for (int i = 0; i < 7; i++) {
100             testAddingFolders();
101             testRemovingFolders();
102         }
103     }
104     
105     public void testChangeEvents() throws Exception JavaDoc {
106         String JavaDoc fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance";
107         FolderChildren fch = new FolderChildren("Tmp/A/B/C/D");
108         L listener = new L();
109         fch.addPropertyChangeListener(listener);
110         
111         List JavaDoc files = fch.getChildren();
112         assertEquals("Wrong number of events", 0, listener.changeEventsCnt);
113         assertEquals("Wrong number of files", 0, files.size());
114         
115         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName);
116
117         assertEquals("Wrong number of events", 1, listener.changeEventsCnt);
118         assertEquals("Wrong event", FolderChildren.PROP_CHILDREN, listener.lastEvent.getPropertyName());
119         files = fch.getChildren();
120         assertEquals("Wrong number of files", 1, files.size());
121         assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt());
122
123         listener.reset();
124         
125         TestUtilities.deleteFile(getWorkDir(), "Tmp/A/");
126
127         assertEquals("Wrong number of events", 1, listener.changeEventsCnt);
128         assertEquals("Wrong event", FolderChildren.PROP_CHILDREN, listener.lastEvent.getPropertyName());
129         files = fch.getChildren();
130         assertEquals("Wrong number of files", 0, files.size());
131     }
132
133     public void testEventsWithMultipleChanges() throws Exception JavaDoc {
134         for (int i = 0; i < 11; i++) {
135             testChangeEvents();
136         }
137     }
138
139     public void testEmptyFolder() throws Exception JavaDoc {
140         FolderChildren fch = new FolderChildren("Tmp/A/B/C/D");
141         L listener = new L();
142         fch.addPropertyChangeListener(listener);
143         
144         List JavaDoc files = fch.getChildren();
145         assertEquals("Wrong number of events", 0, listener.changeEventsCnt);
146         assertEquals("Wrong number of files", 0, files.size());
147         
148         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/");
149
150         assertEquals("Wrong number of events", 0, listener.changeEventsCnt);
151         files = fch.getChildren();
152         assertEquals("Wrong number of files", 0, files.size());
153
154         listener.reset();
155         
156         TestUtilities.deleteFile(getWorkDir(), "Tmp/A/");
157
158         assertEquals("Wrong number of events", 0, listener.changeEventsCnt);
159         files = fch.getChildren();
160         assertEquals("Wrong number of files", 0, files.size());
161     }
162
163     public void testAttributeChanges() throws Exception JavaDoc {
164         TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/");
165         FolderChildren fch = new FolderChildren("Tmp/A/B/C/D");
166         L listener = new L();
167         fch.addPropertyChangeListener(listener);
168         
169         List JavaDoc files = fch.getChildren();
170         assertEquals("Wrong number of events", 0, listener.changeEventsCnt);
171         assertEquals("Wrong number of files", 0, files.size());
172         
173         FileObject f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/C/D");
174         assertNotNull("Can't find the folder", f);
175         
176         f.setAttribute("attrName", "attrValue");
177         
178         assertEquals("Wrong number of events", 1, listener.changeEventsCnt);
179         assertEquals("Wrong event", FolderChildren.PROP_ATTRIBUTES, listener.lastEvent.getPropertyName());
180         files = fch.getChildren();
181         assertEquals("Wrong number of files", 0, files.size());
182
183         listener.reset();
184         
185         f.setAttribute("attrName", null);
186
187         assertEquals("Wrong number of events", 1, listener.changeEventsCnt);
188         assertEquals("Wrong event", FolderChildren.PROP_ATTRIBUTES, listener.lastEvent.getPropertyName());
189         files = fch.getChildren();
190         assertEquals("Wrong number of files", 0, files.size());
191     }
192     
193     private static class L implements PropertyChangeListener JavaDoc {
194         public int changeEventsCnt = 0;
195         public PropertyChangeEvent JavaDoc lastEvent = null;
196         
197         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
198             changeEventsCnt++;
199             lastEvent = evt;
200         }
201         
202         public void reset() {
203             changeEventsCnt = 0;
204             lastEvent = null;
205         }
206     } // End of L class
207
}
208
Popular Tags