KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyVetoException JavaDoc;
24 import java.beans.VetoableChangeListener JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 public class FileSystemTestHid extends TestBaseHid {
28     private FileObject root;
29     private static String JavaDoc[] resources = new String JavaDoc [] {
30         "atleastone"
31     };
32
33     public FileSystemTestHid(String JavaDoc testName) {
34         super(testName);
35     }
36     private static int abacus = 0;
37
38
39     protected String JavaDoc[] getResources (String JavaDoc testName) {
40         return resources;
41     }
42     public void testAddFileStatusListener () {
43         if (!(this.testedFS instanceof TestUtilHid.StatusFileSystem))
44             return;
45         FileStatusListener[] fsListeners = new FileStatusListener [10];
46         abacus = 0;
47
48         for (int i = 0; i < fsListeners.length; i++) {
49             fsListeners[i] = createFileStatusListener ();
50             this.testedFS.addFileStatusListener(fsListeners[i]);
51         }
52         this.testedFS.fireFileStatusChanged(new FileStatusEvent (this.testedFS, true, true) );
53         fsAssert("failure: not all FileStstausListeners invoked: " + abacus, abacus == fsListeners.length);
54
55         abacus = 0;
56         this.testedFS.removeFileStatusListener(fsListeners[0]);
57         this.testedFS.fireFileStatusChanged(new FileStatusEvent (this.testedFS, true, true) );
58         fsAssert("failure: not all FileStstausListeners invoked", abacus == (fsListeners.length -1));
59     }
60
61     
62     public void testAddVetoableChangeListener () {
63         VetoableChangeListener JavaDoc[] vListeners = new VetoableChangeListener JavaDoc[10];
64         abacus = 0;
65
66         for (int i = 0; i < vListeners.length; i++) {
67             vListeners[i] = createVetoableChangeListener ();
68             this.testedFS.addVetoableChangeListener(vListeners[i]);
69         }
70         try {
71             this.testedFS.fireVetoableChange("test", "old", "new");
72         } catch (PropertyVetoException JavaDoc pex) {
73             fsFail("unexpected veto exception");
74         }
75         fsAssert("failure: not all VetoableChangeListeners invoked", abacus == vListeners.length);
76         
77         abacus = 0;
78         this.testedFS.removeVetoableChangeListener(vListeners[0]);
79         try {
80             this.testedFS.fireVetoableChange("test", "old", "new");
81         } catch (PropertyVetoException JavaDoc pex) {
82             fsFail("unexpected veto exception");
83         }
84         fsAssert("failure: not all VetoableChangeListeners invoked", abacus == (vListeners.length -1));
85     }
86     
87     private FileStatusListener createFileStatusListener () {
88         return new FileStatusListener () {
89             public void annotationChanged (FileStatusEvent ev) {
90                 abacus++;
91             }
92         };
93
94     }
95
96     private VetoableChangeListener JavaDoc createVetoableChangeListener () {
97         return new VetoableChangeListener JavaDoc () {
98             public void vetoableChange(PropertyChangeEvent JavaDoc evt) throws PropertyVetoException JavaDoc {
99                     abacus++;
100             }
101         };
102
103     }
104     
105     public void testFsfileFolderCreated() throws IOException JavaDoc {
106         FileSystem fs = this.testedFS;
107         if (!fs.isReadOnly () && !root.isReadOnly()) {
108             root.getChildren();
109             registerDefaultListener (fs);
110             root.createFolder("testtset");
111             fileFolderCreatedAssert ("unexpecetd event count",1);
112         }
113     }
114     
115     public void testFsfileDataCreated() throws IOException JavaDoc {
116         FileSystem fs = this.testedFS;
117         if (!fs.isReadOnly () && !root.isReadOnly()) {
118             root.getChildren();
119             registerDefaultListener (fs);
120             FileObject newF = root.createData("testfile","txe");
121             fileDataCreatedAssert ("unexpecetd event count",1);
122         }
123         
124     }
125
126     public void testFsfileRenamed() throws IOException JavaDoc {
127         FileSystem fs = this.testedFS;
128         if (!fs.isReadOnly () && !root.isReadOnly()) {
129             root.getChildren();
130             registerDefaultListener (fs);
131             FileObject newF = root.createData("testfile","txe");
132             FileLock fLock = newF.lock();
133             try {
134                 newF.rename(fLock,"obscure","uni");
135             } finally {
136                 fLock.releaseLock();
137             }
138
139             fileRenamedAssert("unexpecetd event count",1);
140         }
141         
142     }
143
144     public void testFsfileDeleted() throws IOException JavaDoc {
145         FileSystem fs = this.testedFS;
146         if (!fs.isReadOnly () && !root.isReadOnly()) {
147             root.getChildren();
148             registerDefaultListener (fs);
149             FileObject newF = root.createData("testfile","txe");
150             FileLock fLock = newF.lock();
151             try {
152                 newF.delete(fLock);
153             } finally {
154                 fLock.releaseLock();
155             }
156
157             fileDeletedAssert("unexpecetd event count",1);
158         }
159         
160     }
161     
162     /** Test of isValid method, of class org.openide.filesystems.FileSystem. */
163     public void testIsValid() {
164         Repository r = new Repository(new LocalFileSystem ());
165         //
166
fsAssert("file system, which is not assigned to the repository, should be invalid",
167         !testedFS.isValid());
168         
169         // assign to empty repository -> become valid
170
r.addFileSystem(testedFS);
171         if (!testedFS.getSystemName().equals(""))
172             fsAssert("assign to empty repository -> become valid" , testedFS.isValid());
173         
174         // remove from repo -> become invalid
175
r.removeFileSystem(testedFS);
176         fsAssert("remove from repo -> become invalid", !testedFS.isValid());
177     }
178
179     protected void setUp() throws Exception JavaDoc {
180         super.setUp();
181         root = testedFS.findResource(getResourcePrefix());
182     }
183     
184 }
185
Popular Tags