1 19 package org.netbeans.modules.java.classpath; 20 21 22 import java.net.URL ; 23 import java.util.concurrent.CountDownLatch ; 24 import java.util.concurrent.ExecutorService ; 25 import java.util.concurrent.Executors ; 26 import java.util.concurrent.locks.ReentrantLock ; 27 import junit.framework.TestCase; 28 import junit.framework.*; 29 import org.netbeans.api.java.classpath.ClassPath; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.spi.java.classpath.ClassPathFactory; 32 import org.netbeans.spi.java.classpath.ClassPathImplementation; 33 import org.netbeans.spi.java.classpath.PathResourceImplementation; 34 import java.util.List ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.Collections ; 38 import java.beans.PropertyChangeListener ; 39 import java.beans.PropertyChangeEvent ; 40 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 41 import org.openide.util.WeakListeners; 42 43 47 public class ProxyClassPathImplementationTest extends NbTestCase { 48 49 public ProxyClassPathImplementationTest(String testName) { 50 super(testName); 51 } 52 53 public void testResources () throws Exception { 54 final URL url1 = new URL ("file:///tmp/a/"); 55 final URL url2 = new URL ("file:///tmp/b/"); 56 final URL url3 = new URL ("file:///tmp/b/"); 57 58 final ClassPath cp1 = ClassPathSupport.createClassPath(new URL [] {url1, url2}); 59 final ClassPath cp2 = ClassPathSupport.createClassPath(new URL [] {url3}); 60 final ClassPath prxCp = ClassPathSupport.createProxyClassPath(new ClassPath[] {cp1,cp2}); 61 List <ClassPath.Entry> entries = prxCp.entries(); 62 assertEquals(3,entries.size()); 63 assertEquals(url1,entries.get(0).getURL()); 64 assertEquals(url2,entries.get(1).getURL()); 65 assertEquals(url3,entries.get(2).getURL()); 66 } 67 68 public void testDeadLock() throws Exception { 69 List <PathResourceImplementation> resources = Collections.<PathResourceImplementation>emptyList(); 70 final ReentrantLock lock = new ReentrantLock (false); 71 final CountDownLatch signal = new CountDownLatch (1); 72 final ClassPath cp = ClassPathFactory.createClassPath(ClassPathSupport.createProxyClassPathImplementation(new ClassPathImplementation[] {new LockClassPathImplementation (resources,lock, signal)})); 73 lock.lock(); 74 final ExecutorService es = Executors.newSingleThreadExecutor(); 75 try { 76 es.submit(new Runnable () { 77 public void run () { 78 cp.entries(); 79 } 80 }); 81 signal.await(); 82 cp.entries(); 83 } finally { 84 es.shutdownNow(); 85 } 86 } 87 88 89 private class LockClassPathImplementation implements ClassPathImplementation { 90 91 private List <? extends PathResourceImplementation> resources; 92 private ReentrantLock lock; 93 private CountDownLatch signal; 94 95 public LockClassPathImplementation (final List <? extends PathResourceImplementation> resources, final ReentrantLock lock, final CountDownLatch signal) { 96 this.resources = resources; 97 this.lock = lock; 98 this.signal = signal; 99 } 100 101 public List <? extends PathResourceImplementation> getResources() { 102 this.signal.countDown(); 103 this.lock.lock(); 104 return this.resources; 105 } 106 107 public void addPropertyChangeListener(PropertyChangeListener listener) { 108 } 109 110 public void removePropertyChangeListener(PropertyChangeListener listener) { 111 } 112 113 } 114 115 116 } 117 | Popular Tags |