org.apache.camel.test
Class CamelTestSupport

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.apache.camel.test.TestSupport
              extended by org.apache.camel.test.CamelTestSupport
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
CamelSpringTestSupport, ExchangeTestSupport

public abstract class CamelTestSupport
extends TestSupport

A useful base class which creates a CamelContext with some routes along with a ProducerTemplate for use in the test case

Version:

Field Summary
protected  org.apache.camel.ConsumerTemplate consumer
           
protected  org.apache.camel.model.ModelCamelContext context
           
protected  org.apache.camel.ProducerTemplate template
           
 
Fields inherited from class org.apache.camel.test.TestSupport
log, LS
 
Constructor Summary
CamelTestSupport()
           
 
Method Summary
protected  void assertExpression(org.apache.camel.Exchange exchange, String languageName, String expressionText, Object expectedValue)
          Asserts that the given language name and expression evaluates to the given value on a specific exchange
protected  void assertMockEndpointsSatisfied()
          Asserts that all the expectations of the Mock endpoints are valid
protected  void assertMockEndpointsSatisfied(long timeout, TimeUnit unit)
          Asserts that all the expectations of the Mock endpoints are valid
protected  void assertPredicate(String languageName, String expressionText, org.apache.camel.Exchange exchange, boolean expected)
          Asserts that the given language name and predicate expression evaluates to the expected value on the message exchange
protected  org.apache.camel.spi.Language assertResolveLanguage(String languageName)
          Asserts that the language name can be resolved
protected  void assertValidContext(org.apache.camel.CamelContext context)
           
protected  org.apache.camel.CamelContext createCamelContext()
           
protected  org.apache.camel.Exchange createExchangeWithBody(Object body)
          Creates an exchange with the given body
protected  Context createJndiContext()
           
protected  org.apache.camel.impl.JndiRegistry createRegistry()
           
protected  org.apache.camel.builder.RouteBuilder createRouteBuilder()
          Factory method which derived classes can use to create a RouteBuilder to define the routes for testing
protected  org.apache.camel.builder.RouteBuilder[] createRouteBuilders()
          Factory method which derived classes can use to create an array of RouteBuilders to define the routes for testing
protected  void debugAfter(org.apache.camel.Exchange exchange, org.apache.camel.Processor processor, org.apache.camel.model.ProcessorDefinition definition, String id, String shortName, long timeTaken)
          Single step debugs and Camel invokes this method after processing the given processor
protected  void debugBefore(org.apache.camel.Exchange exchange, org.apache.camel.Processor processor, org.apache.camel.model.ProcessorDefinition definition, String id, String shortName)
          Single step debugs and Camel invokes this method before entering the given processor
protected  void disableJMX()
          Disables the JMX agent.
protected  void enableJMX()
          Enables the JMX agent.
 org.apache.camel.Service getCamelContextService()
           
protected  org.apache.camel.Endpoint getMandatoryEndpoint(String uri)
           
protected
<T extends org.apache.camel.Endpoint>
T
getMandatoryEndpoint(String uri, Class<T> type)
           
protected  org.apache.camel.component.mock.MockEndpoint getMockEndpoint(String uri)
          Resolves the mandatory Mock endpoint using a URI of the form mock:someName
protected  int getShutdownTimeout()
          Returns the timeout to use when shutting down (unit in seconds).
protected  boolean isLazyLoadingTypeConverter()
          Whether or not type converters should be lazy loaded (notice core converters is always loaded)

We enabled lazy by default as it would speedup unit testing.

 String isMockEndpoints()
          Override to enable auto mocking endpoints based on the pattern.
 boolean isUseAdviceWith()
          Override when using advice with and return true.
 boolean isUseDebugger()
          Override to enable debugger

Is default false

 boolean isUseRouteBuilder()
          Use the RouteBuilder or not
protected  void postProcessTest()
          Lets post process this test instance to process any Camel annotations.
protected  void resetMocks()
          Reset all Mock endpoints.
protected  org.apache.camel.Endpoint resolveMandatoryEndpoint(String uri)
          Resolves a mandatory endpoint for the given URI or an exception is thrown
protected
<T extends org.apache.camel.Endpoint>
T
resolveMandatoryEndpoint(String uri, Class<T> endpointType)
          Resolves a mandatory endpoint for the given URI and expected type or an exception is thrown
protected  void sendBodies(String endpointUri, Object... bodies)
          Sends messages to the given endpoint for each of the specified bodies
protected  void sendBody(String endpointUri, Object body)
          Sends a message to the given endpoint URI with the body value
protected  void sendBody(String endpointUri, Object body, Map<String,Object> headers)
          Sends a message to the given endpoint URI with the body value and specified headers
 void setCamelContextService(org.apache.camel.Service camelContextService)
          Allows a service to be registered a separate lifecycle service to start and stop the context; such as for Spring when the ApplicationContext is started and stopped, rather than directly stopping the CamelContext
protected  void setUp()
           
 void setUseRouteBuilder(boolean useRouteBuilder)
           
protected  void startCamelContext()
           
protected  void stopCamelContext()
           
protected  void tearDown()
           
protected  boolean useJmx()
          Whether or not JMX should be used during testing.
 
Methods inherited from class org.apache.camel.test.TestSupport
assertCollectionSize, assertCollectionSize, assertDirectoryEquals, assertDirectoryEquals, assertEndpointUri, assertExpression, assertFileExists, assertInMessageBodyEquals, assertInMessageHeader, assertIsInstanceOf, assertListSize, assertListSize, assertMessageHeader, assertOneElement, assertOutMessageBodyEquals, assertOutMessageHeader, assertPredicate, assertPredicateDoesNotMatch, assertPredicateMatches, assertStringContains, body, bodyAs, canRunOnThisPlatform, createDirectory, createExchangeWithBody, deleteDirectory, deleteDirectory, faultBody, faultBodyAs, getRouteList, getTestMethodName, header, isJava15, isJavaVendor, isPlatform, outBody, outBodyAs, property, resolveMandatoryEndpoint, resolveMandatoryEndpoint, runBare, systemProperty, systemProperty, unwrap, unwrapChannel
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

context

protected volatile org.apache.camel.model.ModelCamelContext context

template

protected volatile org.apache.camel.ProducerTemplate template

consumer

protected volatile org.apache.camel.ConsumerTemplate consumer
Constructor Detail

CamelTestSupport

public CamelTestSupport()
Method Detail

isUseRouteBuilder

public boolean isUseRouteBuilder()
Use the RouteBuilder or not

Returns:
If the return value is true, the camel context will be started in the setup method. If the return value is false, the camel context will not be started in the setup method.

isMockEndpoints

public String isMockEndpoints()
Override to enable auto mocking endpoints based on the pattern.

Return * to mock all endpoints.

See Also:
EndpointHelper.matchEndpoint(String, String)

setUseRouteBuilder

public void setUseRouteBuilder(boolean useRouteBuilder)

isUseDebugger

public boolean isUseDebugger()
Override to enable debugger

Is default false


isUseAdviceWith

public boolean isUseAdviceWith()
Override when using advice with and return true. This helps knowing advice with is to be used, and CamelContext will not be started before the advice with takes place. This helps by ensuring the advice with has been property setup before the CamelContext is started

Important: Its important to start CamelContext manually from the unit test after you are done doing all the advice with.

Returns:
true if you use advice with in your unit tests.

getCamelContextService

public org.apache.camel.Service getCamelContextService()

setCamelContextService

public void setCamelContextService(org.apache.camel.Service camelContextService)
Allows a service to be registered a separate lifecycle service to start and stop the context; such as for Spring when the ApplicationContext is started and stopped, rather than directly stopping the CamelContext


setUp

protected void setUp()
              throws Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
Exception

tearDown

protected void tearDown()
                 throws Exception
Overrides:
tearDown in class junit.framework.TestCase
Throws:
Exception

getShutdownTimeout

protected int getShutdownTimeout()
Returns the timeout to use when shutting down (unit in seconds).

Will default use 10 seconds.

Returns:
the timeout to use

useJmx

protected boolean useJmx()
Whether or not JMX should be used during testing.

Returns:
false by default.

isLazyLoadingTypeConverter

protected boolean isLazyLoadingTypeConverter()
Whether or not type converters should be lazy loaded (notice core converters is always loaded)

We enabled lazy by default as it would speedup unit testing.

Returns:
true by default.

postProcessTest

protected void postProcessTest()
                        throws Exception
Lets post process this test instance to process any Camel annotations. Note that using Spring Test or Guice is a more powerful approach.

Throws:
Exception

stopCamelContext

protected void stopCamelContext()
                         throws Exception
Throws:
Exception

startCamelContext

protected void startCamelContext()
                          throws Exception
Throws:
Exception

createCamelContext

protected org.apache.camel.CamelContext createCamelContext()
                                                    throws Exception
Throws:
Exception

createRegistry

protected org.apache.camel.impl.JndiRegistry createRegistry()
                                                     throws Exception
Throws:
Exception

createJndiContext

protected Context createJndiContext()
                             throws Exception
Throws:
Exception

createRouteBuilder

protected org.apache.camel.builder.RouteBuilder createRouteBuilder()
                                                            throws Exception
Factory method which derived classes can use to create a RouteBuilder to define the routes for testing

Throws:
Exception

createRouteBuilders

protected org.apache.camel.builder.RouteBuilder[] createRouteBuilders()
                                                               throws Exception
Factory method which derived classes can use to create an array of RouteBuilders to define the routes for testing

Throws:
Exception
See Also:
createRouteBuilder()

resolveMandatoryEndpoint

protected org.apache.camel.Endpoint resolveMandatoryEndpoint(String uri)
Resolves a mandatory endpoint for the given URI or an exception is thrown

Parameters:
uri - the Camel URI to use to create or resolve an endpoint
Returns:
the endpoint

resolveMandatoryEndpoint

protected <T extends org.apache.camel.Endpoint> T resolveMandatoryEndpoint(String uri,
                                                                           Class<T> endpointType)
Resolves a mandatory endpoint for the given URI and expected type or an exception is thrown

Parameters:
uri - the Camel URI to use to create or resolve an endpoint
Returns:
the endpoint

getMockEndpoint

protected org.apache.camel.component.mock.MockEndpoint getMockEndpoint(String uri)
Resolves the mandatory Mock endpoint using a URI of the form mock:someName

Parameters:
uri - the URI which typically starts with "mock:" and has some name
Returns:
the mandatory mock endpoint or an exception is thrown if it could not be resolved

sendBody

protected void sendBody(String endpointUri,
                        Object body)
Sends a message to the given endpoint URI with the body value

Parameters:
endpointUri - the URI of the endpoint to send to
body - the body for the message

sendBody

protected void sendBody(String endpointUri,
                        Object body,
                        Map<String,Object> headers)
Sends a message to the given endpoint URI with the body value and specified headers

Parameters:
endpointUri - the URI of the endpoint to send to
body - the body for the message
headers - any headers to set on the message

sendBodies

protected void sendBodies(String endpointUri,
                          Object... bodies)
Sends messages to the given endpoint for each of the specified bodies

Parameters:
endpointUri - the endpoint URI to send to
bodies - the bodies to send, one per message

createExchangeWithBody

protected org.apache.camel.Exchange createExchangeWithBody(Object body)
Creates an exchange with the given body


assertExpression

protected void assertExpression(org.apache.camel.Exchange exchange,
                                String languageName,
                                String expressionText,
                                Object expectedValue)
Asserts that the given language name and expression evaluates to the given value on a specific exchange


assertPredicate

protected void assertPredicate(String languageName,
                               String expressionText,
                               org.apache.camel.Exchange exchange,
                               boolean expected)
Asserts that the given language name and predicate expression evaluates to the expected value on the message exchange


assertResolveLanguage

protected org.apache.camel.spi.Language assertResolveLanguage(String languageName)
Asserts that the language name can be resolved


assertMockEndpointsSatisfied

protected void assertMockEndpointsSatisfied()
                                     throws InterruptedException
Asserts that all the expectations of the Mock endpoints are valid

Throws:
InterruptedException

assertMockEndpointsSatisfied

protected void assertMockEndpointsSatisfied(long timeout,
                                            TimeUnit unit)
                                     throws InterruptedException
Asserts that all the expectations of the Mock endpoints are valid

Throws:
InterruptedException

resetMocks

protected void resetMocks()
Reset all Mock endpoints.


assertValidContext

protected void assertValidContext(org.apache.camel.CamelContext context)

getMandatoryEndpoint

protected <T extends org.apache.camel.Endpoint> T getMandatoryEndpoint(String uri,
                                                                       Class<T> type)

getMandatoryEndpoint

protected org.apache.camel.Endpoint getMandatoryEndpoint(String uri)

disableJMX

protected void disableJMX()
Disables the JMX agent. Must be called before the setUp() method.


enableJMX

protected void enableJMX()
Enables the JMX agent. Must be called before the setUp() method.


debugBefore

protected void debugBefore(org.apache.camel.Exchange exchange,
                           org.apache.camel.Processor processor,
                           org.apache.camel.model.ProcessorDefinition definition,
                           String id,
                           String shortName)
Single step debugs and Camel invokes this method before entering the given processor

Parameters:
exchange - the exchange
processor - the processor about to be invoked
definition - the definition for the processor
id - the id of the definition
shortName - the short name of the definition

debugAfter

protected void debugAfter(org.apache.camel.Exchange exchange,
                          org.apache.camel.Processor processor,
                          org.apache.camel.model.ProcessorDefinition definition,
                          String id,
                          String shortName,
                          long timeTaken)
Single step debugs and Camel invokes this method after processing the given processor

Parameters:
exchange - the exchange
processor - the processor that was invoked
definition - the definition for the processor
id - the id of the definition
shortName - the short name of the definition
timeTaken - time taken to process the processor in millis


Apache CAMEL