FileDocCategorySizeDatePackage
TestResourceLimit.javaAPI DocphoneME MR2 API (J2ME)6905Wed May 02 18:00:10 BST 2007javax.microedition.io

TestResourceLimit

public class TestResourceLimit extends TestCase
Unit tests for resource limit checks

Fields Summary
private static final String
otherSideUri
URI 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.
Constructors Summary
Methods Summary
public voidrunTests()
Main method that launches the unit tests

        declare("testTCPClientRscLimit");
        testTCPClientRscLimit();

        declare("testTCPServerRscLimit");
        testTCPServerRscLimit();

        declare("testDatagramRscLimit");
        testDatagramRscLimit();
    
voidtestDatagramRscLimit()
Test for datagram resource limit check

        boolean exceptionThrown = false;
        boolean cleanupFailed = false;
        int openCount = 0;
        int localServerPort = 10000;
        DatagramConnection[] sc = 
            new DatagramConnection[Constants.UDP_AMS_LIMIT+1];

        try {
            // Setup 
            while (openCount < Constants.UDP_AMS_LIMIT) {
                String s = "datagram://:" + Integer.toString(localServerPort + 
															 openCount);
                sc[openCount] = (DatagramConnection) Connector.open(s);
                openCount++; 
            }

            // Actual Test
            try {
                String s = "datagram://:" + Integer.toString(localServerPort + 
															 openCount);
                sc[openCount] = (DatagramConnection) Connector.open(s);
                openCount++;
            } catch (java.io.IOException io) {
                exceptionThrown = true;
            } 
        } finally {
            // Cleanup
            for (int i = 0; i < openCount; i++) {
                try {
                    sc[i].close();
                } catch (java.io.IOException io) {
                    cleanupFailed = true;
                }
            }
            if (cleanupFailed) {
                throw new java.io.IOException("Cleanup failed datagrams");
            }
        }

        assertTrue(exceptionThrown);
    
voidtestTCPClientRscLimit()
Test for TCP client resource limit check


                
        
        boolean exceptionThrown = false;
        boolean cleanupFailed = false;
        int openCount = 0;
        SocketConnection[] sc = 
            new SocketConnection[Constants.TCP_CLI_AMS_LIMIT+1];

        try {
            // Setup
            while (openCount < Constants.TCP_CLI_AMS_LIMIT) {
                sc[openCount] = (SocketConnection) Connector.open(
                        otherSideUri);
                openCount++;
            }

            // Actual Test
            try {
                sc[openCount] = (SocketConnection) Connector.open(
                        otherSideUri);
                openCount++;
            } catch (java.io.IOException io) {
                exceptionThrown = true;
            }
        } catch (ConnectionNotFoundException cnfe) {
            fail("Exception while opening "+otherSideUri+": "+cnfe.getMessage()+
                 ".  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.");
        } finally {
            // Cleanup
            for (int i = 0; i < openCount; i++) {
                try {
                    sc[i].close();
                } catch (java.io.IOException io) {
                    cleanupFailed = true;
                }
            }
            if (cleanupFailed) {
                throw new java.io.IOException("Cleanup failed TCP clients");
            }
        }

        assertTrue("expected exception thrown?",exceptionThrown);
    
voidtestTCPServerRscLimit()
Test for TCP server resource limit check

        boolean exceptionThrown = false;
        boolean cleanupFailed = false;
        int openCount = 0;
        int localServerPort = 10000;
        ServerSocketConnection[] sc = 
            new ServerSocketConnection[Constants.TCP_SER_AMS_LIMIT+1];

        try {
            // Setup 
            while (openCount < Constants.TCP_SER_AMS_LIMIT) {
                String s = "socket://:" + Integer.toString(localServerPort + 
														   openCount);
                sc[openCount] = (ServerSocketConnection) Connector.open(s);
                openCount++; 
            }

            // Actual Test
            try {
                String s = "socket://:" + Integer.toString(localServerPort + 
														   openCount);
                sc[openCount] = (ServerSocketConnection) Connector.open(s);
                openCount++;
            } catch (java.io.IOException io) {
                exceptionThrown = true;
            } 
        } finally {
            // Cleanup
            for (int i = 0; i < openCount; i++) {
                try {
                    sc[i].close();
                } catch (java.io.IOException io) {
                    cleanupFailed = true;
                }
            }
            if (cleanupFailed) {
                throw new java.io.IOException("Cleanup failed TCP servers");
            }
        }

        assertTrue(exceptionThrown);