Class BrokerRunningSupport
- java.lang.Object
-
- org.springframework.amqp.rabbit.junit.BrokerRunningSupport
-
public final class BrokerRunningSupport extends java.lang.Object
A class that can be used to prevent integration tests from failing if the Rabbit broker application is not running or not accessible. If the Rabbit broker is not running in the background all the tests here will simply be skipped (by default) because of a violated assumption (showing as successful).If you wish to enforce the broker being available, for example, on a CI server, set the environment variable "RABBITMQ_SERVER_REQUIRED" to
true
and the tests will fail fast.- Since:
- 2.2
- Author:
- Dave Syer, Gary Russell
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BrokerRunningSupport.BrokerNotAliveException
TheRuntimeException
thrown when broker is not available on the provided host port.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
BROKER_ADMIN_PW
static java.lang.String
BROKER_ADMIN_URI
static java.lang.String
BROKER_ADMIN_USER
static java.lang.String
BROKER_HOSTNAME
static java.lang.String
BROKER_PORT
static java.lang.String
BROKER_PW
static java.lang.String
BROKER_REQUIRED
static java.lang.String
BROKER_USER
static java.lang.String
DEFAULT_QUEUE_NAME
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
clearEnvironmentVariableOverrides()
Clear any environment variable overrides set insetEnvironmentVariableOverrides(Map)
.void
deleteExchanges(java.lang.String... exchanges)
Delete arbitrary exchanges from the broker.void
deleteQueues(java.lang.String... queuesToDelete)
Delete arbitrary queues from the broker.static boolean
fatal()
java.lang.String
generateId()
Generate the connection id for the connection used by the rule's connection factory.java.lang.String
getAdminPassword()
Return the admin password.java.lang.String
getAdminUri()
Return the admin uri.java.lang.String
getAdminUser()
Return the admin user.com.rabbitmq.client.ConnectionFactory
getConnectionFactory()
Get the connection factory used by this rule.java.lang.String
getHostName()
Return the port.java.lang.String
getPassword()
Return the password.int
getPort()
Return the port.java.lang.String
getUser()
Return the user.static BrokerRunningSupport
isBrokerAndManagementRunning()
static BrokerRunningSupport
isBrokerAndManagementRunningWithEmptyQueues(java.lang.String... queues)
static BrokerRunningSupport
isNotRunning()
boolean
isPurgeAfterEach()
static BrokerRunningSupport
isRunning()
static BrokerRunningSupport
isRunningWithEmptyQueues(java.lang.String... names)
Ensure the broker is running and has a empty queue(s) with the specified name(s) in the default exchange.void
purgeTestQueues()
Delete and re-declare all the configured queues.void
removeExchanges(java.lang.String... exchanges)
Remove exchanges from the broker.void
removeTestQueues(java.lang.String... additionalQueues)
Remove any test queues that were created by anisRunningWithEmptyQueues(String...)
method.void
setAdminPassword(java.lang.String password)
Set the password for the management REST API connection default "guest".void
setAdminUri(java.lang.String adminUri)
Set the uri for the REST API.void
setAdminUser(java.lang.String user)
Set the user for the management REST API connection default "guest".static void
setEnvironmentVariableOverrides(java.util.Map<java.lang.String,java.lang.String> environmentVariables)
Set environment variable overrides for host, port etc.void
setHostName(java.lang.String hostName)
void
setPassword(java.lang.String password)
Set the password for the amqp connection default "guest".void
setPort(int port)
void
setPurgeAfterEach(boolean purgeAfterEach)
Purge the test queues after each test (JUnit 5).void
setUser(java.lang.String user)
Set the user for the amqp connection default "guest".void
test()
Check connectivity to the broker and create any queues.
-
-
-
Field Detail
-
BROKER_ADMIN_URI
public static final java.lang.String BROKER_ADMIN_URI
- See Also:
- Constant Field Values
-
BROKER_HOSTNAME
public static final java.lang.String BROKER_HOSTNAME
- See Also:
- Constant Field Values
-
BROKER_PORT
public static final java.lang.String BROKER_PORT
- See Also:
- Constant Field Values
-
BROKER_USER
public static final java.lang.String BROKER_USER
- See Also:
- Constant Field Values
-
BROKER_PW
public static final java.lang.String BROKER_PW
- See Also:
- Constant Field Values
-
BROKER_ADMIN_USER
public static final java.lang.String BROKER_ADMIN_USER
- See Also:
- Constant Field Values
-
BROKER_ADMIN_PW
public static final java.lang.String BROKER_ADMIN_PW
- See Also:
- Constant Field Values
-
BROKER_REQUIRED
public static final java.lang.String BROKER_REQUIRED
- See Also:
- Constant Field Values
-
DEFAULT_QUEUE_NAME
public static final java.lang.String DEFAULT_QUEUE_NAME
-
-
Method Detail
-
setEnvironmentVariableOverrides
public static void setEnvironmentVariableOverrides(java.util.Map<java.lang.String,java.lang.String> environmentVariables)
Set environment variable overrides for host, port etc. Will override any real environment variables, if present.The variables will only apply to rule instances that are created after this method is called. The overrides will remain until
- Parameters:
environmentVariables
- the variables.
-
clearEnvironmentVariableOverrides
public static void clearEnvironmentVariableOverrides()
Clear any environment variable overrides set insetEnvironmentVariableOverrides(Map)
.
-
isRunningWithEmptyQueues
public static BrokerRunningSupport isRunningWithEmptyQueues(java.lang.String... names)
Ensure the broker is running and has a empty queue(s) with the specified name(s) in the default exchange.- Parameters:
names
- the queues to declare for the test.- Returns:
- a new rule that assumes an existing running broker
-
isRunning
public static BrokerRunningSupport isRunning()
- Returns:
- a new rule that assumes an existing running broker
-
isNotRunning
public static BrokerRunningSupport isNotRunning()
- Returns:
- a new rule that assumes there is no existing broker
-
isBrokerAndManagementRunning
public static BrokerRunningSupport isBrokerAndManagementRunning()
- Returns:
- a new rule that assumes an existing broker with the management plugin
-
isBrokerAndManagementRunningWithEmptyQueues
public static BrokerRunningSupport isBrokerAndManagementRunningWithEmptyQueues(java.lang.String... queues)
- Parameters:
queues
- the queues.- Returns:
- a new rule that assumes an existing broker with the management plugin with the provided queues declared (and emptied if needed)..
-
setPort
public void setPort(int port)
- Parameters:
port
- the port to set
-
setHostName
public void setHostName(java.lang.String hostName)
- Parameters:
hostName
- the hostName to set
-
setUser
public void setUser(java.lang.String user)
Set the user for the amqp connection default "guest".- Parameters:
user
- the user.
-
setPassword
public void setPassword(java.lang.String password)
Set the password for the amqp connection default "guest".- Parameters:
password
- the password.
-
setAdminUri
public void setAdminUri(java.lang.String adminUri)
Set the uri for the REST API.- Parameters:
adminUri
- the uri.
-
setAdminUser
public void setAdminUser(java.lang.String user)
Set the user for the management REST API connection default "guest".- Parameters:
user
- the user.
-
setAdminPassword
public void setAdminPassword(java.lang.String password)
Set the password for the management REST API connection default "guest".- Parameters:
password
- the password.
-
getPort
public int getPort()
Return the port.- Returns:
- the port.
-
getHostName
public java.lang.String getHostName()
Return the port.- Returns:
- the port.
-
getUser
public java.lang.String getUser()
Return the user.- Returns:
- the user.
-
getPassword
public java.lang.String getPassword()
Return the password.- Returns:
- the password.
-
getAdminUser
public java.lang.String getAdminUser()
Return the admin user.- Returns:
- the user.
-
getAdminPassword
public java.lang.String getAdminPassword()
Return the admin password.- Returns:
- the password.
-
isPurgeAfterEach
public boolean isPurgeAfterEach()
-
setPurgeAfterEach
public void setPurgeAfterEach(boolean purgeAfterEach)
Purge the test queues after each test (JUnit 5).- Parameters:
purgeAfterEach
- true to purge.
-
test
public void test() throws BrokerRunningSupport.BrokerNotAliveException
Check connectivity to the broker and create any queues.- Throws:
BrokerRunningSupport.BrokerNotAliveException
- if the broker is not available.
-
fatal
public static boolean fatal()
-
generateId
public java.lang.String generateId()
Generate the connection id for the connection used by the rule's connection factory.- Returns:
- the id.
-
removeTestQueues
public void removeTestQueues(java.lang.String... additionalQueues)
Remove any test queues that were created by anisRunningWithEmptyQueues(String...)
method.- Parameters:
additionalQueues
- additional queues to remove that might have been created by tests.
-
removeExchanges
public void removeExchanges(java.lang.String... exchanges)
Remove exchanges from the broker.- Parameters:
exchanges
- the exchanges.- Since:
- 2.3
-
purgeTestQueues
public void purgeTestQueues()
Delete and re-declare all the configured queues. Can be used between tests when a test might leave stale data and multiple tests use the same queue.
-
deleteQueues
public void deleteQueues(java.lang.String... queuesToDelete)
Delete arbitrary queues from the broker.- Parameters:
queuesToDelete
- the queues to delete.
-
deleteExchanges
public void deleteExchanges(java.lang.String... exchanges)
Delete arbitrary exchanges from the broker.- Parameters:
exchanges
- the exchanges to delete.
-
getConnectionFactory
public com.rabbitmq.client.ConnectionFactory getConnectionFactory()
Get the connection factory used by this rule.- Returns:
- the connection factory.
-
getAdminUri
public java.lang.String getAdminUri()
Return the admin uri.- Returns:
- the uri.
-
-