From d92cdc6af2610cda5fc909330c90f3eaa02394b8 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 13 Mar 2013 13:42:18 +0000
Subject: [PATCH] Fixed a randomly failing test.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java |   70 +++++++++++++++++++++-------------
 1 files changed, 43 insertions(+), 27 deletions(-)

diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
index 34279d5..a7577ff 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -23,7 +23,7 @@
  *
  *
  *      Copyright 2006-2010 Sun Microsystems, Inc.
- *      Portions Copyright 2011-2012 ForgeRock AS
+ *      Portions Copyright 2011-2013 ForgeRock AS
  */
 package org.opends.server;
 
@@ -49,6 +49,7 @@
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.SocketAddress;
 import java.net.SocketException;
 import java.util.*;
 import java.util.logging.ConsoleHandler;
@@ -72,15 +73,15 @@
 import org.opends.server.backends.jeb.RootContainer;
 import org.opends.server.config.ConfigException;
 import org.opends.server.core.AddOperation;
-import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.DeleteOperation;
+import org.opends.server.core.DirectoryServer;
 import org.opends.server.extensions.ConfigFileHandler;
-import org.opends.server.loggers.TextAccessLogPublisher;
-import org.opends.server.loggers.TextErrorLogPublisher;
 import org.opends.server.loggers.AccessLogger;
 import org.opends.server.loggers.ErrorLogger;
-import org.opends.server.loggers.debug.TextDebugLogPublisher;
+import org.opends.server.loggers.TextAccessLogPublisher;
+import org.opends.server.loggers.TextErrorLogPublisher;
 import org.opends.server.loggers.debug.DebugLogger;
+import org.opends.server.loggers.debug.TextDebugLogPublisher;
 import org.opends.server.plugins.InvocationCounterPlugin;
 import org.opends.server.protocols.asn1.ASN1;
 import org.opends.server.protocols.asn1.ASN1Reader;
@@ -135,10 +136,9 @@
 
   /**
    * The name of the system property that specifies if the test instance
-   * direcotry needs to be wiped out before starting the setup or not.
-   * This will let the caller the possibility to copy some files
-   * (ie extensions) inside the test instance directory before the server
-   * starts up.
+   * directory needs to be wiped out before starting the setup or not. This will
+   * let the caller the possibility to copy some files (ie extensions) inside
+   * the test instance directory before the server starts up.
    */
   public static final String PROPERTY_CLEANUP_REQUIRED =
       "org.opends.server.CleanupDirectories";
@@ -152,9 +152,9 @@
        "org.opends.server.LdapPort";
 
   /**
-   * The name of the system property that specifies the admin port.
-   * Set this prtoperty when running the server if you want to use a given
-   * port number, otherwise a port is choosed randomly at test startup time.
+   * The name of the system property that specifies the admin port. Set this
+   * property when running the server if you want to use a given port number,
+   * otherwise a port is chosen randomly at test startup time.
    */
   public static final String PROPERTY_ADMIN_PORT =
        "org.opends.server.AdminPort";
@@ -620,7 +620,7 @@
     }
   }
 
-  public static List<Long> restartTimesMs = new ArrayList<Long>();
+  private static List<Long> restartTimesMs = new ArrayList<Long>();
   public static List<Long> getRestartTimesMs() {
     return Collections.unmodifiableList(restartTimesMs);
   }
@@ -742,14 +742,33 @@
    */
   public static ServerSocket bindFreePort() throws IOException
   {
-    ServerSocket serverLdapSocket;
-    serverLdapSocket = new ServerSocket();
+    ServerSocket serverLdapSocket = new ServerSocket();
     serverLdapSocket.setReuseAddress(true);
     serverLdapSocket.bind(new InetSocketAddress("127.0.0.1", 0));
     return serverLdapSocket;
   }
 
   /**
+   * Finds a free server socket port on the local host.
+   *
+   * @return The free port.
+   */
+  public static SocketAddress findFreeSocketAddress()
+  {
+    try
+    {
+      ServerSocket serverLdapSocket = bindFreePort();
+      final SocketAddress address = serverLdapSocket.getLocalSocketAddress();
+      serverLdapSocket.close();
+      return address;
+    }
+    catch (IOException e)
+    {
+      throw new RuntimeException(e);
+    }
+  }
+
+  /**
    * Shut down the server, if it has been started.
    * @param reason The reason for the shutdown.
    */
@@ -1551,7 +1570,7 @@
   // ---------------------------------------------------------------------------
   // ---------------------------------------------------------------------------
 
-  // The set of loggers for which the console logger has been disabled.
+  /** The set of loggers for which the console logger has been disabled. */
   private final static Map<Logger, Handler> disabledLogHandlers = new HashMap<Logger,Handler>();
 
   /** The original System.err print stream.  Use this if you absolutely
@@ -1757,11 +1776,8 @@
    */
   private static byte[] readFileBytes(File file)
           throws IOException {
-    FileInputStream fis;
-    byte[] bytes;
-    fis = new FileInputStream(file);
-    bytes = readInputStreamBytes(fis, true);
-    return bytes;
+    FileInputStream fis = new FileInputStream(file);
+    return readInputStreamBytes(fis, true);
   }
 
   /**
@@ -1943,21 +1959,21 @@
 
       if (stackElements != null)
       {
-        for (int j=0; j < stackElements.length; j++)
+        for (StackTraceElement stackElement : stackElements)
         {
-          buffer.append("   ").append(stackElements[j].getClassName());
+          buffer.append("   ").append(stackElement.getClassName());
           buffer.append(".");
-          buffer.append(stackElements[j].getMethodName());
+          buffer.append(stackElement.getMethodName());
           buffer.append("(");
-          buffer.append(stackElements[j].getFileName());
+          buffer.append(stackElement.getFileName());
           buffer.append(":");
-          if (stackElements[j].isNativeMethod())
+          if (stackElement.isNativeMethod())
           {
             buffer.append("native");
           }
           else
           {
-            buffer.append(stackElements[j].getLineNumber());
+            buffer.append(stackElement.getLineNumber());
           }
           buffer.append(")").append(EOL);
         }

--
Gitblit v1.10.0