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.suspend;
028:
029: import com.sun.midp.i3test.TestCase;
030: import com.sun.midp.suspend.test.TestUtil;
031:
032: import javax.microedition.io.Connector;
033: import javax.microedition.io.StreamConnectionNotifier;
034: import javax.microedition.io.StreamConnection;
035: import java.io.IOException;
036: import java.io.DataInputStream;
037: import java.io.DataOutputStream;
038:
039: /**
040: * Tests connection behavior within suspend/resume cycles.
041: */
042: public class TestConnectionSuspend extends TestCase {
043: private Side server;
044: private Side client;
045:
046: private void init() {
047: TestUtil.setNoVMSuspendMode();
048: server = new ServerSide();
049: TestUtil.sleep(1000);
050: client = new ClientSide();
051: client.waitReady();
052: server.waitReady();
053: }
054:
055: public void runTests() throws Throwable {
056: init();
057:
058: declare("Test connection before Suspend");
059:
060: assertTrue("positive write", client.canWrite());
061: assertTrue("positive read", server.canRead());
062:
063: declare("Test connection after Suspend");
064: TestUtil.suspendMidp();
065: TestUtil.sleep();
066:
067: assertTrue("negative write", !client.canWrite());
068: assertTrue("negative read", !server.canRead());
069:
070: declare("Test connection after Resume");
071: TestUtil.resumeMidp();
072: TestUtil.sleep();
073:
074: assertTrue("negative write2", !client.canWrite());
075: assertTrue("negative read2", !server.canRead());
076: }
077: }
078:
079: abstract class Side implements Runnable {
080: private StreamConnection conn;
081: private DataInputStream in;
082: private DataOutputStream out;
083: private final Object lock = new Object();
084: private String error;
085: static final int port = 33133;
086:
087: abstract StreamConnection connect() throws IOException;
088:
089: boolean canRead() {
090: try {
091: in.readInt();
092: } catch (IOException e) {
093: return false;
094: }
095: return true;
096: }
097:
098: boolean canWrite() {
099: try {
100: out.writeInt(1);
101: out.flush();
102: } catch (IOException e) {
103: return false;
104: }
105: return true;
106: }
107:
108: void waitReady() {
109: synchronized (lock) {
110: if (null == conn && null == error) {
111: try {
112: lock.wait();
113: } catch (InterruptedException e) {
114: error = "interrupted";
115: }
116: }
117: }
118:
119: if (error != null) {
120: throw new RuntimeException(error);
121: }
122: }
123:
124: public void run() {
125: synchronized (lock) {
126: try {
127: conn = connect();
128: in = conn.openDataInputStream();
129: out = conn.openDataOutputStream();
130: } catch (IOException e) {
131: error = e.getMessage();
132: } finally {
133: lock.notify();
134: }
135: }
136: }
137:
138: Side() {
139: new Thread(this ).start();
140: }
141: }
142:
143: class ServerSide extends Side {
144: StreamConnection connect() throws IOException {
145: StreamConnectionNotifier notif = (StreamConnectionNotifier) Connector
146: .open("socket://:" + port);
147: return notif.acceptAndOpen();
148: }
149: }
150:
151: class ClientSide extends Side {
152: StreamConnection connect() throws IOException {
153: return (StreamConnection) Connector.open("socket://localhost:"
154: + port);
155: }
156: }
|