FileDocCategorySizeDatePackage
TestTransfer.javaAPI DocphoneME MR2 API (J2ME)6865Wed May 02 18:00:02 BST 2007com.sun.midp.links

TestTransfer

public class TestTransfer extends com.sun.midp.i3test.TestCase
Tests simple exchange of different types of messages (data, data subrange, and String) within the same isolate. Covers simple cases only, with one receiver thread and one sender thread. Covers receive-then-send as well as send-then-receive, for each kind of message.

Fields Summary
Constructors Summary
Methods Summary
public voidrunTests()
Runs all tests.

        declare("testReceiveString");
        testReceiveString();

        declare("testSendString");
        testSendString();

        declare("testReceiveData");
        testReceiveData();

        declare("testSendData");
        testSendData();

        declare("testReceiveDataRange");
        testReceiveDataRange();

        declare("testSendDataRange");
        testSendDataRange();
    
voidtestReceiveData()
Tests receive-then-send of a byte array.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Receiver receiver = new Receiver(link);
        byte[] sendarr = new byte[100];
        Utils.fillRandom(sendarr);

        link.send(LinkMessage.newDataMessage(sendarr));
        receiver.await();

        assertTrue("receiver should be done", receiver.done);
        assertNull("receiver should have no exceptions", receiver.exception);
        assertNotNull("receiver should have received a message", receiver.msg);

        byte[] recvarr = receiver.msg.extractData();
        assertTrue("arrays shouldn't be identical", sendarr != recvarr);
        assertTrue("arrays should be equal",
            Utils.bytesEqual(sendarr, recvarr));
    
voidtestReceiveDataRange()
Tests receive-then-send of a byte array subrange.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Receiver receiver = new Receiver(link);
        byte[] sendarr = new byte[100];
        Utils.fillRandom(sendarr);

        link.send(LinkMessage.newDataMessage(sendarr, 17, 32));
        receiver.await();

        assertTrue("receiver should be done", receiver.done);
        assertNull("receiver should have no exceptions", receiver.exception);
        assertNotNull("receiver should have received a message", receiver.msg);

        byte[] recvarr = receiver.msg.extractData();
        assertTrue("arrays shouldn't be identical", sendarr != recvarr);
        assertTrue("arrays should be equal",
            Utils.bytesEqual(sendarr, 17, 32, recvarr));
    
voidtestReceiveString()
Tests receive-then-send of a String.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Receiver receiver = new Receiver(link);
        String sendstr = "bar";

        link.send(LinkMessage.newStringMessage(sendstr));
        receiver.await();

        assertTrue("receiver should be done", receiver.done);
        assertNull("receiver should have no exceptions", receiver.exception);
        assertNotNull("receiver should have received a message", receiver.msg);

        String recvstr = receiver.msg.extractString();
        assertTrue("strings shouldn't be identical", sendstr != recvstr);
        assertEquals("strings should be equal", sendstr, recvstr);
    
voidtestSendData()
Tests send-then-receive of a byte array.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        byte[] sendarr = new byte[100];
        Utils.fillRandom(sendarr);
        Sender sender = new Sender(link,
            LinkMessage.newDataMessage(sendarr));

        LinkMessage lm = link.receive();
        sender.await();

        assertTrue("sender should be done", sender.done);
        assertNull("sender should have no exceptions", sender.exception);

        byte[] recvarr = lm.extractData();
        assertTrue("arrays shouldn't be identical", sendarr != recvarr);
        assertTrue("arrays should be equal",
            Utils.bytesEqual(sendarr, recvarr));
    
voidtestSendDataRange()
Tests send-then-receive of a byte array subrange.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        byte[] sendarr = new byte[100];
        Utils.fillRandom(sendarr);
        Sender sender = new Sender(link,
            LinkMessage.newDataMessage(sendarr, 47, 22));

        LinkMessage lm = link.receive();
        sender.await();

        assertTrue("sender should be done", sender.done);
        assertNull("sender should have no exceptions", sender.exception);

        byte[] recvarr = lm.extractData();
        assertTrue("arrays shouldn't be identical", sendarr != recvarr);
        assertTrue("arrays should be equal",
            Utils.bytesEqual(sendarr, 47, 22, recvarr));
    
voidtestSendString()
Tests send-then-receive of a String.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        String sendstr = "foo";
        Sender sender = new Sender(link,
            LinkMessage.newStringMessage(sendstr));

        LinkMessage lm = link.receive();
        sender.await();

        assertTrue("sender should be done", sender.done);
        assertNull("sender should have no exceptions", sender.exception);

        String recvstr = lm.extractString();
        assertTrue("strings shouldn't be identical", sendstr != recvstr);
        assertEquals("strings should be equal", sendstr, recvstr);