testmebase

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

TXT
83
字号
@@PACKAGE_HEADER@@

import com.sun.tck.j2me.services.messagingService.J2MEDistributedTest;
import com.sun.tck.cldc.lib.Status;

import java.io.*;

public abstract class TestMEDistributedTest extends J2MEDistributedTest {
    
    /** Creates a new instance of TestMEDistributedTest */
    public TestMEDistributedTest(String serverName) {
        this.serverName = serverName;
    }
    
    protected void sendSync(String[] args, byte[] data) throws IOException {
        lastMessage = args[0];
        msgStatus = MSG_OK;
        send(serverName, args, data);
        waitAndCheckResponse();
    }

    protected void waitAndCheckResponse() {
        try {
            synchronized (this) {
                wait();
            }
            if (msgStatus == MSG_FAILURE) {
                throw new RuntimeException(msgError);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while waiting for '" + lastMessage + "' processing.");
        }
    }

    protected void handleMessage(String from, String[] args, byte[] data) {
        if ( serverName.equals(from) && ("startServer".equals(args[0])) ) {
            if (!lastMessage.equals(args[0])) {
                msgStatus = MSG_FAILURE;
                msgError = "Wrong message received, expected '" + lastMessage + "', got " + args[0];
            }
            if (!"OK".equals(args[1])) {
                msgStatus = MSG_FAILURE;
                msgError = "Remote part problems during '" + lastMessage + "' processing: " + args[1];
            }
        } else if ( serverName.equals(from) && ("stopServer".equals(args[0])) ) {
            if (!lastMessage.equals(args[0])) {
                msgStatus = MSG_FAILURE;
                msgError = "Wrong message received, expected '" + lastMessage + "', got " + args[0];
            }
            if (!"OK".equals(args[1])) {
                msgStatus = MSG_FAILURE;
                msgError = "Remote part problems during '" + lastMessage + "' processing: " + args[1];
            }
        } else if ( serverName.equals(from) && ("helloServer".equals(args[0])) ) {
            if (!lastMessage.equals(args[0])) {
                msgStatus = MSG_FAILURE;
                msgError = "Wrong message received, expected '" + lastMessage + "', got " + args[0];
            }
            if (!"OK".equals(args[1])) {
                msgStatus = MSG_FAILURE;
                msgError = "Remote part problems during '" + lastMessage + "' processing: " + args[1];
            }
        } else {
            msgStatus = MSG_FAILURE;
            msgError = "Unknown message from '" + from + "', msg: '" + args[0] + "'";
            handleUnknownMessage(from, args);
        }
        // notify test that message
        // has been handled
        synchronized (this) {
            notifyAll();
        }
    }
    
    private String serverName;
    protected String msgError = null;
    protected int msgStatus = MSG_OK;
    protected String lastMessage = null;
    
    public static final int MSG_OK = 0;
    public static final int MSG_FAILURE = 1;
}

⌨️ 快捷键说明

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