001: /*
002: *
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
004: *
005: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common
009: * Development and Distribution License("CDDL") (collectively, the
010: * "License"). You may not use this file except in compliance with the
011: * License. You can obtain a copy of the License at
012: * http://www.netbeans.org/cddl-gplv2.html
013: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: * specific language governing permissions and limitations under the
015: * License. When distributing the software, include this License Header
016: * Notice in each file and include the License file at
017: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: * particular file as subject to the "Classpath" exception as provided
019: * by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the
021: * License Header, with the fields enclosed by brackets [] replaced by
022: * your own identifying information:
023: * "Portions Copyrighted [year] [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * The Original Software is NetBeans. The Initial Developer of the Original
028: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
029: * Microsystems, Inc. All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041: */
042:
043: /*
044: * JUnitTestRunnerLauncher.java
045: *
046: * Created on October 20, 2003, 6:28 PM
047: */
048:
049: package org.netbeans.xtest.plugin.jvm;
050:
051: import java.io.File;
052: import java.io.FileOutputStream;
053: import java.io.PrintWriter;
054: import java.util.Calendar;
055: import org.netbeans.xtest.testrunner.JUnitTestRunner;
056: import org.netbeans.xtest.util.JNIKill;
057:
058: /**
059: *
060: * @author mb115822
061: */
062: public class JUnitTestRunnerLauncher {
063:
064: /**
065: * @param args the command line arguments
066: */
067: public static void main(String[] args) {
068: // just start the tests
069: System.out.println("VM started - "
070: + Calendar.getInstance().getTime());
071: try {
072: Thread hook = new Thread(new Runnable() {
073: public void run() {
074: System.out
075: .println("ERROR: JVM exiting unexpectedly!");
076: // only 1.5 - can be added when XTest drop support of 1.4
077: //threadDump();
078: }
079: });
080: Runtime.getRuntime().addShutdownHook(hook);
081: initKill();
082: JUnitTestRunner testRunner = new JUnitTestRunner(null,
083: System.out);
084: testRunner.runTests();
085: Runtime.getRuntime().removeShutdownHook(hook);
086: } catch (Throwable t) {
087: System.out
088: .println("Error - during test run caught exception: "
089: + t.getMessage());
090: t.printStackTrace();
091: System.exit(-1);
092: }
093: System.out.println("VM finished");
094: System.exit(0);
095: }
096:
097: /* only 1.5
098: static void threadDump() {
099: System.out.println("Thread dump:");
100: Map<Thread,StackTraceElement[]> all = Thread.getAllStackTraces();
101: for (Map.Entry<Thread,StackTraceElement[]> elem : all.entrySet()) {
102: StringBuffer sb = new StringBuffer();
103: sb.append("Thread: ");
104: sb.append(elem.getKey().getName()).append('\n');
105: for (StackTraceElement e : elem.getValue()) {
106: sb.append(" ").append(e.getClassName()).append('.').append(e.getMethodName())
107: .append(':').append(e.getLineNumber()).append('\n');
108: }
109: System.out.println(sb);
110: }
111: }
112: */
113:
114: /** Initialize JNIKill library, starts dump thread and store PID of
115: * current process into ${xtest.workdir}/jvm.pid file. If timeout expires,
116: * process is killed by JVMExecuteWatchdog.
117: */
118: static void initKill() {
119: String workdir = System.getProperty("xtest.workdir");
120: if (workdir != null) {
121: File jvmPID = new File(workdir, "jvm.pid");
122: PrintWriter writer = null;
123: try {
124: jvmPID.createNewFile();
125: writer = new PrintWriter(new FileOutputStream(jvmPID));
126: // get my pid
127: JNIKill kill = new JNIKill();
128: if (kill.startDumpThread()) {
129: System.out
130: .println("JVM dump thread succesfully started.");
131: }
132: long myPID = kill.getMyPID();
133: // write it out to a file
134: System.out.println("JVM is running under PID:" + myPID);
135: writer.println(myPID);
136: } catch (Throwable e) {
137: System.out.println("There was a problem: " + e);
138: } finally {
139: if (writer != null) {
140: writer.close();
141: }
142: }
143: } else {
144: System.out
145: .println("Cannot get xtest.workdir property - it has to be set to a valid working directory.");
146: }
147: }
148: }
|