1 19 20 package org.openide.filesystems; 21 import java.net.URL ; 22 import java.net.URLStreamHandlerFactory ; 23 import java.util.*; 24 25 30 public abstract class TestBaseHid extends MultiThreadedTestCaseHid { 31 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 resourcePrefix = ""; 41 42 static { 43 URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory () { 44 public java.net.URLStreamHandler createURLStreamHandler(String protocol) { 45 if (protocol.equals("nbfs")) { return FileUtil.nbfsURLStreamHandler(); 47 } 48 return null; 49 }}); 50 } 51 52 53 54 public TestBaseHid(String name) { 55 super(name); 56 } 57 58 59 protected FileSystem testedFS; 60 61 63 protected FileSystem allTestedFS[]; 64 65 protected void setUp() throws Exception { 66 System.setProperty("workdir", getWorkDirPath()); 67 defListener = createFileChangeListener (); 68 69 clearWorkDir(); 71 String [] 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 { 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 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 126 protected abstract String [] getResources (String testName); 128 129 public final void fsTestFrameworkErrorAssert (String message, boolean condition) { 130 fsAssert ("Tests did not fail, but test framework contains errors: " + message,condition); 131 } 132 133 public final void fsFail (String message) { 134 fail (message + " ["+ FileSystemFactoryHid.getTestClassName () + "]"); 135 } 136 137 138 public final void fsAssert (String message, boolean condition) { 139 assertTrue (message + " ["+ FileSystemFactoryHid.getTestClassName () + "]", condition); 140 } 141 142 public final void fileChangedAssert (String message, int expectedCount) { 143 fileEventAssert (fileChangedL, message, expectedCount); 144 } 145 146 public final void fileDataCreatedAssert (String message, int expectedCount) { 147 fileEventAssert (fileDCreatedL, message, expectedCount); 148 } 149 150 public final void fileFolderCreatedAssert (String message, int expectedCount) { 151 fileEventAssert (fileFCreatedL, message, expectedCount); 152 } 153 154 public final void fileDeletedAssert (String message, int expectedCount) { 155 fileEventAssert (fileDeletedL, message, expectedCount); 156 } 157 158 public final void fileRenamedAssert (String message, int expectedCount) { 159 fileEventAssert (fileRenamedL , message, expectedCount); 160 } 161 162 public final void fileAttributeChangedAssert (String message, int expectedCount) { 163 fileEventAssert (fileAttrChangedL , message, expectedCount); 164 } 165 166 private void fileEventAssert (List list, String 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 getResourcePrefix() { 197 return resourcePrefix; 198 } 199 } 200 | Popular Tags |