generictestprovider.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 401 行

JAVA
401
字号
/* * $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.cldc.communication;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import com.sun.cldc.communication.TestProvider;import com.sun.javatest.util.Timer;public class GenericTestProvider implements TestProvider {         public GenericTestProvider(String main, String jarpath) {        appMainClass = main;        jarSourceDir = jarpath;        if (appMainClass == null || jarSourceDir == null) {            throw new NullPointerException();        }    }    /**     * Returns main application class for this test provider.     * The name is the same for all applications generated     * by this test provider.      */    public String getAppMainClass() {        return appMainClass;    }    /**     * Returns the directory in which application jar files     * are stored.     */    public String getJarSourceDirectory() {        return jarSourceDir;    }    /**     * Reads next test.     * Returns null if all tests from this application have already     * been executed. In other words, null is an exit signal to the      * test runner application.     *     * This method has the same effect as getNextTest("")     */    public synchronized byte[] getNextTest() {        return getNextTest("");    }    /**     * Reads next test in the specified bundle.     * Returns null if all tests from this application have already     * been executed. In other words, null is an exit signal to the      * test runner application.     */    public synchronized byte[] getNextTest(String bundleId) {        if (bundleId.equals("")) {            Enumeration e = executingBundleTable.keys();             if (e.hasMoreElements()) {                bundleId = (String)e.nextElement();             } else {                System.err.println("Out of sync!!! Unexpected getNextTest.");                return null;            }        }         TestBundle executingBundle = (TestBundle)executingBundleTable.get(bundleId);            byte[] executingTest = null;        if (executingBundle == null) {            System.err.println("Out of sync!!! "                + "Received getNextTest, expected getNextApp");            return null;        }        if (executingTestTable.containsKey(bundleId)) {            if (verbose) {                System.out.println(verboseId + ": duplicated getNextTest, bundle " + executingBundle.getApp());            }            if (executingBundle.isInterrupted()) {                 executingBundle.finishing();                executingBundleTable.remove(bundleId);                executingTestTable.remove(bundleId);                executingTest = null;                if (verbose) {                    System.out.print(verboseId + ": getNextTest, all tests in the bundle have been executed, finished (interrupted)");                    executingBundle.printAppName();                }            } else {                        executingTest = (byte[])executingTestTable.get(bundleId);                if (verbose) {                    System.out.print(verboseId + ": getNextTest, executing " + executingBundle.getApp() + ", current test is ");                    executingBundle.printTestName(executingTest);                }           }           return executingTest;        }        executingTest = executingBundle.getNextTest();        if (executingTest == null) { //all tests have already been executed            executingBundle.finishing();            executingBundleTable.remove(bundleId);            executingTestTable.remove(bundleId);            if (verbose) {                System.out.print(verboseId + ": getNextTest, all tests in the bundle have been executed, finished ");                executingBundle.printAppName();            }        } else {            executingTestTable.put(bundleId, executingTest);            if (verbose) {                System.out.print(verboseId + ": getNextTest, executing " + executingBundle.getApp() + ", next test is ");                executingBundle.printTestName(executingTest);            }        }        return executingTest;    }    /**     * Sends test result.     *     * This method has the same effect as sendTestResult(res, "")     */    public synchronized void sendTestResult(byte[] res) {        sendTestResult(res, "");    }        /**     * Sends result of the test in the specified bundle.     */    public synchronized void sendTestResult(byte[] res, String bundleId) {        if (bundleId.equals("")) {            Enumeration e = executingBundleTable.keys();             if (e.hasMoreElements())                bundleId = (String)e.nextElement();             else {                System.err.println("Out of sync!!! Unexpected sendTestResult.");                return;            }        }        TestBundle executingBundle = (TestBundle)executingBundleTable.get(bundleId);        byte[] executingTest = (byte[])executingTestTable.get(bundleId);        if (executingBundle == null) {            System.err.println("Out of sync!!! Unexpected sendTestResult.");            return;        }        if (executingTest == null) {            if (verbose) {                System.out.println(verboseId + ": duplicated sendTestResult, bundle " + executingBundle.getApp());            }            return;        }         executingBundle.passTestResult(executingTest, res);        executingTestTable.remove(bundleId);        if (verbose) {            System.out.print(verboseId + ": sendTestResult, bundle " + executingBundle.getApp() + ", test ");            executingBundle.printTestName(executingTest);        }    }    /**     * Returns next application to execute. This is a file name      * relative to the directory returned by getJarSourceDirectory().     *     * May return null if no application is currently available.     * In this case JAM is expected to repeat request some time later.     *     * This method has the same effect as getNextApp("")     */    public synchronized String getNextApp() {        return getNextApp("");    }    /**     * Returns next application to execute on target device specified     * by unique JAM identifier. This is a file name relative to the     * directory returned by getJarSourceDirectory().     *     * May return null if no application is currently available.     * In this case JAM is expected to repeat request some time later.     */    public synchronized String getNextApp(String JAMId) {        setAlarm(timeout);        if (JAMId == null) {            throw new RuntimeException("JAMId == null");        }        TestBundle executingBundle = null;        byte[] executingTest = null;        String bundleId = (String)JAMBundleTable.get(JAMId);        if  (bundleId == null) { // no executing bundle on specified device            if (JAMBundleTable.size() >= MAX_AGENTS_NUMBER) {                System.err.println("WARNING: Running over " + MAX_AGENTS_NUMBER + " JAMs, force JAM " +                         ("".equals(JAMId) ? "" : JAMId+" ") + "to exit");                return null;            }        } else {            executingBundle = (TestBundle)executingBundleTable.get(bundleId);            executingTest = (byte[])executingTestTable.get(bundleId);        }        // first check whether there are tests left in this app        if (executingBundle != null) {            // the previous test has not been completed            if (executingTest == null) {                System.err.println("WARNING: "+ (++nErrors) +                         " getNextApp calls in a row");                if (nErrors >= 500) {                    nErrors = 0;                    throw new RuntimeException("Out of sync!!!");                }            } else {                 // we need to signal VM exit                executingBundle.passVMExitResult(executingTest);                nErrors = 0;                executingTestTable.remove(bundleId);                if (verbose) {                    System.out.print(verboseId + ": getNextApp from JAM" + ("".equals(JAMId) ? "" : " "+JAMId) + " - VM exit signaled for ");                    executingBundle.printTestName(executingTest);                }            }            if (executingBundle.getTestCount() > 0 &&                !executingBundle.isInterrupted()) {                 executingBundle.restarting();                if (verbose) {                    System.out.print(verboseId + ": More tests available in the bundle - restarted ");                    executingBundle.printAppName();                }                return executingBundle.getApp();            }             // interrupted bundle or no more tests in the bundle              executingBundle.finishing();            executingBundleTable.remove(bundleId);            executingTestTable.remove(bundleId);            executingTest = null;            if (verbose) {                System.out.print(verboseId + ": No more tests in the bundle - finished " + (executingBundle.isInterrupted()?"(interrupted) ":""));                executingBundle.printAppName();            }        }        if (executingTest != null) {            throw new RuntimeException("Out of sync!!!");        }        do {            if (bundles.isEmpty()) {                // no more apps yet arrived                // so let JAM wait                if (verbose) {                    System.out.println(verboseId + ": No bundles yet available, let JAM " + ("".equals(JAMId) ? "" : JAMId+" ") + "wait");                }                // Awake other threads which are also waiting for the                // test bundles, to make sure that there are some                // non-blocked server threads that can do other things                // rather then just wait here.                notify();                try {                     wait(5000);                } catch (InterruptedException ignore) {}                if (bundles.isEmpty()) {                    return "";                }            }            if (bundles.elementAt(0) == null) {                JAMBundleTable.remove(JAMId);                           if (verbose) {                    System.out.println(verboseId + ": No more bundles, let JAM " + ("".equals(JAMId) ? "" : JAMId+" ") + "know");                }                return null;            }            executingBundle = (TestBundle) bundles.remove(0);            // skip all interrupted bundles first        } while (executingBundle.isInterrupted());         bundleId = executingBundle.getApp();        JAMBundleTable.put(JAMId, bundleId);                   executingBundleTable.put(bundleId, executingBundle);        nErrors = 0;        executingBundle.starting();        if (verbose) {            System.out.print(verboseId + ": getNextApp, started new bundle for JAM " + ("".equals(JAMId) ? "" : JAMId) + ", bundle Id is ");            executingBundle.printAppName();        }        return executingBundle.getApp();    }    // interface to JavaTest (or other test harness)     public synchronized void dispatchTestBundle(TestBundle bundle) {        bundles.add(bundle);        notify();        if (verbose) {            System.out.print(verboseId + ": dispatchTestBundle ");            if (bundle == null)                System.out.println("- No more bundles to dispatch");            else {                bundle.printAppName();                bundle.printTestList(verboseId+":    ");            }        }    }        public void setRequestsTimeout(int timeout) {        this.timeout = timeout;        setAlarm(timeout);    }    protected static Timer alarmTimer = new Timer();    protected void setAlarm(int timeout) {	if (alarm != null) {	    alarm.cancel();	    alarm = null;	}	if (timeout > 0)	    alarm = new Alarm(timeout);    }    public void setVerbose(boolean val) {        verbose = val;    }    private String appMainClass = null;    private String jarSourceDir = null;    public static final int MAX_AGENTS_NUMBER = 10;    // a hashtable which maps JAMId to bundleId    protected Hashtable JAMBundleTable = new Hashtable(MAX_AGENTS_NUMBER);    // a hashtable which maps bundleId to executingBundle    protected Hashtable executingBundleTable = new Hashtable(MAX_AGENTS_NUMBER);    // a hashtable which maps bundleId to executingTest    private Hashtable executingTestTable = new Hashtable(MAX_AGENTS_NUMBER);    private int nErrors = 0;    // a queue to store bundles    private Vector bundles = new Vector();        private int timeout = 0;        private Alarm alarm;    private class Alarm implements Timer.Timeable {	Alarm(int delay) {	    this.delay = delay;	    entry = alarmTimer.requestDelayedCallback(this, delay);	}	synchronized void cancel() {	    alarmTimer.cancel(entry);	}	public synchronized void timeout() {            System.err.println("No requests from JAM: " +                 (delay * (++count) / 60000) + "min.");            entry = alarmTimer.requestDelayedCallback(this, delay); 	}	private int delay;	private int count;	private Timer.Entry entry;    }        // for verbose output    private boolean verbose = false;    private String  verboseId = "TEST PROVIDER";}

⌨️ 快捷键说明

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