Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B752411C53 for ; Tue, 6 May 2014 09:07:35 +0000 (UTC) Received: (qmail 85863 invoked by uid 500); 6 May 2014 09:07:10 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 85625 invoked by uid 500); 6 May 2014 09:07:06 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 85117 invoked by uid 99); 6 May 2014 09:07:02 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 May 2014 09:07:02 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 824D9920F97; Tue, 6 May 2014 09:07:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ningjiang@apache.org To: commits@camel.apache.org Date: Tue, 06 May 2014 09:07:03 -0000 Message-Id: <4afa777863cc493797615301f18f963e@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [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 Authored: Tue May 6 14:59:57 2014 +0800 Committer: Willem Jiang 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 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() {