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 229281
Collapse All | Expand All

(-)queries/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
        <change id="VersioningQuery">
111
            <api name="general"/>
112
            <summary>added VersioningQuery</summary>
113
            <version major="1" minor="35"/>
114
            <date day="9" month="5" year="2013"/>
115
            <author login="tstupka"/>
116
            <compatibility addition="yes" binary="compatible" semantic="compatible" source="compatible">
117
            </compatibility>
118
            <description>
119
                <p>
120
                    Added a new query <code>VersioningQuery</code> which provides Versioning relevant info like 
121
                    e.g. if a particular file is managed by a Versioning System.
122
                </p>
123
            </description>
124
            <class package="org.netbeans.spi.queries" name="VersioningQuery"/>
125
            <issue number="220749"/>
126
        </change>        
110
        <change id="VQChangeEvent">
127
        <change id="VQChangeEvent">
111
            <api name="general"/>
128
            <api name="general"/>
112
            <summary>VisibilityQueryChangeEvent added</summary>
129
            <summary>VisibilityQueryChangeEvent added</summary>
(-)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.34
3
OpenIDE-Module-Specification-Version: 1.35
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties
5
5
(-)queries/src/org/netbeans/api/queries/VersioningQuery.java (+128 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 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 2013 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.api.queries;
44
45
import java.net.URI;
46
import java.util.logging.Level;
47
import java.util.logging.Logger;
48
import org.netbeans.spi.queries.VersioningQueryImplementation;
49
import org.openide.util.Lookup;
50
51
/**
52
 * Find out Versioning System relevant information about a file.
53
 * 
54
 * @author Tomas Stupka
55
 * @since 1.35
56
 */
57
public final class VersioningQuery {
58
59
    private static final Logger LOG = Logger.getLogger(FileEncodingQuery.class.getName());
60
    
61
    private static final Lookup.Result<org.netbeans.spi.queries.VersioningQueryImplementation> implementations =
62
        Lookup.getDefault().lookupResult(org.netbeans.spi.queries.VersioningQueryImplementation.class);
63
        
64
    private VersioningQuery() { }
65
    
66
    /**
67
     * Determines whether the given local file or directory is managed by a Versioning System 
68
     * - e.g. located in a SVN checkout or Mercurial clone.
69
     * 
70
     * @param uri a {@link org.openide.filesystems.FileUtil#normalizeFile normalized} file to check if managed
71
     * @return <code>true</code> if the file is managed, otherwise <code>false</code>.
72
     * @since 1.35
73
     */
74
    public static boolean isManaged(URI uri) {
75
        java.util.Objects.requireNonNull(uri);
76
        boolean asserts = false;
77
        assert asserts = true;
78
        if (asserts) {
79
            URI normUri = uri.normalize();
80
            if (!uri.equals(normUri)) {
81
                throw new IllegalArgumentException("Must pass a normalized URI: " + uri + " vs. " + normUri);
82
            }
83
        }
84
        
85
        for (VersioningQueryImplementation vqi : implementations.allInstances()) {
86
            if(vqi.isManaged(uri)) {
87
                LOG.log(Level.FINE, "{0} is VCS managed", new Object[] {uri}); // NOI18N
88
                return true;
89
            }
90
        }
91
        
92
        LOG.log(Level.FINE, "{0} isn't managed by any VCS", new Object[] {uri}); // NOI18N
93
        return false;
94
    }
95
96
    /**
97
     * Provides the Versioning System specific information about a files remote repository or origin. 
98
     * This might be for example in case of Subversion the repository url, or in case of Mercurial the default pull url. 
99
     * Also note that only Versioning Systems available by a supported Team Server are expected to return a
100
     * valid value. 
101
     * 
102
     * @param uri a {@link org.openide.filesystems.FileUtil#normalizeFile normalized} file to check if managed
103
     * @return value describing the remote location or null if not available or provided
104
     * @since 1.35
105
     */       
106
    public static String getRemoteLocation(URI uri) {
107
        java.util.Objects.requireNonNull(uri);
108
        boolean asserts = false;
109
        assert asserts = true;
110
        if (asserts) {
111
            URI normUri = uri.normalize();
112
            if (!uri.equals(normUri)) {
113
                throw new IllegalArgumentException("Must pass a normalized URI: " + uri + " vs. " + normUri);
114
            }
115
        }
116
        
117
        for (VersioningQueryImplementation vqi : implementations.allInstances()) {
118
            final String remoteLocation = vqi.getRemoteLocation(uri);
119
            if(remoteLocation != null) {
120
                LOG.log(Level.FINE, "{0}: received remote location {1}", new Object[] {uri, remoteLocation}); // NOI18N
121
                return remoteLocation;
122
            }
123
        }
124
        
125
        LOG.log(Level.FINE, "{0}: received no remote location", new Object[] {uri}); // NOI18N
126
        return null;        
127
    }
128
}
(-)queries/src/org/netbeans/spi/queries/VersioningQueryImplementation.java (+77 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 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 2013 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.spi.queries;
44
45
import java.net.URI;
46
47
/**
48
 * Provide Versioning System relevant information about a file.
49
 * 
50
 * @author Tomas Stupka
51
 * @since 1.35
52
 */
53
public interface VersioningQueryImplementation {
54
    
55
    /**
56
     * Determines whether the given local file or directory is managed by a Versioning System 
57
     * - e.g. located in a SVN checkout or Mercurial clone.
58
     * 
59
     * @param uri a {@link org.openide.filesystems.FileUtil#normalizeFile normalized} file to check if managed
60
     * @return <code>true</code> if the file is managed, otherwise <code>false</code>.
61
     * @since 1.35
62
     */
63
    boolean isManaged(URI uri);
64
    
65
    /**
66
     * Provides the Versioning System specific information about a files remote repository or origin. 
67
     * * This might be for example in case of Subversion the repository url, or in case of Mercurial the default pull url. 
68
     * Also note that only Versioning Systems available by a supported Team Server are expected to return a
69
     * meaningful value. 
70
     * 
71
     * @param uri a {@link org.openide.filesystems.FileUtil#normalizeFile normalized} file to check if managed
72
     * @return value describing the remote location or null if not available or provided
73
     * @since 1.35
74
     */    
75
    String getRemoteLocation(URI uri);
76
    
77
}
(-)queries/test/unit/src/org/netbeans/api/queries/VersioningQueryTest.java (+132 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.netbeans.spi.queries.VersioningQueryImplementation;
54
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileUtil;
56
import org.openide.util.Utilities;
57
58
/**
59
 *
60
 * @author Tomas Stupka
61
 */
62
public class VersioningQueryTest extends NbTestCase {
63
    
64
    public VersioningQueryTest(String testMethod) {
65
        super (testMethod);
66
    }
67
    
68
    private final File home = new File(System.getProperty("user.dir"));
69
    
70
    @Override
71
    public void setUp() throws IOException {
72
        clearWorkDir();
73
        MockServices.setServices(VersioningQueryImplementationImpl.class);
74
    }
75
    
76
    public void testIsManaged() throws IOException {
77
        File file = new File(home, "aFile.vcs");
78
        assertTrue(VersioningQuery.isManaged(Utilities.toURI(file)));
79
    }
80
    
81
    public void testIsNotManaged() throws IOException {
82
        File file = new File(home, "aFile.txt");
83
        assertFalse(VersioningQuery.isManaged(Utilities.toURI(file)));
84
    }
85
    
86
    public void testGetRemoteLocation() throws IOException {
87
        File file = new File(home, "aFile.vcs");
88
        assertEquals(Utilities.toURI(file).toString(), VersioningQuery.getRemoteLocation(Utilities.toURI(file)));
89
    }
90
    
91
    public void testNoRemoteLocation() throws IOException {
92
        File file = new File(home, "aFile.txt");
93
        assertNull(VersioningQuery.getRemoteLocation(Utilities.toURI(file)));
94
    }
95
    
96
    public void testNormalized() throws IOException {
97
        File file = new File(home, "../aFile.txt");
98
        Exception exception = null;
99
        try {
100
            VersioningQuery.isManaged(Utilities.toURI(file));
101
        } catch (IllegalArgumentException e) {
102
            exception = e;
103
        }
104
        assertNotNull(exception);
105
        URI uri = Utilities.toURI(file);
106
        exception = null;
107
        try {
108
            VersioningQuery.getRemoteLocation(Utilities.toURI(file));
109
        } catch (IllegalArgumentException e) {
110
            exception = e;
111
        }
112
        assertNotNull(exception);
113
    }
114
115
    public static class VersioningQueryImplementationImpl implements VersioningQueryImplementation {
116
117
        @Override
118
        public boolean isManaged(URI uri) {
119
            File file = Utilities.toFile(uri);
120
            String path = file.getAbsolutePath();
121
            return path.endsWith(".vcs");
122
        }
123
124
        @Override
125
        public String getRemoteLocation(URI uri) {
126
            File file = Utilities.toFile(uri);
127
            String path = file.getAbsolutePath();
128
            return path.endsWith(".vcs") ? uri.toString() : null;
129
        }
130
    }
131
132
}

Return to bug 229281