You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/05/06 11:07:03 UTC
[2/4] git commit: CAMEL-7415 fixed the issue that lazyLoad with CSV
blows up on last line
CAMEL-7415 fixed the issue that lazyLoad with CSV blows up on last line
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/62756005
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/62756005
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/62756005
Branch: refs/heads/camel-2.13.x
Commit: 6275600501d3a7967606c0e91b42dcb802e27324
Parents: a032bc1
Author: Willem Jiang <wi...@gmail.com>
Authored: Tue May 6 14:59:57 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue May 6 17:05:57 2014 +0800
----------------------------------------------------------------------
.../camel/processor/UnmarshalProcessor.java | 10 ++++--
.../dataformat/csv/CsvUnmarshalStreamTest.java | 38 ++++++++++++++++++++
2 files changed, 45 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/62756005/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java
index c248fe1..934fd2a 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/UnmarshalProcessor.java
@@ -17,6 +17,7 @@
package org.apache.camel.processor;
import java.io.InputStream;
+import java.util.Iterator;
import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
@@ -55,6 +56,7 @@ public class UnmarshalProcessor extends ServiceSupport implements AsyncProcessor
ObjectHelper.notNull(dataFormat, "dataFormat");
InputStream stream = null;
+ Object result = null;
try {
stream = exchange.getIn().getMandatoryBody(InputStream.class);
@@ -62,7 +64,7 @@ public class UnmarshalProcessor extends ServiceSupport implements AsyncProcessor
Message out = exchange.getOut();
out.copyFrom(exchange.getIn());
- Object result = dataFormat.unmarshal(exchange, stream);
+ result = dataFormat.unmarshal(exchange, stream);
if (result instanceof Exchange) {
if (result != exchange) {
// it's not allowed to return another exchange other than the one provided to dataFormat
@@ -79,9 +81,11 @@ public class UnmarshalProcessor extends ServiceSupport implements AsyncProcessor
exchange.setOut(null);
exchange.setException(e);
} finally {
- IOHelper.close(stream, "input stream");
+ // The Iterator will close the stream itself
+ if (!(result instanceof Iterator)) {
+ IOHelper.close(stream, "input stream");
+ }
}
-
callback.done(true);
return true;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/62756005/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamTest.java
----------------------------------------------------------------------
diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamTest.java
index 355cd1e..f41a570 100644
--- a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamTest.java
+++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvUnmarshalStreamTest.java
@@ -16,6 +16,10 @@
*/
package org.apache.camel.dataformat.csv;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.util.List;
import org.apache.camel.EndpointInject;
@@ -38,6 +42,7 @@ public class CsvUnmarshalStreamTest extends CamelTestSupport {
@SuppressWarnings("unchecked")
@Test
public void testCsvUnMarshal() throws Exception {
+ result.reset();
result.expectedMessageCount(EXPECTED_COUNT);
String message = "";
@@ -58,6 +63,39 @@ public class CsvUnmarshalStreamTest extends CamelTestSupport {
}
}
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testCsvUnMarshalWithFile() throws Exception {
+ result.reset();
+ result.expectedMessageCount(EXPECTED_COUNT);
+
+
+ template.sendBody("direct:start", new MyFileInputStream(new File("src/test/resources/data.csv")));
+
+ assertMockEndpointsSatisfied();
+
+ for (int i = 0; i < EXPECTED_COUNT; ++i) {
+ List<String> body = result.getReceivedExchanges().get(i)
+ .getIn().getBody(List.class);
+ assertEquals(2, body.size());
+ assertEquals(String.valueOf(i), body.get(0));
+ assertEquals(String.format("%d\n%d", i, i), body.get(1));
+ }
+ }
+
+ class MyFileInputStream extends FileInputStream {
+
+ public MyFileInputStream(File file) throws FileNotFoundException {
+ super(file);
+ }
+
+ public void close() throws IOException {
+ // Use this to find out how camel close the FileInputStream
+ super.close();
+ }
+
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {