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-2007 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.nbbuild;
043:
044: import java.io.File;
045: import java.io.PrintWriter;
046: import java.util.Hashtable;
047: import java.util.Map;
048: import java.util.SortedMap;
049: import java.util.SortedSet;
050: import java.util.TreeMap;
051: import java.util.TreeSet;
052: import org.apache.tools.ant.BuildException;
053: import org.apache.tools.ant.Task;
054: import org.w3c.dom.Document;
055: import org.w3c.dom.Element;
056: import org.xml.sax.InputSource;
057:
058: /**
059: * Produces a list of all compile-time dependencies between unit tests of modules.
060: */
061: public class ModuleTestDependencies extends Task {
062:
063: private File output;
064:
065: /**
066: * Configures the output file.
067: * @param output a file to generate
068: */
069: public void setOutput(File output) {
070: this .output = output;
071: }
072:
073: /**
074: * Runs the task.
075: * @throws BuildException for the usual reasons
076: */
077: public @Override
078: void execute() throws BuildException {
079: try {
080: @SuppressWarnings("unchecked")
081: Hashtable<String, String> props = getProject()
082: .getProperties();
083: ModuleListParser mlp = new ModuleListParser(props,
084: ParseProjectXml.TYPE_NB_ORG, getProject());
085: SortedMap<String, SortedSet<String>> deps = new TreeMap<String, SortedSet<String>>();
086: File nball = new File(props.get("nb_all"));
087: for (ModuleListParser.Entry entry : mlp.findAll()) {
088: String myCnb = entry.getCnb();
089: String myCluster = entry.getClusterName().replaceFirst(
090: "\\d+$", "");
091: if (myCluster.equals("extra")) {
092: continue;
093: }
094: String myCnbAndCluster = myCnb + " (" + myCluster + ")";
095: File projectXml = new File(
096: nball,
097: (entry.getNetbeansOrgPath() + "/nbproject/project.xml")
098: .replace('/', File.separatorChar));
099: Document pDoc = XMLUtil.parse(new InputSource(
100: projectXml.toURI().toString()), false, true, /*XXX*/
101: null, null);
102: Element config = getConfig(projectXml, pDoc);
103: Element td = ParseProjectXml.findNBMElement(config,
104: "test-dependencies");
105: if (td != null) {
106: for (Element depGroup : XMLUtil.findSubElements(td)) {
107: String testType = ParseProjectXml
108: .findTextOrNull(depGroup, "name");
109: if (testType != null
110: && !testType
111: .equals(ParseProjectXml.TestDeps.UNIT)) {
112: continue;
113: }
114: for (Element dep : XMLUtil
115: .findSubElements(depGroup)) {
116: if (ParseProjectXml.findNBMElement(dep,
117: "test") == null) {
118: continue;
119: }
120: if (ParseProjectXml.findNBMElement(dep,
121: "compile-dependency") == null) {
122: continue;
123: }
124: String targetCnb = ParseProjectXml
125: .findTextOrNull(dep,
126: "code-name-base");
127: if (targetCnb.equals(myCnb)) {
128: continue;
129: }
130: SortedSet<String> target = deps
131: .get(myCnbAndCluster);
132: if (target == null) {
133: target = new TreeSet<String>();
134: deps.put(myCnbAndCluster, target);
135: }
136: String targetCluster = mlp
137: .findByCodeNameBase(targetCnb)
138: .getClusterName().replaceFirst(
139: "\\d+$", "");
140: target.add(targetCnb + " (" + targetCluster
141: + ")");
142: }
143: }
144: }
145: }
146: log("Generating test dependencies to " + output);
147: PrintWriter pw = new PrintWriter(output);
148: for (Map.Entry<String, SortedSet<String>> entry : deps
149: .entrySet()) {
150: pw.printf("MODULE %s\n", entry.getKey());
151: for (String dep : entry.getValue()) {
152: pw.printf(" REQUIRES %s\n", dep);
153: }
154: }
155: pw.flush();
156: pw.close();
157: } catch (Exception x) {
158: throw new BuildException(x);
159: }
160: }
161:
162: private Element getConfig(File projectXml, Document pDoc)
163: throws BuildException {
164: Element e = pDoc.getDocumentElement();
165: Element c = XMLUtil.findElement(e, "configuration",
166: ParseProjectXml.PROJECT_NS);
167: if (c == null) {
168: throw new BuildException("No <configuration>",
169: getLocation());
170: }
171: Element d = ParseProjectXml.findNBMElement(c, "data");
172: if (d == null) {
173: throw new BuildException("No <data> in " + projectXml,
174: getLocation());
175: }
176: return d;
177: }
178:
179: }
|