This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 198060
Collapse All | Expand All

(-)a/projectapi/apichanges.xml (+17 lines)
Lines 107-112 Link Here
107
    <!-- ACTUAL CHANGES BEGIN HERE: -->
107
    <!-- ACTUAL CHANGES BEGIN HERE: -->
108
108
109
    <changes>
109
    <changes>
110
        
111
       <change id="SharabilityQueryBasedOnURI">
112
        <api name="general"/>
113
        <summary>SharabilityQuery and CollocationQuery extended to use URI</summary>
114
        <version major="1" minor="42"/>
115
        <date day="22" month="12" year="2011"/>
116
        <author login="alexvsimon"/>
117
        <compatibility addition="yes" binary="compatible" semantic="compatible" source="compatible">
118
        </compatibility>
119
        <description>
120
          <p>
121
             Added new service ProjectSharabilityQuery2 that delegate implementation of SharabilityQueryImplementation2 to project.
122
          </p>
123
        </description>
124
        <class package="org.netbeans.modules.projectapi" name="ProjectSharabilityQuery2"/>
125
        <issue number="198060"/>
126
      </change>
110
127
111
        <change id="SourceGroup.contains.IAE">
128
        <change id="SourceGroup.contains.IAE">
112
            <api name="general"/>
129
            <api name="general"/>
(-)a/projectapi/manifest.mf (-1 / +1 lines)
Lines 1-7 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.projectapi/1
2
OpenIDE-Module: org.netbeans.modules.projectapi/1
3
OpenIDE-Module-Install: org/netbeans/modules/projectapi/Installer.class
3
OpenIDE-Module-Install: org/netbeans/modules/projectapi/Installer.class
4
OpenIDE-Module-Specification-Version: 1.41
4
OpenIDE-Module-Specification-Version: 1.42
5
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/projectapi/Bundle.properties
5
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/projectapi/Bundle.properties
6
OpenIDE-Module-Layer: org/netbeans/modules/projectapi/layer.xml
6
OpenIDE-Module-Layer: org/netbeans/modules/projectapi/layer.xml
7
7
(-)a/projectapi/src/org/netbeans/modules/projectapi/ProjectSharabilityQuery2.java (+73 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.projectapi;
43
44
import org.netbeans.spi.queries.SharabilityQueryImplementation2;
45
import java.net.URI;
46
import org.netbeans.api.project.FileOwnerQuery;
47
import org.netbeans.api.project.Project;
48
import org.netbeans.api.queries.SharabilityQuery;
49
import org.netbeans.api.queries.SharabilityQuery.Sharability;
50
51
/**
52
 * Delegates {@link SharabilityQuery} to implementations in project lookup.
53
 * @author Jesse Glick
54
 * @author Alexander Simon
55
 */
56
@org.openide.util.lookup.ServiceProvider(service=org.netbeans.spi.queries.SharabilityQueryImplementation2.class)
57
public class ProjectSharabilityQuery2 implements SharabilityQueryImplementation2 {
58
59
    public ProjectSharabilityQuery2() {}
60
    
61
    @Override
62
    public Sharability getSharability(URI uri) {
63
        Project p = FileOwnerQuery.getOwner(uri);
64
        if (p != null) {
65
            SharabilityQueryImplementation2 sqi = p.getLookup().lookup(SharabilityQueryImplementation2.class);
66
            if (sqi != null) {
67
                return sqi.getSharability(uri);
68
            }
69
        }
70
        return Sharability.UNKNOWN;
71
    }
72
    
73
}
(-)a/queries/apichanges.xml (+30 lines)
Lines 107-112 Link Here
107
    <!-- ACTUAL CHANGES BEGIN HERE: -->
107
    <!-- ACTUAL CHANGES BEGIN HERE: -->
108
108
109
    <changes>
109
    <changes>
110
       <change id="SharabilityQueryBasedOnURI">
111
        <api name="general"/>
112
        <summary>SharabilityQuery and CollocationQuery extended to use URI</summary>
113
        <version major="1" minor="27"/>
114
        <date day="22" month="12" year="2011"/>
115
        <author login="alexvsimon"/>
116
        <compatibility addition="yes" binary="compatible" semantic="compatible" source="compatible">
117
        </compatibility>
118
        <description>
119
          <p>
120
             Added new methods CollocationQuery.areCollocated(URI, URI) and CollocationQuery.findRoot(URI).
121
             Methods query all CollocationQueryImplementation (if pissible) and CollocationQueryImplementation2 instances found in global lookup.
122
             Depricated existing methods CollocationQuery.areCollocated(File, File) and CollocationQuery.findRoot(File).
123
             Methods query all CollocationQueryImplementation and CollocationQueryImplementation2 instances found in global lookup.
124
          </p>
125
          <p>
126
             Added new methods SharabilityQuery.getSharability(URI) and SharabilityQuery.getSharability(FileObject).
127
             Methods query all SharabilityQueryImplementation (if pissible) and SharabilityQueryImplementation2 instances found in global lookup.
128
             Depricated existing method SharabilityQuery.getSharability(File).
129
             Method query all SharabilityQueryImplementation and SharabilityQueryImplementation2 instances found in global lookup.
130
          </p>
131
        </description>
132
        <class package="org.netbeans.api.queries" name="CollocationQuery"/>
133
        <class package="org.netbeans.api.queries" name="SharabilityQuery"/>
134
        <class package="org.netbeans.spi.queries" name="CollocationQueryImplementation"/>
135
        <class package="org.netbeans.spi.queries" name="CollocationQueryImplementation2"/>
136
        <class package="org.netbeans.spi.queries" name="SharabilityQueryImplementation"/>
137
        <class package="org.netbeans.spi.queries" name="SharabilityQueryImplementation2"/>
138
        <issue number="198060"/>
139
      </change>
110
      <change id="FileBasedIsVisible">
140
      <change id="FileBasedIsVisible">
111
        <api name="general"/>
141
        <api name="general"/>
112
        <summary>VisibilityQuery.isVisible(File) added</summary>
142
        <summary>VisibilityQuery.isVisible(File) added</summary>
(-)a/queries/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.queries/1
2
OpenIDE-Module: org.netbeans.modules.queries/1
3
OpenIDE-Module-Specification-Version: 1.26
3
OpenIDE-Module-Specification-Version: 1.27
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties
5
5
(-)a/queries/src/org/netbeans/api/queries/CollocationQuery.java (-2 / +87 lines)
Lines 45-51 Link Here
45
package org.netbeans.api.queries;
45
package org.netbeans.api.queries;
46
46
47
import java.io.File;
47
import java.io.File;
48
import java.net.URI;
48
import org.netbeans.spi.queries.CollocationQueryImplementation;
49
import org.netbeans.spi.queries.CollocationQueryImplementation;
50
import org.netbeans.spi.queries.CollocationQueryImplementation2;
49
import org.openide.filesystems.FileUtil;
51
import org.openide.filesystems.FileUtil;
50
import org.openide.util.Lookup;
52
import org.openide.util.Lookup;
51
53
Lines 59-64 Link Here
59
61
60
    private static final Lookup.Result<CollocationQueryImplementation> implementations =
62
    private static final Lookup.Result<CollocationQueryImplementation> implementations =
61
        Lookup.getDefault().lookupResult(CollocationQueryImplementation.class);
63
        Lookup.getDefault().lookupResult(CollocationQueryImplementation.class);
64
    private static final Lookup.Result<CollocationQueryImplementation2> implementations2 =
65
        Lookup.getDefault().lookupResult(CollocationQueryImplementation2.class);
62
    
66
    
63
    private CollocationQuery() {}
67
    private CollocationQuery() {}
64
    
68
    
Lines 70-77 Link Here
70
     * @param file1 one file
74
     * @param file1 one file
71
     * @param file2 another file
75
     * @param file2 another file
72
     * @return true if they are probably part of one logical tree
76
     * @return true if they are probably part of one logical tree
77
     * @deprecated Use {@link org.netbeans.api.queries.CollocationQuery#areCollocated(java.net.URI, java.net.URI)} instead.
73
     */
78
     */
74
    public static boolean areCollocated(File file1, File file2) {
79
    @Deprecated public static boolean areCollocated(File file1, File file2) {
75
        if (!file1.equals(FileUtil.normalizeFile(file1))) {
80
        if (!file1.equals(FileUtil.normalizeFile(file1))) {
76
            throw new IllegalArgumentException("Parameter file1 was not "+  // NOI18N
81
            throw new IllegalArgumentException("Parameter file1 was not "+  // NOI18N
77
                "normalized. Was "+file1+" instead of "+FileUtil.normalizeFile(file1));  // NOI18N
82
                "normalized. Was "+file1+" instead of "+FileUtil.normalizeFile(file1));  // NOI18N
Lines 80-85 Link Here
80
            throw new IllegalArgumentException("Parameter file2 was not "+  // NOI18N
85
            throw new IllegalArgumentException("Parameter file2 was not "+  // NOI18N
81
                "normalized. Was "+file2+" instead of "+FileUtil.normalizeFile(file2));  // NOI18N
86
                "normalized. Was "+file2+" instead of "+FileUtil.normalizeFile(file2));  // NOI18N
82
        }
87
        }
88
        URI uri1 = file1.toURI();
89
        URI uri2 = file2.toURI();
90
        for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) {
91
            if (cqi.areCollocated(uri1, uri2)) {
92
                return true;
93
            }
94
        }
83
        for (CollocationQueryImplementation cqi : implementations.allInstances()) {
95
        for (CollocationQueryImplementation cqi : implementations.allInstances()) {
84
            if (cqi.areCollocated(file1, file2)) {
96
            if (cqi.areCollocated(file1, file2)) {
85
                return true;
97
                return true;
Lines 87-104 Link Here
87
        }
99
        }
88
        return false;
100
        return false;
89
    }
101
    }
102
103
    /**
104
     * Check whether two files are logically part of one directory tree.
105
     * For example, if both files are stored in CVS, with the same server
106
     * (<code>CVSROOT</code>) they might be considered collocated.
107
     * If nothing is known about them, return false.
108
     * @param file1 one file
109
     * @param file2 another file
110
     * @return true if they are probably part of one logical tree
111
     * @since 1.27
112
     */
113
    public static boolean areCollocated(URI file1, URI file2) {
114
        if (!file1.equals(file1.normalize())) {
115
            throw new IllegalArgumentException("Parameter file1 was not "+  // NOI18N
116
                "normalized. Was "+file1+" instead of "+file1.normalize());  // NOI18N
117
        }
118
        if (!file2.equals(file2.normalize())) {
119
            throw new IllegalArgumentException("Parameter file2 was not "+  // NOI18N
120
                "normalized. Was "+file2+" instead of "+file2.normalize());  // NOI18N
121
        }
122
        for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) {
123
            if (cqi.areCollocated(file1, file2)) {
124
                return true;
125
            }
126
        }
127
        if ("file".equals(file1.getScheme()) && "file".equals(file2.getScheme())) { // NOI18N
128
            File f1 = FileUtil.normalizeFile(new File(file1));
129
            File f2 = FileUtil.normalizeFile(new File(file2));
130
            for (CollocationQueryImplementation cqi : implementations.allInstances()) {
131
                if (cqi.areCollocated(f1, f2)) {
132
                    return true;
133
                }
134
            }
135
        }
136
        return false;
137
    }
90
    
138
    
91
    /**
139
    /**
92
     * Find a root of a logical tree containing this file, if any.
140
     * Find a root of a logical tree containing this file, if any.
93
     * @param file a file on disk
141
     * @param file a file on disk
94
     * @return an ancestor directory which is the root of a logical tree,
142
     * @return an ancestor directory which is the root of a logical tree,
95
     *         if any (else null)
143
     *         if any (else null)
144
     * @deprecated Use {@link org.netbeans.api.queries.CollocationQuery#findRoot(java.net.URI)} instead.
96
     */
145
     */
97
    public static File findRoot(File file) {
146
    @Deprecated public static File findRoot(File file) {
98
        if (!file.equals(FileUtil.normalizeFile(file))) {
147
        if (!file.equals(FileUtil.normalizeFile(file))) {
99
            throw new IllegalArgumentException("Parameter file was not "+  // NOI18N
148
            throw new IllegalArgumentException("Parameter file was not "+  // NOI18N
100
                "normalized. Was "+file+" instead of "+FileUtil.normalizeFile(file));  // NOI18N
149
                "normalized. Was "+file+" instead of "+FileUtil.normalizeFile(file));  // NOI18N
101
        }
150
        }
151
        URI uri = file.toURI();
152
        for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) {
153
            URI root = cqi.findRoot(uri);
154
            if (root != null) {
155
                return new File(root);
156
            }
157
        }
102
        for (CollocationQueryImplementation cqi : implementations.allInstances()) {
158
        for (CollocationQueryImplementation cqi : implementations.allInstances()) {
103
            File root = cqi.findRoot(file);
159
            File root = cqi.findRoot(file);
104
            if (root != null) {
160
            if (root != null) {
Lines 108-111 Link Here
108
        return null;
164
        return null;
109
    }
165
    }
110
    
166
    
167
    /**
168
     * Find a root of a logical tree containing this file, if any.
169
     * @param file a file on disk
170
     * @return an ancestor directory which is the root of a logical tree,
171
     *         if any (else null)
172
     * @since 1.27
173
     */
174
    public static URI findRoot(URI file) {
175
        if (!file.equals(file.normalize())) {
176
            throw new IllegalArgumentException("Parameter file was not "+  // NOI18N
177
                "normalized. Was "+file+" instead of "+file.normalize());  // NOI18N
178
        }
179
        for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) {
180
            URI root = cqi.findRoot(file);
181
            if (root != null) {
182
                return root;
183
            }
184
        }
185
        if ("file".equals(file.getScheme())) { // NOI18N
186
            File f = FileUtil.normalizeFile(new File(file));
187
            for (CollocationQueryImplementation cqi : implementations.allInstances()) {
188
                File root = cqi.findRoot(f);
189
                if (root != null) {
190
                    return root.toURI();
191
                }
192
            }
193
        }
194
        return null;
195
    }
111
}
196
}
(-)a/queries/src/org/netbeans/api/queries/SharabilityQuery.java (-5 / +120 lines)
Lines 45-51 Link Here
45
package org.netbeans.api.queries;
45
package org.netbeans.api.queries;
46
46
47
import java.io.File;
47
import java.io.File;
48
import java.net.URI;
49
import java.net.URISyntaxException;
50
import java.net.URL;
51
import java.util.logging.Level;
52
import java.util.logging.Logger;
48
import org.netbeans.spi.queries.SharabilityQueryImplementation;
53
import org.netbeans.spi.queries.SharabilityQueryImplementation;
54
import org.netbeans.spi.queries.SharabilityQueryImplementation2;
55
import org.openide.filesystems.FileObject;
56
import org.openide.filesystems.FileStateInvalidException;
49
import org.openide.filesystems.FileUtil;
57
import org.openide.filesystems.FileUtil;
50
import org.openide.util.Lookup;
58
import org.openide.util.Lookup;
51
import org.openide.util.Parameters;
59
import org.openide.util.Parameters;
Lines 70-105 Link Here
70
    private static final Lookup.Result<SharabilityQueryImplementation> implementations =
78
    private static final Lookup.Result<SharabilityQueryImplementation> implementations =
71
        Lookup.getDefault().lookupResult(SharabilityQueryImplementation.class);
79
        Lookup.getDefault().lookupResult(SharabilityQueryImplementation.class);
72
80
81
    private static final Lookup.Result<SharabilityQueryImplementation2> implementations2 =
82
        Lookup.getDefault().lookupResult(SharabilityQueryImplementation2.class);
83
    
84
    private static final Logger LOG = Logger.getLogger(SharabilityQuery.class.getName());
85
73
    /**
86
    /**
74
     * Constant indicating that nothing is known about whether a given
87
     * Constant indicating that nothing is known about whether a given
75
     * file should be considered sharable or not.
88
     * file should be considered sharable or not.
76
     * A client should therefore behave in the safest way it can.
89
     * A client should therefore behave in the safest way it can.
90
     * @deprecated Use {@link org.netbeans.api.queries.SharabilityQuery.Sharability#UNKNOWN} instead.
77
     */
91
     */
78
    public static final int UNKNOWN = 0;
92
    @Deprecated public static final int UNKNOWN = 0;
79
    
93
    
80
    /**
94
    /**
81
     * Constant indicating that the file or directory is sharable.
95
     * Constant indicating that the file or directory is sharable.
82
     * In the case of a directory, this means that all files and
96
     * In the case of a directory, this means that all files and
83
     * directories recursively contained in this directory are also
97
     * directories recursively contained in this directory are also
84
     * sharable.
98
     * sharable.
99
     * @deprecated Use {@link org.netbeans.api.queries.SharabilityQuery.Sharability#SHARABLE} instead.
85
     */
100
     */
86
    public static final int SHARABLE = 1;
101
    @Deprecated public static final int SHARABLE = 1;
87
    
102
    
88
    /**
103
    /**
89
     * Constant indicating that the file or directory is not sharable.
104
     * Constant indicating that the file or directory is not sharable.
90
     * In the case of a directory, this means that all files and
105
     * In the case of a directory, this means that all files and
91
     * directories recursively contained in this directory are also
106
     * directories recursively contained in this directory are also
92
     * not sharable.
107
     * not sharable.
108
     * @deprecated Use {@link org.netbeans.api.queries.SharabilityQuery.Sharability#NOT_SHARABLE} instead.
93
     */
109
     */
94
    public static final int NOT_SHARABLE = 2;
110
    @Deprecated public static final int NOT_SHARABLE = 2;
95
    
111
    
96
    /**
112
    /**
97
     * Constant indicating that a directory is sharable but files and
113
     * Constant indicating that a directory is sharable but files and
98
     * directories recursively contained in it may or may not be sharable.
114
     * directories recursively contained in it may or may not be sharable.
99
     * A client interested in children of this directory should explicitly
115
     * A client interested in children of this directory should explicitly
100
     * ask about each in turn.
116
     * ask about each in turn.
117
     * @deprecated Use {@link org.netbeans.api.queries.SharabilityQuery.Sharability#MIXED} instead.
101
     */
118
     */
102
    public static final int MIXED = 3;
119
    @Deprecated public static final int MIXED = 3;
120
121
    /**
122
     * Sharability constants.
123
     * @since 1.27
124
     */
125
    public enum Sharability {
126
        /**
127
        * Constant indicating that nothing is known about whether a given
128
        * file should be considered sharable or not.
129
        * A client should therefore behave in the safest way it can.
130
        */
131
        UNKNOWN,
132
133
        /**
134
        * Constant indicating that the file or directory is sharable.
135
        * In the case of a directory, this means that all files and
136
        * directories recursively contained in this directory are also
137
        * sharable.
138
        */
139
        SHARABLE,
140
141
        /**
142
        * Constant indicating that the file or directory is not sharable.
143
        * In the case of a directory, this means that all files and
144
        * directories recursively contained in this directory are also
145
        * not sharable.
146
        */
147
        NOT_SHARABLE,
148
149
        /**
150
        * Constant indicating that a directory is sharable but files and
151
        * directories recursively contained in it may or may not be sharable.
152
        * A client interested in children of this directory should explicitly
153
        * ask about each in turn.
154
        */
155
        MIXED;
156
    }
103
    
157
    
104
    private SharabilityQuery() {}
158
    private SharabilityQuery() {}
105
    
159
    
Lines 107-114 Link Here
107
     * Check whether an existing file is sharable.
161
     * Check whether an existing file is sharable.
108
     * @param file a file or directory (may or may not already exist); should be {@linkplain FileUtil#normalizeFile normalized}
162
     * @param file a file or directory (may or may not already exist); should be {@linkplain FileUtil#normalizeFile normalized}
109
     * @return one of the constants in this class
163
     * @return one of the constants in this class
164
     * @deprecated Use {@link org.netbeans.api.queries.SharabilityQuery#getSharability(java.net.URI)} instead.
110
     */
165
     */
111
    public static int getSharability(File file) {
166
    @Deprecated public static int getSharability(File file) {
112
        Parameters.notNull("file", file);
167
        Parameters.notNull("file", file);
113
        boolean asserts = false;
168
        boolean asserts = false;
114
        assert asserts = true;
169
        assert asserts = true;
Lines 118-123 Link Here
118
                throw new IllegalArgumentException("Must pass a normalized file: " + file + " vs. " + normFile);
173
                throw new IllegalArgumentException("Must pass a normalized file: " + file + " vs. " + normFile);
119
            }
174
            }
120
        }
175
        }
176
        URI uri = null;
177
        for (SharabilityQueryImplementation2 sqi : implementations2.allInstances()) {
178
            if (uri == null) {
179
                uri = file.toURI();
180
            }
181
            Sharability x = sqi.getSharability(uri);
182
            if (x != Sharability.UNKNOWN) {
183
                return x.ordinal();
184
            }
185
        }
121
        for (SharabilityQueryImplementation sqi : implementations.allInstances()) {
186
        for (SharabilityQueryImplementation sqi : implementations.allInstances()) {
122
            int x = sqi.getSharability(file);
187
            int x = sqi.getSharability(file);
123
            if (x != UNKNOWN) {
188
            if (x != UNKNOWN) {
Lines 127-130 Link Here
127
        return UNKNOWN;
192
        return UNKNOWN;
128
    }
193
    }
129
    
194
    
195
    /**
196
     * Check whether an existing file is sharable.
197
     * @param uri a file or directory (may or may not already exist); should be normalized.
198
     * @return one of the constants in this class
199
     * @since 1.27
200
     */
201
    public static Sharability getSharability(URI uri) {
202
        Parameters.notNull("uri", uri);
203
        boolean asserts = false;
204
        assert asserts = true;
205
        if (asserts) {
206
            URI normUri = uri.normalize();
207
            if (!uri.equals(normUri)) {
208
                throw new IllegalArgumentException("Must pass a normalized URI: " + uri + " vs. " + normUri);
209
            }
210
        }
211
        for (SharabilityQueryImplementation2 sqi : implementations2.allInstances()) {
212
            Sharability x = sqi.getSharability(uri);
213
            if (x != Sharability.UNKNOWN) {
214
                return x;
215
            }
216
        }
217
        if ("file".equals(uri.getScheme())) { // NOI18N
218
            File file = FileUtil.normalizeFile(new File(uri));
219
            for (SharabilityQueryImplementation sqi : implementations.allInstances()) {
220
                int x = sqi.getSharability(file);
221
                if (x != UNKNOWN) {
222
                    return Sharability.values()[x];
223
                }
224
            }
225
        }
226
        return Sharability.UNKNOWN;
227
    }
228
    
229
    /**
230
     * Check whether an existing file is sharable.
231
     * @param fo a file or directory; should be normalized.
232
     * @return one of the constants in this class
233
     * @since 1.27
234
     */
235
    public static Sharability getSharability(FileObject fo) {
236
        try {
237
            return getSharability(fo.getURL().toURI());
238
        } catch (URISyntaxException ex) {
239
            LOG.log(Level.FINE, null, ex);
240
        } catch (FileStateInvalidException ex) {
241
            LOG.log(Level.FINE, null, ex);
242
        }
243
        return Sharability.UNKNOWN;
244
    }
130
}
245
}
(-)a/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation.java (-1 / +2 lines)
Lines 68-75 Link Here
68
 * </p>
68
 * </p>
69
 * @see org.netbeans.api.queries.CollocationQuery
69
 * @see org.netbeans.api.queries.CollocationQuery
70
 * @author Jesse Glick
70
 * @author Jesse Glick
71
 * @deprecated Use {@link org.netbeans.spi.queries.CollocationQueryImplementation2} instead.
71
 */
72
 */
72
public interface CollocationQueryImplementation {
73
@Deprecated public interface CollocationQueryImplementation {
73
    
74
    
74
    /**
75
    /**
75
     * Check whether two files are logically part of one directory tree.
76
     * Check whether two files are logically part of one directory tree.
(-)a/queries/src/org/netbeans/spi/queries/CollocationQueryImplementation2.java (+96 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.spi.queries;
43
44
import java.net.URI;
45
46
/**
47
 * A query which should typically be provided by a VCS to give information
48
 * about whether some files can be considered part of one logical directory tree.
49
 * <p>
50
 * This should be treated as a heuristic, useful when deciding whether to use
51
 * absolute or relative links between path locations.
52
 * </p>
53
 * <p>
54
 * The file names might refer to nonexistent files. A provider may or may not
55
 * be able to say anything useful about them in this case.
56
 * </p>
57
 * <p>
58
 * File names passed to this query will already have been normalized according to
59
 * the semantics of {@link org.openide.filesystems.FileUtil#normalizeFile}.
60
 * </p>
61
 * <p>
62
 * Threading note: implementors should avoid acquiring locks that might be held
63
 * by other threads. Generally treat this interface similarly to SPIs in
64
 * {@link org.openide.filesystems} with respect to threading semantics.
65
 * </p>
66
 * @see org.netbeans.api.queries.CollocationQuery
67
 * @since 1.27
68
 * @author Jesse Glick
69
 * @author Alexander Simon
70
 */
71
public interface CollocationQueryImplementation2 {
72
    
73
    /**
74
     * Check whether two files are logically part of one directory tree.
75
     * For example, if both files are stored in CVS, with the same server
76
     * (<code>CVSROOT</code>) they might be considered collocated.
77
     * If they are to be collocated their absolute paths must share a
78
     * prefix directory, i.e. they must be located in the same filesystem root.
79
     * If nothing is known about them, return false.
80
     * @param file1 one file
81
     * @param file2 another file
82
     * @return true if they are probably part of one logical tree
83
     */
84
    boolean areCollocated(URI file1, URI file2);
85
    
86
    /**
87
     * Find a root of a logical tree containing this file, if any.
88
     * The path of the root (if there is one) must be a prefix of the path of the file.
89
     * @param file a file on disk (must be an absolute URI)
90
     * @return an ancestor directory which is the root of a logical tree,
91
     *         if any (else null) (must be an absolute URI)
92
     */
93
    URI findRoot(URI file);
94
    
95
}
96
(-)a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation.java (-1 / +4 lines)
Lines 69-77 Link Here
69
 * </p>
69
 * </p>
70
 * @see org.netbeans.api.queries.SharabilityQuery
70
 * @see org.netbeans.api.queries.SharabilityQuery
71
 * @see <a href="@org-netbeans-modules-project-ant@/org/netbeans/spi/project/support/ant/AntProjectHelper.html#createSharabilityQuery(java.lang.String[],%20java.lang.String[])"><code>AntProjectHelper.createSharabilityQuery(...)</code></a>
71
 * @see <a href="@org-netbeans-modules-project-ant@/org/netbeans/spi/project/support/ant/AntProjectHelper.html#createSharabilityQuery(java.lang.String[],%20java.lang.String[])"><code>AntProjectHelper.createSharabilityQuery(...)</code></a>
72
 * @since 1.27
72
 * @author Jesse Glick
73
 * @author Jesse Glick
74
 * @deprecated Use {@link org.netbeans.spi.queries.SharabilityQueryImplementation2} instead.
75
 * 
73
 */
76
 */
74
public interface SharabilityQueryImplementation {
77
@Deprecated public interface SharabilityQueryImplementation {
75
    
78
    
76
    /**
79
    /**
77
     * Check whether a file or directory should be shared.
80
     * Check whether a file or directory should be shared.
(-)a/queries/src/org/netbeans/spi/queries/SharabilityQueryImplementation2.java (+85 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.spi.queries;
43
44
import java.net.URI;
45
import org.netbeans.api.queries.SharabilityQuery.Sharability;
46
47
/**
48
 * Determine whether files should be shared (for example in a VCS) or are intended
49
 * to be unshared.
50
 * <div class="nonnormative">
51
 * <p>
52
 * Could be implemented e.g. by project types which know that certain files or folders in
53
 * a project (e.g. <samp>src/</samp>) are intended for VCS sharing while others
54
 * (e.g. <samp>build/</samp>) are not.
55
 * </p>
56
 * <p>
57
 * Note that the Project API module registers a default implementation of this query
58
 * which delegates to the project which owns the queried file, if there is one.
59
 * This is more efficient than searching instances in global lookup, so use that
60
 * facility wherever possible.
61
 * </p>
62
 * </div>
63
 * <p>
64
 * Threading note: implementors should avoid acquiring locks that might be held
65
 * by other threads. Generally treat this interface similarly to SPIs in
66
 * {@link org.openide.filesystems} with respect to threading semantics.
67
 * </p>
68
 * @see org.netbeans.api.queries.SharabilityQuery
69
 * @see <a href="@org-netbeans-modules-project-ant@/org/netbeans/spi/project/support/ant/AntProjectHelper.html#createSharabilityQuery(java.lang.String[],%20java.lang.String[])"><code>AntProjectHelper.createSharabilityQuery(...)</code></a>
70
 * @author Jesse Glick
71
 * @author Alexander Simon
72
 * @since 1.27
73
 */
74
public interface SharabilityQueryImplementation2 {
75
    
76
    /**
77
     * Check whether a file or directory should be shared.
78
     * If it is, it ought to be committed to a VCS if the user is using one.
79
     * If it is not, it is either a disposable build product, or a per-user
80
     * private file which is important but should not be shared.
81
     * @param uri a normalized URI to check for sharability (may or may not yet exist).
82
     * @return one of the {@link org.netbeans.api.queries.SharabilityQuery.Sharability}'s constant
83
     */
84
    Sharability getSharability(URI uri);
85
}
(-)a/queries/test/unit/src/org/netbeans/api/queries/CollocationQuery2Test.java (+135 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.queries;
43
44
import java.io.File;
45
import java.io.IOException;
46
import java.net.URI;
47
import org.netbeans.junit.MockServices;
48
import org.netbeans.junit.NbTestCase;
49
import org.netbeans.spi.queries.CollocationQueryImplementation2;
50
51
/**
52
 *
53
 * @author Alexander Simon
54
 */
55
public class CollocationQuery2Test extends NbTestCase {
56
    
57
    public CollocationQuery2Test(String testMethod) {
58
        super (testMethod);
59
    }
60
    
61
    @Override
62
    public void setUp() throws IOException {
63
        MockServices.setServices(CollocationQuery2Test.CollocationQueryImplementation2Impl.class);
64
    }
65
    
66
    public void testAreCollocated() throws Exception {
67
        clearWorkDir();
68
        File base = getWorkDir();
69
        File proj1 = new File(base, "proj1");
70
        proj1.mkdirs();
71
        File proj3 = new File(proj1, "proj3");
72
        proj3.mkdirs();
73
        File proj2 = new File(base, "proj2");
74
        proj2.mkdirs();
75
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1, proj3));
76
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj3.toURI()));
77
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3, proj1));
78
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3.toURI(), proj1.toURI()));
79
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj2));
80
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj2.toURI()));
81
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2, proj1));
82
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2.toURI(), proj1.toURI()));
83
        
84
        // folder does not exist:
85
        File proj4 = new File(base, "proj");
86
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4));
87
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI()));
88
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1));
89
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI()));
90
        proj4.mkdirs();
91
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4));
92
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI()));
93
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1));
94
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI()));
95
        
96
        // files do not exist:
97
        File file1 = new File(base, "file1.txt");
98
        File file2 = new File(base, "file1");
99
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1, file2));
100
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1.toURI(), file2.toURI()));
101
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2, file1));
102
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2.toURI(), file1.toURI()));
103
        
104
        // passing the same parameter
105
        assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1, proj1));
106
        assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1.toURI(), proj1.toURI()));
107
    }
108
109
    public static class CollocationQueryImplementation2Impl implements CollocationQueryImplementation2 {
110
111
        @Override
112
        public boolean areCollocated(URI uri1, URI uri2) {
113
            if (uri1.equals(uri2)) {
114
                return true;
115
            }
116
            File file1 = new File(uri1);
117
            File file2 = new File(uri2);
118
            String f1 = file1.getPath();
119
            if ((file1.isDirectory() || !file1.exists()) && !f1.endsWith(File.separator)) {
120
                f1 += File.separatorChar;
121
            }
122
            String f2 = file2.getAbsolutePath();
123
            if ((file2.isDirectory() || !file2.exists()) && !f2.endsWith(File.separator)) {
124
                f2 += File.separatorChar;
125
            }
126
            return f1.startsWith(f2) || f2.startsWith(f1);
127
        }
128
129
        @Override
130
        public URI findRoot(URI file) {
131
            return null;
132
        }
133
134
    }
135
}
(-)a/queries/test/unit/src/org/netbeans/api/queries/CollocationQueryTest.java (+105 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.queries;
43
44
import java.io.File;
45
import java.io.IOException;
46
import org.netbeans.junit.NbTestCase;
47
48
/**
49
 *
50
 * @author Alexander Simon
51
 */
52
public class CollocationQueryTest extends NbTestCase {
53
    
54
    public CollocationQueryTest(String testMethod) {
55
        super (testMethod);
56
    }
57
    
58
    @Override
59
    public void setUp() throws IOException {
60
    }
61
    
62
    public void testAreCollocated() throws Exception {
63
        clearWorkDir();
64
        File base = getWorkDir();
65
        File proj1 = new File(base, "proj1");
66
        proj1.mkdirs();
67
        File proj3 = new File(proj1, "proj3");
68
        proj3.mkdirs();
69
        File proj2 = new File(base, "proj2");
70
        proj2.mkdirs();
71
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1, proj3));
72
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj3.toURI()));
73
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3, proj1));
74
        assertTrue("Must be collocated", CollocationQuery.areCollocated(proj3.toURI(), proj1.toURI()));
75
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj2));
76
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj2.toURI()));
77
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2, proj1));
78
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj2.toURI(), proj1.toURI()));
79
        
80
        // folder does not exist:
81
        File proj4 = new File(base, "proj");
82
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4));
83
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI()));
84
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1));
85
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI()));
86
        proj4.mkdirs();
87
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1, proj4));
88
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj1.toURI(), proj4.toURI()));
89
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4, proj1));
90
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(proj4.toURI(), proj1.toURI()));
91
        
92
        // files do not exist:
93
        File file1 = new File(base, "file1.txt");
94
        File file2 = new File(base, "file1");
95
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1, file2));
96
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file1.toURI(), file2.toURI()));
97
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2, file1));
98
        assertFalse("Cannot be collocated", CollocationQuery.areCollocated(file2.toURI(), file1.toURI()));
99
        
100
        // passing the same parameter
101
        assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1, proj1));
102
        assertTrue("A file must be collocated with itself", CollocationQuery.areCollocated(proj1.toURI(), proj1.toURI()));
103
    }
104
105
}
(-)a/queries/test/unit/src/org/netbeans/api/queries/SharabilityQueryTest.java (+208 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.queries;
43
44
import java.io.File;
45
import java.io.IOException;
46
import java.net.URI;
47
import java.net.URISyntaxException;
48
import org.netbeans.api.queries.SharabilityQuery.Sharability;
49
import org.netbeans.junit.MockServices;
50
import org.netbeans.junit.NbTestCase;
51
import org.netbeans.spi.queries.SharabilityQueryImplementation;
52
import org.netbeans.spi.queries.SharabilityQueryImplementation2;
53
import org.openide.filesystems.FileObject;
54
import org.openide.filesystems.FileUtil;
55
56
/**
57
 *
58
 * @author Alexander Simon
59
 */
60
public class SharabilityQueryTest extends NbTestCase {
61
    
62
    public SharabilityQueryTest(String testMethod) {
63
        super (testMethod);
64
    }
65
    
66
    @Override
67
    public void setUp() throws IOException {
68
        clearWorkDir();
69
        MockServices.setServices(SharabilityQueryImplementationImpl.class, SharabilityQueryImplementation2Impl.class);
70
    }
71
    
72
    public void testSharableBridge2Old() throws IOException {
73
        File file = new File("/home/user/aFile.sharable");
74
        int sharability = SharabilityQuery.getSharability(file);
75
        assertEquals(SharabilityQuery.SHARABLE, sharability);
76
        URI uri = file.toURI();
77
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
78
        assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2);
79
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "sharable");
80
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
81
    }
82
83
    public void testSharableBridge2New() throws IOException {
84
        File file = new File("/home/user/aFile.sharable2");
85
        int sharability = SharabilityQuery.getSharability(file);
86
        assertEquals(SharabilityQuery.SHARABLE, sharability);
87
        URI uri = file.toURI();
88
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
89
        assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2);
90
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "sharable2");
91
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
92
    }
93
94
    public void testNotSharableBridge2Old() throws IOException {
95
        File file = new File("/home/user/aFile.not_sharable");
96
        int sharability = SharabilityQuery.getSharability(file);
97
        assertEquals(SharabilityQuery.NOT_SHARABLE, sharability);
98
        URI uri = file.toURI();
99
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
100
        assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2);
101
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "not_sharable");
102
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
103
    }
104
105
    public void testNotSharableBridge2New() throws IOException {
106
        File file = new File("/home/user/aFile.not_sharable2");
107
        int sharability = SharabilityQuery.getSharability(file);
108
        assertEquals(SharabilityQuery.NOT_SHARABLE, sharability);
109
        URI uri = file.toURI();
110
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
111
        assertEquals(SharabilityQuery.Sharability.NOT_SHARABLE, sharability2);
112
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "not_sharable2");
113
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
114
    }
115
    
116
    public void testMixedBridge2Old() throws IOException {
117
        File file = new File("/home/user/aFile.mixed");
118
        int sharability = SharabilityQuery.getSharability(file);
119
        assertEquals(SharabilityQuery.MIXED, sharability);
120
        URI uri = file.toURI();
121
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
122
        assertEquals(SharabilityQuery.Sharability.MIXED, sharability2);
123
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "mixed");
124
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
125
    }
126
127
    public void testMixedBridge2New() throws IOException {
128
        File file = new File("/home/user/aFile.mixed2");
129
        int sharability = SharabilityQuery.getSharability(file);
130
        assertEquals(SharabilityQuery.MIXED, sharability);
131
        URI uri = file.toURI();
132
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
133
        assertEquals(SharabilityQuery.Sharability.MIXED, sharability2);
134
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "mixed2");
135
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
136
    }
137
138
    public void testUnknown() throws IOException {
139
        File file = new File("/home/user/aFile.txt");
140
        int sharability = SharabilityQuery.getSharability(file);
141
        assertEquals(SharabilityQuery.UNKNOWN, sharability);
142
        URI uri = file.toURI();
143
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
144
        assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability2);
145
        FileObject fo = FileUtil.toFileObject(getWorkDir()).createData("aFile", "txt");
146
        assertEquals(sharability2, SharabilityQuery.getSharability(fo));
147
    }
148
149
    public void testNormalized() throws IOException {
150
        File file = new File("/home/user/../aFile.txt");
151
        Exception exception = null;
152
        try {
153
            SharabilityQuery.getSharability(file);
154
        } catch (IllegalArgumentException e) {
155
            exception = e;
156
        }
157
        assertNotNull(exception);
158
        URI uri = file.toURI();
159
        exception = null;
160
        try {
161
            SharabilityQuery.getSharability(uri);
162
        } catch (IllegalArgumentException e) {
163
            exception = e;
164
        }
165
        assertNotNull(exception);
166
    }
167
168
    public void testRfs() throws IOException, URISyntaxException {
169
        URI uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable", null, null);
170
        Sharability sharability = SharabilityQuery.getSharability(uri);
171
        assertEquals(SharabilityQuery.Sharability.UNKNOWN, sharability);
172
        uri = new URI("rfs", "tester", "localhost", 22, "/home/tester/aFile.sharable2", null, null);
173
        Sharability sharability2 = SharabilityQuery.getSharability(uri);
174
        assertEquals(SharabilityQuery.Sharability.SHARABLE, sharability2);
175
    }
176
177
    public static class SharabilityQueryImplementationImpl implements SharabilityQueryImplementation {
178
179
        @Override
180
        public int getSharability(File file) {
181
            String path = file.getAbsolutePath();
182
            if (path.endsWith(".sharable")) {
183
                return SharabilityQuery.SHARABLE;
184
            } else if (path.endsWith(".not_sharable")) {
185
                return SharabilityQuery.NOT_SHARABLE;
186
            } else if (path.endsWith(".mixed")) {
187
                return SharabilityQuery.MIXED;
188
            }
189
            return SharabilityQuery.UNKNOWN;
190
        }
191
    }
192
193
    public static class SharabilityQueryImplementation2Impl implements SharabilityQueryImplementation2 {
194
195
        @Override
196
        public Sharability getSharability(URI uri) {
197
            String path = uri.getPath();
198
            if (path.endsWith(".sharable2")) {
199
                return SharabilityQuery.Sharability.SHARABLE;
200
            } else if (path.endsWith(".not_sharable2")) {
201
                return SharabilityQuery.Sharability.NOT_SHARABLE;
202
            } else if (path.endsWith(".mixed2")) {
203
                return SharabilityQuery.Sharability.MIXED;
204
            }
205
            return SharabilityQuery.Sharability.UNKNOWN;
206
        }
207
    }
208
}

Return to bug 198060