FileDocCategorySizeDatePackage
TestInterruptedIO.javaAPI DocphoneME MR2 API (J2ME)6176Wed May 02 18:00:14 BST 2007com.sun.midp.io.j2me.socket

TestInterruptedIO

public class TestInterruptedIO extends TestCase implements Runnable
Test of SocketConnection function that unblocks a pending read when the connection is closed.

Fields Summary
private static final String
otherSideUri
URI to which the test will try to open connections. Use any URI visible from your network, for example, "socket://www.sun.com:80" will do in most cases; "socket://localhost:80" will do if your computer is running an http server. Please, contact your network administrator if you have a problem that you cannot resolve yourself.
SocketConnection
connection
SocketConnection being tested.
InputStream
is
InputStream for the blocking read.
Thread
t1
Thread that is blocked on the read.
boolean
exceptionThrown
Records if the expected InterruptedIOException is thrown.
boolean
done
Records if the thread exited.
Constructors Summary
Methods Summary
voidcloseConnection()
Close the open connection.

        try {
	    if (is != null) {
		is.close();
		is = null;
	    }
	    if (connection != null) {
		connection.close();
		connection = null;
	    }
        } catch (IOException e) {
            assertNull("Exception closing a socket", e);
            e.printStackTrace();
        }
    
public voidrun()
Run method to open the InputStream and block on a read. When the stream/connection is closed the read should unblock with an InterruptedIOException.


                                 
       
        try {
            is = connection.openInputStream();

            // Notify indicating the InputStream is open
            synchronized (this) {
                notifyAll();
            }

            // Block on a read
            int ch = is.read();
            assertTrue("read should not have succeeded", false);
        } catch (InterruptedIOException ioe) {
            assertNotNull("Caught InterruptedIOException", ioe);
            exceptionThrown = true;
        } catch (IOException ioe) {
            assertNull("Unexpected IOException", ioe);
        } finally {
            synchronized (this) {
                done = true;     
                notifyAll();        
            }
        }
    
public voidrunTests()
Run the test by setting up the connection, and starting a thread to block on a read then forcing the connection to be closed and checking that exception occurred.

        declare("Test InterruptedIOException");

        boolean setupSuccess = setUp();

        if (setupSuccess) {
            testForException();

            tearDown();
        }
    
booleansetUp()
Open the connection and start the thread to call the run method. It waits until the InputStream is open before returning.

return
false if testing cannot be continued

        // Create a socket connection 
        try {
            connection = (SocketConnection)Connector.open(otherSideUri);
            //OutputStream os = connection.openOutputStream();
            //os.write("GET /midlets/midlet.jad HTTP/1.0\n\n".getBytes());
        } catch (IOException ioe) {
            assertNull("Exception during socket open", ioe);
        }
        
        assertNotNull("Verify socket open", connection);
        if (null == connection) {
            fail("Could not open connection to "+otherSideUri+
                 ".  If the host name cannot be resolved, you may " +
                 "need to change the i3test source to use a host name that" +
                 " is visible from your network.");
            return false;
        }

        // Launch a thread which would be blocked for some I/O operation
        // for above socket connection
        t1 = new Thread(this);
        t1.start();

        // Wait for the InputStream to have been opened
        synchronized (this) {
            while (is == null) {
                try {
                    wait(200);
                } catch (InterruptedException e) {
                    // Ignore interrupt
                }
            } 
        }
        return true;
    
voidtearDown()
Clean up after the test.

	    closeConnection();
    
voidtestForException()
This test creates another thread that tries to close a socket connection that is in use by first thread. It is supposed to throw InterruptedIOException in this case.

        closeConnection();

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

        assertTrue("Verify InterruptedIOException", exceptionThrown);