KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > classpath > ProxyClassPathImplementationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.classpath;
20
21
22 import java.net.URL JavaDoc;
23 import java.util.concurrent.CountDownLatch JavaDoc;
24 import java.util.concurrent.ExecutorService JavaDoc;
25 import java.util.concurrent.Executors JavaDoc;
26 import java.util.concurrent.locks.ReentrantLock JavaDoc;
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 JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.beans.PropertyChangeListener JavaDoc;
39 import java.beans.PropertyChangeEvent JavaDoc;
40 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
41 import org.openide.util.WeakListeners;
42
43 /**
44  *
45  * @author tom
46  */

47 public class ProxyClassPathImplementationTest extends NbTestCase {
48     
49     public ProxyClassPathImplementationTest(String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public void testResources () throws Exception JavaDoc {
54         final URL JavaDoc url1 = new URL JavaDoc ("file:///tmp/a/");
55         final URL JavaDoc url2 = new URL JavaDoc ("file:///tmp/b/");
56         final URL JavaDoc url3 = new URL JavaDoc ("file:///tmp/b/");
57         
58         final ClassPath cp1 = ClassPathSupport.createClassPath(new URL JavaDoc[] {url1, url2});
59         final ClassPath cp2 = ClassPathSupport.createClassPath(new URL JavaDoc[] {url3});
60         final ClassPath prxCp = ClassPathSupport.createProxyClassPath(new ClassPath[] {cp1,cp2});
61         List JavaDoc<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 JavaDoc{
69         List JavaDoc<PathResourceImplementation> resources = Collections.<PathResourceImplementation>emptyList();
70         final ReentrantLock JavaDoc lock = new ReentrantLock JavaDoc (false);
71         final CountDownLatch JavaDoc signal = new CountDownLatch JavaDoc (1);
72         final ClassPath cp = ClassPathFactory.createClassPath(ClassPathSupport.createProxyClassPathImplementation(new ClassPathImplementation[] {new LockClassPathImplementation (resources,lock, signal)}));
73         lock.lock();
74         final ExecutorService JavaDoc es = Executors.newSingleThreadExecutor();
75         try {
76             es.submit(new Runnable JavaDoc () {
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 JavaDoc<? extends PathResourceImplementation> resources;
92         private ReentrantLock JavaDoc lock;
93         private CountDownLatch JavaDoc signal;
94         
95         public LockClassPathImplementation (final List JavaDoc<? extends PathResourceImplementation> resources, final ReentrantLock JavaDoc lock, final CountDownLatch JavaDoc signal) {
96             this.resources = resources;
97             this.lock = lock;
98             this.signal = signal;
99         }
100         
101         public List JavaDoc<? extends PathResourceImplementation> getResources() {
102             this.signal.countDown();
103             this.lock.lock();
104             return this.resources;
105         }
106
107         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
108         }
109
110         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
111         }
112         
113     }
114     
115     
116 }
117
Popular Tags