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

TestLink

public class TestLink extends com.sun.midp.i3test.TestCase
Tests basic operations on the Link class.

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

        declare("testCreate");
        testCreate();

        declare("testNulls");
        testNulls();

        declare("testEquals");
        testEquals();

        declare("testReceiveClosed");
        testReceiveClosed();

        declare("testSendClosed");
        testSendClosed();

        declare("testReceiveSend");
        testReceiveSend();

        declare("testSendReceive");
        testSendReceive();

        declare("testReceiveClose");
        testReceiveClose();

        declare("testSendClose");
        testSendClose();

        declare("testCleanup");
        testCleanup();

        declare("testIsolateStates");
        testIsolateStates();

        declare("testAccess");
        testAccess();
    
voidtestAccess()
Tests receiver and sender isolate access for a link.

        Isolate i1 = Isolate.currentIsolate();
        Isolate i2 = new Isolate("com.sun.midp.links.Empty", null);
        i2.start();
        Link link1 = Link.newLink(i1, i2);
        Link link2 = Link.newLink(i2, i1);
        boolean thrown;

        thrown = false;
        try {
            LinkMessage lm = link1.receive();
        } catch (IllegalArgumentException iae) {
            thrown = true;
        }
        assertTrue("receive should catch IAE", thrown);

        thrown = false;
        try {
            link2.send(LinkMessage.newStringMessage("hello"));
        } catch (IllegalArgumentException iae) {
            thrown = true;
        }
        assertTrue("send should catch IAE", thrown);

        i2.exit(0);
    
voidtestCleanup()
Tests that the native rendezvous point structure is getting cleaned up properly by the finalizer.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        int rp = link.hashCode();

        assertEquals("refcount must be 1", 1, Utils.getRefCount(link));

        Utils.forceGC();
        /* ignored */ Utils.getFreedRendezvousPoints();
        link = null;
        Utils.forceGC();
        int[] freed = Utils.getFreedRendezvousPoints();
        assertEquals("length must be one", 1, freed.length);
        if (freed.length == 1) {
            assertEquals("freed rp must match", rp, freed[0]);
        }
    
voidtestCreate()
Tests creation, isOpen(), and close().

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);

        assertTrue("link should be open", link.isOpen());

        link.close();
        assertFalse("link should be closed", link.isOpen());
    
voidtestEquals()
Tests for equals() and hashCode().

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Link link2 = Link.newLink(i, i);
        assertFalse("must not equal null", link.equals(null));
        assertTrue("must equal itself", link.equals(link));
        assertFalse("must not equal other obj", link.equals("foobar"));
        assertFalse("must not equal other link", link.equals(link2));

        link.close();
        assertEquals("hashCode of closed should be zero", 0, link.hashCode());
        link2.close();
        assertTrue("two closed links should be equal", link.equals(link2));
    
voidtestIsolateStates()
Tests link creation with isolates in different states.

        boolean thrown;
        Isolate us = Isolate.currentIsolate();
        Isolate them = new Isolate("com.sun.midp.links.Empty", null);

        thrown = false;
        try {
            Link link = Link.newLink(us, them);
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("started,new should throw ISE", thrown);

        thrown = false;
        try {
            Link link = Link.newLink(them, us);
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("new,started should throw ISE", thrown);

        them.start();

        thrown = false;
        try {
            Link link = Link.newLink(us, them);
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertFalse("started,started should not throw ISE", thrown);

        thrown = false;
        try {
            Link link = Link.newLink(them, us);
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertFalse("started,started should not throw ISE", thrown);

        them.exit(0);
        them.waitForExit();

        thrown = false;
        try {
            Link link = Link.newLink(us, them);
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("started,terminated should throw ISE", thrown);

        thrown = false;
        try {
            Link link = Link.newLink(them, us);
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("terminated,started should throw ISE", thrown);
    
voidtestNulls()
Tests nulls.

        Isolate i = Isolate.currentIsolate();
        boolean thrown;

        thrown = false;
        try {
            Link link = Link.newLink(null, null);
        } catch (NullPointerException npe) {
            thrown = true;
        }
        assertTrue("null,null should throw NPE", thrown);

        thrown = false;
        try {
            Link link = Link.newLink(null, i);
        } catch (NullPointerException npe) {
            thrown = true;
        }
        assertTrue("null,i should throw NPE", thrown);

        thrown = false;
        try {
            Link link = Link.newLink(i, null);
        } catch (NullPointerException npe) {
            thrown = true;
        }
        assertTrue("i,null should throw NPE", thrown);

        thrown = false;
        try {
            Link link = Link.newLink(i, i);
        } catch (NullPointerException npe) {
            thrown = true;
        }
        assertFalse("i,i should not throw NPE", thrown);
    
voidtestReceiveClose()
Tests that close() will unblock a thread blocked in receive().

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Receiver receiver = new Receiver(link);

        assertFalse("receiver should be blocked", receiver.done);
        assertNull("receiver should have no exceptions", receiver.exception);

        link.close();
        receiver.await();
        assertTrue("receiver should be done", receiver.done);

        boolean wasIIOE =
            receiver.exception instanceof InterruptedIOException;
        assertTrue("receiver should have gotten InterruptedIOException",
            wasIIOE);
        if (!wasIIOE) {
            System.out.println("### receiver got " + receiver.exception);
        }
    
voidtestReceiveClosed()
Tests receive() on a closed link.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        link.close();

        boolean thrown = false;
        try {
            LinkMessage lm = link.receive();
        } catch (ClosedLinkException cle) {
            thrown = true;
        }

        assertTrue("exception should be thrown", thrown);
    
voidtestReceiveSend()
Tests receive() followed by a send().

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Receiver receiver = new Receiver(link);

        assertFalse("receiver should be blocked", receiver.done);
        assertNull("receiver should have no exceptions", receiver.exception);

        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);
    
voidtestSendClose()
Tests that close() will unblock a thread blocked in send().

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

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

        link.close();
        sender.await();
        assertTrue("sender should be done", sender.done);

        boolean wasIIOE =
            sender.exception instanceof InterruptedIOException;
        assertTrue("sender should have gotten InterruptedIOException",
            wasIIOE);
        if (!wasIIOE) {
            System.out.println("### sender got " + sender.exception);
        }
    
voidtestSendClosed()
Tests send() on a closed link.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        link.close();

        boolean thrown = false;
        try {
            link.send(LinkMessage.newStringMessage("foo"));
        } catch (ClosedLinkException cle) {
            thrown = true;
        }

        assertTrue("exception should be thrown", thrown);
    
voidtestSendReceive()
Tests send() followed by a receive().

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

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

        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);