CAMEL-11466 align source code with camel-zipfile + add non regression unit test to test if resources correctly released after splitting tar
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cff781e7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cff781e7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cff781e7 Branch: refs/heads/camel-2.19.x Commit: cff781e73538f155c1352336a32286dcf52f07c1 Parents: e180eee Author: Adrien PAILHES <apail...@axway.com> Authored: Tue Jun 27 18:10:12 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jun 28 17:42:11 2017 +0200 ---------------------------------------------------------------------- .../dataformat/tarfile/TarFileDataFormat.java | 4 +- .../camel/dataformat/tarfile/TarIterator.java | 14 ++- .../camel/dataformat/tarfile/TarSplitter.java | 3 +- .../tarfile/TarFileSplitAndDeleteTest.java | 102 +++++++++++++++++++ 4 files changed, 112 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cff781e7/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java index d56160b..b226c6e 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java @@ -87,7 +87,7 @@ public class TarFileDataFormat extends ServiceSupport implements DataFormat, Dat @Override public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { if (usingIterator) { - TarIterator tarIterator = new TarIterator(exchange.getIn(), stream); + TarIterator tarIterator = new TarIterator(exchange, stream); tarIterator.setAllowEmptyDirectory(allowEmptyDirectory); return tarIterator; } else { @@ -122,7 +122,7 @@ public class TarFileDataFormat extends ServiceSupport implements DataFormat, Dat public void setUsingIterator(boolean usingIterator) { this.usingIterator = usingIterator; } - + public boolean isAllowEmptyDirectory() { return allowEmptyDirectory; } http://git-wip-us.apache.org/repos/asf/camel/blob/cff781e7/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java index 8b7e22a..e93a01e 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java @@ -49,15 +49,14 @@ public class TarIterator implements Iterator<Message>, Closeable { private static final Logger LOGGER = LoggerFactory.getLogger(TarIterator.class); - private final Message inputMessage; + private final Exchange exchange; private volatile TarArchiveInputStream tarInputStream; private volatile Message parent; private boolean allowEmptyDirectory; - public TarIterator(Message inputMessage, InputStream inputStream) { - this.inputMessage = inputMessage; - //InputStream inputStream = inputMessage.getBody(InputStream.class); - + public TarIterator(Exchange exchange, InputStream inputStream) { + this.exchange = exchange; + this.allowEmptyDirectory = false; if (inputStream instanceof TarArchiveInputStream) { tarInputStream = (TarArchiveInputStream) inputStream; } else { @@ -100,7 +99,6 @@ public class TarIterator implements Iterator<Message>, Closeable { if (parent == null) { parent = getNextElement(); } - Message answer = parent; parent = null; checkNullAnswer(answer); @@ -119,7 +117,7 @@ public class TarIterator implements Iterator<Message>, Closeable { if (current != null) { LOGGER.debug("Reading tarEntry {}", current.getName()); Message answer = new DefaultMessage(); - answer.getHeaders().putAll(inputMessage.getHeaders()); + answer.getHeaders().putAll(exchange.getIn().getHeaders()); answer.setHeader(TARFILE_ENTRY_NAME_HEADER, current.getName()); answer.setHeader(Exchange.FILE_NAME, current.getName()); if (current.getSize() > 0) { @@ -172,7 +170,7 @@ public class TarIterator implements Iterator<Message>, Closeable { IOHelper.close(tarInputStream); tarInputStream = null; } - + public boolean isAllowEmptyDirectory() { return allowEmptyDirectory; } http://git-wip-us.apache.org/repos/asf/camel/blob/cff781e7/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java index 132dd55..2853f6c 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarSplitter.java @@ -33,7 +33,8 @@ public class TarSplitter implements Expression { public Object evaluate(Exchange exchange) { Message inputMessage = exchange.getIn(); - return new TarIterator(inputMessage, inputMessage.getBody(InputStream.class)); + InputStream inputStream = inputMessage.getBody(InputStream.class); + return new TarIterator(exchange, inputStream); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/cff781e7/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileSplitAndDeleteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileSplitAndDeleteTest.java b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileSplitAndDeleteTest.java new file mode 100644 index 0000000..2850fb8 --- /dev/null +++ b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileSplitAndDeleteTest.java @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dataformat.tarfile; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.Iterator; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class TarFileSplitAndDeleteTest extends CamelTestSupport { + + @Override + public void setUp() throws Exception { + deleteDirectory("target/testDeleteTarFileWhenUnmarshalWithDataFormat"); + deleteDirectory("target/testDeleteTarFileWhenUnmarshalWithSplitter"); + super.setUp(); + } + + @Test + public void testDeleteTarFileWhenUnmarshalWithDataFormat() throws Exception { + NotifyBuilder notify = new NotifyBuilder(context).from("file://target/" + "testDeleteTarFileWhenUnmarshalWithDataFormat").whenDone(1).create(); + getMockEndpoint("mock:end").expectedMessageCount(3); + String tarFile = createTarFile("testDeleteTarFileWhenUnmarshalWithDataFormat"); + + assertMockEndpointsSatisfied(); + + notify.matchesMockWaitTime(); + + // the original file should have been deleted + assertFalse("File should been deleted", new File(tarFile).exists()); + } + + @Test + public void testDeleteTarFileWhenUnmarshalWithSplitter() throws Exception { + NotifyBuilder notify = new NotifyBuilder(context).from("file://target/" + "testDeleteTarFileWhenUnmarshalWithSplitter").whenDone(1).create(); + getMockEndpoint("mock:end").expectedMessageCount(3); + String tarFile = createTarFile("testDeleteTarFileWhenUnmarshalWithSplitter"); + + assertMockEndpointsSatisfied(); + + notify.matchesMockWaitTime(); + + // the original file should have been deleted, + assertFalse("File should been deleted", new File(tarFile).exists()); + } + + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + TarFileDataFormat dataFormat = new TarFileDataFormat(); + dataFormat.setUsingIterator(true); + + from("file://target/testDeleteTarFileWhenUnmarshalWithDataFormat?delete=true") + .unmarshal(dataFormat) + .split(bodyAs(Iterator.class)).streaming() + .convertBodyTo(String.class) + .to("mock:end") + .end(); + + from("file://target/testDeleteTarFileWhenUnmarshalWithSplitter?delete=true") + .split(new TarSplitter()).streaming() + .convertBodyTo(String.class) + .to("mock:end") + .end(); + } + }; + } + + private String createTarFile(String folder) throws IOException { + Path source = Paths.get("src/test/resources/data/tarfile3.tar"); + Path target = Paths.get("target" + File.separator + folder + File.separator + "data.tar"); + target.toFile().getParentFile().mkdirs(); + Path copy = Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); + return copy.toAbsolutePath().toString(); + } +}