1 19 20 package org.openide.filesystems; 21 22 import junit.framework.*; 23 25 import java.util.*; 26 import java.io.*; 27 import java.net.URL ; 28 29 35 public class AttributesTestHidden extends TestBaseHid { 36 static final String TEST_ERR = "Not the tested code, but this test contains errors"; 37 static final String COMMON_ATTR_STRING = "testAttribute"; 38 39 private static String [] resources = new String [] { 40 "/compatibility/filesystem.attributes.backup", "/compatibility/Attr.test", }; 43 44 46 protected String [] getResources (String testName) { 47 return resources; 48 } 49 50 51 public AttributesTestHidden(String name) { 52 super(name); 53 } 54 55 62 @SuppressWarnings ("unchecked") 63 public void testAttribute01() throws IOException { 64 assertTrue (TEST_ERR,testedFS != null); 65 if (testedFS.isReadOnly ()) return; 66 67 FileObject foTested = testedFS.getRoot (); 68 List<String > list = new LinkedList<String > (); 69 70 71 int i = 0; 72 for (i = 0; i < 20; i++) { 73 list.add(COMMON_ATTR_STRING); 74 foTested.setAttribute (COMMON_ATTR_STRING, list); 75 } 76 77 System.gc ();System.gc (); 78 try { 79 Thread.sleep(300); 80 } catch (InterruptedException iex){} 81 list = (List)foTested.getAttribute (COMMON_ATTR_STRING); 82 assertTrue ("Caching problem in attributes: "+list.size(),list.size() == i); 83 } 84 85 88 public void testAttribute02 () throws IOException { 89 assertTrue (TEST_ERR,testedFS != null); 90 if (testedFS.isReadOnly ()) return; 91 92 Locale locale = Locale.getDefault(); 94 if (locale != Locale.JAPAN || locale != Locale.JAPANESE) 95 return; 96 97 99 String japanFileName = new String (new char[] {'\u025A','\u012F','\u025A','\u012F'}); 100 FileObject foTested = testedFS.getRoot (); 101 FileObject folder = foTested.getFileObject (japanFileName,null); 102 103 if (folder == null) { 104 try { 105 folder = foTested.createFolder (japanFileName); 106 if (folder == null) return; 107 } catch (IOException iox) { 108 return; 109 } 110 } 111 112 folder.setAttribute (COMMON_ATTR_STRING,COMMON_ATTR_STRING); 113 String result = (String )folder.getAttribute (COMMON_ATTR_STRING); 114 assertTrue ("Coding problem by writing and reading attributes - .nbattrs",result != null); 115 } 116 117 121 public void testAttribute03 () throws IOException { 122 assertTrue (TEST_ERR,testedFS != null); 123 if (testedFS.isReadOnly ()) return; 124 125 126 String testedStr = new String (new char[] {'&','<','>','\"','\''}); 127 String result; 128 129 result = basicAttributeTest (testedStr,COMMON_ATTR_STRING); 130 assertTrue ("XML not allowed chars are bad handled - key problem", 131 result != null && result.equals (COMMON_ATTR_STRING)); 132 133 result = basicAttributeTest (COMMON_ATTR_STRING,testedStr); 134 assertTrue ("XML not allowed chars are bad handled - value problem", 135 result != null && result.equals (testedStr)); 136 } 137 138 143 public void KtestAttribute04 () throws IOException { 144 assertTrue (TEST_ERR,testedFS != null); 145 if (testedFS.isReadOnly ()) return; 146 147 148 FileObject backup = testedFS.findResource ("/compatibility/filesystem.attributes.backup"); 149 assertTrue (TEST_ERR,backup != null); 150 FileLock lock = backup.lock(); 151 backup.rename(lock,"filesystem","attributes"); 152 lock.releaseLock(); 153 FileObject testedFo = testedFS.findResource ("/compatibility/Attr.test"); 154 assertTrue (TEST_ERR,testedFo != null); 155 157 assertTrue ("Back compatibility problem",testedFo.getAttribute ("testAttr") != null); 158 } 159 160 165 public void testAttribute05 () throws IOException { 166 assertTrue (TEST_ERR,testedFS != null); 167 if (testedFS.isReadOnly ()) return; 168 169 170 FileObject testedFo = testedFS.findResource ("/compatibility"); 171 assertTrue (TEST_ERR,testedFo != null); 172 173 assertTrue ("filesystem.attributes must stay hidden for filesystems",testedFo.getFileObject ("filesystem","attributes") == null); 174 } 175 176 177 182 public void testAttribute06 () throws IOException { 183 assertTrue (TEST_ERR,testedFS != null); 184 if (testedFS.isReadOnly ()) return; 185 186 basicAttributeTest (COMMON_ATTR_STRING,COMMON_ATTR_STRING); 187 assertTrue (".nbattrs must stay hidden for filesystems",testedFS.getRoot ().getFileObject (".nbattrs") == null); 188 } 189 190 195 public void testAttribute07 () throws IOException { 196 assertTrue (TEST_ERR,testedFS != null); 197 if (testedFS.isReadOnly ()) return; 198 201 if (!(testedFS instanceof LocalFileSystem)) return; 202 203 String testAttr = COMMON_ATTR_STRING+"Null"; 204 205 basicAttributeTest (testAttr,testAttr); 206 basicAttributeTest (COMMON_ATTR_STRING,COMMON_ATTR_STRING); 207 208 String result = basicAttributeTest (testAttr,null); 209 assertTrue ("After setAttribute (,null) - getAttribute must return null",result == null); 210 211 String content = testAttr; 212 FileInputStream fis = null; 213 try { 214 File parent = FileUtil.toFile(testedFS.getRoot()); 215 fis = new FileInputStream(new File(parent,".nbattrs")); 216 byte[] bytes = new byte [fis.available()]; 217 fis.read(bytes); 218 content = new String (bytes); 219 } finally { 220 if (fis != null) fis.close(); 221 } 222 assertTrue ("After setAttribute (,null) appropriate key cannot be in .nbattrs",content.indexOf("name=\""+testAttr) == -1 ); 223 assertTrue ("There was not found expected key: " + COMMON_ATTR_STRING +" in .nbattrs",content.indexOf("name=\""+COMMON_ATTR_STRING) != -1 ); } 225 226 227 public void testAttribute08 () throws Exception { 228 URL fsURLDef = this.getClass().getResource ("data/Attributes.xml"); 229 assertTrue ("Cannot create XML FS for testing purposes", fsURLDef != null); 230 FileSystem fs = new XMLFileSystem (fsURLDef); 231 FileObject fo = fs.findResource("testMethodValue"); 232 assertTrue ("Cannot acces FileObject named testMethodValue", fo != null); 233 234 String testName = "test1"; 235 Object obj = fo.getAttribute(testName); 236 assertTrue ("methodValue failed", obj != null); 237 assertTrue ("methodValue doesn't keep order ", 238 obj.equals(getObjectViaMethodValue1 (fo, testName))); 239 240 testName = "test2"; 241 obj = fo.getAttribute(testName); 242 assertTrue ("methodValue failed", obj != null); 243 assertTrue ("methodValue doesn't keep order ", 244 obj.equals(getObjectViaMethodValue2 (testName, fo))); 245 246 testName = "test3"; 247 obj = fo.getAttribute(testName); 248 assertTrue ("methodValue failed", obj != null); 249 assertTrue ("methodValue doesn't keep order ", 250 obj.equals(getObjectViaMethodValue3 (fo))); 251 252 testName = "test4"; 253 obj = fo.getAttribute(testName); 254 assertTrue ("methodValue failed", obj != null); 255 assertTrue ("methodValue doesn't keep order ", 256 obj.equals(getObjectViaMethodValue4 (testName))); 257 258 testName = "test5"; 259 obj = fo.getAttribute(testName); 260 assertTrue ("methodValue failed", obj != null); 261 assertTrue ("methodValue doesn't keep order ", 262 obj.equals(getObjectViaMethodValue5 ())); 263 264 testName = "test6"; 265 obj = fo.getAttribute(testName); 266 assertTrue ("methodValue failed", obj != null); 267 assertEquals("even works for java.util.Map", "Ahoj1", obj); 268 269 testName = "test7"; 270 obj = fo.getAttribute(testName); 271 assertTrue ("methodValue failed", obj != null); 272 assertEquals("works for map and string", "Ahoj1test7", obj); 273 } 274 275 public void testAttribute09 () throws IOException { 276 assertTrue (TEST_ERR,testedFS != null); 277 if (testedFS.isReadOnly ()) return; 278 281 if (!(testedFS instanceof LocalFileSystem)) return; 282 FileObject foTested = testedFS.getRoot (); 283 284 char[] testChars = new char [] {'A',0x0000,0x008F,'B'}; 285 String [] atts = new String [] {"c:\\builds\\u3\\AboutExamples", 286 "c:\\builds\\u3A\\AboutExamples", 287 "c:\\builds\\u33\\AboutExamples", 288 "c:\\builds\\u345\\AboutExamples", 289 "c:\\builds\\u3456\\AboutExamples", 290 "c:\\builds\\u3G85\\AboutExamples", 291 "c:\\builds\\au3456\\AboutExamples", 292 String.valueOf(testChars) 293 }; 294 295 for (int i = 0; i < atts.length; i++) { 296 String keyAndValue = atts[i]; 297 foTested.setAttribute(keyAndValue, keyAndValue); 298 Object testIt = foTested.getAttribute(keyAndValue); 299 assertEquals(keyAndValue, testIt); 300 } 301 } 302 303 private static String getObjectViaMethodValue1 (FileObject fo, String testName) { 304 return fo.getPath()+testName; 305 } 306 307 private static String getObjectViaMethodValue1 (String testName, FileObject fo) { 308 return testName+fo.getPath(); 309 } 310 311 private static String getObjectViaMethodValue1 (FileObject fo) { 312 return fo.getPath(); 313 } 314 315 private static String getObjectViaMethodValue1 (String testName) { 316 return testName; 317 } 318 319 private static String getObjectViaMethodValue1 () { 320 return ""; 321 } 322 private static String getObjectViaMethodValue2 (String testName, FileObject fo) { 324 return testName+fo.getPath(); 325 } 326 327 private static String getObjectViaMethodValue2 (FileObject fo) { 328 return fo.getPath(); 329 } 330 331 private static String getObjectViaMethodValue2 (String testName) { 332 return testName; 333 } 334 335 private static String getObjectViaMethodValue2 () { 336 return ""; 337 } 338 private static String getObjectViaMethodValue3 (FileObject fo) { 340 return fo.getPath(); 341 } 342 343 private static String getObjectViaMethodValue3 (String testName) { 344 return testName; 345 } 346 347 private static String getObjectViaMethodValue3 () { 348 return ""; 349 } 350 private static String getObjectViaMethodValue4 (String testName) { 352 return testName; 353 } 354 355 private static String getObjectViaMethodValue4 () { 356 return ""; 357 } 358 private static String getObjectViaMethodValue5 () { 360 return ""; 361 } 362 363 private static Object getObjectViaMethodValue6 (Map attrs) { 364 try { 365 attrs.keySet().iterator().remove(); 366 return "UnsupportedOperationException"; 367 } catch (UnsupportedOperationException ex) { 368 } 370 try { 371 attrs.put("value1", "nothing"); 372 return "UnsupportedOperationException"; 373 } catch (UnsupportedOperationException ex) { 374 } 376 try { 377 attrs.remove("value1"); 378 return "UnsupportedOperationException"; 379 } catch (UnsupportedOperationException ex) { 380 } 382 383 384 return attrs.get("value1"); 385 } 386 private static Object getObjectViaMethodValue7 (Map<String ,Object > attrs, String attrName) { 387 assertEquals(8, attrs.keySet().size()); 388 try { 389 attrs.entrySet().remove(null); 390 return "UnsupportedOperationException"; 391 } catch (UnsupportedOperationException ex) { 392 } 394 395 396 return attrs.get("value1") + attrName; 397 } 398 399 private String basicAttributeTest (String attrName, String attrValue) throws IOException { 400 FileObject foTested = testedFS.getRoot (); 401 foTested.setAttribute (attrName, attrValue); 402 return (String )foTested.getAttribute (attrName); 403 } 404 407 408 } 409 410 411 412 | Popular Tags |