camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [7/7] git commit: CAMEL-6734: Added header for consumed file name in file components. Thanks to James Carman for the patch.
Date Thu, 12 Sep 2013 07:40:51 GMT
CAMEL-6734: Added header for consumed file name in file components. Thanks to James Carman
for the patch.

Conflicts:
	camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/30a5519f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/30a5519f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/30a5519f

Branch: refs/heads/camel-2.9.x
Commit: 30a5519f41d9ef9cb10457d1d64869b52ecf43d0
Parents: e9872a4
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Sep 12 09:25:59 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Sep 12 09:39:49 2013 +0200

----------------------------------------------------------------------
 .../main/java/org/apache/camel/Exchange.java    |  1 +
 .../camel/component/file/GenericFile.java       |  1 +
 .../component/file/GenericFileProducer.java     |  3 +-
 .../file/FileConsumerConsumedFileNameTest.java  | 55 +++++++++++++++++
 ...eProducerConsumedFileNameEvaluationTest.java | 62 ++++++++++++++++++++
 5 files changed, 121 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/30a5519f/camel-core/src/main/java/org/apache/camel/Exchange.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java
index c7e8002..bfd23c4 100644
--- a/camel-core/src/main/java/org/apache/camel/Exchange.java
+++ b/camel-core/src/main/java/org/apache/camel/Exchange.java
@@ -116,6 +116,7 @@ public interface Exchange {
     String FILE_NAME            = "CamelFileName";
     String FILE_NAME_ONLY       = "CamelFileNameOnly";
     String FILE_NAME_PRODUCED   = "CamelFileNameProduced";
+    String FILE_NAME_CONSUMED   = "CamelFileNameConsumed";
     String FILE_PATH            = "CamelFilePath";
     String FILE_PARENT          = "CamelFileParent";
     String FILE_LAST_MODIFIED   = "CamelFileLastModified";

http://git-wip-us.apache.org/repos/asf/camel/blob/30a5519f/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
index 6715655..2f6dfdb 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
@@ -127,6 +127,7 @@ public class GenericFile<T> implements WrappedFile<T>  {
         if (message != null) {
             message.setHeader(Exchange.FILE_NAME_ONLY, getFileNameOnly());
             message.setHeader(Exchange.FILE_NAME, getFileName());
+            message.setHeader(Exchange.FILE_NAME_CONSUMED, getFileName());
             message.setHeader("CamelFileAbsolute", isAbsolute());
             message.setHeader("CamelFileAbsolutePath", getAbsoluteFilePath());
     

http://git-wip-us.apache.org/repos/asf/camel/blob/30a5519f/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
index feab07b..6a9178f 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
@@ -258,10 +258,11 @@ public class GenericFileProducer<T> extends DefaultProducer {
         String answer;
 
         String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
+        String consumed = exchange.getIn().getHeader(Exchange.FILE_NAME_CONSUMED, String.class);
 
         // expression support
         Expression expression = endpoint.getFileName();
-        if (name != null) {
+        if (name != null && !name.equals(consumed)) {
             // the header name can be an expression too, that should override
             // whatever configured on the endpoint
             if (StringHelper.hasStartToken(name, "simple")) {

http://git-wip-us.apache.org/repos/asf/camel/blob/30a5519f/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
new file mode 100644
index 0000000..30de2ce
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
@@ -0,0 +1,55 @@
+/**
+ * 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.component.file;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test that checks for the existence of the CamelFileNameConsumed header.
+ */
+public class FileConsumerConsumedFileNameTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/consumedfilename");
+        super.setUp();
+        // the file name is also starting with target/consumedfilename
+        template.sendBodyAndHeader("file:target/consumedfilename", "Hello World", Exchange.FILE_NAME,
"hello.txt");
+    }
+
+    public void testValidFilenameOnExchange() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).header(Exchange.FILE_NAME).isEqualTo("hello.txt");
+        mock.message(0).header(Exchange.FILE_NAME_CONSUMED).isEqualTo("hello.txt");
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/consumedfilename").to("mock:result");
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/30a5519f/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
new file mode 100644
index 0000000..64da59d
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.component.file;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+
+/**
+ * @version
+ */
+public class FileProducerConsumedFileNameEvaluationTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/producerconsumedfilename");
+        super.setUp();
+    }
+
+    public void testFileNameNotEvaluatedWhenMatchingConsumed() throws Exception {
+        Map<String, Object> headers = new TreeMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "file-${date:now:yyyyMMdd}");
+        headers.put(Exchange.FILE_NAME_CONSUMED, "file-${date:now:yyyyMMdd}");
+        template.sendBodyAndHeaders("file://target/producerconsumedfilename", "Hello World",
headers);
+        assertFileExists("target/producerconsumedfilename/file-${date:now:yyyyMMdd}");
+    }
+
+    public void testFileNameEvaluatedWhenNotMatchingConsumed() throws Exception {
+        Map<String, Object> headers = new TreeMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "file-${date:now:yyyyMMdd}.txt");
+        headers.put(Exchange.FILE_NAME_CONSUMED, "file-consumed");
+        template.sendBodyAndHeaders("file://target/producerconsumedfilename", "Hello World",
headers);
+        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
+        assertFileExists("target/producerconsumedfilename/file-" + date + ".txt");
+    }
+
+    public void testFileNameEvaluatedWhenConsumedNull() throws Exception {
+        Map<String, Object> headers = new TreeMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "file-${date:now:yyyyMMdd}.txt");
+        template.sendBodyAndHeaders("file://target/producerconsumedfilename", "Hello World",
headers);
+        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
+        assertFileExists("target/producerconsumedfilename/file-" + date + ".txt");
+    }
+}


Mime
View raw message