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.xtest.testrunner;
043:
044: import java.io.*;
045:
046: /**
047: * Class which controls the execution from outer space - it runs
048: * in the same VM as harness and is reponsible for launching tested products
049: *
050: * @author mb115822
051: */
052: public class TestRunnerHarness {
053:
054: // whole testbag is run in a single VM instance
055: public static final String TESTRUN_MODE_TESTBAG = "testbag";
056: // each test class (testsuite) is run in a single VM instance
057: public static final String TESTRUN_MODE_TESTSUITE = "testsuite";
058: // all tests are run internally (in the same VM as the class which starts the test run)
059: public static final String TESTRUN_MODE_CURRENT_VM = "currentVM";
060:
061: public static final String TESTLIST_FILENAME = "testrunnerharness.testlist";
062:
063: private static final boolean DEBUG = false;
064:
065: private JUnitTestRunnerProperties testList;
066: private TestBoardLauncher launcher;
067: private File workDir;
068: private String testMode;
069:
070: /** Creates a new instance of TestRunnerHarness */
071: public TestRunnerHarness(TestBoardLauncher boardLauncher,
072: File workDir, String testMode) throws IOException {
073: this .launcher = boardLauncher;
074: this .workDir = workDir;
075: this .testList = loadJUnitTestRunnerProperties(workDir);
076: this .testMode = testMode;
077: }
078:
079: // loads test runner properties from file available at ${xtest.work}/
080: public static JUnitTestRunnerProperties loadJUnitTestRunnerProperties(
081: File workDir) throws IOException {
082: File testRunnerPropertyFile = new File(workDir,
083: TESTLIST_FILENAME);
084: return JUnitTestRunnerProperties.load(testRunnerPropertyFile);
085: }
086:
087: public void runTests() {
088: if (DEBUG)
089: System.out.println("TestRunnerHarness : " + this );
090: if (TESTRUN_MODE_TESTBAG.equalsIgnoreCase(testMode)) {
091: // testbag mode
092: launchTests(testList);
093: } else if (TESTRUN_MODE_TESTSUITE.equalsIgnoreCase(testMode)) {
094: // testsuite mode
095: JUnitTestRunnerProperties[] dividedTestLists = testList
096: .divideByTests();
097: if (DEBUG)
098: System.out.println("Divided to "
099: + dividedTestLists.length + " lists");
100: for (int i = 0; i < dividedTestLists.length; i++) {
101: if (DEBUG)
102: System.out.println("List " + i + ":"
103: + dividedTestLists[i]);
104: if (DEBUG)
105: dividedTestLists[i].list(System.out);
106: launchTests(dividedTestLists[i]);
107: }
108: if (dividedTestLists.length == 0) {
109: // run empty list just to report it as error in JUnitTestRunner.getTestSuites().
110: launchTests(testList);
111: }
112: } else if (TESTRUN_MODE_CURRENT_VM.equalsIgnoreCase(testMode)) {
113: // same vm mode - not supported right now
114: throw new RuntimeException(
115: "Current VM mode not yet supported by TestRunner");
116: } else {
117: // unupported mode
118: throw new RuntimeException("Unkonwn test run mode :"
119: + testList.getTestRunType());
120: }
121: }
122:
123: public void launchTests(JUnitTestRunnerProperties testList) {
124: try {
125: launcher.launchTestBoard(testList);
126: } catch (TestBoardLauncherException tble) {
127: System.err.println("Cannot start tests. Reson: "
128: + tble.getMessage());
129: }
130: }
131:
132: }
|