connectiondistrtest.java.svn-base

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

SVN-BASE
188
字号
/* * $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 sample.distributed;import java.io.IOException;import java.io.InputStream;import javax.microedition.io.ConnectionNotFoundException;import javax.microedition.io.Connector;import javax.microedition.io.SocketConnection;import com.sun.tck.cldc.lib.Status;import com.sun.tck.j2me.services.messagingService.J2MEDistributedTest;/** * Distributed tests that works with remote part on Java SE side. */public class ConnectionDistrTest extends J2MEDistributedTest {    /**     * Creates a named test instance.     */    public ConnectionDistrTest() {        super("ConnectionDistrTest");    }    protected void runTestCases() {        if (isSelected("ConnectionDistrTest")) {            addStatus(ConnectionDistrTest());        }    }    // we need to know the host name of the server to connect to    protected int decodeArg(String[] args, int index) throws SetupException {        if ("-serverHost".equals(args[index]) && index + 1 < args.length) {            serverHost = args[index + 1];            return 2;        } else {            return super.decodeArg(args, index);        }    }    // The three-arg method is preferred, while the two-arg method is    // deprecated    protected void handleMessage(String from, String[] args, byte[] ignore) {        log.println("====== Received Message ======");        log.println("FROM: " + from);        for (int i = 0; i < args.length; i++) {            log.println("args[" + i + "] = " + args[i]);        }        log.println("======= End of Message =======");        if (args[0].equals("serverUp")) {            handle_serverUp(from, args);        } else  if (args[0].equals("serverDown")) {            handle_serverDown(from, args);        } else {            handleUnknownMessage(from, args, ignore);        }    }    // handler for the serverUp response message    synchronized void handle_serverUp(String from, String[] args) {        log.println("Received sererUP response message");        String status = args[1];        if ("ERROR".equals(status)) {            serverError = true;        } else {            serverPort = status;        }        received = true;        notifyAll();    }    // handler for the serverDown response message    synchronized void handle_serverDown(String from, String[] args) {        log.println("Received sererDown response message");        received = true;        notifyAll();    }    synchronized void waitForMessage() {        try {            while (!received) {                wait(10000);            }            received = false;        } catch (InterruptedException e) {            // ignore        }    }    /**     * Main body of the test. The test first sends "serverUp" message to     * the remote component on Java SE side, then receives the port on     * where the server has started, connects to the server and then     * reads the server message. Finally, the test compares the received     * message with the expected value, and sends "serverDown" message     * to shut down the remote server.     *     * @return The test status.     */    public Status ConnectionDistrTest() {        try {            try {                send("ServerSocketRemoteServer", new String[] {"serverUp"});                waitForMessage();                if (serverError) {                    return Status.failed("Cannot start remote server");                }            } catch (IOException e) {                log.println("IOException: " + e);                return Status.failed("Cannot send serverUp message");            }            log.println("Remote host: " + serverHost);            log.println("Remote port: " + serverPort);            SocketConnection sc = (SocketConnection) Connector.open(                    "socket://" + serverHost + ":" + serverPort);            log.println("Connected to server");            InputStream is  = sc.openInputStream();            // Loop while receiving the data from the server            while (true) {                StringBuffer sb = new StringBuffer();                int c = 0;                while (((c = is.read()) != '\n') && (c != -1)) {                    sb.append((char) c);                }                String message = sb.toString();                log.println("Message received - " + message);                // final check:                if ("test".equals(message)) {                    return Status.passed("OK");                } else {                    return Status.failed(                            "Expected: test, received: " + message);                }            }        } catch (ConnectionNotFoundException cnfe) {            return Status.failed("Can't connect to the server: "                    + cnfe.toString());        } catch (IOException ioe) {            ioe.printStackTrace();            return Status.failed("exception " + ioe);        } finally {            try {                send("ServerSocketRemoteServer", new String[] {"serverDown"});            } catch (IOException ioe) {                ioe.printStackTrace();                return Status.failed("exception " + ioe);            }            waitForMessage();        }    }    private boolean serverError = false;    private String serverHost;    private String serverPort;    private boolean received = false;}

⌨️ 快捷键说明

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