Author: davsclaus
Date: Tue Aug 23 06:58:27 2011
New Revision: 1160565

URL: http://svn.apache.org/viewvc?rev=1160565&view=rev
Log:
Added unit test based on user forum issue.

Added:
    
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerTemplateTest.java
      - copied, changed from r1160554, 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java

Copied: 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerTemplateTest.java
 (from r1160554, 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerTemplateTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerTemplateTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java&r1=1160554&r2=1160565&rev=1160565&view=diff
==============================================================================
--- 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java
 (original)
+++ 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerTemplateTest.java
 Tue Aug 23 06:58:27 2011
@@ -21,18 +21,13 @@ 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 FromFtpDeleteFileTest extends FtpServerTestSupport {
+public class FtpConsumerTemplateTest extends FtpServerTestSupport {
 
     protected String getFtpUrl() {
-        return "ftp://admin@localhost:"; + getPort() + 
"/deletefile?password=admin&binary=false&delete=true";
+        return "ftp://admin@localhost:"; + getPort() + 
"/template?password=admin";
     }
 
     @Override
@@ -41,29 +36,76 @@ public class FromFtpDeleteFileTest exten
         super.setUp();
         prepareFtpServer();
     }
-    
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testConsumerTemplate() throws Exception {
+        Exchange exchange = consumer.receive(getFtpUrl(), 5000);
+        assertNotNull(exchange);
+        assertEquals("hello.txt", 
exchange.getIn().getHeader(Exchange.FILE_NAME));
+        assertEquals("Hello World", exchange.getIn().getBody(String.class));
+
+        // must done when we are done using the exchange
+        consumer.doneUoW(exchange);
+
+        Thread.sleep(500);
+
+        // poll the same file again
+        exchange = consumer.receive(getFtpUrl(), 5000);
+        assertNotNull(exchange);
+        assertEquals("hello.txt", 
exchange.getIn().getHeader(Exchange.FILE_NAME));
+        assertEquals("Hello World", exchange.getIn().getBody(String.class));
+
+        // must done when we are done using the exchange
+        consumer.doneUoW(exchange);
+
+        // file should still exists
+        Thread.sleep(500);
+        File file = new File(FTP_ROOT_DIR + "template/hello.txt");
+        file = file.getAbsoluteFile();
+        assertTrue("The file should exist: " + file, file.exists());
+    }
+
     @Test
-    public void testPollFileAndShouldBeDeleted() throws Exception {
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(1);
-        mock.expectedBodiesReceived("Hello World this file will be deleted");
+    public void testConsumerTemplateNotDone() throws Exception {
+        Exchange exchange = consumer.receive(getFtpUrl(), 5000);
+        assertNotNull(exchange);
+        assertEquals("hello.txt", 
exchange.getIn().getHeader(Exchange.FILE_NAME));
+        assertEquals("Hello World", exchange.getIn().getBody(String.class));
 
-        mock.assertIsSatisfied();
+        // forget to call done
 
         Thread.sleep(500);
 
-        // assert the file is deleted
-        File file = new File(FTP_ROOT_DIR + "deletefile/hello.txt");
+        // try poll the same file again
+        Exchange exchange2 = consumer.receive(getFtpUrl(), 2000);
+        assertNull(exchange2);
+
+        // now done the original exchange
+        consumer.doneUoW(exchange);
+
+        // now we can poll the file again as we have done the exchange
+        exchange2 = consumer.receive(getFtpUrl(), 2000);
+        assertNotNull(exchange2);
+        assertEquals("hello.txt", 
exchange2.getIn().getHeader(Exchange.FILE_NAME));
+        assertEquals("Hello World", exchange2.getIn().getBody(String.class));
+        consumer.doneUoW(exchange2);
+
+        // file should still exists
+        Thread.sleep(500);
+        File file = new File(FTP_ROOT_DIR + "template/hello.txt");
         file = file.getAbsoluteFile();
-        assertFalse("The file should have been deleted", file.exists());
+        assertTrue("The file should exist: " + file, 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().setBody("Hello World");
         exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
         Producer producer = endpoint.createProducer();
         producer.start();
@@ -71,16 +113,9 @@ public class FromFtpDeleteFileTest exten
         producer.stop();
 
         // assert file is created
-        File file = new File(FTP_ROOT_DIR + "deletefile/hello.txt");
+        File file = new File(FTP_ROOT_DIR + "template/hello.txt");
         file = file.getAbsoluteFile();
-        assertTrue("The file should exists", file.exists());
+        assertTrue("The file should exist: " + file, file.exists());
     }
 
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() throws Exception {
-                from(getFtpUrl()).to("mock:result");
-            }
-        };
-    }
 }
\ No newline at end of file


Reply via email to