servicebasedtestsuite.java.svn-base

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

SVN-BASE
242
字号
/* * $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.j2me.javatest;import java.io.File;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import com.sun.javatest.Harness;import com.sun.javatest.Parameters;import com.sun.javatest.TestEnvironment;import com.sun.javatest.TestFilter;import com.sun.javatest.TestResult;import com.sun.javatest.TestSuite;import com.sun.javatest.util.I18NResourceBundle;import com.sun.tck.j2me.services.LifeCycleAwareManager;import com.sun.tck.j2me.services.ServiceFault;import com.sun.tck.j2me.utils.Utils;/** * A TestSuite which starts and stops services on demand. * Services are managed by delegating to the LifeCycleAwareManager * set with the class. Sub class can simply overwrite createTestSuiteServices() * API to register the services it needs. */public class ServiceBasedTestSuite extends TestSuite {        private LifeCycleAwareManager serviceManager = new LifeCycleAwareManager();    private boolean verboseJavaTest = false;    private Harness harness = null;        private static final I18NResourceBundle i18n =        I18NResourceBundle.getBundleForClass(ServiceBasedTestSuite.class);    private static final Logger log = Logger.getLogger(            ServiceBasedTestSuite.class.getName());        /**     * Observer class which stops the services at the end     * of the test run.     */    final class ServiceObserver implements Harness.Observer {        private ServiceObserver(Harness harness) {            this.harness = harness;        }        public synchronized void finishedTesting() {            try {                stopRun();            } finally {                harness.removeObserver(this);            }        }        // see JTHarness' ISSUE #18: we have to        // override this method as well to perform        // clean-up in exceptional cases        public void error(String msgs) {            try {                stopRun();            } finally {                harness.removeObserver(this);            }        }        public void finishedTest(TestResult tr) {}        public void finishedTestRun(boolean allOK) {}        public synchronized void startingTest(TestResult tr) {}        public void startingTestRun(Parameters params) {}        public void stoppingTestRun() {}        private Harness harness = null;    };    /**     * Name of JavaTest environment variable which designate whether     * verbose output from JavaTest is desired.     */    public static final String VERBOSE = "verboseJavaTest";     public ServiceBasedTestSuite(File root) {        super(root);    }        public ServiceBasedTestSuite(File root, Map tsInfo, ClassLoader cl) throws Fault {        super(root, tsInfo, cl);    }        /**     * Initialize the TestSuite. The default implementation does nothing and silently     * ignores all arguments.     */    protected void init(String[] args) throws Fault {    }    /**     * Creates a list of TestSuiteServices that the TestSuite needs to start and register     * with the LifeCycleAwareManager. The default implementation     * does nothing.     */    protected void createTestSuiteServices(Harness harness, LifeCycleAwareManager manager) {    }        /**     * First, clears the services previously registered with the LifeCycleAwareManager     * instance. Then calls createTestSuiteServices() to create and register services     * and invoke the start() method of the LifeCycleAwareManager instance.     * <p>          * To provide service based functions, it is recommended that sub classes      * override createTestSuiteServices(). Subclasses should call super.startRun() to enable other registered      * services when overriding this method.     */    protected void startRun(Harness harness) throws Fault {        // clear the services to avoid adding duplicated services        serviceManager.clear();        createTestSuiteServices(harness, serviceManager);        boolean exceptionThrown = false;        try {            serviceManager.start();            log.fine("Service manager started");        } catch (ServiceFault fault) {            exceptionThrown = true;            log.fine(fault.getMessage());            throw new TestSuite.Fault(i18n, "ts.serviceManagerFault", fault.getMessage());        } catch (Throwable e) {            exceptionThrown = true;            log.fine(e.getMessage());            throw new TestSuite.Fault(i18n, "ts.errorStart", e.getMessage());        } finally {            if (exceptionThrown) {                try {                    stopRun();                } catch (Throwable e) {                    if (verboseJavaTest) {                        e.printStackTrace();                    }                }            }        }    }    /**     * Stops the service manager which in turn stops all services.     * Subclasses should call <code>super.stopRun()</code> to stop     * registered services when overriding this method.     */    protected void stopRun() {        try {            serviceManager.stop();        } catch (ServiceFault fault) {            log.log(Level.WARNING, "Error while stopping the test run.", fault);        }    }    /**     * Creates a test suite specific filter to be used to filter the tests to be selected      * for a test run. The default is to return an instance of      * com.sun.tck.j2me.javatest.ExprFilter, or null if there are errors creating     * such a filter.     */    public TestFilter createTestFilter(TestEnvironment env) {        try {            return new ExprFilter(env);        } catch (TestFilter.Fault e) {            log.fine("Cannot create TestFilter: " + e.getMessage());            return null;        }    }        /**     * Loads required parameters before services are started. The default     * implementation does nothing.     *     * @param env TestEnvironment which is associated with the TestSuite.     */    protected void loadParameters(TestEnvironment env) throws TestSuite.Fault {    }        /**     * A notification method that is called when a test suite run is starting.     * It does the following:     * <ul>     * <li> load TestSuite parameters from JavaTest environment variables     * <li> call startRun()     * <li> register with the harness the observer which stops the services      *      at the end of the test run via the observer's callback method      *      finishedTesting(), which in turn calls stopRun().     * </ul>     *     * Sub classes generally should not override this method. If it does,     * it must call super.starting() to enable services.     */         public void starting(Harness harness) throws TestSuite.Fault {        super.starting(harness);        this.harness = harness;        TestEnvironment env = harness.getEnv();        loadParameters(env);        verboseJavaTest = Utils.getBooleanFromEnv(env, VERBOSE, false);        startRun(harness);        harness.addObserver(new ServiceObserver(harness));    }    /**     * Get the class loader specified when this test suite object was created.     * This method overrides method of TestSuite class in order to make it     * public.     * @return the class loader specified when this test suite object was created     */    @Override    public ClassLoader getClassLoader() {        return super.getClassLoader();    }    }

⌨️ 快捷键说明

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