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

TestMMSSendReceive

public class TestMMSSendReceive extends com.sun.midp.i3test.TestCase implements Runnable
Tests if an MMS message can sent and received.

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 String
MMS_CONTENT_MESSAGE
The MMS test message.
private final byte[]
MMS_CONTENT
The contents of an MMS message.
private javax.wireless.messaging.MessageConnection
con
The MMS connection.
private boolean
passed
Test passed/failed flag.
Constructors Summary
Methods Summary
private voidcheckMultipartMessage(javax.wireless.messaging.MultipartMessage msg)
Verify that the contents of the multipart message match the message data that were sent..

param
msg A MultipartMessage.


        // Pick up all message parts.
        MessagePart[] parts = msg.getMessageParts();
        if (parts == null) {
            System.out.println("No message parts.");
            return;
        }

        // Make sure the message part contains the expected content.
        for (int i = 0, n = parts.length; i < n; i++) {
            MessagePart part = parts[i];

            byte[] content = part.getContent();
            if (content == null) {
                System.out.println("No content in message part #" + i);
                return;
            }

            String data = new String(content);
            if (!data.equals(MMS_CONTENT_MESSAGE)) {
                System.out.println("Content message mismatch.");
                return;
            }
        }
    
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;
    
public voidrun()
Create, send and receive a multipart message. This test assumes that the underlying code employs the loopback scheme (i.e., messages are not actually sent or received; however, the messages get added to the pool, which triggers notifications.).


        try {

            MultipartMessage mm =
                createCompleteMessage(MMS_TO_ADDRESS, MMS_FROM_ADDRESS,
                                      MMS_CONTENT);

            con.send(mm);

            Message m = con.receive();
            if (m instanceof MultipartMessage) {
                checkMultipartMessage((MultipartMessage)m);
            } else {
                System.out.println("Not a MultipartMessage.");
                return;
            }

            closeConnection();

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

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    
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();
        }