001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.gsfpath.api.queries;
043:
044: import java.net.URL;
045: import org.netbeans.modules.gsfpath.spi.queries.MultipleRootsUnitTestForSourceQueryImplementation;
046: import org.openide.filesystems.FileObject;
047: import org.openide.util.Lookup;
048:
049: /**
050: * Query to find Java package root of unit tests for Java package root of
051: * sources and vice versa.
052: *
053: * @see org.netbeans.modules.gsfpath.spi.queries.MultipleRootsUnitTestForSourceQueryImplementation
054: * @author David Konecny, Tomas Zezula
055: * @since org.netbeans.modules.gsfpath.api/1 1.4
056: */
057: public class UnitTestForSourceQuery {
058:
059: @SuppressWarnings("deprecation")
060: // NOI18N
061: private static final Lookup.Result<? extends org.netbeans.modules.gsfpath.spi.queries.UnitTestForSourceQueryImplementation> implementations = Lookup
062: .getDefault()
063: .lookupResult(
064: org.netbeans.modules.gsfpath.spi.queries.UnitTestForSourceQueryImplementation.class);
065: private static final Lookup.Result<? extends MultipleRootsUnitTestForSourceQueryImplementation> mrImplementations = Lookup
066: .getDefault()
067: .lookupResult(
068: MultipleRootsUnitTestForSourceQueryImplementation.class);
069:
070: private UnitTestForSourceQuery() {
071: }
072:
073: /**
074: * Returns the test root for a given source root.
075: *
076: * @param source Java package root with sources
077: * @return corresponding Java package root with unit tests. The
078: * returned URL does not have to point to existing file. It can be null
079: * when the mapping from source to unit test is not known.
080: * @deprecated Use {@link #findUnitTests} instead.
081: */
082: public static URL findUnitTest(FileObject source) {
083: URL[] result = findUnitTests(source);
084: return result.length == 0 ? null : result[0];
085: }
086:
087: /**
088: * Returns the test roots for a given source root.
089: *
090: * @param source Java package root with sources
091: * @return corresponding Java package roots with unit tests. The
092: * returned URLs do not have to point to existing files. It can be an empty
093: * array (but not null) when the mapping from source to unit tests is not known.
094: * @since org.netbeans.modules.gsfpath.api/1 1.7
095: */
096: @SuppressWarnings("deprecation")
097: // NOI18N
098: public static URL[] findUnitTests(FileObject source) {
099: if (source == null) {
100: throw new IllegalArgumentException(
101: "Parameter source cannot be null"); // NOI18N
102: }
103: for (MultipleRootsUnitTestForSourceQueryImplementation query : mrImplementations
104: .allInstances()) {
105: URL[] urls = query.findUnitTests(source);
106: if (urls != null) {
107: return urls;
108: }
109: }
110: for (org.netbeans.modules.gsfpath.spi.queries.UnitTestForSourceQueryImplementation query : implementations
111: .allInstances()) {
112: URL u = query.findUnitTest(source);
113: if (u != null) {
114: return new URL[] { u };
115: }
116: }
117: return new URL[0];
118: }
119:
120: /**
121: * Returns the source root for a given test root.
122: *
123: * @param unitTest Java package root with unit tests
124: * @return corresponding Java package root with sources. It can be null
125: * when the mapping from unit test to source is not known.
126: * @deprecated Use {@link #findSources} instead.
127: */
128: public static URL findSource(FileObject unitTest) {
129: URL[] result = findSources(unitTest);
130: return result.length == 0 ? null : result[0];
131: }
132:
133: /**
134: * Returns the source roots for a given test root.
135: *
136: * @param unitTest Java package root with unit tests
137: * @return corresponding Java package roots with sources. It can be an empty array (not null)
138: * when the mapping from unit test to sources is not known.
139: * @since org.netbeans.modules.gsfpath.api/1 1.7
140: */
141: @SuppressWarnings("deprecation")
142: // NOI18N
143: public static URL[] findSources(FileObject unitTest) {
144: if (unitTest == null) {
145: throw new IllegalArgumentException(
146: "Parameter unitTest cannot be null"); // NOI18N
147: }
148: for (MultipleRootsUnitTestForSourceQueryImplementation query : mrImplementations
149: .allInstances()) {
150: URL[] urls = query.findSources(unitTest);
151: if (urls != null) {
152: return urls;
153: }
154: }
155: for (org.netbeans.modules.gsfpath.spi.queries.UnitTestForSourceQueryImplementation query : implementations
156: .allInstances()) {
157: URL u = query.findSource(unitTest);
158: if (u != null) {
159: return new URL[] { u };
160: }
161: }
162: return new URL[0];
163: }
164:
165: }
|