Fields Summary |
---|
private static final String | otherSideUriURI 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 | connectionSocketConnection being tested. |
InputStream | isInputStream for the blocking read. |
Thread | t1Thread that is blocked on the read. |
boolean | exceptionThrownRecords if the expected InterruptedIOException is thrown. |
boolean | doneRecords if the thread exited. |
Methods Summary |
---|
void | closeConnection()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 void | run()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 void | runTests()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();
}
|
boolean | setUp()Open the connection and start the thread to call the run
method. It waits until the InputStream is open before returning.
// 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;
|
void | tearDown()Clean up after the test.
closeConnection();
|
void | testForException()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);
|