1 /* 2 * The contents of this file are subject to the terms of the Common Development 3 * and Distribution License (the License). You may not use this file except in 4 * compliance with the License. 5 * 6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html 7 * or http://www.netbeans.org/cddl.txt. 8 * 9 * When distributing Covered Code, include this CDDL Header Notice in each file 10 * and include the License file at http://www.netbeans.org/cddl.txt. 11 * If applicable, add the following below the CDDL Header, with the fields 12 * enclosed by brackets [] replaced by your own identifying information: 13 * "Portions Copyrighted [year] [name of copyright owner]" 14 * 15 * The Original Software is NetBeans. The Initial Developer of the Original 16 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun 17 * Microsystems, Inc. All Rights Reserved. 18 */ 19 20 package org.netbeans.api.java.queries; 21 22 import java.net.URL; 23 import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation; 24 import org.openide.filesystems.FileObject; 25 import org.openide.util.Lookup; 26 27 28 /** 29 * Query to find Java package root of unit tests for Java package root of 30 * sources and vice versa. 31 * 32 * @see org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation 33 * @author David Konecny, Tomas Zezula 34 * @since org.netbeans.api.java/1 1.4 35 */ 36 public class UnitTestForSourceQuery { 37 38 @SuppressWarnings ("deprecation") // NOI18N 39 private static final Lookup.Result<? extends org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation> implementations = 40 Lookup.getDefault().lookupResult(org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation.class); 41 private static final Lookup.Result<? extends MultipleRootsUnitTestForSourceQueryImplementation> mrImplementations = 42 Lookup.getDefault().lookupResult(MultipleRootsUnitTestForSourceQueryImplementation.class); 43 44 private UnitTestForSourceQuery() { 45 } 46 47 /** 48 * Returns the test root for a given source root. 49 * 50 * @param source Java package root with sources 51 * @return corresponding Java package root with unit tests. The 52 * returned URL does not have to point to existing file. It can be null 53 * when the mapping from source to unit test is not known. 54 * @deprecated Use {@link #findUnitTests} instead. 55 */ 56 public static URL findUnitTest(FileObject source) { 57 URL[] result = findUnitTests (source); 58 return result.length == 0 ? null : result[0]; 59 } 60 61 62 /** 63 * Returns the test roots for a given source root. 64 * 65 * @param source Java package root with sources 66 * @return corresponding Java package roots with unit tests. The 67 * returned URLs do not have to point to existing files. It can be an empty 68 * array (but not null) when the mapping from source to unit tests is not known. 69 * @since org.netbeans.api.java/1 1.7 70 */ 71 @SuppressWarnings ("deprecation") // NOI18N 72 public static URL[] findUnitTests(FileObject source) { 73 if (source == null) { 74 throw new IllegalArgumentException("Parameter source cannot be null"); // NOI18N 75 } 76 for (MultipleRootsUnitTestForSourceQueryImplementation query : mrImplementations.allInstances()) { 77 URL[] urls = query.findUnitTests(source); 78 if (urls != null) { 79 return urls; 80 } 81 } 82 for (org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation query : implementations.allInstances()) { 83 URL u = query.findUnitTest(source); 84 if (u != null) { 85 return new URL[] {u}; 86 } 87 } 88 return new URL[0]; 89 } 90 91 /** 92 * Returns the source root for a given test root. 93 * 94 * @param unitTest Java package root with unit tests 95 * @return corresponding Java package root with sources. It can be null 96 * when the mapping from unit test to source is not known. 97 * @deprecated Use {@link #findSources} instead. 98 */ 99 public static URL findSource(FileObject unitTest) { 100 URL[] result = findSources (unitTest); 101 return result.length == 0 ? null : result[0]; 102 } 103 104 /** 105 * Returns the source roots for a given test root. 106 * 107 * @param unitTest Java package root with unit tests 108 * @return corresponding Java package roots with sources. It can be an empty array (not null) 109 * when the mapping from unit test to sources is not known. 110 * @since org.netbeans.api.java/1 1.7 111 */ 112 @SuppressWarnings ("deprecation") // NOI18N 113 public static URL[] findSources (FileObject unitTest) { 114 if (unitTest == null) { 115 throw new IllegalArgumentException("Parameter unitTest cannot be null"); // NOI18N 116 } 117 for (MultipleRootsUnitTestForSourceQueryImplementation query : mrImplementations.allInstances()) { 118 URL[] urls = query.findSources(unitTest); 119 if (urls != null) { 120 return urls; 121 } 122 } 123 for (org.netbeans.spi.java.queries.UnitTestForSourceQueryImplementation query : implementations.allInstances()) { 124 URL u = query.findSource(unitTest); 125 if (u != null) { 126 return new URL[] {u}; 127 } 128 } 129 return new URL[0]; 130 } 131 132 } 133