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

davidely
20.49.2007 d222583a4c71585b8c207e0f59441400c6ef37b4
Fixed ConcurrentModificationException in the unit test infrastructure that we saw with one build.  I've also added the enhancement to print the output of System.out and System.err to the command line and unit test report file for any test that fails.  I've updated the test docs on the web to reflect this.
4 files modified
202 ■■■■■ changed files
opends/tests/unit-tests-testng/src/server/org/opends/server/TestAccessLogger.java 17 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java 156 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/TestErrorLogger.java 17 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/TestListener.java 12 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/TestAccessLogger.java
@@ -31,6 +31,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import org.opends.server.api.AccessLogger;
import org.opends.server.api.ClientConnection;
@@ -110,16 +111,18 @@
  /**
   * Retrieves the set of messages logged to this access logger since the last
   * time it was cleared.  The caller must not attempt to alter the list in any
   * way.
   * Retrieves a copy of the set of messages logged to this error logger since
   * the last time it was cleared.  A copy of the list is returned to avoid
   * a ConcurrentModificationException.
   *
   * @return  The set of messages logged to this access logger since the last
   * @return  The set of messages logged to this error logger since the last
   *          time it was cleared.
   */
  public static List<String> getMessages()
  {
    return SINGLETON.messageList;
    synchronized (SINGLETON) {
      return new ArrayList<String>(SINGLETON.messageList);
    }
  }
@@ -129,7 +132,9 @@
   */
  public static void clear()
  {
    SINGLETON.messageList.clear();
    synchronized (SINGLETON) {
      SINGLETON.messageList.clear();
    }
  }
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -36,6 +36,8 @@
import java.util.HashMap;
import java.util.Enumeration;
import java.util.Map;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.LogManager;
@@ -667,6 +669,8 @@
    for (int i = 0; i < lines.length; i++) {
      buffer.append(lines[i]).append(EOL);
    }
    // Append an extra line so we can append LDIF Strings.
    buffer.append(EOL);
    return buffer.toString();
  }
@@ -836,6 +840,17 @@
    }
  }
  /**
   * Return a Map constructed via alternating key and value pairs.
   */
  public static LinkedHashMap<String,String> makeMap(String... keyValuePairs) {
    LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
    for (int i = 0; i < keyValuePairs.length; i += 2) {
      map.put(keyValuePairs[i], keyValuePairs[i+1]);
    }
    return map;
  }
  // ---------------------------------------------------------------------------
  // ---------------------------------------------------------------------------
  // ---------------------------------------------------------------------------
@@ -851,12 +866,20 @@
   *  must write something to System.out. */
  public final static PrintStream originalSystemOut = System.out;
  /** System.err is redirected to here so that we can only print it out
   *  if a test fails. */
  private final static ByteArrayOutputStream redirectedSystemErr = new ByteArrayOutputStream();
  /** System.out is redirected to here so that we can only print it out
   *  if a test fails. */
  private final static ByteArrayOutputStream redirectedSystemOut = new ByteArrayOutputStream();
  public synchronized static void suppressOutput() {
    String suppressStr = System.getProperty("org.opends.test.suppressOutput");
    if ((suppressStr != null) && suppressStr.equalsIgnoreCase("true"))
    {
      System.setOut(NullOutputStream.printStream());
      System.setErr(NullOutputStream.printStream());
      System.setOut(new PrintStream(redirectedSystemOut));
      System.setErr(new PrintStream(redirectedSystemErr));
      LogManager logManager = LogManager.getLogManager();
      Enumeration<String> loggerNames = logManager.getLoggerNames();
@@ -877,6 +900,38 @@
    }
  }
  /**
   * @return everything written to System.out since the last time
   * clearSystemOutContents was called.
   */
  public synchronized static String getSystemOutContents() {
    return redirectedSystemOut.toString();
  }
  /**
   * @return everything written to System.err since the last time
   * clearSystemErrContents was called.
   */
  public synchronized static String getSystemErrContents() {
    return redirectedSystemErr.toString();
  }
  /**
   * @return clear everything written to System.out since the last time
   * clearSystemOutContents was called.
   */
  public synchronized static void clearSystemOutContents() {
    redirectedSystemOut.reset();
  }
  /**
   * @return clear everything written to System.err since the last time
   * clearSystemErrContents was called.
   */
  public synchronized static void clearSystemErrContents() {
    redirectedSystemErr.reset();
  }
  public synchronized static void unsupressOutput() {
    System.setOut(originalSystemOut);
    System.setErr(originalSystemErr);
@@ -888,4 +943,101 @@
    }
    disabledLogHandlers.clear();
  }
  /**
   * Read the contents of a file and return it as a String.
   */
  public static String readFile(String name)
          throws IOException {
    return readFile(new File(name));
  }
  /**
   * Read the contents of a file and return it as a String.
   */
  public static String readFile(File file)
          throws IOException {
    byte[] bytes = readFileBytes(file);
    return new String(bytes);
  }
  /**
   * Read the contents of a file and return it as a String.
   */
  private static byte[] readFileBytes(File file)
          throws IOException {
    FileInputStream fis = null;
    byte[] bytes = null;
    fis = new FileInputStream(file);
    bytes = readInputStreamBytes(fis, true);
    return bytes;
  }
  /**
   * @param close - if true, close when finished reading.
   * @return input stream content.
   */
  private static byte[] readInputStreamBytes(InputStream is, boolean close)
          throws IOException {
    byte[] bytes = null;
    if (is != null) {
      ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
      try {
        byte[] buf = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = is.read(buf)) != -1) {
          bout.write(buf, 0, bytesRead);
        } // end of while ((read(buf) != -1)
        bytes = bout.toByteArray();
      }
      finally {
        if (close && is != null) {
          try {
            is.close();
          }
          catch (java.io.IOException ex) {
            // ignore these
          }
        } // end of if (is != null)
      }
    }
    return bytes;
  }
  /**
   * Store the contents of a String in a file.
   */
  public static void writeFile(File file, String contents)
          throws IOException {
    writeFile(file.getAbsolutePath(), contents);
  }
  /**
   * Store the contents of a String in a file.
   */
  public static void writeFile(String name, String contents)
          throws IOException {
    writeFile(name, contents.getBytes());
  }
  /**
   * Store the contents of a String in a file.
   */
  public static void writeFile(String path, byte[] contents)
          throws IOException {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(path);
      fos.write(contents);
    } finally {
      try {
        if (fos != null) fos.close();
      }
      catch (java.io.IOException e) {
        // ignore these
      }
    }
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/TestErrorLogger.java
@@ -31,6 +31,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import org.opends.server.api.ErrorLogger;
import org.opends.server.config.ConfigEntry;
@@ -38,7 +39,6 @@
import org.opends.server.types.ErrorLogSeverity;
/**
 * This class provides an implementation of an error logger which will store all
 * messages logged in memory.  It provides methods to retrieve and clear the
@@ -86,16 +86,18 @@
  /**
   * Retrieves the set of messages logged to this error logger since the last
   * time it was cleared.  The caller must not attempt to alter the list in any
   * way.
   * Retrieves a copy of the set of messages logged to this error logger since
   * the last time it was cleared.  A copy of the list is returned to avoid
   * a ConcurrentModificationException.
   *
   * @return  The set of messages logged to this error logger since the last
   *          time it was cleared.
   */
  public static List<String> getMessages()
  {
    return SINGLETON.messageList;
    synchronized (SINGLETON) {
      return new ArrayList<String>(SINGLETON.messageList);
    }
  }
@@ -105,10 +107,13 @@
   */
  public static void clear()
  {
    SINGLETON.messageList.clear();
    synchronized (SINGLETON) {
      SINGLETON.messageList.clear();
    }
  }
  /**
   * {@inheritDoc}
   */
opends/tests/unit-tests-testng/src/server/org/opends/server/TestListener.java
@@ -171,6 +171,8 @@
    super.onTestStart(tr);
    TestAccessLogger.clear();
    TestErrorLogger.clear();
    TestCaseUtils.clearSystemOutContents();
    TestCaseUtils.clearSystemErrContents();
  }
@@ -229,6 +231,16 @@
      }
    }
    String systemOut = TestCaseUtils.getSystemOutContents();
    if (systemOut.length() > 0) {
      failureInfo.append(EOL + "System.out contents:" + EOL + systemOut);
    }
    String systemErr = TestCaseUtils.getSystemErrContents();
    if (systemOut.length() > 0) {
      failureInfo.append(EOL + "System.err contents:" + EOL + systemErr);
    }
    failureInfo.append(EOL + EOL);
    originalSystemErr.print(EOL + EOL + EOL + "                 T E S T   F A I L U R E ! ! !" + EOL + EOL);
    originalSystemErr.print(failureInfo);