Class 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 Detail

      • PARAMETER_JETTY_SERVER_PORT_ADMIN

        public static final java.lang.String PARAMETER_JETTY_SERVER_PORT_ADMIN
        See Also:
        Constant Field Values
    • Method Detail

      • getInstance

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

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

        public final int findAvailablePort​(java.lang.String environmentVariable)
        Parameters:
        environmentVariable - if not null, set the port nomber in the system environment
        Returns:
        an available port if it exists
        Throws:
        java.lang.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:
        java.lang.IllegalArgumentException - if the port is not between 1 and 65535
      • isListeningOn

        public final boolean isListeningOn​(java.lang.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:
        java.lang.IllegalArgumentException - if the port is not between 1 and 65535
      • consumeInputStream

        public static final long consumeInputStream​(java.io.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​(java.io.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 java.io.InputStream getPerByteInputStream​(java.io.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​(java.lang.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​(java.lang.Class<?> clasz)
        Utility to check empty private constructor
        Parameters:
        clasz - class template