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: /*
043: * XTestTask.java
044: * for now using only static configuration hardcoded in this class
045: * This is clearly far from the best solution, but it works for now
046: * configuration should be based on a xml file ...
047: *
048: * Created on 22.9. 2003, 17:04
049: */
050:
051: package org.netbeans.xtest.usertasks;
052:
053: import org.apache.tools.ant.*;
054: import org.apache.tools.ant.types.*;
055: import org.apache.tools.ant.taskdefs.Ant; //import org.apache.tools.ant.taskdefs.Property;
056: import java.io.File;
057:
058: /**
059: * @author mb115822
060: */
061: public class XTestActionTask extends Task {
062:
063: // action names - should be loaded from the config file
064: private static final String ACTION_BUILD_TESTS = "buildTests";
065: private static final String ACTION_RUN_TESTS = "runTests";
066: private static final String ACTION_CLEAN_TESTS = "cleanTests";
067: private static final String ACTION_CLEAN_RESULTS = "cleanResults";
068: /* Deprecated - use clean instead. */
069: private static final String ACTION_CLEAN_ALL = "cleanAll";
070: private static final String ACTION_CLEAN = "clean";
071: private static final String ACTION_VERSION = "version";
072: private static final String ACTION_GENERATE_FAILED_CONFIG = "generateFailedConfig";
073: private static final String ACTION_GENERATE_EXCLUDED_CONFIG = "generateExcludedConfig";
074:
075: // this is not public action
076: private static final String ACTION_PREPARE_TESTS = "prepare-tests";
077:
078: // targets to be executed -> for now all will be executed from
079: // ${xtest.home}/lib/module_harness.xml, but it might change in the future
080: private static final String ACTIONS_SCRIPT_FILENAME = "lib/module_harness.xml";
081:
082: // target names
083: private static final String TARGET_BUILD_TESTS = "compiler-launcher"; // weird naming - should be named buildtests, but
084: // this is used by the deprecated compilers
085: private static final String TARGET_RUN_TESTS = "runtests";
086: private static final String TARGET_CLEAN_TESTS = "cleantests";
087: private static final String TARGET_CLEAN_RESULTS = "cleanresults";
088: /* Deprecated - use clean instead. */
089: private static final String TARGET_CLEAN_ALL = "realclean";
090: private static final String TARGET_CLEAN = "clean";
091: private static final String TARGET_VERSION = "version";
092: private static final String TARGET_GENERATE_FAILED_CONFIG = "generateFailedConfig";
093: private static final String TARGET_GENERATE_EXCLUDED_CONFIG = "generateExcludedConfig";
094:
095: // non-public target
096: private static final String TARGET_PREPARE_TESTS = "prepare-tests";
097:
098: private static final String[][] ACTION_MATRIX = {
099: { ACTION_BUILD_TESTS, TARGET_BUILD_TESTS },
100: { ACTION_RUN_TESTS, TARGET_RUN_TESTS },
101: { ACTION_CLEAN_TESTS, TARGET_CLEAN_TESTS },
102: { ACTION_CLEAN_RESULTS, TARGET_CLEAN_RESULTS },
103: { ACTION_CLEAN_ALL, TARGET_CLEAN_ALL },
104: { ACTION_CLEAN, TARGET_CLEAN },
105: { ACTION_VERSION, TARGET_VERSION },
106: { ACTION_PREPARE_TESTS, TARGET_PREPARE_TESTS },
107: { ACTION_GENERATE_FAILED_CONFIG,
108: TARGET_GENERATE_FAILED_CONFIG },
109: { ACTION_GENERATE_EXCLUDED_CONFIG,
110: TARGET_GENERATE_EXCLUDED_CONFIG } };
111:
112: private static String getTargetForAction(String action) {
113: for (int i = 0; i < ACTION_MATRIX.length; i++) {
114: if (ACTION_MATRIX[i][0].equalsIgnoreCase(action)) {
115: return ACTION_MATRIX[i][1];
116: }
117: }
118: return null;
119: }
120:
121: private String xtestHome;
122: private static final String XTEST_HOME_PROPERTY_NAME = "xtest.home";
123:
124: private String executeAction;
125:
126: public void setXTestHome(String xtestHome) {
127: this .xtestHome = xtestHome;
128: }
129:
130: public void setExecuteAction(String executeAction) {
131: this .executeAction = executeAction;
132: }
133:
134: private void setXTestHome() throws BuildException {
135: if (this .getProject().getProperty(XTEST_HOME_PROPERTY_NAME) == null) {
136: if (this .xtestHome != null) {
137: this .getProject().setProperty(XTEST_HOME_PROPERTY_NAME,
138: xtestHome);
139: } else {
140: throw new BuildException("Cannot continue - "
141: + XTEST_HOME_PROPERTY_NAME + " is not set");
142: }
143: } else {
144: xtestHome = this .getProject().getProperty(
145: XTEST_HOME_PROPERTY_NAME);
146: }
147: }
148:
149: private void executeAction() throws BuildException {
150: String target = getTargetForAction(executeAction);
151: if (target != null) {
152: File antFile = (new File(new File(xtestHome),
153: ACTIONS_SCRIPT_FILENAME));
154: Ant newAnt = new Ant();
155: newAnt.setProject(this .getProject());
156: newAnt.setOwningTarget(this .getOwningTarget());
157: newAnt.setAntfile(antFile.getAbsolutePath());
158: newAnt.setTarget(target);
159: log("XTest will execute action " + executeAction);
160: // For tests run from binary tests distribution store output messages to a log file
161: // (see nbbuild/templates/xtest.xml#runtestsdist)
162: if (executeAction.equals(ACTION_RUN_TESTS)
163: && getProject().getProperty("xtest.distexec") != null) {
164: newAnt.setOutput(getLogFile());
165: }
166: newAnt.execute();
167: } else {
168: throw new BuildException("Cannot execute unknown action '"
169: + executeAction + "'");
170: }
171: }
172:
173: /** Returns path to log file composed from module name the following way
174: * ${xtest.results.testrun.dir}/logs/${xtest.module}_{xtest.testtype}.log
175: * (e.g. results/testrun_070306-115318/logs/ant_unit.log).
176: * @return path to log file.
177: */
178: private String getLogFile() {
179: String logsDir = getProject().getProperty(
180: "xtest.results.testrun.dir")
181: + "/logs";
182: File logsDirFile = getProject().resolveFile(logsDir);
183: log("logsDirFile=" + logsDirFile, Project.MSG_DEBUG);
184: if (!logsDirFile.exists()) {
185: logsDirFile.mkdirs();
186: }
187: String prefix = getProject().getProperty("xtest.module") + "_"
188: + getProject().getProperty("xtest.testtype");
189: log("prefix=" + prefix, Project.MSG_DEBUG);
190: String newPrefix = prefix.replace('/', '_');
191: File logFile = new File(logsDirFile, newPrefix + ".log");
192: int c = 1;
193: while (logFile.exists()) {
194: logFile = new File(logsDirFile, newPrefix + "_" + c
195: + ".log");
196: c++;
197: }
198: log("Output logFile=" + logFile, Project.MSG_VERBOSE);
199: return logFile.getAbsolutePath();
200: }
201:
202: public void execute() throws BuildException {
203: setXTestHome();
204: executeAction();
205: }
206:
207: }
|