1 19 20 28 29 package DataLoaderTests.LoaderPoolTest; 30 31 import org.openide.*; 32 import org.openide.loaders.*; 33 import org.openide.filesystems.*; 34 import org.openide.util.Lookup; 35 36 import org.netbeans.core.*; 37 38 import java.io.File ; 39 import java.io.FileNotFoundException ; 40 import java.io.IOException ; 41 42 import java.util.Enumeration ; 43 import java.util.jar.JarFile ; 44 import java.util.*; 45 import java.util.jar.Attributes ; 46 import javax.swing.event.ChangeListener ; 47 import javax.swing.event.ChangeEvent ; 48 49 import junit.framework.*; 50 import org.netbeans.junit.*; 51 52 public class LoaderPoolTest extends NbTestCase 53 implements DataLoader.RecognizedFiles 54 { 55 56 57 public LoaderPoolTest(java.lang.String testName) { 58 super(testName); 59 } 60 61 boolean successful = true; 62 63 LoaderPoolNode LPN = null; 64 Repository Rep = null; 65 DataLoaderPool DLP = null; 66 DataLoader javadl = null; DataLoader textdl = null; LoaderPoolTest.ChL changel = null; Enumeration en = null; 70 71 int noOfChanges = 0; 73 74 public void writeLog(String text) { 75 log(text); 76 System.out.println(text); 77 if (text.equals(FAILED)) successful = false; 78 } 79 80 81 public void writeRef(String text) { 82 ref(text); 83 System.out.println(text); 84 if (text.equals(FAILED)) successful = false; 85 } 86 87 88 public void writeRef(String text, String inf) { 89 ref(text); 90 System.out.println(text); 91 if (inf.equals(FAILED)) successful = false; 92 assertTrue(text,successful); 93 } 94 95 96 void printException(Exception e) { 97 if(PRINT_EXCEPTIONS){ 98 e.printStackTrace(); 99 e.printStackTrace(getRef()); 100 } 101 } 102 103 104 108 public String getWorkDirPath() { 109 if (work == null) fail("Working directory not set!"); 110 return work; 112 } 113 114 117 void prepare() { 118 try{ 119 122 if (Manager.getWorkDirPath()==null) System.setProperty("nbjunit.workdir",System.getProperty("netbeans.user")); 124 noOfChanges = 0; 126 Rep = (Repository) Lookup.getDefault().lookup(Repository.class); 127 128 String str = null; 130 java.net.URL url = new LoaderPoolTest("x").getClass().getResource("LoaderPoolTest.class"); 131 if (url.getProtocol().equals("nbfs")) { 132 fail("nbfs is not handled"); 135 } 136 else str = url.getPath(); str = str.substring(0,str.indexOf(new LoaderPoolTest("x").getClass().getPackage().getName().replace('.','/'))); 138 java.io.File ff = new java.io.File (str); 139 org.openide.filesystems.LocalFileSystem lfs = new org.openide.filesystems.LocalFileSystem(); 140 lfs.setRootDirectory(ff); 141 Rep.addFileSystem(lfs); 142 143 DLP = DataLoaderPool.getDefault (); 144 }catch(Exception e){ 151 e.printStackTrace(); 152 e.printStackTrace(getRef()); 153 assertTrue("Initialization of test failed! ->" + e,false); 154 } 155 } 156 157 160 public void clean() { 161 164 DLP.removeChangeListener( changel ); 165 noOfChanges = 0; 166 Rep = null; 167 DLP = null; 168 LPN = null; 169 changel = null; 170 } 171 172 176 void dummyWait(int millis) { 177 try { 178 Thread.sleep(millis); 179 } catch (Exception ex) { 180 printException(ex); 181 } 182 } 183 184 187 class ChL implements ChangeListener { 188 LoaderPoolTest parent = null; 189 public ChL(LoaderPoolTest lp){ 190 super(); 191 parent=lp; 192 } 193 public void stateChanged(ChangeEvent e) { 194 writeRef("\nSome change over DataLoaderPool has happend, added or removed DataLoader!"); 195 parent.noOfChanges ++; 196 } 197 } 198 199 204 public DataLoader getDataLoader(String DLDisplayNameSubstr) { 205 if ( DLDisplayNameSubstr != null ) { 206 en = DLP .allLoaders(); 207 while ( en.hasMoreElements() ) { 208 DataLoader dl = (DataLoader) en.nextElement(); 209 if ( dl.getDisplayName().indexOf(DLDisplayNameSubstr) != -1 ) return dl; 210 } 211 } 212 writeRef("\n" + DLDisplayNameSubstr + " loader not found in the LoaderPool!"); 213 return null; 214 } 215 216 220 void exploreDataLoaderPool() throws Exception { 221 222 writeRef("\nListing all registred DataLoaders ..."); 223 en = DLP .allLoaders(); 224 while ( en.hasMoreElements() ) { 225 DataLoader dl = (DataLoader) en.nextElement(); 226 String str = dl.toString(); 227 System.out.println( dl.getDisplayName() + " / " + str.substring(0,str.indexOf('@')) ); 228 if ( dl.getDisplayName().indexOf("Java Source") != -1 ) javadl = dl; 229 if ( dl.getDisplayName().indexOf("Textual") != -1 ) textdl = dl; 230 } 231 writeRef(PASSED); 232 233 writeRef("\nGetting firstProducerOf ..."); 234 DataLoader[] dla = DLP.toArray(); 235 for ( int i = 0 ; i < dla.length ; i ++ ) { 236 String str = (DLP.firstProducerOf(dla[i].getRepresentationClass()) ).toString(); 237 System.out.println(str.substring(0,str.indexOf('@'))); 238 } 239 writeRef(PASSED); 240 241 writeRef("\nGetting ProducerOf ..."); 242 for ( int i = 0 ; i < dla.length ; i ++ ) { 243 Enumeration e = DLP.producersOf(dla[i].getRepresentationClass()); 244 while (e.hasMoreElements()) { 245 String str = e.nextElement().toString(); 246 System.out.println(str.substring(0,str.indexOf('@'))); 247 } 248 System.out.println("*"); 249 } 250 writeRef(PASSED); 251 252 writeRef("\nGetting prefered DataLoader for java source file ..."); 253 String name = "DataLoaderTests/DataObjectTest/data/ClassObject.java"; 255 256 257 FileObject fo = toFileObject(name); 258 writeRef(fo.toString()); 259 if ( DataLoaderPool.getPreferredLoader( fo ) == null ) writeRef("\nnull"); 260 else { 261 String str = DataLoaderPool.getPreferredLoader( fo ) .toString(); 263 writeRef(str.substring(0,str.indexOf('@'))); 264 } 265 writeRef(PASSED); 266 267 writeRef("\nSetting prefered DataLoader for java source file ..."); 268 DataLoaderPool.setPreferredLoader( fo , javadl ); 269 writeRef(PASSED); 270 271 writeRef("\nGetting prefered DataLoader for java source file ..."); 272 writeRef(fo.toString()); 273 writeRef(PASSED); 277 } 278 279 static LocalFileSystem lfs ; 280 281 private static FileObject toFileObject(String fileName ) throws Exception { 282 if (lfs == null ) { 283 lfs = new LocalFileSystem(); 284 String xtestData = System.getProperty("xtest.data"); 285 if (! xtestData.endsWith(File.separator) ) { 286 xtestData = xtestData + "/"; 287 } 288 lfs.setRootDirectory(new File (xtestData)); 289 } 290 297 FileObject fo = lfs.findResource(fileName); 298 if (fo == null) { 300 throw new FileNotFoundException ("fo:" + fileName); 301 } 302 return fo; 303 } 304 308 void createDataObject(String fileName, DataLoader dl) throws Exception { 309 writeRef("\nCreating DataObject for file " + fileName.substring(fileName.lastIndexOf('/')+1) + " ..."); 310 311 dl.findDataObject (toFileObject(fileName), this ); 312 } 313 314 321 void notCreateDataObject(String file, DataLoader dl) throws Exception { 322 writeRef("\nnotCreating DataObject for file " + file.substring(file.lastIndexOf('/')+1) + " ..."); 323 FileObject fo = toFileObject(file); 324 325 try { 326 dl.findDataObject (fo, this ); 327 writeRef("NotCreating DataObject failed!",FAILED); 328 } catch (Exception ex) { 329 printException(ex); 330 writeRef(PASSED); 331 } 332 } 333 334 339 boolean existDataObject(String fileName) throws Exception { 340 writeRef("\nDataObject for file " + fileName.substring(fileName.lastIndexOf('/')+1) + " should exist ..."); 341 FileObject fo = toFileObject(fileName); 342 try { 343 DataObject.find(fo); 344 writeRef(PASSED); 345 return true; 346 } catch (Exception ex) { 347 printException(ex); 348 writeRef("DataObject should exist but doesn't!",FAILED); 349 return false; 350 } 351 } 352 353 358 371 372 373 379 boolean recognizedAs(String fileName, String loaderDisplayName) throws IOException { 380 writeRef("\nFile " + fileName.substring(fileName.lastIndexOf('/')+1) + " should be recognized as " + loaderDisplayName + " ..."); 381 File file = new File (fileName); 382 if (file.exists() == false) { 383 throw new FileNotFoundException (fileName); 384 } 385 FileObject fo = FileUtil.toFileObject(file); 386 try { 387 boolean status = DataObject.find(fo).getLoader().getDisplayName().indexOf(loaderDisplayName) != -1; 388 writeRef(PASSED); 390 return status; 391 } catch (Exception ex) { 392 printException(ex); 393 writeRef("File should be recognized as "+ loaderDisplayName +" but isn't!",FAILED); 394 return false; 395 } 396 } 397 398 404 boolean notRecognizedAs(String file, String loaderName){ 405 writeRef("\nFile " + file.substring(file.lastIndexOf('/')+1) + " should not be recognized as " + loaderName + " ..."); 406 FileObject fo = FileUtil.toFileObject(new File (file)); 407 try { 408 boolean status = DataObject.find(fo).getLoader().getDisplayName().indexOf(loaderName) == -1; 409 writeRef(PASSED); 411 return status; 412 } catch (Exception ex) { 413 printException(ex); 414 writeRef("File shouldn't be recognized as "+ loaderName +" but is!",FAILED); 415 return false; 416 } 417 } 418 419 423 void removeDataLoader( DataLoader dl ){ 424 writeRef("\nRemoving DataLoader ..."); 425 writeRef(dl.getRepresentationClass().toString()); 426 LPN.remove( dl ); 427 writeRef(PASSED); 428 } 429 430 434 void addDataLoader( String module ){ 435 writeRef("\nAdding DataLoaders from LoadersSection from the module manifest ..."); 436 try{ 437 org.netbeans.ModuleManager mm = org.netbeans.core.NbTopManager.get().getModuleSystem().getManager(); 438 org.netbeans.Module m = mm.get(module); 439 440 System.out.println("Got the Module: " + m.toString()); 441 442 HashSet mysections = new HashSet(25); Iterator it = m.getManifest().getEntries().entrySet().iterator(); while (it.hasNext()) { 445 Map.Entry entry = (Map.Entry)it.next(); 446 org.netbeans.core.startup.ManifestSection section = org.netbeans.core.startup.ManifestSection.create((String )entry.getKey(), (Attributes )entry.getValue(), m); 447 if (section != null) { 448 mysections.add(section); 449 } 450 } 451 452 System.out.println("Got all sections from manifest: " + mysections.toString()); 453 454 it = mysections.iterator(); 455 while (it.hasNext()) { 456 org.netbeans.core.startup.ManifestSection sect = (org.netbeans.core.startup.ManifestSection)it.next(); 457 if (sect instanceof org.netbeans.core.startup.ManifestSection.LoaderSection) { 458 System.out.println("Got the LoaderSection: " + ((org.netbeans.core.startup.ManifestSection.LoaderSection)sect).toString() ); 459 LPN.add((org.netbeans.core.startup.ManifestSection.LoaderSection)sect); 460 } 461 } 462 463 System.out.println("Loader should be added."); 464 465 writeRef(PASSED); 466 }catch(Exception ex){ 467 printException(ex); 468 writeRef("Adding of DataLoader failed!",FAILED); 469 } 470 } 471 472 476 void printExtensions(UniFileLoader ufl){ 477 if ( ufl != null ) { 478 writeRef("\nGetting registered file extension within this Loader ..."); 479 en = ufl .getExtensions() .extensions(); 480 while ( en.hasMoreElements() ) writeRef(en.nextElement().toString()); 481 writeRef(PASSED); 482 } 483 } 484 485 489 boolean refPrintExtensions(DataLoader dl){ 490 if ( dl != null ) { 491 writeRef("\nGetting registered file extension within this Loader ..."); 492 try { 493 java.lang.reflect.Method mm = dl.getClass().getMethod("getExtensions", new Class [0]); 496 Object obj = mm.invoke(dl,new Class [0]); 497 en = ( (ExtensionList) obj).extensions(); 498 while ( en.hasMoreElements() ) writeRef(en.nextElement().toString()); 499 writeRef(PASSED); 500 return true; 501 }catch (Exception ex){ 502 printException(ex); 503 writeRef("Printing extensions failed!",FAILED); 504 } 505 } return false; 506 } 507 508 511 void compareTest(){ 512 } 522 523 528 void eatMemory(int amount){ 529 writeRef("\nConsuming memory ..."); 530 String arr[] = new String [amount]; 531 for (int i = 0 ; i < amount ; i ++) arr[i] = "Something realy stupid here ;-) ..."; 532 writeRef(" Done."); 533 } 534 535 539 void sweepMemory(){ 540 writeRef("\nSweeping memory ..."); 541 for (int i = 0 ; i < 3 ; i ++) System.gc(); 542 try { 543 Thread.currentThread().sleep(5000); 544 }catch(Exception ex) {} 545 System.gc(); 546 try { 547 Thread.currentThread().sleep(5000); 548 }catch(Exception ex) {} 549 writeRef(" Done."); 550 } 551 552 553 554 557 public static void main(String args[]) { 558 559 LoaderPoolTest lpt = new LoaderPoolTest("x"); 560 561 try{ 562 563 lpt.prepare(); 564 565 lpt.writeRef(new LoaderPoolTest("x").getClass().getName()); 566 567 lpt.addDataLoader("org.netbeans.modules.text"); 569 570 lpt.clean(); 571 572 System.out.println("\n" + lpt.successful ); 573 574 }catch(Throwable ee){ 575 ee.printStackTrace(lpt.getRef()); 576 ee.printStackTrace(); 577 lpt.writeRef(lpt.FAILED); 578 } 579 580 lpt.assertTrue(lpt.successful); 581 582 } 583 584 public void markRecognized(FileObject fo) { 585 } 586 587 public final boolean PRINT_EXCEPTIONS = true; 589 590 public final String PASSED = "passed."; 591 public final String FAILED = "failed."; 592 public static String work = null; 595 } 596 | Popular Tags |