FileDocCategorySizeDatePackage
TestDatagramWouldBlock.javaAPI DocphoneME MR2 API (J2ME)7026Wed May 02 18:00:14 BST 2007com.sun.midp.io.j2me.datagram

TestDatagramWouldBlock

public class TestDatagramWouldBlock extends TestCase implements Runnable
Test for a blocked read operation for a datagram.

Fields Summary
static final int
PORT
Port number for datagram server socket
static final String
strTestMsg
Message string sent from client to server
UDPDatagramConnection
server
Server datagram
UDPDatagramConnection
client
client datagram
private int
numBytesToSend
No of bytes in a message
private Datagram
sendDatagram
Datagrm sent from client
private Datagram
receivedDatagram
Datagram received at server
boolean
readData
Flag that indicates that data is read at server
Thread
t1
Thread that is blocked on the read
Constructors Summary
Methods Summary
voidcleanUp()
Clean up after the test.

        try {
            client.close();
            server.close();
        } catch (IOException e) {
            System.out.println("TestSocket cleanUp failed with:");
            e.printStackTrace();
        }
        receivedDatagram = null;
        sendDatagram = null;
    
private DatagramcreateDatagramToReceive()
Creates a datagram packet to be received at server

return
Empty object to receive datagram

        receivedDatagram = null;
        numBytesToSend = strTestMsg.length();

        try {
            receivedDatagram = server.newDatagram(numBytesToSend);
        } catch (IOException ioe) {
            cleanUp();
            System.out.println("Cannot create Datagram. IOException: " + ioe);
        }
        
        return receivedDatagram;
    
private DatagramcreateDatagramToSend(java.lang.String address)
Creates a datagram packet to be sent by client

param
address Target host address
return
Datagram object to be sent

        sendDatagram = null;

        numBytesToSend = strTestMsg.length();
        byte[] buf = new byte[numBytesToSend];

        buf = strTestMsg.getBytes();

        try {
            sendDatagram = client.newDatagram(buf, numBytesToSend, address);
        } catch (IOException ioe) {
            cleanUp();
            System.out.println("Cannot create Datagram. IOException: " + ioe);
        }

        return sendDatagram;
    
public voidrun()
Run method to open the server datagram and block on a read.


                     
       
        try {
            // Create the server listening datagram socket 
            server = (UDPDatagramConnection) 
                     Connector.open("datagram://:"+PORT);
            // Notify indicating that server is available
            synchronized (this) {
                notifyAll();
            }

            receivedDatagram = createDatagramToReceive();

            server.receive(receivedDatagram);
        } catch (IOException ioe) {
            System.out.println("TestDatagram reader thread failed with:");
            ioe.printStackTrace();
        } finally {
            synchronized (this) {
                readData = true;
                notifyAll();
            }
        }
    
public voidrunTests()
Run the test by setting up datagram client in main thread and datagram server in a separate thread. The server thread blocks for read operation. The client writes a data packet to server. The test passes successfully when server receives the data back from client properly.

        setUp();

        declare("TestDatagramWouldBlock");
        testWouldBlock();

        cleanUp();
    
voidsetUp()
Open the client connection and start the thread to call the run method. It waits until the server is available.

        try {
            // Create client datagram socket
            client = (UDPDatagramConnection)
                Connector.open("datagram://localhost:"+PORT);

            // Launch a thread which would be blocked for read operation
            // from above datagram connection
            t1 = new Thread(this);
            t1.start();

            // Wait for the server to become available
            synchronized (this) {
                while (server == null) {
                    try {
                        // Wait for sufficiently long time so that server
                        // is blocked for read operation
                        wait(1200);
                    } catch (InterruptedException e) {
                        // Ignore interrupt
                    }
                }
            }
        } catch (IOException ioe) {
            System.out.println("TestDatagram setUp failed with:");
            ioe.printStackTrace();
        }
    
voidtestWouldBlock()
In this test, client datagram socket sends a packet to server. Upon receiving the packet at server, message is printed back.

        assertNotNull("Verify datagram socket open", client);
        sendDatagram = createDatagramToSend("datagram://localhost:"+PORT);

        try {
            client.send(sendDatagram);
        } catch (IOException ioe) {
            cleanUp();
            System.out.println("Cannot send datagram, send thrown " + ioe);
            return;
        }

        synchronized (this) {
            if (!readData) {
                try {
                    wait(2000);
                } catch (InterruptedException e) {
                    System.out.println("Catch interrupt");
                }
            }
        }

        byte[] buf = receivedDatagram.getData();

        String rcvdMsg = new String(buf);
	// IMPL_NOTE: System.out.println("Received msg : " + rcvdMsg);
        assertTrue("Verify data received at server", readData);