You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/03/26 14:32:10 UTC
svn commit: r1305332 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/component/mock/MockEndpoint.java
test/java/org/apache/camel/component/mock/MockEndpointTest.java
Author: davsclaus
Date: Mon Mar 26 12:32:10 2012
New Revision: 1305332
URL: http://svn.apache.org/viewvc?rev=1305332&view=rev
Log:
CAMEL-4160: Added retainFirst and retainLast option to mock component.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?rev=1305332&r1=1305331&r2=1305332&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java Mon Mar 26 12:32:10 2012
@@ -99,6 +99,8 @@ public class MockEndpoint extends Defaul
private volatile Map<String, Object> expectedPropertyValues;
private volatile Map<String, Object> actualPropertyValues;
private volatile Processor reporter;
+ private volatile int retainFirst;
+ private volatile int retainLast;
public MockEndpoint(String endpointUri, Component component) {
super(endpointUri, component);
@@ -442,7 +444,14 @@ public class MockEndpoint extends Defaul
* Sets a grace period after which the mock endpoint will re-assert
* to ensure the preliminary assertion is still valid.
* <p/>
- * By default this period is disabled
+ * This is used for example to assert that <b>exactly</b> a number of messages
+ * arrives. For example if {@link #expectedMessageCount(int)} was set to 5, then
+ * the assertion is satisfied when 5 or more message arrives. To ensure that
+ * exactly 5 messages arrives, then you would need to wait a little period
+ * to ensure no further message arrives. This is what you can use this
+ * {@link #setAssertPeriod(long)} method for.
+ * <p/>
+ * By default this period is disabled.
*
* @param period grace period in millis
*/
@@ -918,7 +927,7 @@ public class MockEndpoint extends Defaul
}
public int getReceivedCounter() {
- return receivedExchanges.size();
+ return counter;
}
public List<Exchange> getReceivedExchanges() {
@@ -966,10 +975,14 @@ public class MockEndpoint extends Defaul
/**
* Specifies the expected number of message exchanges that should be
- * received by this endpoint
+ * received by this endpoint.
+ * <p/>
+ * If you want to assert that <b>exactly</b> n'th message arrives to this mock
+ * endpoint, then see also the {@link #setAssertPeriod(long)} method for further details.
*
* @param expectedCount the number of message exchanges that should be
* expected by this endpoint
+ * @see #setAssertPeriod(long)
*/
public void setExpectedMessageCount(int expectedCount) {
this.expectedCount = expectedCount;
@@ -1007,6 +1020,60 @@ public class MockEndpoint extends Defaul
this.reporter = reporter;
}
+ /**
+ * Specifies to only retain the first n'th number of received {@link Exchange}s.
+ * <p/>
+ * This is used when testing with big data, to reduce memory consumption by not storing
+ * copies of every {@link Exchange} this mock endpoint receives.
+ * <p/>
+ * <b>Important:</b> When using this limitation, then the {@link #getReceivedCounter()}
+ * will still return the actual number of received {@link Exchange}s. For example
+ * if we have received 5000 {@link Exchange}s, and have configured to only retain the first
+ * 10 {@link Exchange}s, then the {@link #getReceivedCounter()} will still return <tt>5000</tt>
+ * but there is only the first 10 {@link Exchange}s in the {@link #getExchanges()} and
+ * {@link #getReceivedExchanges()} methods.
+ * <p/>
+ * When using this method, then some of the other expecation methods is not supported,
+ * for example the {@link #expectedBodiesReceived(Object...)} sets a expectation on the first
+ * number of bodies received.
+ * <p/>
+ * You can configure both {@link #setRetainFirst(int)} and {@link #setRetainLast(int)} methods,
+ * to limit both the first and last received.
+ *
+ * @param retainFirst to limit and only keep the first n'th received {@link Exchange}s
+ * @see #setRetainLast(int)
+ */
+ public void setRetainFirst(int retainFirst) {
+ this.retainFirst = retainFirst;
+ }
+
+ /**
+ * Specifies to only retain the last n'th number of received {@link Exchange}s.
+ * <p/>
+ * This is used when testing with big data, to reduce memory consumption by not storing
+ * copies of every {@link Exchange} this mock endpoint receives.
+ * <p/>
+ * <b>Important:</b> When using this limitation, then the {@link #getReceivedCounter()}
+ * will still return the actual number of received {@link Exchange}s. For example
+ * if we have received 5000 {@link Exchange}s, and have configured to only retain the last
+ * 20 {@link Exchange}s, then the {@link #getReceivedCounter()} will still return <tt>5000</tt>
+ * but there is only the last 20 {@link Exchange}s in the {@link #getExchanges()} and
+ * {@link #getReceivedExchanges()} methods.
+ * <p/>
+ * When using this method, then some of the other expecation methods is not supported,
+ * for example the {@link #expectedBodiesReceived(Object...)} sets a expectation on the first
+ * number of bodies received.
+ * <p/>
+ * You can configure both {@link #setRetainFirst(int)} and {@link #setRetainLast(int)} methods,
+ * to limit both the first and last received.
+ *
+ * @param retainLast to limit and only keep the last n'th received {@link Exchange}s
+ * @see #setRetainFirst(int)
+ */
+ public void setRetainLast(int retainLast) {
+ this.retainLast = retainLast;
+ }
+
// Implementation methods
// -------------------------------------------------------------------------
private void init() {
@@ -1029,6 +1096,8 @@ public class MockEndpoint extends Defaul
actualHeaderValues = null;
expectedPropertyValues = null;
actualPropertyValues = null;
+ retainFirst = 0;
+ retainLast = 0;
}
protected synchronized void onExchange(Exchange exchange) {
@@ -1105,7 +1174,7 @@ public class MockEndpoint extends Defaul
// record timestamp when exchange was received
copy.setProperty(Exchange.RECEIVED_TIMESTAMP, new Date());
- receivedExchanges.add(copy);
+ addReceivedExchange(copy);
Processor processor = processors.get(getReceivedCounter()) != null
? processors.get(getReceivedCounter()) : defaultProcessor;
@@ -1122,6 +1191,35 @@ public class MockEndpoint extends Defaul
}
}
+ /**
+ * Adds the received exchange.
+ *
+ * @param copy a copy of the received exchange
+ */
+ protected void addReceivedExchange(Exchange copy) {
+ if (retainFirst <= 0 && retainLast <= 0) {
+ // no limitation so keep them all
+ receivedExchanges.add(copy);
+ } else {
+ if (retainFirst > 0 && counter <= retainFirst) {
+ // store a copy as its within the retain first limitation
+ receivedExchanges.add(copy);
+ } else if (retainLast > 0) {
+ // remove the oldest from the last retained boundary,
+ int index = receivedExchanges.size() - retainLast;
+ if (index >= 0) {
+ // but must be outside the first range as well
+ // otherwise we should not remove the oldest
+ if (retainFirst <= 0 || retainFirst <= index) {
+ receivedExchanges.remove(index);
+ }
+ }
+ // store a copy of the last n'th received
+ receivedExchanges.add(copy);
+ }
+ }
+ }
+
protected void waitForCompleteLatch() throws InterruptedException {
if (latch == null) {
fail("Should have a latch!");
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java?rev=1305332&r1=1305331&r2=1305332&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java Mon Mar 26 12:32:10 2012
@@ -824,6 +824,149 @@ public class MockEndpointTest extends Co
// counter should not be changed this time
assertEquals(1, counter.get());
}
+
+ public void testRetainFirst() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.setRetainFirst(5);
+ mock.expectedMessageCount(10);
+
+ sendMessages(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(10, mock.getReceivedCounter());
+ assertEquals(5, mock.getExchanges().size());
+ assertEquals(5, mock.getReceivedExchanges().size());
+
+ assertEquals("<message>0</message>", mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals("<message>1</message>", mock.getReceivedExchanges().get(1).getIn().getBody());
+ assertEquals("<message>2</message>", mock.getReceivedExchanges().get(2).getIn().getBody());
+ assertEquals("<message>3</message>", mock.getReceivedExchanges().get(3).getIn().getBody());
+ assertEquals("<message>4</message>", mock.getReceivedExchanges().get(4).getIn().getBody());
+ }
+
+ public void testRetainLast() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.setRetainLast(5);
+ mock.expectedMessageCount(10);
+
+ sendMessages(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(10, mock.getReceivedCounter());
+ assertEquals(5, mock.getExchanges().size());
+ assertEquals(5, mock.getReceivedExchanges().size());
+
+ assertEquals("<message>5</message>", mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals("<message>6</message>", mock.getReceivedExchanges().get(1).getIn().getBody());
+ assertEquals("<message>7</message>", mock.getReceivedExchanges().get(2).getIn().getBody());
+ assertEquals("<message>8</message>", mock.getReceivedExchanges().get(3).getIn().getBody());
+ assertEquals("<message>9</message>", mock.getReceivedExchanges().get(4).getIn().getBody());
+ }
+
+ public void testRetainFirstAndLast() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.setRetainFirst(5);
+ mock.setRetainLast(5);
+ mock.expectedMessageCount(20);
+
+ sendMessages(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(20, mock.getReceivedCounter());
+ assertEquals(10, mock.getExchanges().size());
+ assertEquals(10, mock.getReceivedExchanges().size());
+
+ assertEquals("<message>0</message>", mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals("<message>1</message>", mock.getReceivedExchanges().get(1).getIn().getBody());
+ assertEquals("<message>2</message>", mock.getReceivedExchanges().get(2).getIn().getBody());
+ assertEquals("<message>3</message>", mock.getReceivedExchanges().get(3).getIn().getBody());
+ assertEquals("<message>4</message>", mock.getReceivedExchanges().get(4).getIn().getBody());
+
+ assertEquals("<message>15</message>", mock.getReceivedExchanges().get(5).getIn().getBody());
+ assertEquals("<message>16</message>", mock.getReceivedExchanges().get(6).getIn().getBody());
+ assertEquals("<message>17</message>", mock.getReceivedExchanges().get(7).getIn().getBody());
+ assertEquals("<message>18</message>", mock.getReceivedExchanges().get(8).getIn().getBody());
+ assertEquals("<message>19</message>", mock.getReceivedExchanges().get(9).getIn().getBody());
+ }
+
+ public void testRetainFirstAndLastOverlap() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.setRetainFirst(5);
+ mock.setRetainLast(5);
+ mock.expectedMessageCount(8);
+
+ sendMessages(0, 1, 2, 3, 4, 5, 6, 7);
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(8, mock.getReceivedCounter());
+ assertEquals(8, mock.getExchanges().size());
+ assertEquals(8, mock.getReceivedExchanges().size());
+
+ assertEquals("<message>0</message>", mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals("<message>1</message>", mock.getReceivedExchanges().get(1).getIn().getBody());
+ assertEquals("<message>2</message>", mock.getReceivedExchanges().get(2).getIn().getBody());
+ assertEquals("<message>3</message>", mock.getReceivedExchanges().get(3).getIn().getBody());
+ assertEquals("<message>4</message>", mock.getReceivedExchanges().get(4).getIn().getBody());
+ assertEquals("<message>5</message>", mock.getReceivedExchanges().get(5).getIn().getBody());
+ assertEquals("<message>6</message>", mock.getReceivedExchanges().get(6).getIn().getBody());
+ assertEquals("<message>7</message>", mock.getReceivedExchanges().get(7).getIn().getBody());
+ }
+
+ public void testRetainFirstAndLastNoGap() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.setRetainFirst(5);
+ mock.setRetainLast(5);
+ mock.expectedMessageCount(10);
+
+ sendMessages(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(10, mock.getReceivedCounter());
+ assertEquals(10, mock.getExchanges().size());
+ assertEquals(10, mock.getReceivedExchanges().size());
+
+ assertEquals("<message>0</message>", mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals("<message>1</message>", mock.getReceivedExchanges().get(1).getIn().getBody());
+ assertEquals("<message>2</message>", mock.getReceivedExchanges().get(2).getIn().getBody());
+ assertEquals("<message>3</message>", mock.getReceivedExchanges().get(3).getIn().getBody());
+ assertEquals("<message>4</message>", mock.getReceivedExchanges().get(4).getIn().getBody());
+ assertEquals("<message>5</message>", mock.getReceivedExchanges().get(5).getIn().getBody());
+ assertEquals("<message>6</message>", mock.getReceivedExchanges().get(6).getIn().getBody());
+ assertEquals("<message>7</message>", mock.getReceivedExchanges().get(7).getIn().getBody());
+ assertEquals("<message>8</message>", mock.getReceivedExchanges().get(8).getIn().getBody());
+ assertEquals("<message>9</message>", mock.getReceivedExchanges().get(9).getIn().getBody());
+ }
+
+ public void testRetainFirstAndLastSingleGap() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.setRetainFirst(5);
+ mock.setRetainLast(5);
+ mock.expectedMessageCount(11);
+
+ sendMessages(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(11, mock.getReceivedCounter());
+ assertEquals(10, mock.getExchanges().size());
+ assertEquals(10, mock.getReceivedExchanges().size());
+
+ assertEquals("<message>0</message>", mock.getReceivedExchanges().get(0).getIn().getBody());
+ assertEquals("<message>1</message>", mock.getReceivedExchanges().get(1).getIn().getBody());
+ assertEquals("<message>2</message>", mock.getReceivedExchanges().get(2).getIn().getBody());
+ assertEquals("<message>3</message>", mock.getReceivedExchanges().get(3).getIn().getBody());
+ assertEquals("<message>4</message>", mock.getReceivedExchanges().get(4).getIn().getBody());
+ assertEquals("<message>6</message>", mock.getReceivedExchanges().get(5).getIn().getBody());
+ assertEquals("<message>7</message>", mock.getReceivedExchanges().get(6).getIn().getBody());
+ assertEquals("<message>8</message>", mock.getReceivedExchanges().get(7).getIn().getBody());
+ assertEquals("<message>9</message>", mock.getReceivedExchanges().get(8).getIn().getBody());
+ assertEquals("<message>10</message>", mock.getReceivedExchanges().get(9).getIn().getBody());
+ }
protected void sendMessages(int... counters) {
for (int counter : counters) {