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

TestLinkMessage

public class TestLinkMessage extends com.sun.midp.i3test.TestCase
Tests basic operations on the LinkMessage class.

Fields Summary
Constructors Summary
Methods Summary
booleancheckRange(byte[] data, int offset, int length)
Checks the creation of a data message with subrange arguments to determine whether it throws IndexOutOfBoundsException. Returns a boolean indicating whether IOOBE was thrown.

        boolean thrown = false;

        try {
            LinkMessage.newDataMessage(data, offset, length);
        } catch (IndexOutOfBoundsException ioobe) {
            thrown = true;
        }

        return thrown;
    
public voidrunTests()
Runs all tests.

        declare("data");
        testData();
        declare("data subrange");
        testDataSubrange();
        declare("link");
        testLink();
        declare("string");
        testString();
    
voidtestData()
Tests a LinkMessage containing a basic byte array (that is, not a subrange).

        byte[] data = Utils.extractBytes("this is a test string");
        LinkMessage lm = LinkMessage.newDataMessage(data);

        assertTrue("containsData must return true", lm.containsData());
        assertFalse("containsLink must return false", lm.containsLink());
        assertFalse("containsString must return false", lm.containsString());
        assertSame("extract must return same object", data, lm.extract());

        boolean thrown;
        byte[] retData = null;

        thrown = false;
        try {
            retData = lm.extractData();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertFalse("extractData must not throw exception", thrown);
        assertTrue("extractData must return equal data",
            Utils.bytesEqual(data, retData));

        thrown = false;
        Object obj;
        try {
            obj = lm.extractLink();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("extractLink must throw exception", thrown);

        thrown = false;
        try {
            obj = lm.extractString();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("extractString must throw exception", thrown);
    
voidtestDataSubrange()
Tests a LinkMessage that contains a byte array subrange.

        byte[] data = Utils.extractBytes("this is test data fodder");

        assertTrue("offset -7 must throw ioobe", checkRange(data, -7, 10));
        assertTrue("offset 9853 must throw ioobe", checkRange(data, 9853, 3));
        assertTrue("length 2387 must throw ioobe", checkRange(data, 0, 2387));
        assertTrue("length -2 must throw ioobe", checkRange(data, 5, -2));
        assertTrue("20, 20 must throw ioobe", checkRange(data, 20, 20));

        assertFalse("0,0 ok",   checkRange(data, 0, 0));
        assertFalse("0,len ok", checkRange(data, 0, data.length));
        assertFalse("len,0 ok", checkRange(data, data.length, 0));
        assertFalse("3,6 ok",   checkRange(data, 3, 6));

        LinkMessage lm;
        byte[] retData;

        lm = LinkMessage.newDataMessage(data, 0, data.length);
        retData = lm.extractData();
        assertTrue("full data", Utils.bytesEqual(data, retData));

        byte[] nullData = new byte[0];

        lm = LinkMessage.newDataMessage(data, 0, 0);
        retData = lm.extractData();
        assertTrue("zero null", Utils.bytesEqual(nullData, retData));

        lm = LinkMessage.newDataMessage(data, data.length, 0);
        retData = lm.extractData();
        assertTrue("end null", Utils.bytesEqual(nullData, retData));

        lm = LinkMessage.newDataMessage(data, 5, 9);
        retData = lm.extractData();
        assertTrue("subrange", Utils.bytesEqual(data, 5, 9, retData));
    
voidtestLink()
Tests a LinkMessage containing a Link.

        Isolate isolate = Isolate.currentIsolate();
        Link link = Link.newLink(isolate, isolate);
        LinkMessage lm = LinkMessage.newLinkMessage(link);

        assertFalse("containsData must return false", lm.containsData());
        assertTrue("containsLink must return true", lm.containsLink());
        assertFalse("containsString must return false", lm.containsString());
        assertSame("extract must return same object", link, lm.extract());

        boolean thrown;
        Object obj;

        thrown = false;
        try {
            obj = lm.extractData();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("extractData must throw exception", thrown);

        thrown = false;
        Link retLink = null;
        try {
            retLink = lm.extractLink();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertFalse("extractLink must not throw exception", thrown);
        assertSame("extractLink must return same link",
            link, retLink);

        thrown = false;
        try {
            obj = lm.extractString();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("extractString must throw exception", thrown);
    
voidtestString()
Tests a LinkMessage containing a String.

        String msg = "this is a string message";
        LinkMessage lm = LinkMessage.newStringMessage(msg);

        assertFalse("containsData must return false", lm.containsData());
        assertFalse("containsLink must return false", lm.containsLink());
        assertTrue("containsString must return true", lm.containsString());
        assertSame("extract must return same object", msg, lm.extract());

        boolean thrown;
        Object obj;

        thrown = false;
        try {
            obj = lm.extractData();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("extractData must throw exception", thrown);

        thrown = false;
        try {
            obj = lm.extractLink();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertTrue("extractLink must throw exception", thrown);

        thrown = false;
        String str = null;
        try {
            str = lm.extractString();
        } catch (IllegalStateException ise) {
            thrown = true;
        }
        assertFalse("extractString must not thrown exception", thrown);
        assertTrue("strings must match", msg.equals(str));