Index: api/manifest.mf
===================================================================
RCS file: /cvs/java/api/manifest.mf,v
retrieving revision 1.16
diff -u -r1.16 manifest.mf
--- api/manifest.mf 12 Dec 2005 15:38:20 -0000 1.16
+++ api/manifest.mf 1 Feb 2007 08:23:27 -0000
@@ -1,5 +1,5 @@
Manifest-Version: 1.0
OpenIDE-Module: org.netbeans.api.java/1
-OpenIDE-Module-Specification-Version: 1.11
+OpenIDE-Module-Specification-Version: 1.12
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/java/classpath/Bundle.properties
Index: api/src/org/netbeans/api/java/queries/BinaryForSourceQuery.java
===================================================================
RCS file: api/src/org/netbeans/api/java/queries/BinaryForSourceQuery.java
diff -N api/src/org/netbeans/api/java/queries/BinaryForSourceQuery.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ api/src/org/netbeans/api/java/queries/BinaryForSourceQuery.java 1 Feb 2007 08:23:27 -0000
@@ -0,0 +1,130 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
+ * Microsystems, Inc. All Rights Reserved.
+ */
+package org.netbeans.api.java.queries;
+
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+import javax.swing.event.ChangeListener;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileStateInvalidException;
+import org.openide.filesystems.URLMapper;
+import org.openide.util.Exceptions;
+import org.openide.util.Lookup;
+
+/**
+ *
+ * The query is used for finding binaries for sources.
+ * @see BinaryForSourceQueryImplementation
+ * @since org.netbeans.api.java/1 1.12
+ * @author Tomas Zezula
+ *
+ */
+public final class BinaryForSourceQuery {
+
+ private static final Lookup.Result extends BinaryForSourceQueryImplementation> implementations =
+ Lookup.getDefault().lookupResult (BinaryForSourceQueryImplementation.class);
+
+ /** Creates a new instance of BInaryForSOurceQuery */
+ private BinaryForSourceQuery() {
+ }
+
+ /**
+ * Returns the binary root for given source root.
+ * @param sourceRoot the source path root.
+ * @return a result object encapsulating the answer (never null)
+ */
+ public static Result findBinaryRoots (final URL sourceRoot) {
+ assert sourceRoot != null;
+ for (BinaryForSourceQueryImplementation impl : implementations.allInstances()) {
+ BinaryForSourceQuery.Result result = impl.findBinaryRoots (sourceRoot);
+ if (result != null) {
+ return result;
+ }
+ }
+ return new DefaultResult (sourceRoot);
+ }
+
+ /**
+ * Result of finding binaries, encapsulating the answer as well as the
+ * ability to listen to it.
+ */
+ public static interface Result {
+
+ /**
+ * Get the binary roots.
+ * @return array of roots of compiled classes (may be empty but not null)
+ */
+ URL[] getRoots();
+
+ /**
+ * Add a listener to changes in the roots.
+ * @param l a listener to add
+ */
+ void addChangeListener(ChangeListener l);
+
+ /**
+ * Remove a listener to changes in the roots.
+ * @param l a listener to remove
+ */
+ void removeChangeListener(ChangeListener l);
+ }
+
+ private static class DefaultResult implements Result {
+
+ private final URL sourceRoot;
+
+ DefaultResult (final URL sourceRoot) {
+ this.sourceRoot = sourceRoot;
+ }
+
+ public URL[] getRoots() {
+ FileObject fo = URLMapper.findFileObject(sourceRoot);
+ if (fo == null) {
+ return new URL[0];
+ }
+ ClassPath exec = ClassPath.getClassPath(fo, ClassPath.EXECUTE);
+ if (exec == null) {
+ return new URL[0];
+ }
+ Set result = new HashSet();
+ for (ClassPath.Entry e : exec.entries()) {
+ FileObject[] roots = SourceForBinaryQuery.findSourceRoots(e.getURL()).getRoots();
+ for (FileObject root : roots) {
+ try {
+ if (sourceRoot.equals (root.getURL())) {
+ result.add (e.getURL());
+ }
+ } catch (FileStateInvalidException fsie) {
+ Exceptions.printStackTrace(fsie);
+ }
+ }
+ }
+ return result.toArray(new URL[result.size()]);
+ }
+
+ public void addChangeListener(ChangeListener l) {
+ }
+
+ public void removeChangeListener(ChangeListener l) {
+ }
+ }
+}
Index: api/src/org/netbeans/spi/java/queries/BinaryForSourceQueryImplementation.java
===================================================================
RCS file: api/src/org/netbeans/spi/java/queries/BinaryForSourceQueryImplementation.java
diff -N api/src/org/netbeans/spi/java/queries/BinaryForSourceQueryImplementation.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ api/src/org/netbeans/spi/java/queries/BinaryForSourceQueryImplementation.java 1 Feb 2007 08:23:27 -0000
@@ -0,0 +1,57 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
+ * Microsystems, Inc. All Rights Reserved.
+ */
+package org.netbeans.spi.java.queries;
+
+import java.net.URL;
+import org.netbeans.api.java.queries.BinaryForSourceQuery;
+
+/**
+ * Information about where binaries (classfiles) corresponding to
+ * Java sources can be found.
+ * @since org.netbeans.api.java/1 1.12
+ * @see BinaryForSourceQuery
+ * @author Tomas Zezula
+ */
+public interface BinaryForSourceQueryImplementation {
+
+ /**
+ * Returns the binary root(s) for a given source root.
+ *
+ * The returned BinaryForSourceQuery.Result must be a singleton. It means that for
+ * repeated calling of this method with the same recognized root the method has to
+ * return the same instance of BinaryForSourceQuery.Result.
+ * The typical implemantation of the findBinaryRoots contains 3 steps:
+ *
+ * - Look into the cache if there is already a result for the root, if so return it
+ * - Check if the sourceRoot is recognized, if not return null
+ * - Create a new BinaryForSourceQuery.Result for the sourceRoot, put it into the cache
+ * and return it.
+ *
+ *
+ *
+ * Any absolute URL may be used but typically it will use the file
+ * protocol for directory entries and jar
protocol for JAR entries
+ * (e.g. jar:file:/tmp/foo.jar!/).
+ *
+ * @param sourceRoot the source path root
+ * @return a result object encapsulating the answer or null if the sourceRoot is not recognized
+ */
+ public BinaryForSourceQuery.Result findBinaryRoots(URL sourceRoot);
+
+}
Index: api/test/unit/src/org/netbeans/api/java/queries/BinaryForSourceQueryTest.java
===================================================================
RCS file: api/test/unit/src/org/netbeans/api/java/queries/BinaryForSourceQueryTest.java
diff -N api/test/unit/src/org/netbeans/api/java/queries/BinaryForSourceQueryTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ api/test/unit/src/org/netbeans/api/java/queries/BinaryForSourceQueryTest.java 1 Feb 2007 08:23:27 -0000
@@ -0,0 +1,179 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
+ * Microsystems, Inc. All Rights Reserved.
+ */
+
+package org.netbeans.api.java.queries;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import javax.swing.event.ChangeListener;
+import junit.framework.Assert;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.queries.SourceForBinaryQuery.Result;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.spi.java.classpath.ClassPathProvider;
+import org.netbeans.spi.java.classpath.support.ClassPathSupport;
+import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.filesystems.URLMapper;
+import org.openide.util.Lookup;
+import org.openide.util.lookup.Lookups;
+import org.openide.util.lookup.ProxyLookup;
+
+/**
+ * @author Tomas Zezula
+ */
+public class BinaryForSourceQueryTest extends NbTestCase {
+
+ private FileObject srcRoot1;
+ private FileObject srcRoot2;
+ private FileObject binaryRoot2;
+
+
+
+ static {
+ BinaryForSourceQueryTest.class.getClassLoader().setDefaultAssertionStatus(true);
+ System.setProperty("org.openide.util.Lookup", BinaryForSourceQueryTest.Lkp.class.getName());
+ Assert.assertEquals(BinaryForSourceQueryTest.Lkp.class, Lookup.getDefault().getClass());
+ }
+
+ public static class Lkp extends ProxyLookup {
+
+ private static Lkp DEFAULT;
+
+ public Lkp () {
+ Assert.assertNull(DEFAULT);
+ DEFAULT = this;
+ ClassLoader l = Lkp.class.getClassLoader();
+ this.setLookups(
+ new Lookup [] {
+ Lookups.fixed (CPProvider.getDefault(), SFBQImpl.getDefault()),
+ Lookups.metaInfServices(l),
+ Lookups.singleton(l),
+ });
+ }
+
+ public void setLookupsWrapper(Lookup... l) {
+ setLookups(l);
+ }
+ }
+
+
+ public BinaryForSourceQueryTest (String n) {
+ super(n);
+ }
+
+ @Override
+ protected void setUp () throws IOException {
+ this.clearWorkDir();
+ File wd = this.getWorkDir();
+ FileObject root = FileUtil.toFileObject(wd);
+ assertNotNull(root);
+ srcRoot1 = root.createFolder("src1");
+ assertNotNull(srcRoot1);
+ srcRoot2 = root.createFolder("src2");
+ assertNotNull(srcRoot2);
+ binaryRoot2 = root.createFolder("binary2");
+ assertNotNull(binaryRoot2);
+ SFBQImpl.getDefault().register(srcRoot2.getURL(), binaryRoot2.getURL());
+ CPProvider.getDefault().register(srcRoot2, ClassPath.SOURCE, ClassPathSupport.createClassPath(new FileObject[] {srcRoot2}));
+ CPProvider.getDefault().register(srcRoot2, ClassPath.EXECUTE, ClassPathSupport.createClassPath(new FileObject[] {binaryRoot2}));
+ }
+
+ public void testQuery() throws Exception {
+ BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(srcRoot1.getURL());
+ assertEquals(0,result.getRoots().length);
+ result = BinaryForSourceQuery.findBinaryRoots(srcRoot2.getURL());
+ assertEquals(1,result.getRoots().length);
+ assertEquals(binaryRoot2.getURL(), result.getRoots()[0]);
+ }
+
+ private static class SFBQImpl implements SourceForBinaryQueryImplementation {
+
+ private static SFBQImpl instance;
+
+ private final Map data = new HashMap();
+
+ void register (URL source, URL binary) {
+ data.put (binary,source);
+ }
+
+ public Result findSourceRoots(URL binaryRoot) {
+ URL src = data.get (binaryRoot);
+ if (src == null) {
+ return null;
+ }
+ final FileObject fo = URLMapper.findFileObject(src);
+ if (fo == null) {
+ return null;
+ }
+ return new SourceForBinaryQuery.Result () {
+ public FileObject[] getRoots() {
+ return new FileObject[] {fo};
+ }
+ public void addChangeListener (ChangeListener l) {}
+
+ public void removeChangeListener (ChangeListener l) {}
+ };
+ }
+
+ static synchronized SFBQImpl getDefault () {
+ if (instance == null) {
+ instance = new SFBQImpl ();
+ }
+ return instance;
+ }
+ }
+
+ private static class CPProvider implements ClassPathProvider {
+
+ private static CPProvider instance;
+
+ private final Map> data = new HashMap>();
+
+ void register (FileObject fo, String type, ClassPath cp) {
+ Map m = data.get (fo);
+ if (m == null) {
+ m = new HashMap();
+ data.put (fo,m);
+ }
+ m.put (type,cp);
+ }
+
+ public ClassPath findClassPath(FileObject file, String type) {
+ Map m = data.get (file);
+ if (m == null) {
+ return null;
+ }
+ return m.get (type);
+ }
+
+ public static synchronized CPProvider getDefault () {
+ if (instance == null) {
+ instance = new CPProvider ();
+ }
+ return instance;
+ }
+ }
+
+
+}
Index: j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProject.java
===================================================================
RCS file: /cvs/java/j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProject.java,v
retrieving revision 1.72
diff -u -r1.72 J2SEProject.java
--- j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProject.java 16 Jan 2007 14:25:12 -0000 1.72
+++ j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProject.java 1 Feb 2007 08:23:29 -0000
@@ -13,7 +13,7 @@
* "Portions Copyrighted [year] [name of copyright owner]"
*
* The Original Software is NetBeans. The Initial Developer of the Original
- * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
* Microsystems, Inc. All Rights Reserved.
*/
@@ -49,6 +49,7 @@
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
import org.netbeans.modules.java.j2seproject.wsclient.J2SEProjectWebServicesClientSupport;
import org.netbeans.modules.java.j2seproject.jaxws.J2SEProjectJAXWSClientSupport;
+import org.netbeans.modules.java.j2seproject.queries.BinaryForSourceQueryImpl;
import org.netbeans.modules.java.j2seproject.wsclient.J2SEProjectWebServicesSupportProvider;
import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
@@ -254,7 +255,8 @@
UILookupMergerSupport.createPrivilegedTemplatesMerger(),
UILookupMergerSupport.createRecommendedTemplatesMerger(),
LookupProviderSupport.createSourcesMerger(),
- new J2SEPropertyEvaluatorImpl(evaluator())
+ new J2SEPropertyEvaluatorImpl(evaluator()),
+ new BinaryForSourceQueryImpl(this.sourceRoots, this.testRoots, this.helper, this.eval) //Does not use APH to get/put properties/cfgdata
});
return LookupProviderSupport.createCompositeLookup(base, "Projects/org-netbeans-modules-java-j2seproject/Lookup"); //NOI18N
}
Index: j2seproject/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImpl.java
===================================================================
RCS file: j2seproject/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImpl.java
diff -N j2seproject/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ j2seproject/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImpl.java 1 Feb 2007 08:23:29 -0000
@@ -0,0 +1,133 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
+ * Microsystems, Inc. All Rights Reserved.
+ */
+
+package org.netbeans.modules.java.j2seproject.queries;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import org.netbeans.api.java.queries.BinaryForSourceQuery;
+import org.netbeans.api.java.queries.BinaryForSourceQuery.Result;
+import org.netbeans.modules.java.j2seproject.SourceRoots;
+import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
+import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
+import org.netbeans.spi.project.support.ant.AntProjectHelper;
+import org.netbeans.spi.project.support.ant.PropertyEvaluator;
+import org.openide.util.Exceptions;
+
+/**
+ *
+ * @author Tomas Zezula
+ */
+public class BinaryForSourceQueryImpl implements BinaryForSourceQueryImplementation {
+
+
+ private final Map cache = new HashMap();
+ private final SourceRoots src;
+ private final SourceRoots test;
+ private final PropertyEvaluator eval;
+ private final AntProjectHelper helper;
+
+ /** Creates a new instance of BinaryForSourceQueryImpl */
+ public BinaryForSourceQueryImpl(SourceRoots src, SourceRoots test, AntProjectHelper helper, PropertyEvaluator eval) {
+ assert src != null;
+ assert test != null;
+ assert helper != null;
+ assert eval != null;
+ this.src = src;
+ this.test = test;
+ this.eval = eval;
+ this.helper = helper;
+ }
+
+ public Result findBinaryRoots(URL sourceRoot) {
+ assert sourceRoot != null;
+ BinaryForSourceQuery.Result result = cache.get(sourceRoot);
+ if (result == null) {
+ for (URL root : this.src.getRootURLs()) {
+ if (root.equals(sourceRoot)) {
+ result = new R (J2SEProjectProperties.BUILD_CLASSES_DIR);
+ cache.put (sourceRoot,result);
+ break;
+ }
+ }
+ for (URL root : this.test.getRootURLs()) {
+ if (root.equals(sourceRoot)) {
+ result = new R (J2SEProjectProperties.BUILD_TEST_CLASSES_DIR);
+ cache.put (sourceRoot,result);
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ class R implements BinaryForSourceQuery.Result, PropertyChangeListener {
+
+ private final String propName;
+ private final List listeners = new CopyOnWriteArrayList();
+
+ R (final String propName) {
+ assert propName != null;
+ this.propName = propName;
+ eval.addPropertyChangeListener(this);
+ }
+
+ public URL[] getRoots() {
+ String val = eval.getProperty(propName);
+ if (val != null) {
+ File f = helper.resolveFile(val);
+ if (f != null) {
+ try {
+ return new URL[] {f.toURI().toURL()};
+ } catch (MalformedURLException e) {
+ Exceptions.printStackTrace(e);
+ }
+ }
+ }
+ return new URL[0];
+ }
+
+ public void addChangeListener(ChangeListener l) {
+ assert l != null;
+ this.listeners.add (l);
+ }
+
+ public void removeChangeListener(ChangeListener l) {
+ assert l != null;
+ this.listeners.remove (l);
+ }
+
+ public void propertyChange(PropertyChangeEvent event) {
+ ChangeEvent ce = new ChangeEvent (this);
+ for (ChangeListener l : listeners) {
+ l.stateChanged(ce);
+ }
+ }
+}
+
+}
Index: j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImplTest.java
===================================================================
RCS file: j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImplTest.java
diff -N j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImplTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ j2seproject/test/unit/src/org/netbeans/modules/java/j2seproject/queries/BinaryForSourceQueryImplTest.java 1 Feb 2007 08:23:30 -0000
@@ -0,0 +1,103 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
+ * Microsystems, Inc. All Rights Reserved.
+ */
+
+package org.netbeans.modules.java.j2seproject.queries;
+
+import java.io.IOException;
+import java.util.Properties;
+import org.netbeans.api.java.queries.BinaryForSourceQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectManager;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
+import org.openide.filesystems.FileObject;
+import org.netbeans.api.project.TestUtil;
+import org.netbeans.spi.project.support.ant.AntProjectHelper;
+import org.openide.filesystems.FileUtil;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.Lookup;
+
+/**
+ * Tests for BinaryForSourceQueryImpl
+ *
+ * @author Tomas Zezula
+ */
+public class BinaryForSourceQueryImplTest extends NbTestCase {
+
+ public BinaryForSourceQueryImplTest(String testName) {
+ super(testName);
+ }
+
+ private FileObject scratch;
+ private FileObject projdir;
+ private FileObject sources;
+ private FileObject buildClasses;
+ private ProjectManager pm;
+ private Project pp;
+ AntProjectHelper helper;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ TestUtil.setLookup(new Object[] {
+ new org.netbeans.modules.java.j2seproject.J2SEProjectType(),
+ new org.netbeans.modules.java.project.ProjectSourceForBinaryQuery(),
+ new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
+ });
+ Properties p = System.getProperties();
+ }
+
+ protected void tearDown() throws Exception {
+ scratch = null;
+ projdir = null;
+ pm = null;
+ TestUtil.setLookup(Lookup.EMPTY);
+ super.tearDown();
+ }
+
+
+ private void prepareProject () throws IOException {
+ scratch = TestUtil.makeScratchDir(this);
+ projdir = scratch.createFolder("proj");
+ J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
+ helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null);
+ J2SEProjectGenerator.setDefaultSourceLevel(null); //NOI18N
+ pm = ProjectManager.getDefault();
+ pp = pm.findProject(projdir);
+ sources = projdir.getFileObject("src");
+ FileObject fo = projdir.createFolder("build");
+ buildClasses = fo.createFolder("classes");
+ }
+
+ public void testBinaryForSourceQuery() throws Exception {
+ this.prepareProject();
+ FileObject folder = scratch.createFolder("SomeFolder");
+ BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(folder.getURL());
+ assertEquals("Non-project folder does not have any source folder", 0, result.getRoots().length);
+ folder = projdir.createFolder("SomeFolderInProject");
+ result = BinaryForSourceQuery.findBinaryRoots(folder.getURL());
+ assertEquals("Project non build folder does not have any source folder", 0, result.getRoots().length);
+ result = BinaryForSourceQuery.findBinaryRoots(sources.getURL());
+ assertEquals("Project build folder must have source folder", 1, result.getRoots().length);
+ assertEquals("Project build folder must have source folder",buildClasses.getURL(),result.getRoots()[0]);
+ assertEquals(BinaryForSourceQueryImpl.R.class, result.getClass());
+ BinaryForSourceQuery.Result result2 = BinaryForSourceQuery.findBinaryRoots(sources.getURL());
+ assertTrue (result == result2);
+ }
+
+}
Index: project/src/META-INF/services/org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation
===================================================================
RCS file: project/src/META-INF/services/org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation
diff -N project/src/META-INF/services/org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ project/src/META-INF/services/org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation 1 Feb 2007 08:23:30 -0000
@@ -0,0 +1,2 @@
+org.netbeans.modules.java.project.ProjectBinaryForSourceQuery
+#position=100
Index: project/src/org/netbeans/modules/java/project/ProjectBinaryForSourceQuery.java
===================================================================
RCS file: project/src/org/netbeans/modules/java/project/ProjectBinaryForSourceQuery.java
diff -N project/src/org/netbeans/modules/java/project/ProjectBinaryForSourceQuery.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ project/src/org/netbeans/modules/java/project/ProjectBinaryForSourceQuery.java 1 Feb 2007 08:23:30 -0000
@@ -0,0 +1,56 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
+ * Microsystems, Inc. All Rights Reserved.
+ */
+package org.netbeans.modules.java.project;
+
+import java.net.URISyntaxException;
+import java.net.URL;
+import org.netbeans.api.java.queries.BinaryForSourceQuery.Result;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
+import org.openide.util.Exceptions;
+
+/**
+ * Finds binary roots corresponding to source roots.
+ * Assumes an instance of BinaryForSourceQueryImplementation is in project's lookup.
+ * @author Tomas Zezula
+ */
+public class ProjectBinaryForSourceQuery implements BinaryForSourceQueryImplementation {
+
+ /** Creates a new instance of ProjectBinaryForSourceQuery */
+ public ProjectBinaryForSourceQuery() {
+ }
+
+ public Result findBinaryRoots(URL sourceRoot) {
+ try {
+ Project p = FileOwnerQuery.getOwner(sourceRoot.toURI());
+ if (p != null) {
+ BinaryForSourceQueryImplementation impl = p.getLookup().
+ lookup(BinaryForSourceQueryImplementation.class);
+ if (impl != null) {
+ return impl.findBinaryRoots(sourceRoot);
+ }
+ }
+ } catch (URISyntaxException e) {
+ Exceptions.printStackTrace(e);
+ }
+ return null;
+ }
+
+}