mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Ludovic Poitou
14.52.2010 72650d4cc41c64136d064967d7fec3726d850fee
sdk/tests/unit-tests-testng/src/org/opends/sdk/TestCaseUtils.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2009 Sun Microsystems, Inc.
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 */
package org.opends.sdk;
@@ -31,6 +31,9 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
@@ -47,10 +50,10 @@
  public static final String PROPERTY_LDAP_PORT = "org.opends.server.LdapPort";
  /**
   * Port number that's used by the server. Need to be used by the testcases to
   * Port number that's used by the server. Need to be used by the test cases to
   * create connections.
   */
  public static int port = 11389;
  public static int port;
  static
  {
@@ -59,6 +62,10 @@
    {
      port = Integer.valueOf(ldapPort);
    }
    else
    {
      port = findFreePort();
    }
  }
@@ -67,6 +74,8 @@
   * Creates a temporary text file with the specified contents. It will be
   * marked for automatic deletion when the JVM exits.
   *
   * @param lines
   *          The file contents.
   * @return The absolute path to the file that was created.
   * @throws Exception
   *           If an unexpected problem occurs.
@@ -90,6 +99,31 @@
  /**
   * Finds a free server socket port on the local host.
   *
   * @return The free port.
   */
  public static int findFreePort()
  {
    int port;
    try
    {
      ServerSocket serverLdapSocket = new ServerSocket();
      serverLdapSocket.setReuseAddress(true);
      serverLdapSocket.bind(new InetSocketAddress("127.0.0.1", 0));
      port = serverLdapSocket.getLocalPort();
      serverLdapSocket.close();
    }
    catch (IOException e)
    {
      throw new RuntimeException(e);
    }
    return port;
  }
  /**
   * Returns an internal client connection to the running ldap server.
   *
   * @return The internal client connection.
@@ -122,10 +156,10 @@
   * Starts the test ldap server.
   *
   * @throws Exception
   *           If an error occurs when starting the server.
   */
  public static void startServer() throws Exception
  {
    // TODO:Try a couple of random ports before throwing exception.
    LDAPServer.getInstance().start(port);
  }