Package org.citrusframework.context
Class TestContext
java.lang.Object
org.citrusframework.context.TestContext
- All Implemented Interfaces:
TestActionListenerAware,ReferenceResolverAware
Class holding and managing test variables. The test context also provides utility methods
for replacing dynamic content(variables and functions) in message payloads and headers.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddException(CitrusRuntimeException exception) Add new exception to the context marking the test as failed.voidaddTestActionListener(TestActionListener listener) Adds a new test action listener.voidaddVariables(String[] variableNames, Object[] variableValues) Add variables to context.voidaddVariables(Map<String, Object> variablesToSet) Add several new variables to test context.voidclear()Clears variables in this test context.Obtains the afterTest.Obtains the beforeTest.Gets the endpoint factory.Gets the value of the exceptions property.Get the current function registry.Set global variables.Gets the logModifier.Gets the message listeners.Gets the global message processors.getMessageProcessors(MessageDirection direction) Gets the global message processors for given direction.Gets the value of the messageStore property.Get the message validator registry.Gets the namespace context builder.Gets the value of the referenceResolver property.Obtains the segmentVariableExtractorRegistryObtains the testActionListeners.Gets the test listeners.Obtains the typeConverter.Get the current validation matcher registrygetVariable(String variableExpression) Gets the value for the given variable expression.<T> TgetVariable(String variableExpression, Class<T> type) Gets typed variable value.getVariableObject(String variableExpression) Gets the value for the given variable as object representation.Getter for test variables in this context.handleError(String testName, String packageName, String message, Exception cause) Handles error creating a new CitrusRuntimeException and informs test listeners.booleanGets exception collection state.booleanChecks if variables are present right now.booleanisSuccess(TestResult testResult) Checks test result success in combination with this context exception state.voidonInboundMessage(Message receivedMessage) Informs message listeners if present that inbound message was received.voidonOutboundMessage(Message message) Informs message listeners if present that new outbound message is about to be sent.voidregisterTimer(String timerId, StopTimer timer) Registers a StopTimer in the test context, so that the associated timer can be stopped later on.Method replacing variable declarations and place holders as well as function expressions in a stringreplaceDynamicContentInString(String str, boolean enableQuoting) Method replacing variable declarations and functions in a string, optionally the variable values get surrounded with single quotes.resolveDynamicValue(String expression) Checks weather the given expression is a variable or function and resolves the value accordingly<T> T[]resolveDynamicValuesInArray(T[] array) Replaces variables and functions in array with respective values and returns the new array representation.<T> List<T>resolveDynamicValuesInList(List<T> list) Replaces variables and functions in a list with respective values and returns the new list representation.resolveDynamicValuesInMap(Map<String, T> map) Replaces variables and functions inside a map with respective values and returns a new map representation.voidsetAfterTest(List<AfterTest> afterTest) Specifies the afterTest.voidsetBeforeTest(List<BeforeTest> beforeTest) Specifies the beforeTest.voidsetEndpointFactory(EndpointFactory endpointFactory) Sets the endpoint factory.voidsetFunctionRegistry(FunctionRegistry functionRegistry) Set the function registry.voidsetGlobalVariables(GlobalVariables globalVariables) Copies the passedglobalVariablesand adds them to the test context.voidsetLogModifier(LogModifier logModifier) Sets the logModifier.voidsetMessageListeners(MessageListeners messageListeners) Set the message listeners.voidsetMessageProcessors(MessageProcessors messageProcessors) Sets the global message processors.voidsetMessageStore(MessageStore messageStore) Sets the messageStore property.voidsetMessageValidatorRegistry(MessageValidatorRegistry messageValidatorRegistry) Set the message validator registry.voidsetNamespaceContextBuilder(NamespaceContextBuilder namespaceContextBuilder) Sets the namespace context builder.voidsetReferenceResolver(ReferenceResolver referenceResolver) Sets the reference resolver.voidsetSegmentVariableExtractorRegistry(SegmentVariableExtractorRegistry segmentVariableExtractorRegistry) Specifies the segmentVariableExtractorRegistryvoidsetTestActionListeners(TestActionListeners testActionListeners) Specifies the testActionListeners.voidsetTestListeners(TestListeners testListeners) Set the test listeners.voidsetTypeConverter(TypeConverter typeConverter) Specifies the typeConverter.voidsetValidationMatcherRegistry(ValidationMatcherRegistry validationMatcherRegistry) Set the validation matcher registryvoidsetVariable(String variableName, Object value) Creates a new variable in this test context with the respective value.voidsetVariables(Map<String, Object> variables) Setter for test variables in this context.booleanStops the timer matching the supplied idvoidStops all timers
-
Field Details
-
variables
Local variables -
timers
Timers registered in test context, that can be stopped
-
-
Constructor Details
-
TestContext
public TestContext()Default constructor
-
-
Method Details
-
getVariable
Gets the value for the given variable expression. Expression usually is the simple variable name, with optional expression prefix/suffix.In case variable is not known to the context throw runtime exception.
- Parameters:
variableExpression- expression to search for.- Returns:
- value of the variable
- Throws:
CitrusRuntimeException
-
getVariable
Gets typed variable value.- Type Parameters:
T-- Parameters:
variableExpression-type-- Returns:
-
getVariableObject
Gets the value for the given variable as object representation. Use this method if you seek for test objects stored in the context.- Parameters:
variableExpression- expression to search for.- Returns:
- value of the variable as object
- Throws:
CitrusRuntimeException
-
setVariable
Creates a new variable in this test context with the respective value. In case variable already exists variable is overwritten.- Parameters:
variableName- the name of the new variablevalue- the new variable value- Throws:
CitrusRuntimeException
-
addVariables
Add variables to context.- Parameters:
variableNames- the variable names to setvariableValues- the variable values to set
-
addVariables
Add several new variables to test context. Existing variables will be overwritten.- Parameters:
variablesToSet- the list of variables to set.
-
resolveDynamicValuesInMap
Replaces variables and functions inside a map with respective values and returns a new map representation.- Parameters:
map- optionally having variable entries.- Returns:
- the constructed map without variable entries.
-
resolveDynamicValuesInList
Replaces variables and functions in a list with respective values and returns the new list representation.- Parameters:
list- having optional variable entries.- Returns:
- the constructed list without variable entries.
-
resolveDynamicValuesInArray
public <T> T[] resolveDynamicValuesInArray(T[] array) Replaces variables and functions in array with respective values and returns the new array representation.- Parameters:
array- having optional variable entries.- Returns:
- the constructed list without variable entries.
-
clear
public void clear()Clears variables in this test context. Initially adds all global variables. -
hasVariables
public boolean hasVariables()Checks if variables are present right now.- Returns:
- boolean flag to mark existence
-
replaceDynamicContentInString
Method replacing variable declarations and place holders as well as function expressions in a string- Parameters:
str- the string to parse.- Returns:
- resulting string without any variable place holders.
-
replaceDynamicContentInString
Method replacing variable declarations and functions in a string, optionally the variable values get surrounded with single quotes.- Parameters:
str- the string to parse for variable place holders.enableQuoting- flag marking surrounding quotes should be added or not.- Returns:
- resulting string without any variable place holders.
-
resolveDynamicValue
Checks weather the given expression is a variable or function and resolves the value accordingly- Parameters:
expression- the expression to resolve- Returns:
- the resolved expression value
-
handleError
public CitrusRuntimeException handleError(String testName, String packageName, String message, Exception cause) Handles error creating a new CitrusRuntimeException and informs test listeners.- Parameters:
testName-packageName-message-cause-- Returns:
-
setVariables
Setter for test variables in this context.- Parameters:
variables-
-
getVariables
Getter for test variables in this context.- Returns:
- test variables for this test context.
-
setGlobalVariables
Copies the passedglobalVariablesand adds them to the test context.
If any of the copied global variables contain dynamic content (references to other global variables or functions) then this is resolved now. As a result it is importantsetFunctionRegistry(FunctionRegistry)is called first before calling this method.- Parameters:
globalVariables-
-
getGlobalVariables
Set global variables.- Returns:
- the globalVariables
-
setMessageStore
Sets the messageStore property.- Parameters:
messageStore-
-
getMessageStore
Gets the value of the messageStore property.- Returns:
- the messageStore
-
getFunctionRegistry
Get the current function registry.- Returns:
- the functionRegistry
-
setFunctionRegistry
Set the function registry.- Parameters:
functionRegistry- the functionRegistry to set
-
setMessageValidatorRegistry
Set the message validator registry.- Parameters:
messageValidatorRegistry- the messageValidatorRegistry to set
-
getMessageValidatorRegistry
Get the message validator registry.- Returns:
- the messageValidatorRegistry
-
getValidationMatcherRegistry
Get the current validation matcher registry- Returns:
-
setValidationMatcherRegistry
Set the validation matcher registry- Parameters:
validationMatcherRegistry-
-
getMessageListeners
Gets the message listeners.- Returns:
-
setMessageListeners
Set the message listeners.- Parameters:
messageListeners-
-
getTestListeners
Gets the test listeners.- Returns:
-
setTestListeners
Set the test listeners.- Parameters:
testListeners-
-
getTestActionListeners
Obtains the testActionListeners.- Returns:
-
setTestActionListeners
Specifies the testActionListeners.- Parameters:
testActionListeners-
-
addTestActionListener
Description copied from interface:TestActionListenerAwareAdds a new test action listener.- Specified by:
addTestActionListenerin interfaceTestActionListenerAware
-
getBeforeTest
Obtains the beforeTest.- Returns:
-
setBeforeTest
Specifies the beforeTest.- Parameters:
beforeTest-
-
getAfterTest
Obtains the afterTest.- Returns:
-
setAfterTest
Specifies the afterTest.- Parameters:
afterTest-
-
getSegmentVariableExtractorRegistry
Obtains the segmentVariableExtractorRegistry- Returns:
-
setSegmentVariableExtractorRegistry
public void setSegmentVariableExtractorRegistry(SegmentVariableExtractorRegistry segmentVariableExtractorRegistry) Specifies the segmentVariableExtractorRegistry- Parameters:
segmentVariableExtractorRegistry-
-
getMessageProcessors
Gets the global message processors for given direction.- Returns:
-
getMessageProcessors
Gets the global message processors.- Returns:
-
setMessageProcessors
Sets the global message processors.- Parameters:
messageProcessors-
-
getEndpointFactory
Gets the endpoint factory.- Returns:
-
setEndpointFactory
Sets the endpoint factory.- Parameters:
endpointFactory-
-
getReferenceResolver
Gets the value of the referenceResolver property.- Returns:
- the referenceResolver
-
setReferenceResolver
Description copied from interface:ReferenceResolverAwareSets the reference resolver.- Specified by:
setReferenceResolverin interfaceReferenceResolverAware
-
setNamespaceContextBuilder
Sets the namespace context builder.- Parameters:
namespaceContextBuilder-
-
getNamespaceContextBuilder
Gets the namespace context builder.- Returns:
-
getTypeConverter
Obtains the typeConverter.- Returns:
-
setTypeConverter
Specifies the typeConverter.- Parameters:
typeConverter-
-
getLogModifier
Gets the logModifier.- Returns:
-
setLogModifier
Sets the logModifier.- Parameters:
logModifier-
-
onInboundMessage
Informs message listeners if present that inbound message was received.- Parameters:
receivedMessage-
-
onOutboundMessage
Informs message listeners if present that new outbound message is about to be sent.- Parameters:
message-
-
registerTimer
Registers a StopTimer in the test context, so that the associated timer can be stopped later on.- Parameters:
timerId- a unique timer id
-
stopTimer
Stops the timer matching the supplied id- Parameters:
timerId-- Returns:
- true if time found and stopped, matching the supplied timerId
-
stopTimers
public void stopTimers()Stops all timers -
addException
Add new exception to the context marking the test as failed. This is usually used by actions to mark exceptions during forked operations.- Parameters:
exception-
-
getExceptions
Gets the value of the exceptions property.- Returns:
- the exceptions
-
hasExceptions
public boolean hasExceptions()Gets exception collection state.- Returns:
-
isSuccess
Checks test result success in combination with this context exception state.- Parameters:
testResult-- Returns:
-