otatest.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 926 行 · 第 1/3 页

JAVA
926
字号
/* * $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.midp.lib;import java.io.File;import java.io.PrintWriter;import java.util.Hashtable;import java.util.jar.Attributes;import com.sun.javatest.Status;import com.sun.javatest.lib.MultiTest;/** * Base class for OTA tests. Provides various convenience methods and services. */public class OTATest extends MultiTest {    private final String SIGNER = "-signer=";    private final String SIGNER_ARGS = "-signerArgs=";    private String signerClass = null;    private String signerArgs = null;    // TBD Use interactive handler by default    protected OTAHandler handler = null;    protected String otaHandlerClassName = null;    protected String otaHandlerArgs = null;    protected OTASupportServer otaServer = null;    protected String host = null;    protected String port = null;    protected String address = null;    protected File testDir;    protected int timeout = 5000;    protected String authenticationString = null;    protected int jadAttrFlags = 0;    /**     * @deprecated This field is not used anymore and remains     * here for compatibility reasons. Changing the value     * of this field has no effect.     */    protected boolean checkHeaders = false;    /**     * A jar or jad name that should send notification.     */    // Passed to OTASupportServer by *WithNotification method.    protected String name;    /**     * Expected notification code.     */    // Passed to server's corresponding check method by NotifySender.    protected String expectedCode;    /**     * Current operation.     * <ul>     * Defined operations:     * <li> installNegative1 </li>     * <li> installNegative2 </li>     * <li> run </li>     * <li> remove </li>     * </ul>     */    // Set by *WithNotification methods to be used as switch in NotifySender.    protected volatile String operation;    /*  //non-API comment     * Set by NotifySender to:     * Passed     * Failed - no notification     * Failed - wrong notification code for installNegative2.     *     * Returned by *WithNotification     * method id null handlerStatus received.     */    protected volatile Status status = null;    protected void resetNotifications() {        if (otaServer != null) {            otaServer.resetInstallNotification();            otaServer.resetRunNotification();            otaServer.resetRemoveNotification();        }    }    protected int decodeArg( String argv[], int index ) throws SetupException {	if( argv[index].equals( "-httpServerPort" ) ) {	    if ( ++index < argv.length && ! argv[index].startsWith( "-" ) ) {                port = argv[index];	        try {                    int portNum = Integer.parseInt(port);                    if (portNum < 0 || portNum > 0xFFFF)                        throw new SetupException("\"OTA Port\" value out " +                                                 "of range: " + portNum);                } catch (NumberFormatException nfe) {                    throw new SetupException("Non integer passed as " +                                             "\"OTA Port\" value " + port);                }                return 2;	    } else		throw new SetupException( "No server port specified" );	} if( argv[index].equals( "-testDir" ) ) {	    if ( ++index < argv.length && ! argv[index].startsWith( "-" ) ) {                testDir = new File(argv[index]);                return 2;	    } else		throw new SetupException( "No TestDir specified" );	} if( argv[index].equals( "-OTAHandlerClass" ) ) {	    if ( ++index < argv.length && ! argv[index].startsWith( "-" ) ) {                otaHandlerClassName = argv[index];                return 2;	    } else		throw new SetupException( "No OTAHandlerClass specified" );	} if( argv[index].equals( "-OTAHandlerArgs" ) ) {	    if ( ++index < argv.length && ! argv[index].startsWith( "-" ) ) {                otaHandlerArgs = argv[index];                return 2;	    } else		throw new SetupException( "No OTAHandlerArgs specified" );        } else if (argv[index].startsWith(SIGNER)) {            if(argv[index].length() > SIGNER.length()) {                signerClass = argv[index].substring(SIGNER.length());            }            return 1;        } else if (argv[index].startsWith(SIGNER_ARGS)) {            if(argv[index].length() > SIGNER_ARGS.length()) {                signerArgs = argv[index].substring(SIGNER_ARGS.length());            }            return 1;        } else if ( argv[index].equals( "-otaNotificationTimeout" ) ) {	    if ( ++index < argv.length && ! argv[index].startsWith( "-" ) ) {	        try {                    // convert from string to integer and from secs to msecs                    timeout = Integer.parseInt(argv[index]) * 1000;                    if (timeout < 0)                        throw new SetupException("\"OTA Notification " +                                                 "Timeout\" value is less " +                                                 "then 0: " + timeout);                } catch (NumberFormatException nfe) {                    throw new SetupException("Non integer passed as " +                                             "\"OTA Notification " +                                             "Timeout\" value " + timeout);                }                return 2;	    } else		throw new SetupException("No \"OTA Notification Timeout\" specified");        } else if ( argv[index].equals( "-httpHost" ) ) {	    if ( ++index < argv.length && ! argv[index].startsWith( "-" ) ) {                host = argv[index];                return 2;	    } else		throw new SetupException("No \"JavaTest Host Name\" specified");	} else {	    return super.decodeArg( argv, index );	}    }    /**     * Creates and initializes OTA server (invokes {@link #createOtaServer()}     * for that), then initializes signer, starts otaServer, creates and     * initializes otaHandler.     * <p>     * Also, if {@link #getAllowedNames()} returns a non-null value, restricts     * the server to handle GET requests only for allowed file names.     *     * @throws SetupException     *             in case of any problems that prevent successful execution.     * @see #createOtaServer()     * @see #getAllowedNames()     * @see OTASupportServer#setURLs(String[])     */    protected void init() throws SetupException {        otaServer = createOtaServer();        otaServer.init(new String[]{"http.url=http://" + host + ":" + port});        otaServer.setURLs(getAllowedNames());        if (signerClass != null) {            otaServer.initSigner(signerClass, signerArgs);        }        try {            otaServer.start();        } catch (RuntimeException re) {        // catch RuntimeException thrown in BaseHttpServer.start        // instead of caught ioe to implement graceful exit            re.printStackTrace(ref);            throw new SetupException("Can not start OTA Support Server. " + re);        }        // get the correct port name from the OTA server        // (if it is dynamically assigned)        port = "" + otaServer.getPort();        address = "http://" + host + ":" + port;        try {            Class otaHandlerClass = Class.forName(otaHandlerClassName);            handler = (OTAHandler)(otaHandlerClass.newInstance());        } catch (ClassNotFoundException e) {            throw new SetupException("Class not found: " + otaHandlerClassName);        } catch (InstantiationException e) {            throw new SetupException("Class cannot be instantiated: "                     + otaHandlerClassName);        } catch (IllegalAccessException e) {            throw new SetupException("Cannot access class: "                     + otaHandlerClassName);        }        Status s = handler.init(otaHandlerArgs, log);        if (s == null || !s.isPassed()) {            throw new SetupException("Class initialize ota handler. "                     + " Received status " + s);        }    }    /**     * Creates the OTA server to be used in OTA tests. This method is called     * by {@link #init()} and provides an extension point for subclasses.     * <p>     * Subclasses can override this method to provide custom implementations,     * but this option is rarely needed and should be used with care.     *     * @return The OTA server to be used in OTA tests.     */    protected OTASupportServer createOtaServer() {        if (jadAttrFlags != 0) {            return new OTASupportServer(testDir, ref, jadAttrFlags);        } else {            return new OTASupportServer(testDir, ref, authenticationString);        }    }    protected void cleanUp() {        if (otaServer != null) {             otaServer.stop();        }    }    public Status run(String[] argv, PrintWriter log, PrintWriter ref) {        Status s = null;        try {            s = super.run(argv, log, ref);            return s;        } finally {            cleanUp();        }    }    /**     * An extension method that returns a list of URLs, relative to the server     * root. The URLs represent jad and jar files, needed by this test.     * <p>     * This method is invoked by {@link #init()} only once, during server     * initialization, to restrict the server to handle only allowed URLs.     * <p>     * Subclasses may override this method, but this is rarely needed, since     * the standard install methods in this class automatically restrict the     * server URLs. In those rare cases when a test installs applications via     * other means, not invoking install methods from this class, it's always     * possible to use {@link OTASupportServer#setURLs(String[])} directly.     * <p>     * The default value is <code>null</code>.     *     * @see #init()     * @deprecated Use {@link OTASupportServer#setURLs(String[])} directly.     */    protected String[] getAllowedNames() {        return null;    }    protected Hashtable getJadRequestHeaders() {        return otaServer.getJadRequestHeaders();    }

⌨️ 快捷键说明

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