001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.midp.io.j2me.socket;
027:
028: import java.io.*;
029: import javax.microedition.io.*;
030: import com.sun.midp.i3test.*;
031:
032: public class TestNonblockingSocket extends TestCase {
033:
034: static final int PORT = 1234;
035: static final String TEST_MSG = "klaatu barada nikto\r\n";
036:
037: ServerSocketConnection server;
038: SocketConnection client;
039:
040: void setUp() {
041: try {
042: // Create the server listening socket
043: server = (ServerSocketConnection) Connector
044: .open("socket://:" + PORT);
045: // System.out.println("Server socket opened");
046:
047: // Create client socket
048: client = (SocketConnection) Connector
049: .open("socket://localhost:" + PORT);
050: // System.out.println("Client socket opened");
051: } catch (IOException ioe) {
052: System.out.println("TestSocket setUp failed with:");
053: ioe.printStackTrace();
054: }
055: }
056:
057: /**
058: * This test writes to client socket before accepting server socket.
059: * If client socket write is blocking, the server socket won't be
060: * responding to its write. Therefore, the accepting will fail.
061: * Passing this test proves that client socket write is nonblocking.
062: */
063: void testNonblocking() {
064: try {
065: // Write to client socket
066: // System.out.println("Writing: " + TEST_MSG);
067: OutputStream os = client.openOutputStream();
068: os.write(TEST_MSG.getBytes());
069: os.close();
070:
071: // Wait for incoming connection to server socket
072: // System.out.println("Accepting serversocket");
073: SocketConnection sc = (SocketConnection) server
074: .acceptAndOpen();
075: // System.out.println("Accepted serversocket");
076:
077: // Read from server socket
078: InputStream is = sc.openInputStream();
079: byte buf[] = new byte[2 * TEST_MSG.length()];
080: int i = 0;
081: do {
082: buf[i++] = (byte) is.read();
083: } while (buf[i - 1] != -1 && buf[i - 1] != '\n'
084: && i < buf.length);
085:
086: is.close();
087: sc.close();
088:
089: String recvd = new String(buf, 0, i);
090: // System.out.println("recvd=\""+recvd+"\"");
091:
092: assertTrue(recvd.equals(TEST_MSG));
093:
094: } catch (IOException ioe) {
095: System.out
096: .println("TestSocket:Nonblocking test failed with:");
097: ioe.printStackTrace();
098: }
099: }
100:
101: void tearDown() {
102: try {
103: // System.out.println("Closing connection");
104: client.close();
105: server.close();
106: } catch (IOException e) {
107: System.out.println("TestSocket tearDown failed with:");
108: e.printStackTrace();
109: }
110: }
111:
112: public void runTests() {
113: setUp();
114:
115: declare("Test Nonblocking");
116: testNonblocking();
117:
118: tearDown();
119: }
120:
121: }
|