# HG changeset patch # User Alexander Simon # Date 1324390160 -14400 # Node ID 904005494cacc56df0798024cdac48764a95858c # Parent 10e5eda8ef7806ae1ff7f0ee598a49ca267dd8ba fixing Bug #198060 SharabilityQuery / SharabilityQueryImplementation should be extended to use FileObject - third variant API (check scheme) diff --git a/queries/src/org/netbeans/api/queries/CollocationQuery.java b/queries/src/org/netbeans/api/queries/CollocationQuery.java --- a/queries/src/org/netbeans/api/queries/CollocationQuery.java +++ b/queries/src/org/netbeans/api/queries/CollocationQuery.java @@ -45,7 +45,9 @@ package org.netbeans.api.queries; import java.io.File; +import java.net.URI; import org.netbeans.spi.queries.CollocationQueryImplementation; +import org.netbeans.spi.queries.CollocationQueryImplementation2; import org.openide.filesystems.FileUtil; import org.openide.util.Lookup; @@ -59,6 +61,8 @@ private static final Lookup.Result implementations = Lookup.getDefault().lookupResult(CollocationQueryImplementation.class); + private static final Lookup.Result implementations2 = + Lookup.getDefault().lookupResult(CollocationQueryImplementation2.class); private CollocationQuery() {} @@ -71,7 +75,7 @@ * @param file2 another file * @return true if they are probably part of one logical tree */ - public static boolean areCollocated(File file1, File file2) { + @Deprecated public static boolean areCollocated(File file1, File file2) { if (!file1.equals(FileUtil.normalizeFile(file1))) { throw new IllegalArgumentException("Parameter file1 was not "+ // NOI18N "normalized. Was "+file1+" instead of "+FileUtil.normalizeFile(file1)); // NOI18N @@ -80,6 +84,25 @@ throw new IllegalArgumentException("Parameter file2 was not "+ // NOI18N "normalized. Was "+file2+" instead of "+FileUtil.normalizeFile(file2)); // NOI18N } + URI uri1 = null; + URI uri2 = null; + for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) { + if (uri1 == null) { + uri1 = file1.toURI(); + if (!uri1.equals(uri1.normalize())) { + throw new IllegalArgumentException("Parameter file1 was not "+ // NOI18N + "normalized. Was "+uri1+" instead of "+uri1.normalize()); // NOI18N + } + uri2 = file2.toURI(); + if (!uri2.equals(uri2.normalize())) { + throw new IllegalArgumentException("Parameter file2 was not "+ // NOI18N + "normalized. Was "+uri2+" instead of "+uri2.normalize()); // NOI18N + } + } + if (cqi.areCollocated(uri1, uri2)) { + return true; + } + } for (CollocationQueryImplementation cqi : implementations.allInstances()) { if (cqi.areCollocated(file1, file2)) { return true; @@ -87,6 +110,53 @@ } return false; } + + /** + * Check whether two files are logically part of one directory tree. + * For example, if both files are stored in CVS, with the same server + * (CVSROOT) they might be considered collocated. + * If nothing is known about them, return false. + * @param file1 one file + * @param file2 another file + * @return true if they are probably part of one logical tree + */ + public static boolean areCollocated(URI file1, URI file2) { + if (!file1.equals(file1.normalize())) { + throw new IllegalArgumentException("Parameter file1 was not "+ // NOI18N + "normalized. Was "+file1+" instead of "+file1.normalize()); // NOI18N + } + if (!file2.equals(file2.normalize())) { + throw new IllegalArgumentException("Parameter file2 was not "+ // NOI18N + "normalized. Was "+file2+" instead of "+file2.normalize()); // NOI18N + } + for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) { + if (cqi.areCollocated(file1, file2)) { + return true; + } + } + if ("file".equals(file1.getScheme()) && "file".equals(file2.getScheme())) { // NOI18N + File f1 = null; + File f2 = null; + for (CollocationQueryImplementation cqi : implementations.allInstances()) { + if (f1 == null) { + f1 = new File(file1); + if (!f1.equals(FileUtil.normalizeFile(f1))) { + throw new IllegalArgumentException("Parameter file1 was not "+ // NOI18N + "normalized. Was "+f1+" instead of "+FileUtil.normalizeFile(f1)); // NOI18N + } + f2 = new File(file2); + if (!f2.equals(FileUtil.normalizeFile(f2))) { + throw new IllegalArgumentException("Parameter file2 was not "+ // NOI18N + "normalized. Was "+f2+" instead of "+FileUtil.normalizeFile(f2)); // NOI18N + } + } + if (cqi.areCollocated(f1, f2)) { + return true; + } + } + } + return false; + } /** * Find a root of a logical tree containing this file, if any. @@ -94,11 +164,25 @@ * @return an ancestor directory which is the root of a logical tree, * if any (else null) */ - public static File findRoot(File file) { + @Deprecated public static File findRoot(File file) { if (!file.equals(FileUtil.normalizeFile(file))) { throw new IllegalArgumentException("Parameter file was not "+ // NOI18N "normalized. Was "+file+" instead of "+FileUtil.normalizeFile(file)); // NOI18N } + URI uri = null; + for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) { + if (uri == null) { + uri = file.toURI(); + if (!uri.equals(uri.normalize())) { + throw new IllegalArgumentException("Parameter file was not "+ // NOI18N + "normalized. Was "+uri+" instead of "+uri.normalize()); // NOI18N + } + } + URI root = cqi.findRoot(uri); + if (root != null) { + return new File(root); + } + } for (CollocationQueryImplementation cqi : implementations.allInstances()) { File root = cqi.findRoot(file); if (root != null) { @@ -108,4 +192,39 @@ return null; } + /** + * Find a root of a logical tree containing this file, if any. + * @param file a file on disk + * @return an ancestor directory which is the root of a logical tree, + * if any (else null) + */ + public static URI findRoot(URI file) { + if (!file.equals(file.normalize())) { + throw new IllegalArgumentException("Parameter file was not "+ // NOI18N + "normalized. Was "+file+" instead of "+file.normalize()); // NOI18N + } + for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) { + URI root = cqi.findRoot(file); + if (root != null) { + return root; + } + } + if ("file".equals(file.getScheme())) { // NOI18N + File f = null; + for (CollocationQueryImplementation cqi : implementations.allInstances()) { + if (f == null) { + f = new File(file); + if (!f.equals(FileUtil.normalizeFile(f))) { + throw new IllegalArgumentException("Parameter file was not "+ // NOI18N + "normalized. Was "+file+" instead of "+FileUtil.normalizeFile(f)); // NOI18N + } + } + File root = cqi.findRoot(f); + if (root != null) { + return root.toURI(); + } + } + } + return null; + } } diff --git a/queries/src/org/netbeans/api/queries/SharabilityQuery.java b/queries/src/org/netbeans/api/queries/SharabilityQuery.java --- a/queries/src/org/netbeans/api/queries/SharabilityQuery.java +++ b/queries/src/org/netbeans/api/queries/SharabilityQuery.java @@ -45,7 +45,9 @@ package org.netbeans.api.queries; import java.io.File; +import java.net.URI; import org.netbeans.spi.queries.SharabilityQueryImplementation; +import org.netbeans.spi.queries.SharabilityQueryImplementation2; import org.openide.filesystems.FileUtil; import org.openide.util.Lookup; import org.openide.util.Parameters; @@ -70,12 +72,15 @@ private static final Lookup.Result implementations = Lookup.getDefault().lookupResult(SharabilityQueryImplementation.class); + private static final Lookup.Result implementations2 = + Lookup.getDefault().lookupResult(SharabilityQueryImplementation2.class); + /** * Constant indicating that nothing is known about whether a given * file should be considered sharable or not. * A client should therefore behave in the safest way it can. */ - public static final int UNKNOWN = 0; + @Deprecated public static final int UNKNOWN = 0; /** * Constant indicating that the file or directory is sharable. @@ -83,7 +88,7 @@ * directories recursively contained in this directory are also * sharable. */ - public static final int SHARABLE = 1; + @Deprecated public static final int SHARABLE = 1; /** * Constant indicating that the file or directory is not sharable. @@ -91,7 +96,7 @@ * directories recursively contained in this directory are also * not sharable. */ - public static final int NOT_SHARABLE = 2; + @Deprecated public static final int NOT_SHARABLE = 2; /** * Constant indicating that a directory is sharable but files and @@ -99,7 +104,40 @@ * A client interested in children of this directory should explicitly * ask about each in turn. */ - public static final int MIXED = 3; + @Deprecated public static final int MIXED = 3; + + public static enum Sharability { + /** + * Constant indicating that nothing is known about whether a given + * file should be considered sharable or not. + * A client should therefore behave in the safest way it can. + */ + UNKNOWN, + + /** + * Constant indicating that the file or directory is sharable. + * In the case of a directory, this means that all files and + * directories recursively contained in this directory are also + * sharable. + */ + SHARABLE, + + /** + * Constant indicating that the file or directory is not sharable. + * In the case of a directory, this means that all files and + * directories recursively contained in this directory are also + * not sharable. + */ + NOT_SHARABLE, + + /** + * Constant indicating that a directory is sharable but files and + * directories recursively contained in it may or may not be sharable. + * A client interested in children of this directory should explicitly + * ask about each in turn. + */ + MIXED; + } private SharabilityQuery() {} @@ -108,7 +146,7 @@ * @param file a file or directory (may or may not already exist); should be {@linkplain FileUtil#normalizeFile normalized} * @return one of the constants in this class */ - public static int getSharability(File file) { + @Deprecated public static int getSharability(File file) { Parameters.notNull("file", file); boolean asserts = false; assert asserts = true; @@ -118,6 +156,16 @@ throw new IllegalArgumentException("Must pass a normalized file: " + file + " vs. " + normFile); } } + URI uri = null; + for (SharabilityQueryImplementation2 sqi : implementations2.allInstances()) { + if (uri == null) { + uri = file.toURI(); + } + Sharability x = sqi.getSharability(uri); + if (x != Sharability.UNKNOWN) { + return x.ordinal(); + } + } for (SharabilityQueryImplementation sqi : implementations.allInstances()) { int x = sqi.getSharability(file); if (x != UNKNOWN) { @@ -127,4 +175,46 @@ return UNKNOWN; } + /** + * Check whether an existing file is sharable. + * @param uri a file or directory (may or may not already exist); should be normalized. + * @return one of the constants in this class + * @since 1.27 + */ + public static Sharability getSharability(URI uri) { + Parameters.notNull("file", uri); + boolean asserts = false; + assert asserts = true; + if (asserts && !Utilities.isMac()) { + URI normUri = uri.normalize(); + if (!uri.equals(normUri)) { + throw new IllegalArgumentException("Must pass a normalized URI: " + uri + " vs. " + normUri); + } + } + for (SharabilityQueryImplementation2 sqi : implementations2.allInstances()) { + Sharability x = sqi.getSharability(uri); + if (x != Sharability.UNKNOWN) { + return x; + } + } + if ("file".equals(uri.getScheme())) { // NOI18N + File file = null; + for (SharabilityQueryImplementation sqi : implementations.allInstances()) { + if (file == null) { + file = new File(uri); + if (asserts && !Utilities.isMac()) { + File normFile = FileUtil.normalizeFile(file); + if (!file.equals(normFile)) { + throw new IllegalArgumentException("Must pass a normalized file: " + file + " vs. " + normFile); + } + } + } + int x = sqi.getSharability(file); + if (x != UNKNOWN) { + return Sharability.values()[x]; + } + } + } + return Sharability.UNKNOWN; + } } diff --git a/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation.java b/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation.java --- a/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation.java +++ b/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation.java @@ -68,8 +68,9 @@ *

* @see org.netbeans.api.queries.CollocationQuery * @author Jesse Glick + * @deprecated Use (@link org.netbeans.spi.queries.CollocationQueryImplementation2) instead. */ -public interface CollocationQueryImplementation { +@Deprecated public interface CollocationQueryImplementation { /** * Check whether two files are logically part of one directory tree. diff --git a/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation2.java b/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation2.java new file mode 100644 --- /dev/null +++ b/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation2.java @@ -0,0 +1,95 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, 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-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.spi.queries; + +import java.net.URI; + +/** + * A query which should typically be provided by a VCS to give information + * about whether some files can be considered part of one logical directory tree. + *

+ * This should be treated as a heuristic, useful when deciding whether to use + * absolute or relative links between path locations. + *

+ *

+ * The file names might refer to nonexistent files. A provider may or may not + * be able to say anything useful about them in this case. + *

+ *

+ * File names passed to this query will already have been normalized according to + * the semantics of {@link org.openide.filesystems.FileUtil#normalizeFile}. + *

+ *

+ * Threading note: implementors should avoid acquiring locks that might be held + * by other threads. Generally treat this interface similarly to SPIs in + * {@link org.openide.filesystems} with respect to threading semantics. + *

+ * @see org.netbeans.api.queries.CollocationQuery + * @author Jesse Glick + * @author Alexander Simon + */ +public interface CollocationQueryImplementation2 { + + /** + * Check whether two files are logically part of one directory tree. + * For example, if both files are stored in CVS, with the same server + * (CVSROOT) they might be considered collocated. + * If they are to be collocated their absolute paths must share a + * prefix directory, i.e. they must be located in the same filesystem root. + * If nothing is known about them, return false. + * @param file1 one file + * @param file2 another file + * @return true if they are probably part of one logical tree + */ + boolean areCollocated(URI file1, URI file2); + + /** + * Find a root of a logical tree containing this file, if any. + * The path of the root (if there is one) must be a prefix of the path of the file. + * @param file a file on disk (must be an absolute URI) + * @return an ancestor directory which is the root of a logical tree, + * if any (else null) (must be an absolute URI) + */ + URI findRoot(URI file); + +} + diff --git a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation.java b/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation.java --- a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation.java +++ b/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation.java @@ -70,8 +70,10 @@ * @see org.netbeans.api.queries.SharabilityQuery * @see AntProjectHelper.createSharabilityQuery(...) * @author Jesse Glick + * @deprecated Use (@link org.netbeans.spi.queries.SharabilityQueryImplementation2) instead. + * */ -public interface SharabilityQueryImplementation { +@Deprecated public interface SharabilityQueryImplementation { /** * Check whether a file or directory should be shared. diff --git a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation2.java b/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation2.java new file mode 100644 --- /dev/null +++ b/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation2.java @@ -0,0 +1,85 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, 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-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.spi.queries; + +import java.net.URI; +import org.netbeans.api.queries.SharabilityQuery.Sharability; + +/** + * Determine whether files should be shared (for example in a VCS) or are intended + * to be unshared. + *
+ *

+ * Could be implemented e.g. by project types which know that certain files or folders in + * a project (e.g. src/) are intended for VCS sharing while others + * (e.g. build/) are not. + *

+ *

+ * Note that the Project API module registers a default implementation of this query + * which delegates to the project which owns the queried file, if there is one. + * This is more efficient than searching instances in global lookup, so use that + * facility wherever possible. + *

+ *
+ *

+ * Threading note: implementors should avoid acquiring locks that might be held + * by other threads. Generally treat this interface similarly to SPIs in + * {@link org.openide.filesystems} with respect to threading semantics. + *

+ * @see org.netbeans.api.queries.SharabilityQuery + * @see AntProjectHelper.createSharabilityQuery(...) + * @author Jesse Glick + * @author Alexander Simon + * @since 1.27 + */ +public interface SharabilityQueryImplementation2 { + + /** + * Check whether a file or directory should be shared. + * If it is, it ought to be committed to a VCS if the user is using one. + * If it is not, it is either a disposable build product, or a per-user + * private file which is important but should not be shared. + * @param uri a normalized URI to check for sharability (may or may not yet exist). + * @return {@link org.netbeans.api.queries.SharabilityQuery.Sharability} + */ + Sharability getSharability(URI uri); +} diff --git a/queries/test/unit/src/org/netbeans/api/queries/CollocationQuery2Test.java b/queries/test/unit/src/org/netbeans/api/queries/CollocationQuery2Test.java new file mode 100644 --- /dev/null +++ b/queries/test/unit/src/org/netbeans/api/queries/CollocationQuery2Test.java @@ -0,0 +1,135 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, 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-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.api.queries; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.netbeans.spi.queries.CollocationQueryImplementation2; + +/** + * + * @author Alexander Simon + */ +public class CollocationQuery2Test extends NbTestCase { + + public CollocationQuery2Test(String testMethod) { + super (testMethod); + } + + @Override + public void setUp() throws IOException { + MockServices.setServices(CollocationQuery2Test.CollocationQueryImplementation2Impl.class); + } + + public void testAreCollocated() throws Exception { + clearWorkDir(); + File base = getWorkDir(); + File proj1 = new File(base, "proj1"); + proj1.mkdirs(); + File proj3 = new File(proj1, "proj3"); + proj3.mkdirs(); + File proj2 = new File(base, "proj2"); + proj2.mkdirs(); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1, proj3)); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj3.toURI())); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3, proj1)); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3.toURI(), proj1.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj2)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj2.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2, proj1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2.toURI(), proj1.toURI())); + + // folder does not exist: + File proj4 = new File(base, "proj"); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI())); + proj4.mkdirs(); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI())); + + // files do not exist: + File file1 = new File(base, "file1.txt"); + File file2 = new File(base, "file1"); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1, file2)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1.toURI(), file2.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2, file1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2.toURI(), file1.toURI())); + + // passing the same parameter + assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1, proj1)); + assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1.toURI(), proj1.toURI())); + } + + public static class CollocationQueryImplementation2Impl implements CollocationQueryImplementation2 { + + @Override + public boolean areCollocated(URI uri1, URI uri2) { + if (uri1.equals(uri2)) { + return true; + } + File file1 = new File(uri1); + File file2 = new File(uri2); + String f1 = file1.getPath(); + if ((file1.isDirectory() || !file1.exists()) && !f1.endsWith(File.separator)) { + f1 += File.separatorChar; + } + String f2 = file2.getAbsolutePath(); + if ((file2.isDirectory() || !file2.exists()) && !f2.endsWith(File.separator)) { + f2 += File.separatorChar; + } + return f1.startsWith(f2) || f2.startsWith(f1); + } + + @Override + public URI findRoot(URI file) { + return null; + } + + } +} diff --git a/queries/test/unit/src/org/netbeans/api/queries/CollocationQueryTest.java b/queries/test/unit/src/org/netbeans/api/queries/CollocationQueryTest.java new file mode 100644 --- /dev/null +++ b/queries/test/unit/src/org/netbeans/api/queries/CollocationQueryTest.java @@ -0,0 +1,105 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, 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-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.api.queries; + +import java.io.File; +import java.io.IOException; +import org.netbeans.junit.NbTestCase; + +/** + * + * @author Alexander Simon + */ +public class CollocationQueryTest extends NbTestCase { + + public CollocationQueryTest(String testMethod) { + super (testMethod); + } + + @Override + public void setUp() throws IOException { + } + + public void testAreCollocated() throws Exception { + clearWorkDir(); + File base = getWorkDir(); + File proj1 = new File(base, "proj1"); + proj1.mkdirs(); + File proj3 = new File(proj1, "proj3"); + proj3.mkdirs(); + File proj2 = new File(base, "proj2"); + proj2.mkdirs(); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1, proj3)); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj3.toURI())); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3, proj1)); + assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3.toURI(), proj1.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj2)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj2.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2, proj1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2.toURI(), proj1.toURI())); + + // folder does not exist: + File proj4 = new File(base, "proj"); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI())); + proj4.mkdirs(); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI())); + + // files do not exist: + File file1 = new File(base, "file1.txt"); + File file2 = new File(base, "file1"); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1, file2)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1.toURI(), file2.toURI())); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2, file1)); + assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2.toURI(), file1.toURI())); + + // passing the same parameter + assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1, proj1)); + assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1.toURI(), proj1.toURI())); + } + +} diff --git a/queries/test/unit/src/org/netbeans/api/queries/SharabilityQueryTest.java b/queries/test/unit/src/org/netbeans/api/queries/SharabilityQueryTest.java new file mode 100644 --- /dev/null +++ b/queries/test/unit/src/org/netbeans/api/queries/SharabilityQueryTest.java @@ -0,0 +1,191 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, 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-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.api.queries; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import org.netbeans.api.queries.SharabilityQuery.Sharability; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.netbeans.spi.queries.SharabilityQueryImplementation; +import org.netbeans.spi.queries.SharabilityQueryImplementation2; + +/** + * + * @author Alexander Simon + */ +public class SharabilityQueryTest extends NbTestCase { + + public SharabilityQueryTest(String testMethod) { + super (testMethod); + } + + @Override + public void setUp() throws IOException { + MockServices.setServices(SharabilityQueryImplementationImpl.class, SharabilityQueryImplementation2Impl.class); + } + + public void testSharableBridge2Old() throws IOException { + File file = new File("/home/user/aFile.sharable"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.SHARABLE, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); + } + + public void testSharableBridge2New() throws IOException { + File file = new File("/home/user/aFile.sharable2"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.SHARABLE, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); + } + + public void testNotSharableBridge2Old() throws IOException { + File file = new File("/home/user/aFile.not_sharable"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.NOT_SHARABLE, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2); + } + + public void testNotSharableBridge2New() throws IOException { + File file = new File("/home/user/aFile.not_sharable2"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.NOT_SHARABLE, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2); + } + + public void testMixedBridge2Old() throws IOException { + File file = new File("/home/user/aFile.mixed"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.MIXED, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.MIXED, sharability2); + } + + public void testMixedBridge2New() throws IOException { + File file = new File("/home/user/aFile.mixed2"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.MIXED, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.MIXED, sharability2); + } + + public void testUnknown() throws IOException { + File file = new File("/home/user/aFile.txt"); + int sharability = SharabilityQuery.getSharability(file); + assertEquals(SharabilityQuery.UNKNOWN, sharability); + URI uri = file.toURI(); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability2); + } + + public void testNormalized() throws IOException { + File file = new File("/home/user/../aFile.txt"); + Exception exception = null; + try { + SharabilityQuery.getSharability(file); + } catch (IllegalArgumentException e) { + exception = e; + } + assertNotNull(exception); + URI uri = file.toURI(); + exception = null; + try { + SharabilityQuery.getSharability(uri); + } catch (IllegalArgumentException e) { + exception = e; + } + assertNotNull(exception); + } + + public void testRfs() throws IOException, URISyntaxException { + URI uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable", null, null); + Sharability sharability = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability); + uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable2", null, null); + Sharability sharability2 = SharabilityQuery.getSharability(uri); + assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2); + } + + public static class SharabilityQueryImplementationImpl implements SharabilityQueryImplementation { + + @Override + public int getSharability(File file) { + String path = file.getAbsolutePath(); + if (path.endsWith(".sharable")) { + return SharabilityQuery.SHARABLE; + } else if (path.endsWith(".not_sharable")) { + return SharabilityQuery.NOT_SHARABLE; + } else if (path.endsWith(".mixed")) { + return SharabilityQuery.MIXED; + } + return SharabilityQuery.UNKNOWN; + } + } + + public static class SharabilityQueryImplementation2Impl implements SharabilityQueryImplementation2 { + + @Override + public Sharability getSharability(URI uri) { + String path = uri.getPath(); + if (path.endsWith(".sharable2")) { + return SharabilityQuery.Sharability.SHARABLE; + } else if (path.endsWith(".not_sharable2")) { + return SharabilityQuery.Sharability.NOT_SHARABLE; + } else if (path.endsWith(".mixed2")) { + return SharabilityQuery.Sharability.MIXED; + } + return SharabilityQuery.Sharability.UNKNOWN; + } + } +}