KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
22 import java.net.URLStreamHandlerFactory JavaDoc;
23 import java.util.*;
24
25 /**
26  *
27  * @author vs124454, rm111737
28  * @version
29  */

30 public abstract class TestBaseHid extends MultiThreadedTestCaseHid {
31     /** Support for events*/
32     protected List<FileEvent> fileChangedL = new ArrayList<FileEvent> ();
33     protected List<FileEvent> fileDCreatedL = new ArrayList<FileEvent> ();
34     protected List<FileEvent> fileFCreatedL = new ArrayList<FileEvent> ();
35     protected List<FileEvent> fileDeletedL = new ArrayList<FileEvent> ();
36     protected List<FileRenameEvent> fileRenamedL = new ArrayList<FileRenameEvent> ();
37     protected List<FileAttributeEvent> fileAttrChangedL = new ArrayList<FileAttributeEvent> ();
38
39     private FileChangeListener defListener;
40     private String JavaDoc resourcePrefix = "";
41     
42     static {
43         URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory JavaDoc() {
44             public java.net.URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
45                 if (protocol.equals("nbfs")) { // NOI18N
46
return FileUtil.nbfsURLStreamHandler();
47                 }
48                 return null;
49         }});
50     }
51     
52     
53     /** Creates new FSTest */
54     public TestBaseHid(String JavaDoc name) {
55         super(name);
56     }
57     
58     /** first filesystem allTestedFS[0]*/
59     protected FileSystem testedFS;
60     
61     /** array of filesystems that can be used for tests. All filesystems should
62      * satisfy requirements for resources @see getResources () */

63     protected FileSystem allTestedFS[];
64     
65     protected void setUp() throws Exception JavaDoc {
66         System.setProperty("workdir", getWorkDirPath());
67         defListener = createFileChangeListener ();
68
69         //FileSystemFactoryHid.destroyFileSystem (this.getName(),this);
70
clearWorkDir();
71         String JavaDoc[] resources = getResources (getName());
72         resourcePrefix = FileSystemFactoryHid.getResourcePrefix(this.getName(),this, resources);
73         allTestedFS = FileSystemFactoryHid.createFileSystem(getName(),resources,this);
74         if (allTestedFS != null) testedFS = allTestedFS[0];
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78         if (testedFS instanceof JarFileSystem) {
79             testedFS.removeNotify();
80         }
81         testedFS = null;
82         allTestedFS = null;
83     }
84
85     protected final void registerDefaultListener (FileObject fo) {
86         fo.addFileChangeListener(defListener);
87     }
88     
89     protected final void registerDefaultListener (FileSystem fs) {
90         fs.addFileChangeListener(defListener);
91     }
92
93     protected final void registerDefaultListener (Repository rep) {
94         rep.addFileChangeListener(defListener);
95     }
96
97 //
98
private void reinitDefListener() {
99         fileChangedL = new ArrayList<FileEvent> ();
100         fileDCreatedL = new ArrayList<FileEvent> ();
101         fileFCreatedL = new ArrayList<FileEvent> ();
102         fileDeletedL = new ArrayList<FileEvent> ();
103         fileRenamedL = new ArrayList<FileRenameEvent> ();
104         fileAttrChangedL = new ArrayList<FileAttributeEvent> ();
105     }
106
107     protected final void deregisterDefaultListener (FileObject fo) {
108         reinitDefListener();
109         fo.removeFileChangeListener(defListener);
110     }
111
112     protected final void deregisterDefaultListener (FileSystem fs) {
113         reinitDefListener();
114         fs.removeFileChangeListener(defListener);
115     }
116
117     protected final void deregisterDefaultListener (Repository rep) {
118         reinitDefListener();
119         rep.removeFileChangeListener(defListener);
120     }
121     
122     
123     /** Test can require some resources to be part of filesystem that will be tested
124      * @return array of resources
125      */

126     protected abstract String JavaDoc[] getResources (String JavaDoc testName);// {return new String[] {};}
127

128        
129     public final void fsTestFrameworkErrorAssert (String JavaDoc message, boolean condition) {
130         fsAssert ("Tests did not fail, but test framework contains errors: " + message,condition);
131     }
132     
133     public final void fsFail (String JavaDoc message) {
134         fail (message + " ["+ FileSystemFactoryHid.getTestClassName () + "]");
135     }
136
137     
138     public final void fsAssert (String JavaDoc message, boolean condition) {
139         assertTrue (message + " ["+ FileSystemFactoryHid.getTestClassName () + "]", condition);
140     }
141     
142     public final void fileChangedAssert (String JavaDoc message, int expectedCount) {
143         fileEventAssert (fileChangedL, message, expectedCount);
144     }
145
146     public final void fileDataCreatedAssert (String JavaDoc message, int expectedCount) {
147         fileEventAssert (fileDCreatedL, message, expectedCount);
148     }
149
150     public final void fileFolderCreatedAssert (String JavaDoc message, int expectedCount) {
151         fileEventAssert (fileFCreatedL, message, expectedCount);
152     }
153
154     public final void fileDeletedAssert (String JavaDoc message, int expectedCount) {
155         fileEventAssert (fileDeletedL, message, expectedCount);
156     }
157     
158     public final void fileRenamedAssert (String JavaDoc message, int expectedCount) {
159         fileEventAssert (fileRenamedL , message, expectedCount);
160     }
161     
162     public final void fileAttributeChangedAssert (String JavaDoc message, int expectedCount) {
163         fileEventAssert (fileAttrChangedL , message, expectedCount);
164     }
165      
166     private void fileEventAssert (List list, String JavaDoc message, int expectedCount) {
167         fsAssert (message+" Fired : " +list.size () + " ,but expected: " + expectedCount,expectedCount == list.size ());
168     }
169     
170     protected FileChangeListener createFileChangeListener () {
171      return new FileChangeAdapter () {
172             public void fileChanged (FileEvent fe) {
173                 fileChangedL.add (fe);
174             }
175             public void fileDeleted (FileEvent fe) {
176                 fileDeletedL.add (fe);
177             }
178             public void fileFolderCreated (FileEvent fe) {
179                 fsAssert("Unexpected data file", fe.getFile().isFolder());
180                 fileFCreatedL.add (fe);
181             }
182             public void fileDataCreated (FileEvent fe) {
183                 fsAssert("Unexpected folder", fe.getFile().isData());
184                 fileDCreatedL.add (fe);
185             }
186             public void fileRenamed (FileRenameEvent fe) {
187                 fileRenamedL.add (fe);
188             }
189             
190             public void fileAttributeChanged (FileAttributeEvent fe) {
191                 fileAttrChangedL.add (fe);
192             }
193         };
194     }
195
196     protected String JavaDoc getResourcePrefix() {
197         return resourcePrefix;
198     }
199 }
200
Popular Tags