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 int
MIN_PORT
static java.lang.String
PARAMETER_JETTY_SERVER_PORT_ADMIN
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
awaitFullGc()
For benchmark: clean the used memory using a full GC. Usage: JunitHelper.awaitFullGc(); long firstAvailableMemory = Runtime.getRuntime().freeMemory(); ...static long
consumeInputStream(java.io.InputStream inputStream)
Read and close the inputStream using buffer read (read(buffer))static long
consumeInputStreamPerByte(java.io.InputStream inputStream)
Read and close the inputStream one byte at a time (read())int
findAvailablePort()
int
findAvailablePort(java.lang.String environmentVariable)
static JunitHelper
getInstance()
static java.io.InputStream
getPerByteInputStream(java.io.InputStream inputStream)
boolean
isListeningOn(int port)
boolean
isListeningOn(java.lang.String host, int port)
void
releasePort(int port)
Remove the used portstatic void
setJettyPortSystemProperty(java.lang.String environmentVariable, int port)
Set JettyPort System Propertystatic void
testPrivateConstructor(java.lang.Class<?> clasz)
Utility to check empty private constructorstatic void
unsetJettyPortSystemProperty()
Unset JettyPort System Property
-
-
-
Field Detail
-
MIN_PORT
public static final int MIN_PORT
- See Also:
- Constant Field Values
-
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 checkport
- 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
-
-