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: package javax.microedition.io;
027:
028: import com.sun.midp.i3test.*;
029: import javax.microedition.rms.RecordStore;
030: import javax.microedition.rms.RecordStoreException;
031: import com.sun.midp.configurator.Constants;
032:
033: /**
034: * Unit tests for resource limit checks
035: */
036:
037: public class TestResourceLimit extends TestCase {
038: /**
039: * URI to which the test will try to open connections.
040: * Use any URI visible from your network, for example,
041: * "socket://www.sun.com:80" will do in most cases;
042: * "socket://localhost:80" will do if your computer is
043: * running an http server. Please, contact your network
044: * administrator if you have a problem that you cannot
045: * resolve yourself.
046: */
047: static private final String otherSideUri =
048: //"socket://www.sun.com:80";
049: "socket://localhost:80";
050:
051: /**
052: * Test for TCP client resource limit check
053: */
054: void testTCPClientRscLimit() throws java.io.IOException {
055: boolean exceptionThrown = false;
056: boolean cleanupFailed = false;
057: int openCount = 0;
058: SocketConnection[] sc = new SocketConnection[Constants.TCP_CLI_AMS_LIMIT + 1];
059:
060: try {
061: // Setup
062: while (openCount < Constants.TCP_CLI_AMS_LIMIT) {
063: sc[openCount] = (SocketConnection) Connector
064: .open(otherSideUri);
065: openCount++;
066: }
067:
068: // Actual Test
069: try {
070: sc[openCount] = (SocketConnection) Connector
071: .open(otherSideUri);
072: openCount++;
073: } catch (java.io.IOException io) {
074: exceptionThrown = true;
075: }
076: } catch (ConnectionNotFoundException cnfe) {
077: fail("Exception while opening "
078: + otherSideUri
079: + ": "
080: + cnfe.getMessage()
081: + ". If the host name cannot be resolved, you may "
082: + "need to change the i3test source to use a host name that"
083: + " is visible from your network.");
084: } finally {
085: // Cleanup
086: for (int i = 0; i < openCount; i++) {
087: try {
088: sc[i].close();
089: } catch (java.io.IOException io) {
090: cleanupFailed = true;
091: }
092: }
093: if (cleanupFailed) {
094: throw new java.io.IOException(
095: "Cleanup failed TCP clients");
096: }
097: }
098:
099: assertTrue("expected exception thrown?", exceptionThrown);
100: }
101:
102: /**
103: * Test for TCP server resource limit check
104: */
105: void testTCPServerRscLimit() throws java.io.IOException {
106: boolean exceptionThrown = false;
107: boolean cleanupFailed = false;
108: int openCount = 0;
109: int localServerPort = 10000;
110: ServerSocketConnection[] sc = new ServerSocketConnection[Constants.TCP_SER_AMS_LIMIT + 1];
111:
112: try {
113: // Setup
114: while (openCount < Constants.TCP_SER_AMS_LIMIT) {
115: String s = "socket://:"
116: + Integer.toString(localServerPort + openCount);
117: sc[openCount] = (ServerSocketConnection) Connector
118: .open(s);
119: openCount++;
120: }
121:
122: // Actual Test
123: try {
124: String s = "socket://:"
125: + Integer.toString(localServerPort + openCount);
126: sc[openCount] = (ServerSocketConnection) Connector
127: .open(s);
128: openCount++;
129: } catch (java.io.IOException io) {
130: exceptionThrown = true;
131: }
132: } finally {
133: // Cleanup
134: for (int i = 0; i < openCount; i++) {
135: try {
136: sc[i].close();
137: } catch (java.io.IOException io) {
138: cleanupFailed = true;
139: }
140: }
141: if (cleanupFailed) {
142: throw new java.io.IOException(
143: "Cleanup failed TCP servers");
144: }
145: }
146:
147: assertTrue(exceptionThrown);
148: }
149:
150: /**
151: * Test for datagram resource limit check
152: */
153: void testDatagramRscLimit() throws java.io.IOException {
154: boolean exceptionThrown = false;
155: boolean cleanupFailed = false;
156: int openCount = 0;
157: int localServerPort = 10000;
158: DatagramConnection[] sc = new DatagramConnection[Constants.UDP_AMS_LIMIT + 1];
159:
160: try {
161: // Setup
162: while (openCount < Constants.UDP_AMS_LIMIT) {
163: String s = "datagram://:"
164: + Integer.toString(localServerPort + openCount);
165: sc[openCount] = (DatagramConnection) Connector.open(s);
166: openCount++;
167: }
168:
169: // Actual Test
170: try {
171: String s = "datagram://:"
172: + Integer.toString(localServerPort + openCount);
173: sc[openCount] = (DatagramConnection) Connector.open(s);
174: openCount++;
175: } catch (java.io.IOException io) {
176: exceptionThrown = true;
177: }
178: } finally {
179: // Cleanup
180: for (int i = 0; i < openCount; i++) {
181: try {
182: sc[i].close();
183: } catch (java.io.IOException io) {
184: cleanupFailed = true;
185: }
186: }
187: if (cleanupFailed) {
188: throw new java.io.IOException(
189: "Cleanup failed datagrams");
190: }
191: }
192:
193: assertTrue(exceptionThrown);
194: }
195:
196: /**
197: * Main method that launches the unit tests
198: */
199: public void runTests() throws Throwable {
200: declare("testTCPClientRscLimit");
201: testTCPClientRscLimit();
202:
203: declare("testTCPServerRscLimit");
204: testTCPServerRscLimit();
205:
206: declare("testDatagramRscLimit");
207: testDatagramRscLimit();
208: }
209:
210: }
|