Package fr.gouv.vitam.common.junit

# 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 Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `MIN_PORT`
  `static final String`
  `PARAMETER_JETTY_SERVER_PORT_ADMIN`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static final void`
  `awaitFullGc()`
  For benchmark: clean the used memory using a full GC. Usage: JunitHelper.awaitFullGc(); long firstAvailableMemory = Runtime.getRuntime().freeMemory(); ...
  `static final long`
  `consumeInputStream(InputStream inputStream)`
  Read and close the inputStream using buffer read (read(buffer))
  `static final long`
  `consumeInputStreamPerByte(InputStream inputStream)`
  Read and close the inputStream one byte at a time (read())
  `final int`
  `findAvailablePort()`
  `final int`
  `findAvailablePort(String environmentVariable)`
  `static final JunitHelper`
  `getInstance()`
  `static InputStream`
  `getPerByteInputStream(InputStream inputStream)`
  `final boolean`
  `isListeningOn(int port)`
  `final boolean`
  `isListeningOn(String host, int port)`
  `final void`
  `releasePort(int port)`
  Remove the used port
  `static final void`
  `setJettyPortSystemProperty(String environmentVariable, int port)`
  Set JettyPort System Property
  `static final void`
  `testPrivateConstructor(Class<?> clasz)`
  Utility to check empty private constructor
  `static final void`
  `unsetJettyPortSystemProperty()`
  Unset JettyPort System Property

  ### Methods inherited from class org.junit.rules.ExternalResource

  `after, apply, before`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### MIN_PORT

    public static final int MIN_PORT
    See Also:
      - Constant Field Values

  - ### PARAMETER_JETTY_SERVER_PORT_ADMIN

    public static final String PARAMETER_JETTY_SERVER_PORT_ADMIN
    See Also:
      - Constant Field Values

- ## 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