001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.java.classpath;
042:
043: import java.net.URL;
044: import java.util.concurrent.CountDownLatch;
045: import java.util.concurrent.ExecutorService;
046: import java.util.concurrent.Executors;
047: import java.util.concurrent.locks.ReentrantLock;
048: import junit.framework.TestCase;
049: import junit.framework.*;
050: import org.netbeans.api.java.classpath.ClassPath;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.spi.java.classpath.ClassPathFactory;
053: import org.netbeans.spi.java.classpath.ClassPathImplementation;
054: import org.netbeans.spi.java.classpath.PathResourceImplementation;
055: import java.util.List;
056: import java.util.ArrayList;
057: import java.util.Iterator;
058: import java.util.Collections;
059: import java.beans.PropertyChangeListener;
060: import java.beans.PropertyChangeEvent;
061: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
062: import org.openide.util.WeakListeners;
063:
064: /**
065: *
066: * @author tom
067: */
068: public class ProxyClassPathImplementationTest extends NbTestCase {
069:
070: public ProxyClassPathImplementationTest(String testName) {
071: super (testName);
072: }
073:
074: public void testResources() throws Exception {
075: final URL url1 = new URL("file:///tmp/a/");
076: final URL url2 = new URL("file:///tmp/b/");
077: final URL url3 = new URL("file:///tmp/b/");
078:
079: final ClassPath cp1 = ClassPathSupport
080: .createClassPath(new URL[] { url1, url2 });
081: final ClassPath cp2 = ClassPathSupport
082: .createClassPath(new URL[] { url3 });
083: final ClassPath prxCp = ClassPathSupport
084: .createProxyClassPath(new ClassPath[] { cp1, cp2 });
085: List<ClassPath.Entry> entries = prxCp.entries();
086: assertEquals(3, entries.size());
087: assertEquals(url1, entries.get(0).getURL());
088: assertEquals(url2, entries.get(1).getURL());
089: assertEquals(url3, entries.get(2).getURL());
090: }
091:
092: public void testDeadLock() throws Exception {
093: List<PathResourceImplementation> resources = Collections
094: .<PathResourceImplementation> emptyList();
095: final ReentrantLock lock = new ReentrantLock(false);
096: final CountDownLatch signal = new CountDownLatch(1);
097: final ClassPath cp = ClassPathFactory
098: .createClassPath(ClassPathSupport
099: .createProxyClassPathImplementation(new ClassPathImplementation[] { new LockClassPathImplementation(
100: resources, lock, signal) }));
101: lock.lock();
102: final ExecutorService es = Executors.newSingleThreadExecutor();
103: try {
104: es.submit(new Runnable() {
105: public void run() {
106: cp.entries();
107: }
108: });
109: signal.await();
110: cp.entries();
111: } finally {
112: es.shutdownNow();
113: }
114: }
115:
116: private class LockClassPathImplementation implements
117: ClassPathImplementation {
118:
119: private List<? extends PathResourceImplementation> resources;
120: private ReentrantLock lock;
121: private CountDownLatch signal;
122:
123: public LockClassPathImplementation(
124: final List<? extends PathResourceImplementation> resources,
125: final ReentrantLock lock, final CountDownLatch signal) {
126: this .resources = resources;
127: this .lock = lock;
128: this .signal = signal;
129: }
130:
131: public List<? extends PathResourceImplementation> getResources() {
132: this .signal.countDown();
133: this .lock.lock();
134: return this .resources;
135: }
136:
137: public void addPropertyChangeListener(
138: PropertyChangeListener listener) {
139: }
140:
141: public void removePropertyChangeListener(
142: PropertyChangeListener listener) {
143: }
144:
145: }
146:
147: }
|