tckscript.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 298 行

SVN-BASE
298
字号
/* * $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.tck.javatest;import java.io.PrintWriter;import java.util.Set;import com.sun.javatest.Script;import com.sun.javatest.Status;import com.sun.javatest.TestDescription;import com.sun.javatest.TestEnvironment;import com.sun.javatest.util.I18NResourceBundle;import com.sun.javatest.util.StringArray;/** * Script to execute TCK tests, based on the keywords in the test description. * <p>This Script understands the following keywords: jtonly, negative. * <p> The jtonly tests (see {@link #isJTOnly}) will be executed by * {@link #JTONLY_EXECUTE} command, all others will be executed * by {@link #TEST_EXECUTE} command. If the test is negative * (see {@link #isNegative}), the result will be inverted. */public class TCKScript extends Script {    /**     * Initializes test description.     *     * @param td the test description for the test to be run.     */    public void initTestDescription(TestDescription td) {        keywords = td.getKeywordTable();        super.initTestDescription(td);    }    /**     * Runs the script. This is the primary method to be provided by Scripts.     *     * @param args any script-specific options specified in the script property.     * (Ignored by this implementation).     * @param td the test description for the test to be performed.     * @param env the test environment giving the details of how to run the test.     * @return the result of running the script.     */    public Status run(String[] args, TestDescription td, TestEnvironment env) {        Status err = initContext(td, env);        if (err != null) {            return err;        }        return runTest();    }    /**     * Gets the timeout to be used for a test.     * A value of zero means no timeout.     * <p>Override this method to provide different behaviors.     * <p>This implementation returns <code>"timeout"</code> value from     * the test description scaled by a value found in     * the environment (<code>"javatestTimeoutFactor"</code>).     *     * @return the number of seconds in which the test is expected to     * complete its execution.     */    public int getTestTimeout() {        String s = td.getParameter("timeout");        int t = (s == null ? 10 * 60 : Integer.parseInt(s));        float factor = 1.0f;        try {            String f = env.lookup("javatestTimeoutFactor")[1];            if (f != null) {                factor = Float.parseFloat(f);            }        } catch (TestEnvironment.Fault f) {            // silently ignore. will use default factor        }        return (int) (t * factor);    }    //////////////////////////////////////////////////////////////////////////    /**     * Returns test execute args.     * <p>This implementation extracts the args from the test description     * (parameter <code>"executeArgs"</code>) and adds the context, if present.     *     * @param td test description.     * @return test execute args.     */    protected String getExecuteArgs(TestDescription td) {        String executeArgs = td.getParameter("executeArgs");        if (contextArgs == null) {            return executeArgs;        } else if (executeArgs == null) {            return contextArgs;        } else {            return (contextArgs + ' ' + executeArgs);        }    }    /**     * Initializes test context from the test description and test environment.     *     * @param td test description.     * @param env test environment.     * @return context <code>String</code>.     */    protected Status initContext(TestDescription td, TestEnvironment env) {        // get context info        String[] context = StringArray.split(td.getParameter("context"));        if (context != null) {            StringBuffer sb = new StringBuffer();            for (int i = 0; i < context.length; i++) {                try {                    String[] contextValue = env.lookup(context[i]);                    if (contextValue == null || contextValue.length == 0) {                        return Status.error(                                i18n.getString(                                        "tckScript.initUndefContextError",                                         context[i]));                    }                    if (contextValue.length != 1) {                        return Status.error(                                i18n.getString(                                        "tckScript.initBadContextError",                                        context[i])                                + i18n.getString(                                        "tckScript.initBadContextManyWords",                                        StringArray.join(contextValue)));                    }                    sb.append('-');                    sb.append(context[i]);                    sb.append(' ');                    sb.append(contextValue[0]);                    sb.append(' ');                } catch (TestEnvironment.Fault e) {                    return Status.error(                            i18n.getString(                                    "tckScript.initBadContextError", context[i])                                    + "(" + e.getMessage() + ")");                }            }            contextArgs = sb.toString();        }        return null;    }    /**     * Executes the test.     * <p> This implementation extracts a Class name to be executed     * and it's args from the test description and then delegates actual     * test execution to {@link #executeTest(String, String)}.     * Returned <code>Status</code> will be inverted     * if the test is marked with <code>negative</code> keyword.     *     * @return Status of the test execution, inverted if the test is negative.     */    protected Status runTest() {        PrintWriter trOut = getTestResult().getTestCommentWriter();        // Exercise the test component...        Status executeStatus;        String executeArgs = getExecuteArgs(td);        String executeClass = td.getParameter("executeClass");        trOut.println(i18n.getString("tckScript.execTestClass"));        executeStatus = executeTest(executeClass, executeArgs);        // Test component has been executed; now check the results...        if (isNegative()) {            // ... negative test: check that execution failed--            // swap pass and fail, propogate others            switch (executeStatus.getType()) {            case Status.PASSED:                return fail_execSuccUnexp;            case Status.FAILED:                return pass_execFailExp.augment(executeStatus);            default:                return executeStatus;            }        } else {            // ... positive test: check that code executes on reference platform            return executeStatus;        }    }    /**     * Executes the given test class with the given arguments.     * <p>This implementation invokes <code>JTONLY_EXECUTE</code>     * command to execute "jtonly" tests (as determined by {@link #isJTOnly})     * or <code>TEST_EXECUTE</code> command for all other tests.     *     * @param executeClass the name of the class to be executed.     * @param executeArgs any arguments to be passed to the class     * to be executed.     * @return the <code>Status</code> of the execution.     */    protected Status executeTest(String executeClass, String executeArgs) {        if (isJTOnly()) {            return execute(JTONLY_EXECUTE, executeClass, executeArgs);        } else {            // classic simple runtime test            return execute(TEST_EXECUTE, executeClass, executeArgs);        }    }    /**     * Returns <code>true</code> if the test is negative,     * <code>false</code> otherwise.     * <p> This implementation returns <code>true</code> if the test     * being executed by this <code>Script</code> is marked with     * <code>negative</code> keyword.     * <p> This method can be invoked only after {@link #initTestDescription},     * or <code>Error</code> will be thrown.     *     * @return <code>true</code> if the test is negative,     * <code>false</code> otherwise.     * @throws Error if keywords are <code>null</code>  or     * the method has been invoked before {@link #initTestDescription}.     */    protected boolean isNegative() throws Error {        if (keywords == null) {            throw new Error(i18n.getString("tckScript.isNegativeError"));        }        return keywords.contains("negative");    }    /**     * Returns <code>true</code> if the test is of type jtonly,     * <code>false</code> otherwise.     * <p> This implementation returns <code>true</code> if the test     * being executed by this <code>Script</code> is marked with     * <code>jtOnly</code> keyword.     * <p> This method can be invoked only after {@link #initTestDescription},     * or <code>Error</code> will be thrown.     *     * @return <code>true</code> if the test is of type jtonly,     * <code>false</code> otherwise.     * @throws Error if keywords are <code>null</code> or     * the method has been invoked before {@link #initTestDescription}.     */    protected boolean isJTOnly() throws Error {        if (keywords == null) {            throw new Error(i18n.getString("tckScript.isNegativeError"));        }        return keywords.contains("jtonly");    }    private static I18NResourceBundle i18n =         I18NResourceBundle.getBundleForClass(TCKScript.class);    /**     * Context, determined from context spec in test description,     * and environment.     */    protected String contextArgs;    /**     * Set of keywords from the test description.     */    protected Set keywords;    /**     * Name of a {@link com.sun.javatest.Command}     * that executes tests by default.     */    protected static final String TEST_EXECUTE = "testExecute";    /**     * Name of a {@link com.sun.javatest.Command}     * that executes jtonly tests.     */    protected static final String JTONLY_EXECUTE = "jtOnly";}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?