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.apisupport.project.universe;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.URI;
047: import java.net.URISyntaxException;
048: import java.net.URL;
049: import org.netbeans.api.project.FileOwnerQuery;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.modules.apisupport.project.NbModuleProject;
052: import org.netbeans.modules.apisupport.project.Util;
053: import org.netbeans.spi.project.SubprojectProvider;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056:
057: /**
058: * Representation of jarfile with tests
059: */
060: public final class TestEntry {
061:
062: private static final String JAR_NAME = "tests.jar"; // NOI18N
063: private static final String QA_FUNCTIONAL = "qa-functional"; // NOI18N
064: private static final String UNIT = "unit"; // NOI18N;
065: /** Hardcoded location of testdistribution relatively to nb source root. */
066: private static final String TEST_DIST_DIR = "nbbuild/build/testdist"; // NOI18N;
067: private final String codeNameBase;
068: private final boolean unit;
069: private final String cluster;
070: private final File jarFile;
071:
072: /**
073: * Creates a new instance of TestEntry
074: */
075: private TestEntry(File jarFile, String codeNameBase, boolean unit,
076: String cluster) {
077: this .jarFile = jarFile;
078: this .codeNameBase = codeNameBase;
079: this .unit = unit;
080: this .cluster = cluster;
081:
082: }
083:
084: /**
085: * get TestEntry for jarfile with tests
086: *
087: * @param jarFile input file with tests
088: * @return null when the file is not jarfile with tests
089: */
090: public static TestEntry get(File jarFile) {
091: // testtype/cluster/codenamebase/testsjar
092: String path = jarFile.getPath()
093: .replace(File.separatorChar, '/');
094: if (path.endsWith(JAR_NAME)) {
095: String tokens[] = path.split("/");
096: int len = tokens.length;
097: if (len > 3) {
098: String cnb = tokens[len - 2].replace('-', '.');
099: String cluster = tokens[len - 3];
100: String testType = tokens[len - 4];
101: boolean unit = true;
102: if (!testType.equals(UNIT)) {
103: if (testType.equals(QA_FUNCTIONAL)) {
104: unit = false;
105: } else {
106: return null;
107: }
108: }
109: return new TestEntry(jarFile, cnb, unit, cluster);
110: }
111: }
112: return null;
113: }
114:
115: public String getCodeNameBase() {
116: return codeNameBase;
117: }
118:
119: public boolean isUnit() {
120: return unit;
121: }
122:
123: public String getCluster() {
124: // set default cluster for modules in module suite
125: return (cluster == null) ? "cluster" : cluster; // NOI18N
126: }
127:
128: public File getJarFile() {
129: return jarFile;
130: }
131:
132: /** Get root folder of binary tests distribution.
133: */
134: public File getTestDistRoot() {
135: return getJarFile().getParentFile().getParentFile()
136: .getParentFile().getParentFile();
137: }
138:
139: /** Get source dir with tests.
140: * @return null if source dir was not located
141: */
142: public URL getSrcDir() throws IOException {
143: String nborgPath = getNetBeansOrgPath();
144: if (nborgPath != null) {
145: return new File(getNBRoot(), nborgPath).toURI().toURL();
146: }
147: File prjDir = getTestDistRoot();
148: // find parent when dir was not created
149: while (!prjDir.exists()) {
150: prjDir = prjDir.getParentFile();
151: if (prjDir == null) {
152: // parent doesn't exist
153: return null;
154: }
155: }
156: Project prj = FileOwnerQuery.getOwner(FileUtil
157: .toFileObject(prjDir));
158: if (prj != null) {
159: // ModuleSuite
160: SubprojectProvider subprojects = prj.getLookup().lookup(
161: SubprojectProvider.class);
162: if (subprojects != null) {
163: for (Project p : subprojects.getSubprojects()) {
164: if (p instanceof NbModuleProject) {
165: NbModuleProject nbm = (NbModuleProject) p;
166: if (nbm != null
167: && nbm.getCodeNameBase().equals(
168: getCodeNameBase())) {
169: FileObject file = (isUnit()) ? nbm
170: .getTestSourceDirectory() : nbm
171: .getFunctionalTestSourceDirectory();
172: if (file != null) {
173: return file.getURL();
174: }
175: }
176: }
177: }
178: }
179: }
180: return null;
181: }
182:
183: File getNBRoot() {
184: File rootDir = getTestDistRoot();
185: String path = rootDir.getAbsolutePath().replace(
186: File.separatorChar, '/');
187: File nbroot = null;
188: // hardcoded location of testdistribution relatively to nb source root
189: if (path.endsWith(TEST_DIST_DIR)) {
190: nbroot = rootDir.getParentFile().getParentFile()
191: .getParentFile();
192: }
193: return nbroot;
194: }
195:
196: public String getNetBeansOrgPath() throws IOException {
197: File nbroot = getNBRoot();
198: if (nbroot != null && ModuleList.isNetBeansOrg(nbroot)) {
199: ModuleList list = ModuleList
200: .findOrCreateModuleListFromNetBeansOrgSources(nbroot);
201: ModuleEntry entry = list.getEntry(codeNameBase);
202: if (entry == null) {
203: return null;
204: }
205: return entry.getNetBeansOrgPath() + "/test/"
206: + getTestType() + "/src";
207: }
208: return null;
209: }
210:
211: public String getTestType() {
212: return (isUnit()) ? UNIT : QA_FUNCTIONAL;
213: }
214:
215: /**
216: * Get project for TestEntry
217: * @return null when project was not found
218: */
219: public Project getProject() {
220: try {
221: URL url = getSrcDir();
222: if (url != null) {
223: URI uri = url.toURI();
224: if (uri != null) {
225: return FileOwnerQuery.getOwner(uri);
226: }
227: }
228: } catch (IOException ex) {
229: Util.err.notify(ex);
230: } catch (URISyntaxException ex) {
231: Util.err.notify(ex);
232: }
233: return null;
234: }
235:
236: }
|