~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/api/src/org/netbeans/api/java/queries/UnitTestForSourceQuery.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.api.java.queries;
 
43
 
 
44
import java.net.URL;
 
45
import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation;
 
46
import org.openide.filesystems.FileObject;
 
47
import org.openide.util.Lookup;
 
48
 
 
49
 
 
50
/**
 
51
 * Query to find Java package root of unit tests for Java package root of
 
52
 * sources and vice versa.
 
53
 *
 
54
 * @see org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation
 
55
 * @author David Konecny, Tomas Zezula
 
56
 * @since org.netbeans.api.java/1 1.4
 
57
 */
 
58
public class UnitTestForSourceQuery {
 
59
 
 
60
    @SuppressWarnings ("deprecation")   // NOI18N
 
61
    private static final Lookup.Result<? extends org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation> implementations =
 
62
        Lookup.getDefault().lookupResult(org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation.class);
 
63
    private static final Lookup.Result<? extends MultipleRootsUnitTestForSourceQueryImplementation> mrImplementations = 
 
64
        Lookup.getDefault().lookupResult(MultipleRootsUnitTestForSourceQueryImplementation.class);
 
65
 
 
66
    private UnitTestForSourceQuery() {
 
67
    }
 
68
 
 
69
    /**
 
70
     * Returns the test root for a given source root.
 
71
     *
 
72
     * @param source Java package root with sources
 
73
     * @return corresponding Java package root with unit tests. The
 
74
     *     returned URL does not have to point to existing file. It can be null
 
75
     *     when the mapping from source to unit test is not known.
 
76
     * @deprecated Use {@link #findUnitTests} instead.
 
77
     */
 
78
    public static URL findUnitTest(FileObject source) {
 
79
        URL[] result = findUnitTests (source);
 
80
        return result.length == 0 ? null : result[0];
 
81
    }
 
82
 
 
83
 
 
84
    /**
 
85
     * Returns the test roots for a given source root.
 
86
     *
 
87
     * @param source Java package root with sources
 
88
     * @return corresponding Java package roots with unit tests. The
 
89
     *     returned URLs do not have to point to existing files. It can be an empty
 
90
     *     array (but not null) when the mapping from source to unit tests is not known.
 
91
     * @since org.netbeans.api.java/1 1.7
 
92
     */
 
93
    @SuppressWarnings ("deprecation")   // NOI18N
 
94
    public static URL[] findUnitTests(FileObject source) {
 
95
        if (source == null) {
 
96
            throw new IllegalArgumentException("Parameter source cannot be null"); // NOI18N
 
97
        }
 
98
        for (MultipleRootsUnitTestForSourceQueryImplementation query : mrImplementations.allInstances()) {            
 
99
            URL[] urls = query.findUnitTests(source);
 
100
            if (urls != null) {
 
101
                return urls;
 
102
            }
 
103
        }
 
104
        for (org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation query : implementations.allInstances()) {
 
105
            URL u = query.findUnitTest(source);
 
106
            if (u != null) {
 
107
                return new URL[] {u};
 
108
            }
 
109
        }
 
110
        return new URL[0];
 
111
    }
 
112
 
 
113
    /**
 
114
     * Returns the source root for a given test root.
 
115
     *
 
116
     * @param unitTest Java package root with unit tests
 
117
     * @return corresponding Java package root with sources. It can be null
 
118
     *     when the mapping from unit test to source is not known.
 
119
     * @deprecated Use {@link #findSources} instead.
 
120
     */
 
121
    public static URL findSource(FileObject unitTest) {
 
122
        URL[] result =  findSources (unitTest);
 
123
        return result.length == 0 ? null : result[0];
 
124
    }
 
125
 
 
126
    /**
 
127
     * Returns the source roots for a given test root.
 
128
     *
 
129
     * @param unitTest Java package root with unit tests
 
130
     * @return corresponding Java package roots with sources. It can be an empty array (not null)
 
131
     *     when the mapping from unit test to sources is not known.
 
132
     * @since org.netbeans.api.java/1 1.7
 
133
     */
 
134
    @SuppressWarnings ("deprecation")   // NOI18N
 
135
    public static URL[] findSources (FileObject unitTest) {
 
136
        if (unitTest == null) {
 
137
            throw new IllegalArgumentException("Parameter unitTest cannot be null"); // NOI18N
 
138
        }
 
139
        for (MultipleRootsUnitTestForSourceQueryImplementation query : mrImplementations.allInstances()) {
 
140
            URL[] urls = query.findSources(unitTest);
 
141
            if (urls != null) {
 
142
                return urls;
 
143
            }
 
144
        }
 
145
        for (org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation query : implementations.allInstances()) {            
 
146
            URL u = query.findSource(unitTest);
 
147
            if (u != null) {
 
148
                return new URL[] {u};
 
149
            }
 
150
        }
 
151
        return new URL[0];
 
152
    }
 
153
 
 
154
}