001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.io.j2me.socket;
028:
029: import java.io.*;
030: import javax.microedition.io.*;
031: import com.sun.midp.i3test.*;
032:
033: /**
034: * Test of SocketConnection function that unblocks a pending read
035: * when the connection is closed.
036: */
037: public class TestInterruptedIO extends TestCase implements Runnable {
038:
039: /**
040: * URI to which the test will try to open connections.
041: * Use any URI visible from your network, for example,
042: * "socket://www.sun.com:80" will do in most cases;
043: * "socket://localhost:80" will do if your computer is
044: * running an http server. Please, contact your network
045: * administrator if you have a problem that you cannot
046: * resolve yourself.
047: */
048: static private final String otherSideUri =
049: //"socket://www.sun.com:80";
050: "socket://localhost:80";
051:
052: /** SocketConnection being tested. */
053: SocketConnection connection;
054:
055: /** InputStream for the blocking read. */
056: InputStream is;
057:
058: /** Thread that is blocked on the read. */
059: Thread t1;
060:
061: /** Records if the expected InterruptedIOException is thrown. */
062: boolean exceptionThrown = false;
063:
064: /** Records if the thread exited. */
065: boolean done = false;
066:
067: /**
068: * Run method to open the InputStream and block on a read.
069: * When the stream/connection is closed the read should
070: * unblock with an InterruptedIOException.
071: */
072: public void run() {
073: try {
074: is = connection.openInputStream();
075:
076: // Notify indicating the InputStream is open
077: synchronized (this ) {
078: notifyAll();
079: }
080:
081: // Block on a read
082: int ch = is.read();
083: assertTrue("read should not have succeeded", false);
084: } catch (InterruptedIOException ioe) {
085: assertNotNull("Caught InterruptedIOException", ioe);
086: exceptionThrown = true;
087: } catch (IOException ioe) {
088: assertNull("Unexpected IOException", ioe);
089: } finally {
090: synchronized (this ) {
091: done = true;
092: notifyAll();
093: }
094: }
095: }
096:
097: /**
098: * Open the connection and start the thread to call the run
099: * method. It waits until the InputStream is open before returning.
100: * @return false if testing cannot be continued
101: */
102: boolean setUp() {
103: // Create a socket connection
104: try {
105: connection = (SocketConnection) Connector
106: .open(otherSideUri);
107: //OutputStream os = connection.openOutputStream();
108: //os.write("GET /midlets/midlet.jad HTTP/1.0\n\n".getBytes());
109: } catch (IOException ioe) {
110: assertNull("Exception during socket open", ioe);
111: }
112:
113: assertNotNull("Verify socket open", connection);
114: if (null == connection) {
115: fail("Could not open connection to "
116: + otherSideUri
117: + ". If the host name cannot be resolved, you may "
118: + "need to change the i3test source to use a host name that"
119: + " is visible from your network.");
120: return false;
121: }
122:
123: // Launch a thread which would be blocked for some I/O operation
124: // for above socket connection
125: t1 = new Thread(this );
126: t1.start();
127:
128: // Wait for the InputStream to have been opened
129: synchronized (this ) {
130: while (is == null) {
131: try {
132: wait(200);
133: } catch (InterruptedException e) {
134: // Ignore interrupt
135: }
136: }
137: }
138: return true;
139: }
140:
141: /**
142: * Close the open connection.
143: */
144: void closeConnection() {
145: try {
146: if (is != null) {
147: is.close();
148: is = null;
149: }
150: if (connection != null) {
151: connection.close();
152: connection = null;
153: }
154: } catch (IOException e) {
155: assertNull("Exception closing a socket", e);
156: e.printStackTrace();
157: }
158: }
159:
160: /**
161: * This test creates another thread that tries to close a socket
162: * connection that is in use by first thread. It is supposed to throw
163: * InterruptedIOException in this case.
164: */
165: void testForException() {
166: closeConnection();
167:
168: synchronized (this ) {
169: if (!done) {
170: try {
171: wait(2000);
172: } catch (InterruptedException e) {
173: System.out.println("Catch interrupt");
174: }
175: }
176: }
177:
178: assertTrue("Verify InterruptedIOException", exceptionThrown);
179: }
180:
181: /**
182: * Clean up after the test.
183: */
184: void tearDown() {
185: closeConnection();
186: }
187:
188: /**
189: * Run the test by setting up the connection, and starting
190: * a thread to block on a read then forcing the
191: * connection to be closed and checking that exception occurred.
192: */
193: public void runTests() {
194: declare("Test InterruptedIOException");
195:
196: boolean setupSuccess = setUp();
197:
198: if (setupSuccess) {
199: testForException();
200:
201: tearDown();
202: }
203: }
204:
205: }
|