Skip to content

Commit

Permalink
Add new property names: INDEX and SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Ribeaud committed Mar 9, 2017
1 parent edb2e69 commit 1825fd3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel</groupId>
<artifactId>camel-data-provider</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ private DataProviderConstants() {
*/
public final static String LAST_BATCH = DataProviderConstants.class.getName() + ".LastBatch";

/**
* Constant to specify the index (relative to {@link IDataProvider#getSize()}) of current {@link org.apache.camel.Exchange}.
* <p>
* Value stored should be a <b>int</b> or associated object.
* </p>
*/
public final static String INDEX = DataProviderConstants.class.getName() + ".Index";

/**
* Constant to specify total number of {@link org.apache.camel.Exchange} which will be generated.
* <p>
* Value stored should be a <b>int</b> or associated object.
* </p>
*/
public final static String SIZE = DataProviderConstants.class.getName() + ".Size";

/**
* Constant to specify whether current {@link org.apache.camel.Exchange} is the last one.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected int poll() throws Exception {
DataProviderEndpoint endpoint = getDataProviderEndoint();
IDataProvider<?> dataProvider = endpoint.getDataProvider();
final Range<Integer> range = this.rangeReference.get();
int index = range.lowerEndpoint();
if (range.isEmpty()) {
if (!finished.getAndSet(true)) {
LogUtils.info(LOG, () -> "Nothing to poll. Last range handled.");
Expand All @@ -85,6 +86,8 @@ protected int poll() throws Exception {
Queue<Exchange> exchanges = new LinkedList<>();
for (Object item : dataProvider.partition(range)) {
Exchange exchange = endpoint.createExchange();
exchange.setProperty(DataProviderConstants.INDEX, index++);
exchange.setProperty(DataProviderConstants.SIZE, size);
exchange.setProperty(DataProviderConstants.LAST_BATCH, range.upperEndpoint() == size);
exchange.getIn().setBody(item);
exchanges.add(exchange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void testDataProvider() throws Exception {
assertEquals(lastExchange.getProperty(Exchange.BATCH_COMPLETE), true);
assertEquals(lastExchange.getProperty(Exchange.BATCH_SIZE), 20);
assertEquals(lastExchange.getProperty(Exchange.BATCH_INDEX), 19);
assertEquals(lastExchange.getProperty(DataProviderConstants.SIZE), 100);
assertEquals(lastExchange.getProperty(DataProviderConstants.INDEX), 99);
// First
Exchange firstExchange = exchanges.get(0);
assertNotNull(firstExchange);
Expand All @@ -45,6 +47,8 @@ public void testDataProvider() throws Exception {
assertEquals(firstExchange.getProperty(Exchange.BATCH_COMPLETE), false);
assertEquals(firstExchange.getProperty(Exchange.BATCH_SIZE), 20);
assertEquals(firstExchange.getProperty(Exchange.BATCH_INDEX), 0);
assertEquals(firstExchange.getProperty(DataProviderConstants.INDEX), 0);
assertEquals(firstExchange.getProperty(DataProviderConstants.SIZE), 100);
}

@Test
Expand Down

0 comments on commit 1825fd3

Please sign in to comment.