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

neil_a_wilson
31.14.2006 aa97bfb29f18f9fe83fe1d9056c7f56e67654c89
Update the test case framework so that it is now possible to suppress anything
written to standard output or standard error while the unit tests are running.
This includes direct references to System.out and System.err as well as
logging performed through the JDK logger.

By default, there will be no change in behavior. If the
org.opends.test.suppressOutput property is defined and has a value of "true",
then no output will be generated.

OpenDS Issue Number: 903
2 files modified
70 ■■■■■ changed files
opendj-sdk/opends/build.xml 9 ●●●●● patch | view | raw | blame | history
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/DirectoryServerTestCase.java 61 ●●●●● patch | view | raw | blame | history
opendj-sdk/opends/build.xml
@@ -678,6 +678,14 @@
        <isset property="TESTASSERT" />
      </not>
    </condition>
    <!-- This sets org.opends.test.suppressOutput if and only if it's not
         already set. -->
    <condition property="org.opends.test.suppressOutput" value="false">
      <not>
        <isset property="org.opends.test.suppressOutput" />
      </not>
    </condition>
    
    <testng outputdir="${unittest.report.dir}" haltonfailure="true"
            enableAssert="${TESTASSERT}">
@@ -700,6 +708,7 @@
      <jvmarg  value="-Demma.coverage.out.file=${coverage.data.dir}/unit.emma" />
      <jvmarg value="-Demma.coverage.out.merge=false" />
      <jvmarg value="-Dorg.opends.server.BuildRoot=${basedir}" />
      <jvmarg value="-Dorg.opends.test.suppressOutput=${org.opends.test.suppressOutput}" />
      <jvmarg value="-Xmx${MEM}" />
      <jvmarg value="-Xms${MEM}" />
      <xmlfileset dir="${unittest.resource.dir}" includes="testng.xml" />
opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/DirectoryServerTestCase.java
@@ -27,8 +27,17 @@
package org.opends.server;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.LogManager;
import org.opends.server.types.NullOutputStream;
/**
 * This class defines a base test case that should be subclassed by all
@@ -38,12 +47,60 @@
 * have them include the class name.
 */
public abstract class DirectoryServerTestCase {
  // The set of loggers for which the console logger has been disabled.
  private HashMap<Logger,Handler> disabledLogHandlers;
  // The print stream to use for printing error messages.
  private PrintStream errorStream;
  // The original System.err print stream.
  private PrintStream originalSystemErr;
  // The original System.out print stream.
  private PrintStream originalSystemOut;
  @BeforeSuite
  public final void suppressOutput() {
    String suppressStr = System.getProperty("org.opends.test.suppressOutput");
    if ((suppressStr != null) && suppressStr.equalsIgnoreCase("true"))
    {
      System.setOut(NullOutputStream.printStream());
      System.setErr(NullOutputStream.printStream());
      errorStream = NullOutputStream.printStream();
      LogManager logManager = LogManager.getLogManager();
      Enumeration<String> loggerNames = logManager.getLoggerNames();
      while (loggerNames.hasMoreElements())
      {
        String loggerName = loggerNames.nextElement();
        Logger logger = logManager.getLogger(loggerName);
        for (Handler h : logger.getHandlers())
        {
          if (h instanceof ConsoleHandler)
          {
            disabledLogHandlers.put(logger, h);
            logger.removeHandler(h);
            break;
          }
        }
      }
    }
  }
  @AfterSuite
  public final void shutdownServer() {
    TestCaseUtils.shutdownServer("The current test suite has finished.");
    System.setOut(originalSystemOut);
    System.setErr(originalSystemErr);
    errorStream = originalSystemErr;
    for (Logger l : disabledLogHandlers.keySet())
    {
      Handler h = disabledLogHandlers.get(l);
      l.addHandler(h);
    }
    disabledLogHandlers.clear();
  }
  /**
@@ -51,6 +108,10 @@
   */
  protected DirectoryServerTestCase() {
    this.errorStream = System.err;
    disabledLogHandlers = new HashMap<Logger,Handler>();
    originalSystemOut   = System.out;
    originalSystemErr   = System.err;
  }
  /**