camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/4] camel git commit: CAMEL-8887: Added unit test
Date Wed, 05 Aug 2015 11:30:40 GMT
Repository: camel
Updated Branches:
  refs/heads/master e61f7f5a2 -> 01c0963d4


CAMEL-8887: Added unit test


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

Branch: refs/heads/master
Commit: 82e9fd33ea78d2576a63e2641d89921dafd26bdd
Parents: e61f7f5
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Aug 5 12:49:38 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Aug 5 12:49:38 2015 +0200

----------------------------------------------------------------------
 .../component/file/GenericFileConsumer.java     | 25 +++---
 .../file/remote/FromFtpSedaDeleteFileTest.java  | 90 ++++++++++++++++++++
 .../remote/sftp/SftpFromSedaDeleteFileTest.java | 80 +++++++++++++++++
 3 files changed, 185 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/82e9fd33/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index c188ebf..d1f58d6 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -432,17 +432,22 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
 
             log.debug("About to process file: {} using exchange: {}", target, exchange);
 
-            // process the exchange using the async consumer to support async routing engine
-            // which can be supported by this file consumer as all the done work is
-            // provided in the GenericFileOnCompletion
-            getAsyncProcessor().process(exchange, new AsyncCallback() {
-                public void done(boolean doneSync) {
-                    // noop
-                    if (log.isTraceEnabled()) {
-                        log.trace("Done processing file: {} {}", target, doneSync ? "synchronously"
: "asynchronously");
+            if (endpoint.isSynchronous()) {
+                // process synchronously
+                getProcessor().process(exchange);
+            } else {
+                // process the exchange using the async consumer to support async routing
engine
+                // which can be supported by this file consumer as all the done work is
+                // provided in the GenericFileOnCompletion
+                getAsyncProcessor().process(exchange, new AsyncCallback() {
+                    public void done(boolean doneSync) {
+                        // noop
+                        if (log.isTraceEnabled()) {
+                            log.trace("Done processing file: {} {}", target, doneSync ? "synchronously"
: "asynchronously");
+                        }
                     }
-                }
-            });
+                });
+            }
 
         } catch (Exception e) {
             // remove file from the in progress list due to failure

http://git-wip-us.apache.org/repos/asf/camel/blob/82e9fd33/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
new file mode 100644
index 0000000..dfb8806
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
@@ -0,0 +1,90 @@
+/**
+ * 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.remote;
+
+import java.io.File;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test to test delete option.
+ */
+public class FromFtpSedaDeleteFileTest extends FtpServerTestSupport {
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/deletefile?password=admin&binary=false&delete=true";
+    }
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        prepareFtpServer();
+    }
+    
+    @Test
+    public void testPollFileAndShouldBeDeleted() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World this file will be deleted");
+
+        mock.assertIsSatisfied();
+
+        Thread.sleep(500);
+
+        // assert the file is deleted
+        File file = new File(FTP_ROOT_DIR + "/deletefile/hello.txt");
+        assertFalse("The file should have been deleted", file.exists());
+    }
+
+    private void prepareFtpServer() throws Exception {
+        // prepares the FTP Server by creating a file on the server that we want to unit
+        // test that we can pool and store as a local file
+        Endpoint endpoint = context.getEndpoint(getFtpUrl());
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setBody("Hello World this file will be deleted");
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+        Producer producer = endpoint.createProducer();
+        producer.start();
+        producer.process(exchange);
+        producer.stop();
+
+        // assert file is created
+        File file = new File(FTP_ROOT_DIR + "/deletefile/hello.txt");
+        assertTrue("The file should exists", file.exists());
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("seda:foo");
+
+                from("seda:foo")
+                    .delay(750)
+                    .log("${body}")
+                    .delay(750)
+                    .to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/82e9fd33/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
new file mode 100644
index 0000000..5926a9e
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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.remote.sftp;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test to test delete option.
+ */
+public class SftpFromSedaDeleteFileTest extends SftpServerTestSupport {
+
+    protected String getFtpUrl() {
+        return "sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR
+                + "?username=admin&knownHostsFile=./src/test/resources/known_hosts&privateKeyFile=./src/test/resources/id_rsa"
+                + "&privateKeyFilePassphrase=secret&delay=500&disconnect=false&delete=true";
+    }
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        createSampleFile();
+    }
+    
+    @Test
+    public void testPollFileAndShouldBeDeleted() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World this file will be deleted");
+
+        mock.assertIsSatisfied();
+
+        Thread.sleep(500);
+
+        // assert the file is deleted
+        File file = new File(FTP_ROOT_DIR + "/hello.txt");
+        assertFalse("The file should have been deleted", file.exists());
+    }
+
+    private void createSampleFile() throws IOException {
+        File file = new File(FTP_ROOT_DIR + "/" + "foo.txt");
+
+        FileUtils.write(file, "Hello World this file will be deleted");
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("seda:foo");
+
+                from("seda:foo")
+                        .delay(750)
+                        .log("${body}")
+                        .delay(750)
+                        .to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file


Mime
View raw message