cldctckscript.java

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

JAVA
154
字号
/* * $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.javatest;import com.sun.javatest.Status;import com.sun.javatest.TestDescription;import com.sun.javatest.TestEnvironment;import com.sun.javatest.util.Timer;/** * Script to execute CLDC tests, based on the keywords in the test description. */public class CldcTCKScript extends TCKScript {    /**     * Runs the script.     *     * @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) {        setAlarm(0);        CldcTCKAlarm al = new CldcTCKAlarm();        al.parent = Thread.currentThread();        al.setName("Alarm_"+td.getName());        synchronized (al.parent) {             al.start();            try {                al.parent.wait();            }            catch (InterruptedException i) {                testFinished = true;                al.interrupt();                setMyAlarm(0, Thread.currentThread());                return Status.error("Thread " +  Thread.currentThread() + " Interrupted while waiting for alarm");            }        }        try {            return super.run(args, td, env);        } finally {            synchronized (al.parent) {                 testFinished = true;                al.interrupt();                setMyAlarm(0, Thread.currentThread());            }        }    }    protected boolean isNegative() {        if (keywords == null) {            throw new Error("run is invoked before initTestDescription or "                            + "keyword set is null.");        }        return keywords.contains("negative") || keywords.contains("cldc_typechecker_specific");    }    /**     * Set an alarm that will interrupt the thread after     * a specified delay (in milliseconds), and repeatedly      * thereafter until cancelled.     * @param timeout the interval (in milliseconds) after which      * the thread will be interrupted, if not cancelled in the meantime.     * @param thr the thread     */    protected void setMyAlarm(int timeout, Thread thr) {	if (alarm != null) {	    alarm.cancel();	    alarm = null;	}	if (timeout > 0)	    alarm = new Alarm(timeout, thr);    }    private class CldcTCKAlarm extends Thread  {        public Thread parent;        public void run() {            synchronized(parent) {                parent.notifyAll();                try {                    parent.wait();                } catch (InterruptedException x) {                    return;                }            }            int timeout = getTestTimeout();            synchronized(parent) {                if (timeout > 0 && !testFinished)                    setMyAlarm(timeout*1000, parent);            }        }    }    private Alarm alarm;    private class Alarm implements Timer.Timeable {	Alarm(int delay, Thread caller) {	    this.delay = delay;	    this.caller = caller;	    entry = alarmTimer.requestDelayedCallback(this, delay);	}	synchronized void cancel() {	    alarmTimer.cancel(entry);	}	public synchronized void timeout() {	    if (count == 0)		trOut.println("CLDC TCK Alarm: Timeout signaled after " + delay/1000.0 + " seconds");	    else if (count%100 == 0) {		trOut.println("CLDC TCK Alarm: Test not responding after " + count + " interrupts");		if (count%1000 == 0) 		    System.err.println("CLDC TCK Alarm: Test " + td.getRootRelativeURL() + " has timed out and is not responding after " + count + " interrupts");	    }	    caller.interrupt();	    count++;            // keep requesting interrupts until cancelled            entry = alarmTimer.requestDelayedCallback(this, 100); 	}	private int delay;	private Thread caller;	private int count;	private Timer.Entry entry;    }    private boolean testFinished=false;}

⌨️ 快捷键说明

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