KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > RepositoryTestHid


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.openide.filesystems;
21 import java.io.*;
22 /**
23  *
24  * @author rm111737
25  */

26 public class RepositoryTestHid extends TestBaseHid {
27     Repository repo;
28     FileSystem defFs;
29     String JavaDoc pkg = "/root/folder1/folder2";
30     String JavaDoc name = "resource";
31     String JavaDoc ext = "ext";
32     String JavaDoc resource1 = pkg+"/"+name+"."+ext;
33     /** Creates new TT */
34     public RepositoryTestHid(String JavaDoc testName) {
35         super (testName);
36     }
37
38     /** Test can require some resources to be part of filesystem that will be tested
39      * @return array of resources
40      */

41     protected String JavaDoc[] getResources(String JavaDoc testName) {
42         return new String JavaDoc[] {resource1};
43     }
44
45     protected void setUp() throws java.lang.Exception JavaDoc {
46         super.setUp();
47         defFs = TestUtilHid.createLocalFileSystem("defaultFs", new String JavaDoc[] {} );
48         repo = new Repository (defFs);
49     }
50     
51     /** Test of getDefault method, of class org.openide.filesystems.Repository.
52      * No able to test this method in external execution
53      */

54     /*public void testGetDefault() {
55         // Add your test code below by replacing the default call to fail.
56         fail("The test case is empty.");
57     }*/

58     
59     /** Test of getDefaultFileSystem method, of class org.openide.filesystems.Repository. */
60     public void testGetDefaultFileSystem() {
61         FileSystem retFs = repo.getDefaultFileSystem();
62         fsAssert("Default file system should not be null", retFs != null);
63         fsAssert(defFs + " should be default file system", defFs.equals(retFs));
64     }
65     
66
67     /** Test of addFileSystem method, of class org.openide.filesystems.Repository. */
68     public void testAddFileSystem() {
69         repo.addFileSystem(testedFS);
70         repo.addFileSystem(testedFS);
71         int count = 0;
72         java.util.Enumeration JavaDoc en = repo.getFileSystems();
73         while (en.hasMoreElements()) {
74             if (en.nextElement().equals(testedFS))
75                 count++;
76         }
77         fsAssert(testedFS+" can be present in Repository only once.",count == 1);
78     }
79     
80     /** Test of addFileSystem method, of class org.openide.filesystems.Repository. */
81     public void testAddFileSystem2() {
82         if (!testedFS.getSystemName().equals("")) {
83             repo.addFileSystem(testedFS);
84             Repository repo2 = new Repository (testedFS);
85             int count = 0;
86             if (repo.findFileSystem(testedFS.getSystemName()) != null)
87                 count++;
88             if (repo2.findFileSystem(testedFS.getSystemName()) != null)
89                 count++;
90
91             fsAssert(" FileSystem can be included only in one Repository.",count == 1);
92         }
93     }
94
95         
96     /** Test of removeFileSystem method, of class org.openide.filesystems.Repository. */
97     public void testRemoveFileSystem() {
98         if (!testedFS.getSystemName().equals("")) {
99             repo.addFileSystem(testedFS);
100             FileSystem retFs = repo.findFileSystem(testedFS.getSystemName());
101             fsAssert(testedFS+" was added to Repository and was not found.",testedFS.equals(retFs));
102             repo.removeFileSystem(retFs);
103             retFs = repo.findFileSystem(retFs.getSystemName());
104             fsAssert(testedFS + " " + testedFS.getSystemName() +" was removed from Repository and is still present.",retFs == null);
105         }
106     }
107     
108     /** Test of reorder method, of class org.openide.filesystems.Repository. */
109     public void testReorder() {
110         MultiFileSystem mfs = new MultiFileSystem (new FileSystem[] {testedFS});
111         repo.addFileSystem(testedFS);
112         repo.addFileSystem(mfs);
113         repo.reorder(new int[] {2,0,1});
114         FileSystem[] fss = repo.toArray();
115         fsAssert("Wrong reordered",fss[0] == mfs && fss[1] == defFs && fss[2] == testedFS);
116     }
117     
118     /** Test of getFileSystems method, of class org.openide.filesystems.Repository. */
119     public void testGetFileSystems() {
120         repo.addFileSystem(testedFS);
121         java.util.Enumeration JavaDoc en = repo.getFileSystems();
122         FileSystem[] fss = new FileSystem[2];
123         for (int i = 0; en.hasMoreElements(); i++) {
124             fss[i] = (FileSystem)en.nextElement();
125         }
126         fsAssert("Expected two elements in enumeration",fss.length == 2);
127         fsAssert("Expected two different elements in enumeration",fss[0] != fss[1]);
128         fsAssert("Wrong filesystems in enumeration",
129         (fss[0] == defFs && fss[1] == testedFS) || (fss[1] == defFs && fss[0] == testedFS));
130     }
131     
132     /** Test of fileSystems method, of class org.openide.filesystems.Repository. */
133     public void testFileSystems() {
134         testGetFileSystems();
135     }
136     
137     /** Test of toArray method, of class org.openide.filesystems.Repository. */
138     public void testToArray() {
139         FileSystem[] fss;
140         fss = repo.toArray();
141         fsAssert("Expected one element in enumeration",fss.length == 1 && fss[0] == defFs);
142
143         repo.addFileSystem(testedFS);
144         fss = repo.toArray();
145         fsAssert("Expected two elements in enumeration",fss.length == 2 && fss[1] == testedFS);
146         
147         MultiFileSystem mfs = new MultiFileSystem (new FileSystem[] {testedFS});
148         repo.addFileSystem(mfs);
149         fss = repo.toArray();
150         fsAssert("Expected two elements in enumeration",fss.length == 3 && fss[2] == mfs);
151     }
152     
153     /** Test of findFileSystem method, of class org.openide.filesystems.Repository. */
154     public void testFindFileSystem() {
155         fsAssert("Default file system expected to in Repository",
156         repo.findFileSystem(defFs.getSystemName()) != null);
157         
158     }
159         
160     /** Test of find method, of class org.openide.filesystems.Repository. * /
161     public void testFind() {
162         repo.addFileSystem(testedFS);
163         fsAssert("Method find should find resource: " + resource1,repo.find(pkg.replace('/','.'), name, ext) != null);
164     }
165     */

166     
167     /** Test of findResource method, of class org.openide.filesystems.Repository. * /
168     public void testFindResource() {
169         repo.addFileSystem(testedFS);
170         fsAssert("Method find should find resource: " + resource1,repo.findResource(resource1) != null);
171     }
172     */

173     
174     /** Test of findAllResources method, of class org.openide.filesystems.Repository. * /
175     public void testFindAllResources() {
176         MultiFileSystem mfs = new MultiFileSystem (new FileSystem[] {testedFS});
177         repo.addFileSystem(testedFS);
178         repo.addFileSystem(mfs);
179         java.util.Enumeration en = repo.findAllResources(resource1);
180         int count;
181         for (count = 0; en.hasMoreElements();count++) {
182           en.nextElement();
183         }
184         fsAssert("findAllResource should returned 2 elements",count == 2);
185     }
186     */

187     
188     /** Test of findAll method, of class org.openide.filesystems.Repository. * /
189     public void testFindAll() {
190         MultiFileSystem mfs = new MultiFileSystem (new FileSystem[] {testedFS});
191         repo.addFileSystem(testedFS);
192         repo.addFileSystem(mfs);
193         java.util.Enumeration en = repo.findAll(pkg.replace('/','.'),name,ext);
194         int count;
195         for (count = 0; en.hasMoreElements();count++) {
196           en.nextElement();
197         }
198         fsAssert("findAllResource should returned 2 elements",count == 2);
199     }
200     */

201     
202     /** Test of addRepositoryListener method, of class org.openide.filesystems.Repository. */
203     public void testAddRepositoryListener() {
204         RepList repList = new RepList ();
205         repo.addRepositoryListener(repList);
206         repo.addFileSystem(testedFS);
207         fsAssert("Expected one event",repList.getAdded () == 1);
208         fsAssert("Unexpected event",repList.getRemoved () == 0);
209         fsAssert("Unexpected event",repList.getReordered () == 0);
210     }
211     
212     /** Test of removeRepositoryListener method, of class org.openide.filesystems.Repository. */
213     public void testRemoveRepositoryListener() {
214         RepList repList = new RepList ();
215         repo.addRepositoryListener(repList);
216         repo.removeRepositoryListener(repList);
217         repo.addFileSystem(testedFS);
218         fsAssert("Expected one event",repList.getAdded () == 0);
219         fsAssert("Unexpected event",repList.getRemoved () == 0);
220         fsAssert("Unexpected event",repList.getReordered () == 0);
221     }
222     
223     
224     public void testRepfileFolderCreated() throws IOException {
225         FileSystem fs = this.testedFS;
226         FileObject root = fs.getRoot ();
227         repo.addFileSystem(fs);
228         if (!fs.isReadOnly () && !root.isReadOnly()) {
229             root.getChildren();
230             registerDefaultListener (repo);
231             root.createFolder("testtset");
232             fileFolderCreatedAssert ("unexpecetd event count",1);
233         }
234     }
235     
236     public void testRepfileDataCreated() throws IOException {
237         FileSystem fs = this.testedFS;
238         FileObject root = fs.getRoot ();
239         repo.addFileSystem(fs);
240         if (!fs.isReadOnly () && !root.isReadOnly()) {
241             root.getChildren();
242             registerDefaultListener (repo);
243             FileObject newF = root.createData("testfile","txe");
244             fileDataCreatedAssert ("unexpecetd event count",1);
245         }
246         
247     }
248
249     public void testRepfileRenamed() throws IOException {
250         FileSystem fs = this.testedFS;
251         FileObject root = fs.getRoot ();
252         repo.addFileSystem(fs);
253         if (!fs.isReadOnly () && !root.isReadOnly()) {
254             root.getChildren();
255             registerDefaultListener (repo);
256             FileObject newF = root.createData("testfile","txe");
257             FileLock fLock = newF.lock();
258             try {
259                 newF.rename(fLock,"obscure","uni");
260             } finally {
261                 fLock.releaseLock();
262             }
263
264             fileRenamedAssert("unexpecetd event count",1);
265         }
266         
267     }
268
269     public void testRepfileDeleted() throws IOException {
270         FileSystem fs = this.testedFS;
271         FileObject root = fs.getRoot ();
272         repo.addFileSystem(fs);
273         if (!fs.isReadOnly () && !root.isReadOnly()) {
274             root.getChildren();
275             registerDefaultListener (repo);
276             FileObject newF = root.createData("testfile","txe");
277             FileLock fLock = newF.lock();
278             try {
279                 newF.delete(fLock);
280             } finally {
281                 fLock.releaseLock();
282             }
283
284             fileDeletedAssert("unexpecetd event count",1);
285         }
286         
287     }
288     
289     public class RepList implements RepositoryListener{
290         int added = 0;
291         int removed = 0;
292         int reordered = 0;
293         public void fileSystemAdded(RepositoryEvent ev) {
294             added++;
295         }
296         public void fileSystemRemoved(RepositoryEvent ev) {
297             removed++;
298         }
299         public void fileSystemPoolReordered(RepositoryReorderedEvent ev) {
300             reordered++;
301         }
302         
303         int getAdded () {
304             return added;
305         }
306         int getRemoved () {
307             return removed;
308         }
309         int getReordered () {
310             return reordered;
311         }
312     }
313 }
314
Popular Tags