org.apache.camel.component.mock
Class MockEndpoint

java.lang.Object
  extended by org.apache.camel.impl.ServiceSupport
      extended by org.apache.camel.impl.DefaultEndpoint
          extended by org.apache.camel.component.mock.MockEndpoint
All Implemented Interfaces:
CamelContextAware, Endpoint, IsSingleton, Service, ShutdownableService, BrowsableEndpoint, HasId
Direct Known Subclasses:
DataSetEndpoint, TestEndpoint

public class MockEndpoint
extends DefaultEndpoint
implements BrowsableEndpoint

A Mock endpoint which provides a literate, fluent API for testing routes using a JMock style API.

The mock endpoint have two set of methods

Its important to know the difference between the two set. The former is used to set expectations before the test is being started (eg before the mock receives messages). The latter is used after the test has been executed, to verify the expectations; or other assertions which you can perform after the test has been completed.

Version:

Constructor Summary
MockEndpoint()
           
MockEndpoint(String endpointUri)
           
MockEndpoint(String endpointUri, Component component)
           
 
Method Summary
 AssertionClause allMessages()
          Adds an assertion to all the received messages
protected  void assertEquals(String message, Object expectedValue, Object actualValue)
           
 Exchange assertExchangeReceived(int index)
          Asserts that the given index of message is received (starting at zero)
 void assertIsNotSatisfied()
          Validates that the assertions fail on this endpoint
 void assertIsNotSatisfied(long timeoutForEmptyEndpoints)
          Validates that the assertions fail on this endpoint
 void assertIsSatisfied()
          Validates that all the available expectations on this endpoint are satisfied; or throw an exception
static void assertIsSatisfied(CamelContext context)
          Asserts that all the expectations on any MockEndpoint instances registered in the given context are valid
static void assertIsSatisfied(CamelContext context, long timeout, TimeUnit unit)
          Asserts that all the expectations on any MockEndpoint instances registered in the given context are valid
 void assertIsSatisfied(long timeoutForEmptyEndpoints)
          Validates that all the available expectations on this endpoint are satisfied; or throw an exception
static void assertIsSatisfied(long timeout, TimeUnit unit, MockEndpoint... endpoints)
           
static void assertIsSatisfied(MockEndpoint... endpoints)
           
 void assertMessagesAscending(Expression expression)
          Asserts that the messages have ascending values of the given expression
 void assertMessagesDescending(Expression expression)
          Asserts that the messages have descending values of the given expression
protected  void assertMessagesSorted(Expression expression, boolean ascending)
           
 void assertNoDuplicates(Expression expression)
           
protected  void assertTrue(String message, boolean predicate)
           
static void assertWait(long timeout, TimeUnit unit, MockEndpoint... endpoints)
           
 void await()
           
 boolean await(long timeout, TimeUnit unit)
           
 Consumer createConsumer(Processor processor)
          Creates a new Event Driven Consumer which consumes messages from the endpoint using the given processor
 Producer createProducer()
          Creates a new producer which is used send messages into the endpoint
protected  void doAssertIsSatisfied(long timeoutForEmptyEndpoints)
           
 void expectedBodiesReceived(List bodies)
          Adds an expectation that the given body values are received by this endpoint in the specified order
 void expectedBodiesReceived(Object... bodies)
          Sets an expectation that the given body values are received by this endpoint
 void expectedBodiesReceivedInAnyOrder(List bodies)
          Adds an expectation that the given body values are received by this endpoint in any order
 void expectedBodiesReceivedInAnyOrder(Object... bodies)
          Adds an expectation that the given body values are received by this endpoint in any order
 ExpressionClause<?> expectedBodyReceived()
          Adds an expectation that the given body value are received by this endpoint
 void expectedFileExists(String name)
          Adds an expectation that a file exists with the given name
 void expectedFileExists(String name, String content)
          Adds an expectation that a file exists with the given name

Will wait at most 5 seconds while checking for the existence of the file.

 void expectedHeaderReceived(String name, Object value)
          Sets an expectation that the given header name & value are received by this endpoint
 void expectedMessageCount(int expectedCount)
          Specifies the expected number of message exchanges that should be received by this endpoint
 void expectedMessagesMatches(Predicate... predicates)
          Sets an expectation that the given predicates matches the received messages by this endpoint
 void expectedMinimumMessageCount(int expectedCount)
          Specifies the minimum number of expected message exchanges that should be received by this endpoint
 void expectedPropertyReceived(String name, Object value)
          Sets an expectation that the given property name & value are received by this endpoint
 void expects(Runnable runnable)
          Adds the expectation which will be invoked when enough messages are received
 ExpressionClause<?> expectsAscending()
          Adds an expectation that messages received should have ascending values of the given expression such as a user generated counter value
 void expectsAscending(Expression expression)
          Adds an expectation that messages received should have ascending values of the given expression such as a user generated counter value
 ExpressionClause<?> expectsDescending()
          Adds an expectation that messages received should have descending values of the given expression such as a user generated counter value
 void expectsDescending(Expression expression)
          Adds an expectation that messages received should have descending values of the given expression such as a user generated counter value
static void expectsMessageCount(int count, MockEndpoint... endpoints)
           
 ExpressionClause<?> expectsNoDuplicates()
          Adds an expectation that no duplicate messages should be received using the expression to determine the message ID
 void expectsNoDuplicates(Expression expression)
          Adds an expectation that no duplicate messages should be received using the expression to determine the message ID
protected  void fail(Object message)
           
 List<Exchange> getExchanges()
          Return the exchanges available on this endpoint
 int getExpectedCount()
           
 int getExpectedMinimumCount()
           
 List<Throwable> getFailures()
           
 int getReceivedCounter()
           
 List<Exchange> getReceivedExchanges()
           
 Processor getReporter()
           
 long getResultWaitTime()
           
 long getSleepForEmptyTest()
           
 void handle(Exchange exchange)
          Handles the incoming exchange.
 boolean isLenientProperties()
          Should all properties be known or does the endpoint allow unknown options?

lenient = false means that the endpoint should validate that all given options is known and configured properly.

 boolean isSingleton()
          Whether this class supports being singleton or not.
 AssertionClause message(int messageIndex)
          Adds an assertion to the given message index
protected  void onExchange(Exchange exchange)
           
protected  void performAssertions(Exchange exchange)
           
 void reset()
           
static void resetMocks(CamelContext context)
          Reset all mock endpoints
static MockEndpoint resolve(CamelContext context, String uri)
          A helper method to resolve the mock endpoint of the given URI on the given context
 void returnReplyBody(Expression expression)
          Set the expression which value will be set to the message body
 void returnReplyHeader(String headerName, Expression expression)
          Set the expression which value will be set to the message header
static void setAssertPeriod(CamelContext context, long period)
          Sets the assert period on all the expectations on any MockEndpoint instances registered in the given context.
 void setAssertPeriod(long period)
          Sets a grace period after which the mock endpoint will re-assert to ensure the preliminary assertion is still valid.
 void setExpectedMessageCount(int expectedCount)
          Specifies the expected number of message exchanges that should be received by this endpoint
 void setMinimumExpectedMessageCount(int expectedCount)
          Specifies the minimum number of expected message exchanges that should be received by this endpoint
 void setMinimumResultWaitTime(long resultMinimumWaitTime)
          Sets the minimum expected amount of time (in millis) the assertIsSatisfied() will wait on a latch until it is satisfied
 void setReporter(Processor reporter)
          Allows a processor to added to the endpoint to report on progress of the test
 void setResultWaitTime(long resultWaitTime)
          Sets the maximum amount of time (in millis) the assertIsSatisfied() will wait on a latch until it is satisfied
 void setSleepForEmptyTest(long sleepForEmptyTest)
          Allows a sleep to be specified to wait to check that this endpoint really is empty when expectedMessageCount(int) is called with zero
protected  void waitForCompleteLatch()
           
protected  void waitForCompleteLatch(long timeout)
           
 void whenAnyExchangeReceived(Processor processor)
          Set the processor that will be invoked when the some message is received.
 void whenExchangeReceived(int index, Processor processor)
          Set the processor that will be invoked when the index message is received.
 
Methods inherited from class org.apache.camel.impl.DefaultEndpoint
configureProperties, createEndpointUri, createExchange, createExchange, createExchange, createPollingConsumer, doStart, doStop, equals, getCamelContext, getComponent, getEndpointKey, getEndpointUri, getExchangePattern, getExchangeType, getId, hashCode, isSynchronous, sanitizeUri, setCamelContext, setEndpointUri, setEndpointUriIfNotSpecified, setExchangePattern, setSynchronous, toString
 
Methods inherited from class org.apache.camel.impl.ServiceSupport
addChildService, doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, removeChildService, resume, shutdown, start, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.camel.Endpoint
configureProperties, createExchange, createExchange, createExchange, createPollingConsumer, getCamelContext, getEndpointKey, getEndpointUri, setCamelContext
 
Methods inherited from interface org.apache.camel.Service
start, stop
 

Constructor Detail

MockEndpoint

public MockEndpoint(String endpointUri,
                    Component component)

MockEndpoint

public MockEndpoint(String endpointUri)

MockEndpoint

public MockEndpoint()
Method Detail

resolve

public static MockEndpoint resolve(CamelContext context,
                                   String uri)
A helper method to resolve the mock endpoint of the given URI on the given context

Parameters:
context - the camel context to try resolve the mock endpoint from
uri - the uri of the endpoint to resolve
Returns:
the endpoint

assertWait

public static void assertWait(long timeout,
                              TimeUnit unit,
                              MockEndpoint... endpoints)
                       throws InterruptedException
Throws:
InterruptedException

assertIsSatisfied

public static void assertIsSatisfied(long timeout,
                                     TimeUnit unit,
                                     MockEndpoint... endpoints)
                              throws InterruptedException
Throws:
InterruptedException

assertIsSatisfied

public static void assertIsSatisfied(MockEndpoint... endpoints)
                              throws InterruptedException
Throws:
InterruptedException

assertIsSatisfied

public static void assertIsSatisfied(CamelContext context)
                              throws InterruptedException
Asserts that all the expectations on any MockEndpoint instances registered in the given context are valid

Parameters:
context - the camel context used to find all the available endpoints to be asserted
Throws:
InterruptedException

assertIsSatisfied

public static void assertIsSatisfied(CamelContext context,
                                     long timeout,
                                     TimeUnit unit)
                              throws InterruptedException
Asserts that all the expectations on any MockEndpoint instances registered in the given context are valid

Parameters:
context - the camel context used to find all the available endpoints to be asserted
timeout - timeout
unit - time unit
Throws:
InterruptedException

setAssertPeriod

public static void setAssertPeriod(CamelContext context,
                                   long period)
Sets the assert period on all the expectations on any MockEndpoint instances registered in the given context.

Parameters:
context - the camel context used to find all the available endpoints
period - the period in millis

resetMocks

public static void resetMocks(CamelContext context)
Reset all mock endpoints

Parameters:
context - the camel context used to find all the available endpoints to reset

expectsMessageCount

public static void expectsMessageCount(int count,
                                       MockEndpoint... endpoints)
                                throws InterruptedException
Throws:
InterruptedException

getExchanges

public List<Exchange> getExchanges()
Description copied from interface: BrowsableEndpoint
Return the exchanges available on this endpoint

Specified by:
getExchanges in interface BrowsableEndpoint
Returns:
the exchanges on this endpoint

createConsumer

public Consumer createConsumer(Processor processor)
                        throws Exception
Description copied from interface: Endpoint
Creates a new Event Driven Consumer which consumes messages from the endpoint using the given processor

Specified by:
createConsumer in interface Endpoint
Parameters:
processor - the given processor
Returns:
a newly created consumer
Throws:
Exception - can be thrown

createProducer

public Producer createProducer()
                        throws Exception
Description copied from interface: Endpoint
Creates a new producer which is used send messages into the endpoint

Specified by:
createProducer in interface Endpoint
Returns:
a newly created producer
Throws:
Exception - can be thrown

reset

public void reset()

handle

@Handler
public void handle(Exchange exchange)
            throws Exception
Handles the incoming exchange.

This method turns this mock endpoint into a bean which you can use in the Camel routes, which allows you to inject MockEndpoint as beans in your routes and use the features of the mock to control the bean.

Parameters:
exchange - the exchange
Throws:
Exception - can be thrown

whenExchangeReceived

public void whenExchangeReceived(int index,
                                 Processor processor)
Set the processor that will be invoked when the index message is received.


whenAnyExchangeReceived

public void whenAnyExchangeReceived(Processor processor)
Set the processor that will be invoked when the some message is received. This processor could be overwritten by whenExchangeReceived(int, Processor) method.


returnReplyBody

public void returnReplyBody(Expression expression)
Set the expression which value will be set to the message body

Parameters:
expression - which is use to set the message body

returnReplyHeader

public void returnReplyHeader(String headerName,
                              Expression expression)
Set the expression which value will be set to the message header

Parameters:
headerName - that will be set value
expression - which is use to set the message header

assertIsSatisfied

public void assertIsSatisfied()
                       throws InterruptedException
Validates that all the available expectations on this endpoint are satisfied; or throw an exception

Throws:
InterruptedException

assertIsSatisfied

public void assertIsSatisfied(long timeoutForEmptyEndpoints)
                       throws InterruptedException
Validates that all the available expectations on this endpoint are satisfied; or throw an exception

Parameters:
timeoutForEmptyEndpoints - the timeout in milliseconds that we should wait for the test to be true
Throws:
InterruptedException

doAssertIsSatisfied

protected void doAssertIsSatisfied(long timeoutForEmptyEndpoints)
                            throws InterruptedException
Throws:
InterruptedException

assertIsNotSatisfied

public void assertIsNotSatisfied()
                          throws InterruptedException
Validates that the assertions fail on this endpoint

Throws:
InterruptedException

assertIsNotSatisfied

public void assertIsNotSatisfied(long timeoutForEmptyEndpoints)
                          throws InterruptedException
Validates that the assertions fail on this endpoint

Parameters:
timeoutForEmptyEndpoints - the timeout in milliseconds that we should wait for the test to be true
Throws:
InterruptedException

expectedMessageCount

public void expectedMessageCount(int expectedCount)
Specifies the expected number of message exchanges that should be received by this endpoint

Parameters:
expectedCount - the number of message exchanges that should be expected by this endpoint

setAssertPeriod

public void setAssertPeriod(long period)
Sets a grace period after which the mock endpoint will re-assert to ensure the preliminary assertion is still valid.

By default this period is disabled

Parameters:
period - grace period in millis

expectedMinimumMessageCount

public void expectedMinimumMessageCount(int expectedCount)
Specifies the minimum number of expected message exchanges that should be received by this endpoint

Parameters:
expectedCount - the number of message exchanges that should be expected by this endpoint

expectedHeaderReceived

public void expectedHeaderReceived(String name,
                                   Object value)
Sets an expectation that the given header name & value are received by this endpoint


expectedPropertyReceived

public void expectedPropertyReceived(String name,
                                     Object value)
Sets an expectation that the given property name & value are received by this endpoint


expectedBodiesReceived

public void expectedBodiesReceived(List bodies)
Adds an expectation that the given body values are received by this endpoint in the specified order


expectedMessagesMatches

public void expectedMessagesMatches(Predicate... predicates)
Sets an expectation that the given predicates matches the received messages by this endpoint


expectedBodiesReceived

public void expectedBodiesReceived(Object... bodies)
Sets an expectation that the given body values are received by this endpoint


expectedBodyReceived

public ExpressionClause<?> expectedBodyReceived()
Adds an expectation that the given body value are received by this endpoint


expectedBodiesReceivedInAnyOrder

public void expectedBodiesReceivedInAnyOrder(List bodies)
Adds an expectation that the given body values are received by this endpoint in any order


expectedBodiesReceivedInAnyOrder

public void expectedBodiesReceivedInAnyOrder(Object... bodies)
Adds an expectation that the given body values are received by this endpoint in any order


expectedFileExists

public void expectedFileExists(String name)
Adds an expectation that a file exists with the given name

Parameters:
name - name of file, will cater for / and \ on different OS platforms

expectedFileExists

public void expectedFileExists(String name,
                               String content)
Adds an expectation that a file exists with the given name

Will wait at most 5 seconds while checking for the existence of the file.

Parameters:
name - name of file, will cater for / and \ on different OS platforms
content - content of file to compare, can be null to not compare content

expectsAscending

public void expectsAscending(Expression expression)
Adds an expectation that messages received should have ascending values of the given expression such as a user generated counter value


expectsAscending

public ExpressionClause<?> expectsAscending()
Adds an expectation that messages received should have ascending values of the given expression such as a user generated counter value


expectsDescending

public void expectsDescending(Expression expression)
Adds an expectation that messages received should have descending values of the given expression such as a user generated counter value


expectsDescending

public ExpressionClause<?> expectsDescending()
Adds an expectation that messages received should have descending values of the given expression such as a user generated counter value


expectsNoDuplicates

public void expectsNoDuplicates(Expression expression)
Adds an expectation that no duplicate messages should be received using the expression to determine the message ID

Parameters:
expression - the expression used to create a unique message ID for message comparison (which could just be the message payload if the payload can be tested for uniqueness using Object.equals(Object) and Object.hashCode()

expectsNoDuplicates

public ExpressionClause<?> expectsNoDuplicates()
Adds an expectation that no duplicate messages should be received using the expression to determine the message ID


assertMessagesAscending

public void assertMessagesAscending(Expression expression)
Asserts that the messages have ascending values of the given expression


assertMessagesDescending

public void assertMessagesDescending(Expression expression)
Asserts that the messages have descending values of the given expression


assertMessagesSorted

protected void assertMessagesSorted(Expression expression,
                                    boolean ascending)

assertNoDuplicates

public void assertNoDuplicates(Expression expression)

expects

public void expects(Runnable runnable)
Adds the expectation which will be invoked when enough messages are received


message

public AssertionClause message(int messageIndex)
Adds an assertion to the given message index

Parameters:
messageIndex - the number of the message
Returns:
the assertion clause

allMessages

public AssertionClause allMessages()
Adds an assertion to all the received messages

Returns:
the assertion clause

assertExchangeReceived

public Exchange assertExchangeReceived(int index)
Asserts that the given index of message is received (starting at zero)


getFailures

public List<Throwable> getFailures()

getReceivedCounter

public int getReceivedCounter()

getReceivedExchanges

public List<Exchange> getReceivedExchanges()

getExpectedCount

public int getExpectedCount()

getSleepForEmptyTest

public long getSleepForEmptyTest()

setSleepForEmptyTest

public void setSleepForEmptyTest(long sleepForEmptyTest)
Allows a sleep to be specified to wait to check that this endpoint really is empty when expectedMessageCount(int) is called with zero

Parameters:
sleepForEmptyTest - the milliseconds to sleep for to determine that this endpoint really is empty

getResultWaitTime

public long getResultWaitTime()

setResultWaitTime

public void setResultWaitTime(long resultWaitTime)
Sets the maximum amount of time (in millis) the assertIsSatisfied() will wait on a latch until it is satisfied


setMinimumResultWaitTime

public void setMinimumResultWaitTime(long resultMinimumWaitTime)
Sets the minimum expected amount of time (in millis) the assertIsSatisfied() will wait on a latch until it is satisfied


setExpectedMessageCount

public void setExpectedMessageCount(int expectedCount)
Specifies the expected number of message exchanges that should be received by this endpoint

Parameters:
expectedCount - the number of message exchanges that should be expected by this endpoint

setMinimumExpectedMessageCount

public void setMinimumExpectedMessageCount(int expectedCount)
Specifies the minimum number of expected message exchanges that should be received by this endpoint

Parameters:
expectedCount - the number of message exchanges that should be expected by this endpoint

getReporter

public Processor getReporter()

setReporter

public void setReporter(Processor reporter)
Allows a processor to added to the endpoint to report on progress of the test


onExchange

protected void onExchange(Exchange exchange)

performAssertions

protected void performAssertions(Exchange exchange)
                          throws Exception
Throws:
Exception

waitForCompleteLatch

protected void waitForCompleteLatch()
                             throws InterruptedException
Throws:
InterruptedException

waitForCompleteLatch

protected void waitForCompleteLatch(long timeout)
                             throws InterruptedException
Throws:
InterruptedException

assertEquals

protected void assertEquals(String message,
                            Object expectedValue,
                            Object actualValue)

assertTrue

protected void assertTrue(String message,
                          boolean predicate)

fail

protected void fail(Object message)

getExpectedMinimumCount

public int getExpectedMinimumCount()

await

public void await()
           throws InterruptedException
Throws:
InterruptedException

await

public boolean await(long timeout,
                     TimeUnit unit)
              throws InterruptedException
Throws:
InterruptedException

isSingleton

public boolean isSingleton()
Description copied from interface: IsSingleton
Whether this class supports being singleton or not.

Specified by:
isSingleton in interface IsSingleton
Returns:
true to be a single shared instance, false to create new instances.

isLenientProperties

public boolean isLenientProperties()
Description copied from interface: Endpoint
Should all properties be known or does the endpoint allow unknown options?

lenient = false means that the endpoint should validate that all given options is known and configured properly. lenient = true means that the endpoint allows additional unknown options to be passed to it but does not throw a ResolveEndpointFailedException when creating the endpoint.

This options is used by a few components for instance the HTTP based that can have dynamic URI options appended that is targeted for an external system.

Most endpoints is configured to be not lenient.

Specified by:
isLenientProperties in interface Endpoint
Overrides:
isLenientProperties in class DefaultEndpoint
Returns:
whether properties is lenient or not


Apache CAMEL