You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/12/10 21:17:57 UTC

svn commit: r725414 - /activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java

Author: davsclaus
Date: Wed Dec 10 12:17:57 2008
New Revision: 725414

URL: http://svn.apache.org/viewvc?rev=725414&view=rev
Log:
camel-ftp: Added sample based on end user request on user forum

Added:
    activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java   (contents, props changed)
      - copied, changed from r725336, activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java

Copied: activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java (from r725336, activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java?p2=activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java&p1=activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java&r1=725336&r2=725414&rev=725414&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java Wed Dec 10 12:17:57 2008
@@ -16,23 +16,33 @@
  */
 package org.apache.camel.component.file.remote;
 
-import java.io.File;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
+import org.apache.camel.Consumer;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.file.FileComponent;
 import org.apache.camel.component.mock.MockEndpoint;
 
 /**
- * Unit test to test both consumer.moveNamePrefix and consumer.moveNamePostfix options.
+ * Unit test based on user forum question
  */
-public class FromFtpMoveFileTest extends FtpServerTestSupport {
+public class FromQueueThenConsumeFtpToMockTest extends FtpServerTestSupport {
 
-    private int port = 20032;
-    private String ftpUrl = "ftp://admin@localhost:" + port + "/movefile?password=admin&binary=false"
-        + "&consumer.moveNamePrefix=done/sub2/&consumer.moveNamePostfix=.old";
+    private int port = 20034;
+    private String storeUrl = "ftp://admin@localhost:" + port + "/getme?password=admin&binary=false";
+
+    // START SNIPPET: e1
+    // we use directory=false to indicate we only want to consume a single file
+    // we use delay=5000 to use 5 sec delay between pools to avoid polling a second time before we stop the consumer
+    // this is because we only want to run a single poll and get the file
+    // file=getme/ is the path to the folder where the file is
+    private String getUrl = "ftp://admin@localhost:" + port + "?password=admin&binary=false&directory=false&consumer.delay=5000&file=getme/";
+    // END SNIPPET: e1
 
     public int getPort() {
         return port;
@@ -41,47 +51,73 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        deleteDirectory("./res/home/movefile");
+        deleteDirectory("./res/home/getme");
         prepareFtpServer();
     }
 
     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(ftpUrl);
+        // test that we can pool once
+        Endpoint endpoint = context.getEndpoint(storeUrl);
         Exchange exchange = endpoint.createExchange();
-        exchange.getIn().setBody("Hello World this file will be moved");
+        exchange.getIn().setBody("Bye World");
         exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, "hello.txt");
         Producer producer = endpoint.createProducer();
         producer.start();
         producer.process(exchange);
         producer.stop();
-
-        // assert file is created
-        File file = new File("./res/home/movefile/hello.txt");
-        file = file.getAbsoluteFile();
-        assertTrue("The file should exists", file.exists());
     }
 
-    public void testPollFileAndShouldBeMoved() throws Exception {
+    public void testFromQueueThenConsumeFtp() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(1);
-        mock.expectedBodiesReceived("Hello World this file will be moved");
+        mock.expectedBodiesReceived("Bye World");
 
-        mock.assertIsSatisfied();
+        template.sendBodyAndHeader("seda:start", "Hello World", "myfile", "hello.txt");
 
-        // assert the file is deleted
-        File file = new File("./res/home/movefile/done/sub2/hello.txt.old");
-        file = file.getAbsoluteFile();
-        assertTrue("The file should have been moved", file.exists());
+        assertMockEndpointsSatisfied();
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from(ftpUrl).to("mock:result");
+                // START SNIPPET: e2
+                from("seda:start").process(new Processor() {
+                    public void process(final Exchange exchange) throws Exception {
+                        // get the filename from our custome header we want to get from a remote server
+                        String filename = exchange.getIn().getHeader("myfile", String.class);
+
+                        // construct the total url for the ftp consumer
+                        String url = getUrl + filename;
+
+                        // create a ftp endpoint
+                        Endpoint ftp = context.getEndpoint(url);
+
+                        // use this latch to wait for completion of the consumer
+                        final CountDownLatch latch = new CountDownLatch(1);
+
+                        // create a consumer and process the result using the processor
+                        Consumer consumer = ftp.createConsumer(new Processor() {
+                            public void process(Exchange result) throws Exception {
+                                // the result is the response from the FTP consumer (the downloaded file)
+                                // replace the outher exchange with the content from the downloaded file
+                                exchange.getIn().setBody(result.getIn().getBody());
+                                // signal we are complete
+                                latch.countDown();
+                            }
+                        });
+                        // start the consumer
+                        consumer.start();
+
+                        // wait for the completion or timeout after 30 seconds
+                        latch.await(30, TimeUnit.SECONDS);
+
+                        // stop the consumer
+                        consumer.stop();
+                    }
+                }).to("mock:result");
+                // END SNIPPET: e2
             }
         };
     }
 
-}
+}
\ No newline at end of file

Propchange: activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/branches/camel-1.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromQueueThenConsumeFtpToMockTest.java
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Wed Dec 10 12:17:57 2008
@@ -0,0 +1 @@
+/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713136,713273,713290,713292,713295,713314,713475,713625,713932,713944,714032,717965,717989,718242,718273,718312-718515,719163-719184,719334,719339,719524,719662,719848,719851,719855,719864,719978-719979,720207,720435-720437,720806,721272,721331,721333-721334,721360,721669,721764,721813,721985,722005,722070,722110,722415,722438,722726,722845,722878,723264,723314,723325-723327,723409,723835,723966,724122,724619,724681,725040