FileDocCategorySizeDatePackage
TestMMSNumberOfSegments.javaAPI DocphoneME MR2 API (J2ME)9945Wed May 02 18:00:44 BST 2007com.sun.midp.io.j2me.mms

TestMMSNumberOfSegments

public class TestMMSNumberOfSegments extends com.sun.midp.i3test.TestCase implements Runnable
Tests if the number of transport-layer data segments is computed correctly.

Fields Summary
private final String
TEST_NAME
The fully qualified name of this test.
private final String
MMS_TO_DEVICE_ID
The device ID ("phone number") of the recipient.
private final String
MMS_TO_APP_ID
The application ID of the recipient.
private final String
MMS_TO_ADDRESS
The MMS address to which the message will be sent.
private final String
MMS_FROM_DEVICE_ID
The device ID ("phone number") of the sender.
private final String
MMS_FROM_APP_ID
The application ID of the sender.
private final String
MMS_FROM_ADDRESS
The MMS address from which the message will be sent.
private final String
MMS_CLIENT_ADDRESS
The MMS client address.
private final int
MAX_FRAGMENT_SIZE
The maximum size of a fragment.
private final int
MAX_TOTAL_SIZE
The maximum number of bytes permitted in an MMS message (30k).
private javax.wireless.messaging.MessageConnection
con
The MMS connection.
private boolean
passed
Test passed/failed flag.
Constructors Summary
Methods Summary
voidcleanUp()
Provide clean-up services, following the run of this test.

        closeConnection();
    
private voidcloseConnection()
Close the MMS connection.

        try {
            con.close();
        } catch (IOException ioe) {
            // Fail silently.
        } catch (Exception e) {
            // Fail silently.
        }
    
private voidcreateClientConnection(java.lang.String clientAddress)
Create and open an MMS connection.

param
clientAddress The MMS address of the client that will receive the message.


        con = (MessageConnection)Connector.open(clientAddress);
    
private javax.wireless.messaging.MultipartMessagecreateCompleteMessage(java.lang.String toAddress, java.lang.String fromAddress, byte[] content)
Create the MMS message to be sent.

param
toAddress The recipient's MMS address.
param
fromAddress The sender's MMS address.
param
content The payload to be sent.
return
The MultipartMessage that was created for the supplied content.


        // The MIME type for this message.
        String mimeType = "mms:";

        // The unique content ID.
        String contentID = "message1";

        // No content location.
        String contentLocation = null;

        // No encoding.
        String encoding = null;

        MessagePart part = new MessagePart(content, 0, content.length,
            mimeType, contentID, contentLocation, encoding);

        MultipartObject mm = new MultipartObject(toAddress);
        mm.addMessagePart(part);

        mm.setFromAddress(fromAddress);

        return mm;
    
intnumberOfMMSSegments(byte[] msgBuffer)
Compute the number of segments required to send the message. This will be matched against the number of segments computed by the actual code.

param
msgBuffer The payload to be sent.
return
The number of transport-layer segments required to send the message.


        /** The maximum fragment size for each segment to be sent. */
        final int FRAGMENT_SIZE = 1200;

        /** The actual fragment size. */
        int fragmentSize = FRAGMENT_SIZE;

        /** Extra header size for concatenated messages. */
        int headerSize = 7;

        /** The number of segments required to send the message. */
        int segments = 0;

        if (msgBuffer == null) {
            return 1;
        }

        int msgLen = msgBuffer.length;
        if (msgLen < FRAGMENT_SIZE) {
            segments = 1;
        } else {
            fragmentSize = fragmentSize - headerSize;
            segments = (msgLen + fragmentSize - 1) / fragmentSize;
        }

        return segments;
    
public voidrun()
Compute transport-layer segment counts for varying message lengths.


        try {

            // Make sure that an SMS text object can't be used here.
            String smsAddress = "sms://+5551111:1234";
            Message smsMessage = new TextObject(smsAddress);
            int segs = con.numberOfSegments(smsMessage);
            if (segs != 0) {
                throw new IllegalArgumentException(
                    "TextMessage cannot be used in MMS connection " +
                    "(Segments cannot be computed.).");
            }

            // Make sure that an SMS binary object can't be used here.
            smsMessage = new BinaryObject(smsAddress);
            segs = con.numberOfSegments(smsMessage);
            if (segs != 0) {
                throw new IllegalArgumentException(
                    "BinaryMessage cannot be used in MMS connection " +
                    "(Segments cannot be computed.).");
            }

            // Lower bounds test: This should not fail.
            testMessageLengthOf(0);

            // The following message sizes should not fail:
            testMessageLengthOf(1);
            testMessageLengthOf(MAX_FRAGMENT_SIZE - 1);
            testMessageLengthOf(MAX_FRAGMENT_SIZE);
            testMessageLengthOf(MAX_FRAGMENT_SIZE + 1);
            testMessageLengthOf(MAX_TOTAL_SIZE - 1);
            testMessageLengthOf(MAX_TOTAL_SIZE);

            // Upper bounds test: This should fail.
            try {
                testMessageLengthOf(MAX_TOTAL_SIZE + 1);
            } catch (SizeExceededException see) {
                // ignore
            }

            closeConnection();

            passed = true;
            assertTrue("Indicate that the test passed.", passed);

        } catch (Exception e) {

            // Test failure!
            e.printStackTrace();
        }
    
public voidrunTests()
Main entry point.

        setUp();

        declare(TEST_NAME);

        run();

        cleanUp();
    
voidsetUp()
Set up the physical connection.



              
      

        try {
            createClientConnection(MMS_CLIENT_ADDRESS);
        } catch (IOException ioe) {
            System.out.println(TEST_NAME + " set-up failed:");
            ioe.printStackTrace();
        }
    
private voidtestMessageLengthOf(int msgLen)
Create a message of the given length. Then compute both the actual and test segment counts required to send that message. If there is a difference between the counts, flag this as a problem.

param
msgLen The total number of bytes in the message.


        // Create the message content.
        StringBuffer sb = new StringBuffer(msgLen);
        for (int i = 0; i < msgLen; i++) {
            sb.append('x");
        }
        String mmsContent = sb.toString();

        // Create an MMS multipart object and compute its segment count.
        MultipartMessage mm =
            createCompleteMessage(MMS_TO_ADDRESS, MMS_FROM_ADDRESS,
                                  mmsContent.getBytes());

        int segs = con.numberOfSegments(mm);

        // Perform an independent segment computation for comparison.
        byte[] msgBytes = ((MultipartObject)mm).getAsByteArray();
        int compareSegs = numberOfMMSSegments(msgBytes);

        // Bail out if there are discrepancies.
        if (segs < compareSegs) {
            throw new IllegalArgumentException("Actual segment count of " + segs
                + " is lower than comparison count of " + compareSegs + ".");
        } else if (segs > compareSegs) {
            throw new IllegalArgumentException("Actual segment count of " + segs
                + " is higher than comparison count of " + compareSegs + ".");
        }