@@ -, +, @@
- third variant API (check scheme)
--- a/queries/src/org/netbeans/api/queries/CollocationQuery.java
+++ a/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.ResultCVSROOT
) 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;
+ }
}
--- a/queries/src/org/netbeans/api/queries/SharabilityQuery.java
+++ a/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
+ * 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);
+
+}
+
--- a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation.java
+++ a/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.
--- a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation2.java
+++ a/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 + * @seeAntProjectHelper.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);
+}
--- a/queries/test/unit/src/org/netbeans/api/queries/CollocationQuery2Test.java
+++ a/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;
+ }
+
+ }
+}
--- a/queries/test/unit/src/org/netbeans/api/queries/CollocationQueryTest.java
+++ a/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()));
+ }
+
+}
--- a/queries/test/unit/src/org/netbeans/api/queries/SharabilityQueryTest.java
+++ a/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;
+ }
+ }
+}