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.io.StringWriter;
047: import java.util.HashMap;
048: import java.util.Map;
049: import org.apache.tools.ant.BuildException;
050: import org.apache.tools.ant.Project;
051: import org.apache.tools.ant.Task;
052: import org.apache.tools.ant.taskdefs.Ant;
053: import org.apache.tools.ant.types.Path;
054:
055: /**
056: * Akin to (a simple subset of) <subant> but failures in subtasks are collected
057: * and optionally sent to a JUnit-format report rather than halting the build.
058: */
059: public class SubAntJUnitReport extends Task {
060:
061: private Path buildPath;
062:
063: public void setBuildPath(Path buildPath) {
064: this .buildPath = buildPath;
065: }
066:
067: private String targetToRun;
068:
069: public void setTarget(String target) {
070: this .targetToRun = target;
071: }
072:
073: private boolean failOnError;
074:
075: public void setFailOnError(boolean failOnError) {
076: this .failOnError = failOnError;
077: }
078:
079: private File report;
080:
081: public void setReport(File report) {
082: this .report = report;
083: }
084:
085: public @Override
086: void execute() throws BuildException {
087: Map<String, String> pseudoTests = new HashMap<String, String>();
088: for (String path : buildPath.list()) {
089: log("Entering: " + path);
090: File dir = new File(path);
091: Ant ant = new Ant(this );
092: ant.init();
093: ant.setTarget(targetToRun);
094: ant.setDir(dir);
095: String msg = null;
096: try {
097: ant.execute();
098: } catch (BuildException x) {
099: if (failOnError) {
100: throw x;
101: } else {
102: msg = x
103: .getMessage()
104: .replaceFirst(
105: "(?s).*The following error occurred while executing this line:\r?\n",
106: "");
107: }
108: } catch (Throwable x) {
109: if (failOnError) {
110: throw new BuildException(x, getLocation());
111: } else {
112: StringWriter sw = new StringWriter();
113: x.printStackTrace(new PrintWriter(sw));
114: msg = sw.toString();
115: }
116: }
117: pseudoTests.put(path, msg);
118: if (msg != null) {
119: log("Failed to build " + path + ": " + msg,
120: Project.MSG_WARN);
121: } else {
122: log("Exiting: " + path);
123: }
124: }
125: // XXX would be nice to permit the 'classname' field to be customized in output...
126: JUnitReportWriter.writeReport(this, report, pseudoTests);
127: }
128:
129: }
|