Class JunitHelper

java.lang.Object
org.junit.rules.ExternalResource
fr.gouv.vitam.common.junit.JunitHelper
All Implemented Interfaces:
org.junit.rules.TestRule

public class JunitHelper extends org.junit.rules.ExternalResource
This class allows to get an available port during Junit execution
  • Field Details

  • Method Details

    • getInstance

      public static final JunitHelper getInstance()
      Returns:
      the unique instance
    • findAvailablePort

      public final int findAvailablePort()
      Returns:
      an available port if it exists
      Throws:
      IllegalStateException - if no port available
    • findAvailablePort

      public final int findAvailablePort(String environmentVariable)
      Parameters:
      environmentVariable - if not null, set the port nomber in the system environment
      Returns:
      an available port if it exists
      Throws:
      IllegalStateException - if no port available
    • releasePort

      public final void releasePort(int port)
      Remove the used port
      Parameters:
      port - to release
    • isListeningOn

      public final boolean isListeningOn(int port)
      Parameters:
      port - the port to check on localhost
      Returns:
      True if the port is used by the localhost server
      Throws:
      IllegalArgumentException - if the port is not between 1 and 65535
    • isListeningOn

      public final boolean isListeningOn(String host, int port)
      Parameters:
      host - the host to check
      port - the port to check on host
      Returns:
      True if the port is used by the specified host
      Throws:
      IllegalArgumentException - if the port is not between 1 and 65535
    • consumeInputStream

      public static final long consumeInputStream(InputStream inputStream)
      Read and close the inputStream using buffer read (read(buffer))
      Parameters:
      inputStream - to read and close
      Returns:
      the size of the inputStream read
    • consumeInputStreamPerByte

      public static final long consumeInputStreamPerByte(InputStream inputStream)
      Read and close the inputStream one byte at a time (read())
      Parameters:
      inputStream - to read and close
      Returns:
      the size of the inputStream read
    • getPerByteInputStream

      public static InputStream getPerByteInputStream(InputStream inputStream)
    • awaitFullGc

      public static final void awaitFullGc()
      For benchmark: clean the used memory using a full GC.

      Usage:
      JunitHelper.awaitFullGc();
      long firstAvailableMemory = Runtime.getRuntime().freeMemory();
      ... do some tests consuming memory JunitHelper.awaitFullGc();
      long secondAvailableMemory = Runtime.getRuntime().freeMemory();
      long usedMemory = firstAvailableMemory - secondAvailableMemory;
    • setJettyPortSystemProperty

      public static final void setJettyPortSystemProperty(String environmentVariable, int port)
      Set JettyPort System Property
      Parameters:
      environmentVariable -
      port - set to jetty server
    • unsetJettyPortSystemProperty

      public static final void unsetJettyPortSystemProperty()
      Unset JettyPort System Property
    • testPrivateConstructor

      public static final void testPrivateConstructor(Class<?> clasz)
      Utility to check empty private constructor
      Parameters:
      clasz - class template