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
-
Method Summary
Modifier and TypeMethodDescriptionstatic final void
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
final int
findAvailablePort
(String environmentVariable) static final JunitHelper
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 portstatic final void
setJettyPortSystemProperty
(String environmentVariable, int port) Set JettyPort System Propertystatic final void
testPrivateConstructor
(Class<?> clasz) Utility to check empty private constructorstatic final void
Unset JettyPort System PropertyMethods inherited from class org.junit.rules.ExternalResource
after, apply, before
-
Field Details
-
MIN_PORT
public static final int MIN_PORT- See Also:
-
PARAMETER_JETTY_SERVER_PORT_ADMIN
- See Also:
-
-
Method Details
-
getInstance
- Returns:
- the unique instance
-
findAvailablePort
public final int findAvailablePort()- Returns:
- an available port if it exists
- Throws:
IllegalStateException
- if no port available
-
findAvailablePort
- 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
- Parameters:
host
- the host to checkport
- 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
Read and close the inputStream using buffer read (read(buffer))- Parameters:
inputStream
- to read and close- Returns:
- the size of the inputStream read
-
consumeInputStreamPerByte
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
-
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
Set JettyPort System Property- Parameters:
environmentVariable
-port
- set to jetty server
-
unsetJettyPortSystemProperty
public static final void unsetJettyPortSystemProperty()Unset JettyPort System Property -
testPrivateConstructor
Utility to check empty private constructor- Parameters:
clasz
- class template
-