1 19 20 package org.netbeans.api.java.classpath; 21 22 import java.io.File ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeSupport ; 26 import java.io.FileOutputStream ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.SortedSet ; 34 import java.util.TreeSet ; 35 import java.util.jar.JarOutputStream ; 36 import java.util.zip.ZipEntry ; 37 import org.netbeans.junit.NbTestCase; 38 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 39 import org.netbeans.spi.java.classpath.ClassPathImplementation; 40 import org.netbeans.spi.java.classpath.ClassPathFactory; 41 import org.netbeans.spi.java.classpath.FilteringPathResourceImplementation; 42 import org.netbeans.spi.java.classpath.PathResourceImplementation; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 46 public class ClassPathTest extends NbTestCase { 47 48 public ClassPathTest(String testName) { 49 super(testName); 50 } 51 52 protected void setUp() throws Exception { 53 super.setUp(); 54 clearWorkDir(); 55 } 56 57 private File getBaseDir() throws Exception { 58 return FileUtil.normalizeFile(getWorkDir()); 59 } 60 61 64 public void testGetResourceName() throws Exception { 65 File f = getBaseDir(); 66 f = new File (f.getPath()+"/w.e.i.r.d/f o l d e r"); 67 f.mkdirs(); 68 File f2 = new File (f, "org/netbeans/test"); 69 f2.mkdirs(); 70 File f3 = new File (f2, "Main.java"); 71 f3.createNewFile(); 72 73 FileObject cpRoot = FileUtil.toFileObject(f); 74 FileObject cpItem = FileUtil.toFileObject(f2); 75 FileObject clazz = FileUtil.toFileObject(f3); 76 ClassPath cp = ClassPathSupport.createClassPath(new FileObject[]{cpRoot}); 77 String pkg = cp.getResourceName(cpItem); 78 assertEquals("org/netbeans/test", pkg); 79 80 pkg = cp.getResourceName(cpItem, '.', true); 81 assertEquals("org.netbeans.test", pkg); 82 83 pkg = cp.getResourceName(cpItem, '.', false); 84 assertEquals("org.netbeans.test", pkg); 85 86 pkg = cp.getResourceName(cpItem, '#', true); 87 assertEquals("org#netbeans#test", pkg); 88 89 pkg = cp.getResourceName(cpItem, '#', false); 90 assertEquals("org#netbeans#test", pkg); 91 92 pkg = cp.getResourceName(clazz); 93 assertEquals("org/netbeans/test/Main.java", pkg); 94 95 pkg = cp.getResourceName(clazz, '.', true); 96 assertEquals("org.netbeans.test.Main.java", pkg); 97 98 pkg = cp.getResourceName(clazz, '.', false); 99 assertEquals("org.netbeans.test.Main", pkg); 100 101 pkg = cp.getResourceName(clazz, '@', true); 102 assertEquals("org@netbeans@test@Main.java", pkg); 103 104 pkg = cp.getResourceName(clazz, '@', false); 105 assertEquals("org@netbeans@test@Main", pkg); 106 } 107 108 113 public void testGetResource () throws Exception { 114 File root_1 = new File (getBaseDir(),"root_1"); 115 root_1.mkdir(); 116 File root_2 = new File (getBaseDir(),"root_2"); 117 root_2.mkdir(); 118 FileObject[] roots = new FileObject [] { 119 FileUtil.toFileObject(root_1), 120 FileUtil.toFileObject(root_2), 121 }; 122 123 FileObject tmp = roots[0].createFolder("org"); 124 tmp = tmp.createFolder("me"); 125 FileObject testFo_1 = tmp.createData("Foo","txt"); 126 tmp = roots[1].createFolder("org"); 127 tmp = tmp.createFolder("me"); 128 FileObject testFo_2 = tmp.createData("Foo","txt"); 129 ClassPath cp = ClassPathSupport.createClassPath(roots); 130 131 assertTrue(cp.findResource ("org/me/Foo.txt")==testFo_1); 133 assertTrue (cp.findResource("org/me/None.txt")==null); 134 135 List res = cp.findAllResources ("org/me/Foo.txt"); 137 assertTrue (res.size() == 2); 138 assertTrue (res.contains(testFo_1)); 139 assertTrue (res.contains(testFo_2)); 140 141 assertTrue (cp.contains (testFo_1)); 143 assertTrue (cp.contains (testFo_2)); 144 assertFalse (cp.contains (roots[0].getParent())); 145 146 assertTrue (cp.findOwnerRoot(testFo_1)==roots[0]); 148 assertTrue (cp.findOwnerRoot(testFo_2)==roots[1]); 149 150 155 156 cp = null; 157 roots[0].delete(); 158 roots[1].delete(); 159 } 160 161 165 public void testListening() throws Exception { 166 167 File root_1 = new File (getBaseDir(),"root_1"); 168 root_1.mkdir(); 169 File root_2 = new File (getBaseDir(),"root_2"); 170 root_2.mkdir(); 171 File root_3 = new File (getBaseDir(),"root_3.jar"); 172 JarOutputStream out = new JarOutputStream ( new FileOutputStream (root_3)); 173 try { 174 out.putNextEntry(new ZipEntry ("test.txt")); 175 out.write ("test".getBytes()); 176 } finally { 177 out.close (); 178 } 179 assertNotNull("Cannot find file",FileUtil.toFileObject(root_1)); 180 assertNotNull("Cannot find file",FileUtil.toFileObject(root_2)); 181 assertNotNull("Cannot find file",FileUtil.toFileObject(root_3)); 182 TestClassPathImplementation impl = new TestClassPathImplementation(); 183 ClassPath cp = ClassPathFactory.createClassPath (impl); 184 impl.addResource(root_1.toURI().toURL()); 185 cp.addPropertyChangeListener (impl); 186 impl.addResource (root_2.toURI().toURL()); 187 impl.assertEvents(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS); 188 assertTrue (cp.getRoots().length==2); 189 impl.removeResource (root_2.toURI().toURL()); 190 impl.assertEvents(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS); 191 assertTrue (cp.getRoots().length==1); 192 FileObject fo = cp.getRoots()[0]; 193 FileObject parentFolder = fo.getParent(); 194 fo.delete(); 195 impl.assertEvents(ClassPath.PROP_ROOTS); 196 assertTrue (cp.getRoots().length==0); 197 parentFolder.createFolder("root_1"); 198 assertTrue (cp.getRoots().length==1); 199 impl.assertEvents(ClassPath.PROP_ROOTS); 200 FileObject archiveFile = FileUtil.toFileObject(root_3); 201 impl.addResource(FileUtil.getArchiveRoot(archiveFile.getURL())); 202 assertEquals (cp.getRoots().length,2); 203 impl.assertEvents(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS); 204 root_3.delete(); 205 root_3 = new File (getBaseDir(),"root_3.jar"); 206 Thread.sleep(1000); 207 out = new JarOutputStream ( new FileOutputStream (root_3)); 208 try { 209 out.putNextEntry(new ZipEntry ("test2.txt")); 210 out.write ("test2".getBytes()); 211 } finally { 212 out.close (); 213 } 214 archiveFile.refresh(); 215 impl.assertEvents(ClassPath.PROP_ROOTS); 216 root_1.delete(); 217 root_2.delete(); 218 root_3.delete(); 219 cp = null; 220 } 221 222 public void testListening2() throws Exception { 223 class FiringPRI implements PathResourceImplementation { 225 private URL [] roots = new URL [0]; 226 public URL [] getRoots() { 227 return roots; 228 } 229 void changeRoots(URL [] nue) { 230 roots = nue; 231 pcs.firePropertyChange(PROP_ROOTS, null, null); 232 } 233 public ClassPathImplementation getContent() { 234 return null; 235 } 236 PropertyChangeSupport pcs = new PropertyChangeSupport (this); 237 public void addPropertyChangeListener(PropertyChangeListener listener) { 238 pcs.addPropertyChangeListener(listener); 239 } 240 public void removePropertyChangeListener(PropertyChangeListener listener) { 241 pcs.removePropertyChangeListener(listener); 242 } 243 } 244 FiringPRI pri = new FiringPRI(); 245 TestClassPathImplementation impl = new TestClassPathImplementation(); 246 impl.addResource(pri); 247 ClassPath cp = ClassPathFactory.createClassPath(impl); 248 assertEquals(Collections.emptyList(), Arrays.asList(cp.getRoots())); 249 cp.addPropertyChangeListener(impl); 250 File d = new File (getBaseDir(), "d"); 251 d.mkdir(); 252 pri.changeRoots(new URL [] {d.toURI().toURL()}); 253 impl.assertEvents(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS); 254 assertEquals(Collections.singletonList(FileUtil.toFileObject(d)), Arrays.asList(cp.getRoots())); 255 } 256 257 public void testChangesAcknowledgedWithoutListener() throws Exception { 258 clearWorkDir(); 260 File root = new File (getWorkDir(), "root"); 261 URL rootU = root.toURI().toURL(); 262 if (!rootU.toExternalForm().endsWith("/")) { 263 rootU = new URL (rootU.toExternalForm() + "/"); 264 } 265 ClassPath cp = ClassPathSupport.createClassPath(new URL [] {rootU}); 266 assertEquals("nothing there yet", null, cp.findResource("f")); 267 FileObject f = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), "root/f"); 268 assertEquals("found new file", f, cp.findResource("f")); 269 f.delete(); 270 assertEquals("again empty", null, cp.findResource("f")); 271 } 272 273 static final class TestClassPathImplementation implements ClassPathImplementation, PropertyChangeListener { 274 275 private final PropertyChangeSupport support = new PropertyChangeSupport (this); 276 private final List <PathResourceImplementation> resources = new ArrayList <PathResourceImplementation> (); 277 private final SortedSet <String > events = new TreeSet <String >(); 278 279 public synchronized void addResource (URL resource) { 280 PathResourceImplementation pr = ClassPathSupport.createResource (resource); 281 addResource(pr); 282 } 283 284 public synchronized void addResource(PathResourceImplementation pr) { 285 this.resources.add (pr); 286 this.support.firePropertyChange (ClassPathImplementation.PROP_RESOURCES,null,null); 287 } 288 289 public synchronized void removeResource (URL resource) { 290 for (Iterator it = this.resources.iterator(); it.hasNext();) { 291 PathResourceImplementation pr = (PathResourceImplementation) it.next (); 292 if (Arrays.asList(pr.getRoots()).contains (resource)) { 293 this.resources.remove (pr); 294 this.support.firePropertyChange (ClassPathImplementation.PROP_RESOURCES,null,null); 295 break; 296 } 297 } 298 } 299 300 public synchronized List <? extends PathResourceImplementation> getResources() { 301 return this.resources; 302 } 303 304 public void addPropertyChangeListener(PropertyChangeListener listener) { 305 this.support.addPropertyChangeListener (listener); 306 } 307 308 public void removePropertyChangeListener(PropertyChangeListener listener) { 309 this.support.removePropertyChangeListener (listener); 310 } 311 312 public void propertyChange (PropertyChangeEvent event) { 313 events.add(event.getPropertyName()); 314 } 315 316 void assertEvents(String ... events) { 317 assertEquals(new TreeSet <String >(Arrays.asList(events)), this.events); 318 this.events.clear(); 319 } 320 } 321 322 public void testFilteredClassPaths() throws Exception { 323 FileObject bd = FileUtil.toFileObject(getBaseDir()); 324 FileObject u1fo = bd.createFolder("u1"); 325 FileObject u2fo = bd.createFolder("u2"); 326 final URL u1 = u1fo.getURL(); 327 final URL u2 = u2fo.getURL(); 328 class FPRI implements FilteringPathResourceImplementation { 329 private int modulus = 2; 330 public void changeIncludes(int modulus) { 331 this.modulus = modulus; 332 pcs.firePropertyChange(PROP_INCLUDES, null, null); 333 } 334 public URL [] getRoots() { 335 return new URL [] {u1, u2}; 336 } 337 public boolean includes(URL root, String resource) { 338 int offset; 339 if (root.equals(u1)) { 340 offset = 0; 341 } else if (root.equals(u2)) { 342 offset = 1; 343 } else { 344 throw new IllegalArgumentException (root.toString()); 345 } 346 return (offset + resource.length()) % modulus == 0; 347 } 348 public ClassPathImplementation getContent() { 349 return null; 350 } 351 private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 352 public void addPropertyChangeListener(PropertyChangeListener listener) { 353 pcs.addPropertyChangeListener(listener); 354 } 355 public void removePropertyChangeListener(PropertyChangeListener listener) { 356 pcs.removePropertyChangeListener(listener); 357 } 358 } 359 FPRI pr = new FPRI(); 360 TestClassPathImplementation impl = new TestClassPathImplementation(); 361 impl.addResource(pr); 362 ClassPath cp = ClassPathFactory.createClassPath(impl); 363 FileObject xx1 = u1fo.createData("xx"); 364 FileObject xxx1 = u1fo.createData("xxx"); 365 FileObject xy1 = FileUtil.createData(u1fo, "x/y"); 366 FileObject x_1 = u1fo.createData("x "); 367 String cau = "\u010Dau"; 368 FileObject cau1 = u1fo.createData(cau); 369 FileObject folder = u1fo.createFolder("folder"); 370 FileObject foldr = u1fo.createFolder("foldr"); 371 FileObject xx2 = u2fo.createData("xx"); 372 FileObject xxx2 = u2fo.createData("xxx"); 373 FileObject xy2 = FileUtil.createData(u2fo, "x/y"); 374 FileObject x_2 = u2fo.createData("x "); 375 FileObject cau2 = u2fo.createData(cau); 376 assertEquals(Arrays.asList(u1fo, u2fo), Arrays.asList(cp.getRoots())); 377 assertTrue(cp.contains(xx1)); 378 assertTrue(cp.contains(x_1)); 379 assertFalse(cp.contains(xxx1)); 380 assertFalse(cp.contains(cau1)); 381 assertFalse(cp.contains(xy1)); 382 assertFalse(cp.contains(xx2)); 383 assertFalse(cp.contains(x_2)); 384 assertTrue(cp.contains(xxx2)); 385 assertTrue(cp.contains(cau2)); 386 assertTrue(cp.contains(xy2)); 387 assertFalse(cp.contains(folder)); 388 assertTrue(cp.contains(foldr)); 389 assertEquals(xx1, cp.findResource("xx")); 390 assertEquals(x_1, cp.findResource("x ")); 391 assertEquals(xxx2, cp.findResource("xxx")); 392 assertEquals(cau2, cp.findResource(cau)); 393 assertEquals(xy2, cp.findResource("x/y")); 394 assertEquals(null, cp.findResource("folder")); 395 assertEquals(foldr, cp.findResource("foldr")); 396 assertEquals(Collections.singletonList(xx1), cp.findAllResources("xx")); 397 assertEquals(Collections.singletonList(x_1), cp.findAllResources("x ")); 398 assertEquals(Collections.singletonList(xxx2), cp.findAllResources("xxx")); 399 assertEquals(Collections.singletonList(cau2), cp.findAllResources(cau)); 400 assertEquals(Collections.singletonList(xy2), cp.findAllResources("x/y")); 401 assertEquals(Collections.emptyList(), cp.findAllResources("folder")); 402 assertEquals(Collections.singletonList(foldr), cp.findAllResources("foldr")); 403 assertEquals("xx", cp.getResourceName(xx1)); 404 assertEquals("x ", cp.getResourceName(x_1)); 405 assertEquals("xxx", cp.getResourceName(xxx1)); 406 assertEquals(cau, cp.getResourceName(cau1)); 407 assertEquals("x/y", cp.getResourceName(xy1)); 408 assertEquals("folder", cp.getResourceName(folder)); 409 assertEquals("foldr", cp.getResourceName(foldr)); 410 assertEquals(u1fo, cp.findOwnerRoot(xx1)); 411 assertEquals(u1fo, cp.findOwnerRoot(x_1)); 412 assertEquals(u1fo, cp.findOwnerRoot(xxx1)); 413 assertEquals(u1fo, cp.findOwnerRoot(cau1)); 414 assertEquals(u1fo, cp.findOwnerRoot(xy1)); 415 assertEquals(u1fo, cp.findOwnerRoot(folder)); 416 assertEquals(u1fo, cp.findOwnerRoot(foldr)); 417 assertTrue(cp.isResourceVisible(xx1)); 418 assertTrue(cp.isResourceVisible(x_1)); 419 assertFalse(cp.isResourceVisible(xxx1)); 420 assertFalse(cp.isResourceVisible(cau1)); 421 assertFalse(cp.isResourceVisible(xy1)); 422 assertFalse(cp.isResourceVisible(folder)); 423 assertTrue(cp.isResourceVisible(foldr)); 424 ClassPath.Entry e1 = cp.entries().get(0); 425 assertTrue(e1.includes("xx")); 426 assertTrue(e1.includes("x ")); 427 assertFalse(e1.includes("xxx")); 428 assertFalse(e1.includes(cau)); 429 assertFalse(e1.includes("x/y")); 430 assertFalse(e1.includes("folder/")); 431 assertTrue(e1.includes("foldr/")); 432 assertTrue(e1.includes(xx1)); 433 assertTrue(e1.includes(x_1)); 434 assertFalse(e1.includes(xxx1)); 435 assertFalse(e1.includes(cau1)); 436 assertFalse(e1.includes(xy1)); 437 assertFalse(e1.includes(folder)); 438 assertTrue(e1.includes(foldr)); 439 try { 440 e1.includes(xx2); 441 fail(); 442 } catch (IllegalArgumentException iae) {} 443 assertTrue(e1.includes(xx1.getURL())); 444 assertTrue(e1.includes(x_1.getURL())); 445 assertFalse(e1.includes(xxx1.getURL())); 446 assertFalse(e1.includes(cau1.getURL())); 447 assertFalse(e1.includes(xy1.getURL())); 448 assertFalse(e1.includes(folder.getURL())); 449 assertTrue(e1.includes(foldr.getURL())); 450 try { 451 e1.includes(xx2.getURL()); 452 fail(); 453 } catch (IllegalArgumentException iae) {} 454 cp.addPropertyChangeListener(impl); 455 pr.changeIncludes(3); 456 impl.assertEvents(ClassPath.PROP_INCLUDES); 457 assertFalse(cp.contains(xx1)); 458 assertFalse(cp.contains(x_1)); 459 assertTrue(cp.contains(xxx1)); 460 assertTrue(cp.contains(cau1)); 461 assertTrue(cp.contains(xy1)); 462 assertTrue(cp.contains(xx2)); 463 assertTrue(cp.contains(x_2)); 464 assertFalse(cp.contains(xxx2)); 465 assertFalse(cp.contains(cau2)); 466 assertFalse(cp.contains(xy2)); 467 assertEquals(xx2, cp.findResource("xx")); 468 assertEquals(x_2, cp.findResource("x ")); 469 assertEquals(xxx1, cp.findResource("xxx")); 470 assertEquals(cau1, cp.findResource(cau)); 471 assertEquals(xy1, cp.findResource("x/y")); 472 e1 = cp.entries().get(0); 473 assertFalse(e1.includes("xx")); 474 assertFalse(e1.includes("x ")); 475 assertTrue(e1.includes("xxx")); 476 assertTrue(e1.includes(cau)); 477 assertTrue(e1.includes("x/y")); 478 assertFalse(e1.includes(xx1)); 479 assertFalse(e1.includes(x_1)); 480 assertTrue(e1.includes(xxx1)); 481 assertTrue(e1.includes(cau1)); 482 assertTrue(e1.includes(xy1)); 483 assertFalse(e1.includes(xx1.getURL())); 484 assertFalse(e1.includes(x_1.getURL())); 485 assertTrue(e1.includes(xxx1.getURL())); 486 assertTrue(e1.includes(cau1.getURL())); 487 assertTrue(e1.includes(xy1.getURL())); 488 } 489 490 public void testFpriChangeFiring() throws Exception { 491 class FPRI implements FilteringPathResourceImplementation { 492 URL root; 493 PropertyChangeSupport pcs = new PropertyChangeSupport (this); 494 FPRI(URL root) { 495 this.root = root; 496 } 497 public boolean includes(URL root, String resource) { 498 return true; 499 } 500 public URL [] getRoots() { 501 return new URL [] {root}; 502 } 503 public ClassPathImplementation getContent() { 504 return null; 505 } 506 public void addPropertyChangeListener(PropertyChangeListener listener) { 507 pcs.addPropertyChangeListener(listener); 508 } 509 public void removePropertyChangeListener(PropertyChangeListener listener) { 510 pcs.removePropertyChangeListener(listener); 511 } 512 void fire(Object propid) { 513 PropertyChangeEvent e = new PropertyChangeEvent (this, FilteringPathResourceImplementation.PROP_INCLUDES, null, null); 514 e.setPropagationId(propid); 515 pcs.firePropertyChange(e); 516 } 517 } 518 FPRI fpri1 = new FPRI(new File (getWorkDir(), "src1").toURI().toURL()); 519 FPRI fpri2 = new FPRI(new File (getWorkDir(), "src2").toURI().toURL()); 520 class L implements PropertyChangeListener { 521 int cnt; 522 public void propertyChange(PropertyChangeEvent e) { 523 if (ClassPath.PROP_INCLUDES.equals(e.getPropertyName())) { 524 cnt++; 525 } 526 } 527 } 528 ClassPath cp = ClassPathSupport.createClassPath(Arrays.asList(fpri1, fpri2)); 529 L l = new L (); 530 cp.addPropertyChangeListener(l); 531 fpri1.fire(null); 532 assertEquals(1, l.cnt); 533 fpri2.fire(null); 534 assertEquals(2, l.cnt); 535 fpri1.fire("hello"); 536 assertEquals(3, l.cnt); 537 fpri2.fire("goodbye"); 538 assertEquals(4, l.cnt); 539 fpri1.fire("fixed"); 540 assertEquals(5, l.cnt); 541 fpri2.fire("fixed"); 542 assertEquals(5, l.cnt); 543 fpri1.fire("new"); 544 assertEquals(6, l.cnt); 545 } 546 547 } 548 | Popular Tags |