execjcktestsamejvmcmd.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 466 行
SVN-BASE
466 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.jck.lib;import java.io.File;import java.io.OutputStreamWriter;import java.io.PrintStream;import java.io.PrintWriter;import java.io.Writer;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import com.sun.javatest.Command;import com.sun.javatest.Status;import com.sun.javatest.Test;import com.sun.javatest.util.DirectoryClassLoader;import com.sun.javatest.util.WriterStream;// NOTE: This class also uses (explicitly) the following classes// javasoft.sqe.harness.Status// javasoft.sqe.harness.Test// javasoft.sqe.javatest.Status// javasoft.sqe.javatest.Test/** * ExecJCKTestSameJVMCmd executes a JCK or TCK test in the same Java Virtual * Machine as the caller of this class. * * <p> JCK or TCK tests may be "standard" tests, which implement the Test * interface, and which return a result via the value returned from the run method, * or they may be a "simple" which implements a standard simple method * which an integer return code. * * <p> It can use either a private class loader or the system class loader. * A private class loader will be created if the -loadDir option is given; * otherwise the system class loader will be used. A private class * loader minimizes the interference between tests, but you may be * restricted from using private class loaders if you are running the * harness inside a web browser. * * <p> If the the <code>-repeat</code> option is provided, then the test will be * run multiple times in the same JVM. <code>Status.error()</code> will be * returned (and the remainder of the iterations will not be performed) if any * repetition of the test returns an error, or if the status return type changes * between iterations. The returned status after each iteration will be * included in the log. If this option is not given, the test will be run once. * */public class ExecJCKTestSameJVMCmd extends Command{ /** * Constructs a new ExecJCKTestSameJVMCmd. */ public ExecJCKTestSameJVMCmd() { } /** * Entry point for stand-alone debugging. * @param args Array of arguments for stand-alone debugging. */ public static void main(String[] args) { Command c = new ExecJCKTestSameJVMCmd(); PrintWriter log = new PrintWriter(new OutputStreamWriter(System.err)); PrintWriter ref = new PrintWriter(new OutputStreamWriter(System.out)); Status s = c.run(args, log, ref); log.flush(); ref.flush(); s.exit(); } /** * The method that that does the work of the command. * @param args [-loadDir <em>dir</em>] <em>executeClass</em> <em>executeArgs</em>. * @param log A stream to which to report messages and errors. * @param ref A stream to which to write reference output. * @return The result of the command. */ public Status run(String[] args, PrintWriter log, PrintWriter ref) { int repeat = 1; boolean finalizeWhenDone = false; String className = null; String[] executeArgs = { }; ClassLoader loader = getClassLoader(); int i = 0; for (; i < args.length && args[i].startsWith("-"); i++) { if ("-loadDir".equalsIgnoreCase(args[i]) && i+1 < args.length) { // -loadDir is optional; if given, a new class loader will // be created to load the class to execute; if not given, // the system class loader will be used. loader = new DirectoryClassLoader(new File(args[++i])); } else if ("-repeat".equalsIgnoreCase(args[i]) && i+1 < args.length) { // -repeat is optional; if given, the test will be run that // number of times (in the same JVM) try { if ((repeat = Integer.parseInt(args[++i])) < 1) return Status.error("Unexpected number of repetitions: " + repeat); } catch (NumberFormatException e) { return Status.error("Unrecognized number of repetitions: " + repeat); } } else if ("-finalizeWhenDone".equalsIgnoreCase(args[i])) { finalizeWhenDone = true; } else throw new Error("unrecognized argument to " + getClass().getName() + ": " + args[i]); } // Next must come the executeClass if (i < args.length) { className = args[i]; i++; } else return Status.failed("No executeClass specified"); // Finally, any optional args if (i < args.length) { executeArgs = new String[args.length - i]; System.arraycopy(args, i, executeArgs, 0, executeArgs.length); } try { return execute(loader, className, executeArgs, log, ref, repeat); } finally { if (finalizeWhenDone) { loader = null; System.gc(); System.runFinalization(); } } } private Status execute(ClassLoader loader, String className, String[] executeArgs, PrintWriter log, PrintWriter ref, int repeat) { Class testClass; try { if (loader == null) testClass = Class.forName(className); else testClass = loader.loadClass(className); } catch (ClassNotFoundException e) { // should really be Status.error, but left as Status.failed for // benefit of negative tests return Status.failed("Can't load test: " + e); } catch (VerifyError e) { // should really be Status.error, but left as Status.failed for // benefit of negative tests return Status.failed("Class verification error " + "while trying to load test class \"" + className + "\": " + e); } catch (LinkageError e) { // should really be Status.error, but left as Status.failed // for benefit of negative tests return Status.failed("Class linking error while" + " trying to load test class \"" + className + "\": " + e); } catch (ThreadDeath e) { throw (ThreadDeath)(e.fillInStackTrace()); } catch (Throwable e) { String t = (e instanceof Exception ? "exception" : e instanceof Error ? "error" : "throwable"); PrintStream ps = Deprecated.createPrintStream(new WriterStream(log)); e.printStackTrace(ps); ps.close(); return Status.error("Unexpected " + t + " trying to load test class \"" + className + "\": " + e); } // Evaluate type of test outside main repetition loop Test t; // checks sorted by likely frequency of success in JCK if (Version2Test.accepts(testClass)) { // javasoft.sqe.javatest.Test t = new Version2Test(testClass); } else if (Version1Test.accepts(testClass)) { // javasoft.sqe.harness.Test t = new Version1Test(testClass); } else if (SimpleTest.accepts(testClass)) { // static int run(String[], PrintStream) t = new SimpleTest(testClass); } else if (Version3Test.accepts(testClass)) { // com.sun.javatest.Test t = new Version3Test(testClass); } else { return Status.error("Test class not recognized: \"" + className + "\""); } // execute test, possible repeatedly Status prevStatus = null; for (int j = 0; j < repeat; j++) { if (repeat > 1) log.println("iteration: " + (j+1)); Status currStatus = t.run(executeArgs, log, ref); if (repeat > 1) log.println(" " + currStatus); if ((prevStatus != null) && (currStatus.getType() != prevStatus.getType())) currStatus = Status.error("Return status type " + "changed at repetition: " + (j+1)); if (!currStatus.isError()) prevStatus = currStatus; else return currStatus; } return prevStatus; } private static class SimpleTest implements Test { static boolean accepts(Class testClass) { try { testClass.getMethod("run", runParamTypes); return true; } catch (Throwable e) { return false; } } SimpleTest(Class testClass) { this.testClass = testClass; try { runMethod = testClass.getMethod("run", runParamTypes); } catch (Throwable e) { throw new IllegalArgumentException(); } } public Status run(String[] executeArgs, PrintWriter log, PrintWriter ref) { try { Object[] runArgs = {executeArgs, Deprecated.createPrintStream( new WriterStream(log))}; Object result = runMethod.invoke(null, runArgs); if (false) log.println("result: " + result); switch (((Integer)result).intValue()) { case 0: return Status.passed(""); case 2: return Status.failed(""); default: return Status.failed("Unexpected exit code: " + result); } } catch (ClassCastException e) { return Status.failed("\"run\" method did not " + "return an integer result"); } catch (IllegalAccessException e) { return Status.failed("Illegal access to test: " + e); } catch (InvocationTargetException e) { PrintStream ps = Deprecated.createPrintStream( new WriterStream(log)); e.getTargetException().printStackTrace(ps); ps.close(); return Status.failed("Exception thrown from " + "\"run\" method: " + e.getTargetException()); } catch (ThreadDeath e) { throw (ThreadDeath)(e.fillInStackTrace()); } catch (Throwable e) { String tt = (e instanceof Exception ? "exception" : e instanceof Error ? "error" : "throwable"); PrintStream ps = Deprecated.createPrintStream( new WriterStream(log)); e.printStackTrace(ps); ps.close(); return Status.error("Unexpected " + tt + " trying to invoke \"run\" method in \"" + testClass.getName() + "\": " + e); } } private Class testClass; private Method runMethod; private static Class[] runParamTypes = {String[].class, PrintStream.class}; } private static abstract class StandardTest implements Test { StandardTest(Class testClass) { this.testClass = testClass; } public Status run(String[] executeArgs, PrintWriter log, PrintWriter ref) { // instantiate test object, and handle all likely errors try { testObject = testClass.newInstance(); } catch (InstantiationException e) { return Status.error("Can't instantiate test: " + e); } catch (IllegalAccessException e) { return Status.error("Illegal access to test: " + e); } catch (ThreadDeath e) { throw (ThreadDeath)(e.fillInStackTrace()); } catch (VerifyError e) { return Status.failed("Class verification error " + "trying to instantiate test class \"" + testClass.getName() + "\": " + e); } catch (LinkageError e) { return Status.failed("Class linking error trying " + "to instantiate test class \"" + testClass.getName() + "\": " + e); } catch (Throwable e) { String tt = (e instanceof Exception ? "exception" : e instanceof Error ? "error" : "throwable"); PrintStream ps = Deprecated.createPrintStream(new WriterStream(log)); e.printStackTrace(ps); ps.close(); return Status.error("Unexpected " + tt + " trying to instantiate test class \"" + testClass.getName() + "\": " + e); } // execute test object, and handle all likely errors try { return execute(executeArgs, log, ref); } catch (ThreadDeath e) { throw (ThreadDeath)(e.fillInStackTrace()); } catch (VerifyError e) { return Status.failed("Class verification error " + "trying to execute test class \"" + testClass.getName() + "\": " + e); } catch (LinkageError e) { return Status.failed("Class linking error trying " + "to execute test class \"" + testClass.getName() + "\": " + e); } catch (Throwable e) { String tt = (e instanceof Exception ? "exception" : e instanceof Error ? "error" : "throwable"); PrintStream ps = Deprecated.createPrintStream( new WriterStream(log)); e.printStackTrace(ps); ps.close(); return Status.error("Unexpected " + tt + " trying to execute test class \"" + testClass.getName() + "\": " + e); } } abstract Status execute(String[] executeArgs, PrintWriter log, PrintWriter ref); protected Class testClass; protected Object testObject; } private static class Version1Test extends StandardTest { static boolean accepts(Class c) { return (javasoft.sqe.harness.Test.class.isAssignableFrom(c)); } Version1Test(Class c) { super(c); } Status execute(String[] executeArgs, PrintWriter log, PrintWriter ref) { javasoft.sqe.harness.Test t = (javasoft.sqe.harness.Test) testObject; javasoft.sqe.harness.Status s = t.run(executeArgs, Deprecated.createPrintStream(new WriterStream(log)), Deprecated.createPrintStream(new WriterStream(ref))); return new Status(s.getType(), s.getReason()); } } private static class Version2Test extends StandardTest { static boolean accepts(Class c) { return (javasoft.sqe.javatest.Test.class.isAssignableFrom(c)); } Version2Test(Class c) { super(c); } Status execute(String[] executeArgs, PrintWriter log, PrintWriter ref) { javasoft.sqe.javatest.Test t = (javasoft.sqe.javatest.Test) testObject; javasoft.sqe.javatest.Status s = t.run(executeArgs, log, ref); return new Status(s.getType(), s.getReason()); } } private static class Version3Test extends StandardTest { static boolean accepts(Class c) { return (com.sun.javatest.Test.class.isAssignableFrom(c)); } Version3Test(Class c) { super(c); } Status execute(String[] executeArgs, PrintWriter log, PrintWriter ref) { com.sun.javatest.Test t = (com.sun.javatest.Test) testObject; return t.run(executeArgs, log, ref); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?