1 19 20 package org.openide.filesystems; 21 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.File ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 public class XMLFileSystemTestHid extends TestBaseHid { 30 31 private String [] resources = new String [] {"a/b/c"}; 32 XMLFileSystem xfs = null; 33 34 public XMLFileSystemTestHid(String testName) { 35 super(testName); 36 } 37 38 protected String [] getResources (String testName) { 39 return resources; 40 } 41 42 43 public void testReset () throws Exception { 44 FileObject a = xfs.findResource("a"); 45 assertNotNull(a); 46 47 48 FileChangeAdapter fcl = new FileChangeAdapter(); 49 a.addFileChangeListener(fcl); 50 51 resources = new String [] {"a/b/c","a/b1/c"}; 52 xfs.setXmlUrl(createXMLLayer().toURL()); 53 54 FileObject b1 = xfs.findResource("a/b1"); 55 assertNotNull(b1); 56 assertTrue(b1.isFolder()); 57 } 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 File f = createXMLLayer(); 63 xfs = new XMLFileSystem (); 64 xfs.setXmlUrl(f.toURL()); 65 this.testedFS = xfs; 66 } 67 68 private File createXMLLayer() throws IOException { 69 String testName = getName(); 70 File f = TestUtilHid.createXMLLayer(testName, getResources(testName)); 71 return f; 72 } 73 74 public void testChangesAreFiredOnSetXMLUrlsIssue59160() throws Exception { 75 File f = writeFile ("layer1.xml", 76 "<filesystem>\n" + 77 "<folder name='TestModule'>\n" + 78 "<file name='sample.txt' >Ahoj</file>\n" + 79 "</folder>\n" + 80 "</filesystem>\n" 81 ); 82 83 File f2 = writeFile ("layer2.xml", 84 "<filesystem>\n" + 85 "<folder name='TestModule'>\n" + 86 "<file name='sample.txt' >Hello!</file>\n" + 87 "</folder>\n" + 88 "</filesystem>\n" 89 ); 90 91 92 93 94 xfs = new XMLFileSystem (f.toURL()); 95 96 FileObject fo = xfs.findResource ("TestModule/sample.txt"); 97 assertEquals ("Four bytes there", 4, fo.getSize ()); 98 registerDefaultListener (fo); 99 100 xfs.setXmlUrl (f2.toURL ()); 101 102 assertEquals ("Six bytes there", 6, fo.getSize ()); 103 104 fileChangedAssert ("Change in the content", 1); 105 } 106 107 public void testChangesAreFiredOnSetXMLUrlsWithURLsIssue59160() throws Exception { 108 File u1 = writeFile("u1.txt", "Ahoj"); 109 File u2 = writeFile("u2.txt", "Hello!"); 110 111 File f = writeFile ("layer1.xml", 112 "<filesystem>\n" + 113 "<folder name='TestModule'>\n" + 114 "<file name='sample.txt' url='u1.txt' />\n" + 115 "</folder>\n" + 116 "</filesystem>\n" 117 ); 118 119 File f2 = writeFile ("layer2.xml", 120 "<filesystem>\n" + 121 "<folder name='TestModule'>\n" + 122 "<file name='sample.txt' url='u2.txt' />\n" + 123 "</folder>\n" + 124 "</filesystem>\n" 125 ); 126 127 128 129 130 xfs = new XMLFileSystem (f.toURL()); 131 132 FileObject fo = xfs.findResource ("TestModule/sample.txt"); 133 assertEquals ("Four bytes there", 4, fo.getSize ()); 134 registerDefaultListener (fo); 135 136 xfs.setXmlUrl (f2.toURL ()); 137 138 assertEquals ("Six bytes there", 6, fo.getSize ()); 139 140 fileChangedAssert ("Change in the content", 1); 141 } 142 143 public void testChangesAreFiredOnSetXMLUrlsWithAttributesIssue21204() throws Exception { 144 File f = writeFile ("layer1.xml", 145 "<filesystem>\n" + 146 "<folder name='TestModule'>\n" + 147 "<file name='sample.txt' >\n" + 148 " <attr name='value' stringvalue='old' />\n" + 149 "</file>\n" + 150 "</folder>\n" + 151 "</filesystem>\n" 152 ); 153 154 File f2 = writeFile ("layer2.xml", 155 "<filesystem>\n" + 156 "<folder name='TestModule'>\n" + 157 "<file name='sample.txt' >\n" + 158 " <attr name='value' stringvalue='new' />\n" + 159 "</file>\n" + 160 "</folder>\n" + 161 "</filesystem>\n" 162 ); 163 164 165 166 167 xfs = new XMLFileSystem (f.toURL()); 168 169 FileObject fo = xfs.findResource ("TestModule/sample.txt"); 170 assertEquals("Old value is in the attribute", "old", fo.getAttribute("value")); 171 registerDefaultListener (fo); 172 173 xfs.setXmlUrl (f2.toURL ()); 174 175 assertEquals("New value is in the attribute", "new", fo.getAttribute("value")); 176 fileAttributeChangedAssert("Change in the content", 1); 177 } 178 179 public void testChangesAreFiredOnSetXMLUrlsEvenWhenRemoved() throws Exception { 180 File u1 = writeFile("u1.txt", "Ahoj"); 181 182 File f = writeFile("layer1.xml", 183 "<filesystem>\n" + 184 "<folder name='TestModule'>\n" + 185 "<file name='sample.txt' url='u1.txt' />\n" + 186 "</folder>\n" + 187 "</filesystem>\n" 188 ); 189 190 File f2 = writeFile("layer2.xml", 191 "<filesystem>\n" + 192 "</filesystem>\n" 193 ); 194 195 196 197 198 xfs = new XMLFileSystem(f.toURL()); 199 200 FileObject fo = xfs.findResource("TestModule/sample.txt"); 201 assertEquals("Four bytes there", 4, fo.getSize()); 202 registerDefaultListener(fo); 203 204 xfs.setXmlUrl(f2.toURL()); 205 206 assertFalse("Valid no more", fo.isValid()); 207 assertEquals("Empty now", 0, fo.getSize()); 208 209 fileDeletedAssert("Change in the content", 1); 210 } 211 212 public void testIssue62570() throws Exception { 213 File f = writeFile("layer3.xml", 214 "<filesystem>\n" + 215 "<folder name='TestModule'>\n" + 216 "<file name='sample.txt' >Ahoj</file>\n" + 217 "<file name='sample2.txt' url='sample2.txt'/>\n" + 218 "</folder>\n" + 219 "</filesystem>\n" 220 ); 221 222 File f2 = new File (f.getParentFile(), "sample2.txt"); 223 if (!f2.exists()) { 224 Thread.sleep(3000); 225 assertTrue(f2.createNewFile()); 226 } 227 xfs = new XMLFileSystem(f.toURL()); 228 FileObject fo = xfs.findResource ("TestModule/sample.txt"); 229 assertNotNull(fo); 230 assertEquals(fo.lastModified().getTime(), f.lastModified()); 231 232 FileObject fo2 = xfs.findResource ("TestModule/sample2.txt"); 233 assertNotNull(fo2); 234 assertEquals(fo2.lastModified().getTime(), f2.lastModified()); 235 assertFalse(fo2.lastModified().equals(fo.lastModified())); 236 237 } 238 239 240 241 private File writeFile(String name, String content) throws IOException { 242 File f = new File (getWorkDir (), name); 243 java.io.FileWriter w = new java.io.FileWriter (f); 244 w.write(content); 245 w.close(); 246 return f; 247 } 248 249 250 public void testChangeOfAnAttributeInLayerIsFiredIfThereIsRealChange() throws Exception { 251 XMLFileSystem fs = new XMLFileSystem(); 252 253 File f1 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.List"); 254 File f2 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.Button"); 255 File f3 = changeOfAnAttributeInLayerIsFiredgenerateLayer("NoChange", "nochange"); 256 257 fs.setXmlUrls (new URL [] { f1.toURL(), f3.toURL() } ); 258 259 FileObject file = fs.findResource("Folder/empty.xml"); 260 assertNotNull("File found in layer", file); 261 262 FSListener l = new FSListener(); 263 file.addFileChangeListener(l); 264 265 FileObject nochange = fs.findResource("NoChange/empty.xml"); 266 assertNotNull("File found in layer", nochange); 267 FSListener no = new FSListener(); 268 nochange.addFileChangeListener(no); 269 270 assertAttr("The first value is list", file, "value", "java.awt.List"); 271 assertAttr("Imutable value is nochange", nochange, "value", "nochange"); 272 273 fs.setXmlUrls (new URL [] { f2.toURL(), f3.toURL() } ); 274 String v2 = (String ) file.getAttribute("value"); 275 assertEquals("The second value is button", "java.awt.Button", v2); 276 277 assertEquals("One change: " + l.events, 1, l.events.size()); 278 279 if (!(l.events.get(0) instanceof FileAttributeEvent)) { 280 fail("Wrong event: " + l.events); 281 } 282 283 assertAttr("Imutable value is still nochange", nochange, "value", "nochange"); 284 assertEquals("No change in this attribute: " + no.events, 0, no.events.size()); 285 } 286 287 private static void assertAttr(String msg, FileObject fo, String attr, String value) throws IOException { 288 Object v = fo.getAttribute(attr); 289 assertEquals(msg + "[" + fo + "]", value, v); 290 } 291 292 int cnt; 293 private File changeOfAnAttributeInLayerIsFiredgenerateLayer(String folderName, String string) throws IOException { 294 File f = new File (getWorkDir(), "layer" + (cnt++) + ".xml"); 295 FileWriter w = new FileWriter (f); 296 w.write( 297 "<filesystem>" + 298 "<folder name='" + folderName + "'>" + 299 " <file name='empty.xml' >" + 300 " <attr name='value' stringvalue='" + string + "' />" + 301 " </file>" + 302 "</folder>" + 303 "</filesystem>" 304 ); 305 w.close(); 306 return f; 307 } 308 309 private static class FSListener extends FileChangeAdapter { 310 public List <FileEvent> events = new ArrayList <FileEvent>(); 311 public List <FileEvent> change = new ArrayList <FileEvent>(); 312 313 314 public void fileRenamed(FileRenameEvent fe) { 315 events.add(fe); 316 } 317 318 public void fileAttributeChanged(FileAttributeEvent fe) { 319 events.add(fe); 320 } 321 322 public void fileFolderCreated(FileEvent fe) { 323 events.add(fe); 324 } 325 326 public void fileDeleted(FileEvent fe) { 327 events.add(fe); 328 } 329 330 public void fileDataCreated(FileEvent fe) { 331 events.add(fe); 332 } 333 334 public void fileChanged(FileEvent fe) { 335 change.add(fe); 336 } 337 338 } 339 340 } 341 | Popular Tags |