camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r680083 - in /activemq/camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/
Date Sun, 27 Jul 2008 09:49:55 GMT
Author: davsclaus
Date: Sun Jul 27 02:49:54 2008
New Revision: 680083

URL: http://svn.apache.org/viewvc?rev=680083&view=rev
Log:
Polished camel-ftp. Added missing unit test. Trying to close Apache FTP Server so Bamboo is
happy. Better handling of exception thrown during ftp consumer if its stopping etc.

Added:
    activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRegexPatternTest.java
      - copied, changed from r679955, activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSetNamesWithMultiDirectoriesTest.java
Modified:
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
    activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpExclusiveReadTest.java
    activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpNonExclusiveReadTest.java
    activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java
    activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpServerTestSupport.java

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
Sun Jul 27 02:49:54 2008
@@ -64,7 +64,13 @@
     protected void doStop() throws Exception {
         LOG.info("Stopping");
         // disconnect when stopping
-        disconnect();
+        try {
+            disconnect();
+        } catch (Exception e) {
+            // ignore just log a warning
+            LOG.warn("Exception occured during disconecting from " + remoteServer() + ".
" +
+                e.getClass().getCanonicalName() + " message: " + e.getMessage());
+        }
         super.doStop();
     }
 
@@ -95,11 +101,12 @@
             if (endpoint.getConfiguration().isDirectory()) {
                 pollDirectory(fileName);
             } else {
-                // TODO: This code can be nicer
                 int index = fileName.lastIndexOf('/');
                 if (index > -1) {
+                    // cd to the folder of the filename
                     client.changeWorkingDirectory(fileName.substring(0, index));
                 }
+                // list the files in the fold and poll the first file
                 final FTPFile[] files = client.listFiles(fileName.substring(index + 1));
                 pollFile(files[0]);
             }
@@ -147,6 +154,10 @@
     }
 
     private void pollFile(FTPFile ftpFile) throws Exception {
+        if (ftpFile == null) {
+            return;
+        }
+
         if (LOG.isTraceEnabled()) {
             LOG.trace("Polling file: " + ftpFile);
         }
@@ -234,6 +245,9 @@
         if (regexPattern != null && regexPattern.length() > 0) {
             result = file.getName().matches(regexPattern);
         }
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Matching file: " + file.getName() + " is " + result);
+        }
         return result;
     }
 

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
Sun Jul 27 02:49:54 2008
@@ -116,7 +116,14 @@
     @Override
     protected void doStop() throws Exception {
         LOG.info("Stopping");
-        disconnect();
+        // disconnect when stopping
+        try {
+            disconnect();
+        } catch (Exception e) {
+            // ignore just log a warning
+            LOG.warn("Exception occured during disconecting from " + remoteServer() + ".
" +
+                e.getClass().getCanonicalName() + " message: " + e.getMessage());
+        }
         super.doStop();
     }
 

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
Sun Jul 27 02:49:54 2008
@@ -19,6 +19,7 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.Vector;
 
 import com.jcraft.jsch.ChannelSftp;
 import com.jcraft.jsch.JSchException;
@@ -63,7 +64,13 @@
     protected void doStop() throws Exception {
         LOG.info("Stopping");
         // disconnect when stopping
-        disconnect();
+        try {
+            disconnect();
+        } catch (Exception e) {
+            // ignore just log a warning
+            LOG.warn("Exception occured during disconecting from " + remoteServer() + ".
" +
+                e.getClass().getCanonicalName() + " message: " + e.getMessage());
+        }
         super.doStop();
     }
 
@@ -105,9 +112,15 @@
             if (endpoint.getConfiguration().isDirectory()) {
                 pollDirectory(fileName);
             } else {
-                // TODO: This code could be neater
-                channel.cd(fileName.substring(0, fileName.lastIndexOf('/')));
-                final ChannelSftp.LsEntry file = (ChannelSftp.LsEntry)channel.ls(fileName.substring(fileName.lastIndexOf('/')
+ 1)).get(0);
+                int index = fileName.lastIndexOf('/');
+                if (index > -1) {
+                    // cd to the folder of the filename
+                    channel.cd(fileName.substring(0, index));
+                }
+
+                // list the files in the fold and poll the first file
+                final Vector files = channel.ls(fileName.substring(index + 1));
+                final ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) files.get(0);
                 pollFile(file);
             }
             lastPollTime = System.currentTimeMillis();
@@ -246,7 +259,10 @@
     protected boolean isMatched(ChannelSftp.LsEntry sftpFile) {
         boolean result = true;
         if (regexPattern != null && regexPattern.length() > 0) {
-            result = sftpFile.getFilename().matches(getRegexPattern());
+            result = sftpFile.getFilename().matches(regexPattern);
+        }
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Matching file: " + sftpFile.getFilename() + " is " + result);
         }
         return result;
     }

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
Sun Jul 27 02:49:54 2008
@@ -127,7 +127,14 @@
     @Override
     protected void doStop() throws Exception {
         LOG.info("Stopping");
-        disconnect();
+        // disconnect when stopping
+        try {
+            disconnect();
+        } catch (Exception e) {
+            // ignore just log a warning
+            LOG.warn("Exception occured during disconecting from " + remoteServer() + ".
" +
+                e.getClass().getCanonicalName() + " message: " + e.getMessage());
+        }
         super.doStop();
     }
 

Modified: activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpExclusiveReadTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpExclusiveReadTest.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpExclusiveReadTest.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpExclusiveReadTest.java
Sun Jul 27 02:49:54 2008
@@ -31,8 +31,8 @@
 
     private static final Log LOG = LogFactory.getLog(FromFtpExclusiveReadTest.class);
 
-    private String port = "20025";
-    private String ftpUrl = "ftp://admin@localhost:" + port + "/slowfile?password=admin&binary=false&consumer.exclusiveRead=true&consumer.delay=500";
+    private String port = "20099";
+    private String ftpUrl = "ftp://admin@localhost:" + port + "/slowfile?password=admin&consumer.exclusiveRead=true&consumer.delay=500";
 
     public String getPort() {
         return port;

Modified: activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpNonExclusiveReadTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpNonExclusiveReadTest.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpNonExclusiveReadTest.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpNonExclusiveReadTest.java
Sun Jul 27 02:49:54 2008
@@ -32,13 +32,13 @@
     private static final Log LOG = LogFactory.getLog(FromFtpExclusiveReadTest.class);
 
     private String port = "20027";
-    private String ftpUrl = "ftp://admin@localhost:" + port + "/slowfile?password=admin&binary=false&consumer.exclusiveRead=false&consumer.delay=500";
+    private String ftpUrl = "ftp://admin@localhost:" + port + "/slowfile?password=admin&consumer.exclusiveRead=false&consumer.delay=500";
 
     public String getPort() {
         return port;
     }
 
-    public void testPollFileWhileSlowFileIsBeingWritten() throws Exception {
+    public void testPollFileWhileSlowFileIsBeingWrittenUsingNonExclusiveRead() throws Exception
{
         deleteDirectory("./res/home");
         createDirectory("./res/home/slowfile");
         MockEndpoint mock = getMockEndpoint("mock:result");

Copied: activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRegexPatternTest.java
(from r679955, activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSetNamesWithMultiDirectoriesTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRegexPatternTest.java?p2=activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRegexPatternTest.java&p1=activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSetNamesWithMultiDirectoriesTest.java&r1=679955&r2=680083&rev=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSetNamesWithMultiDirectoriesTest.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRegexPatternTest.java
Sun Jul 27 02:49:54 2008
@@ -16,51 +16,24 @@
  */
 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.file.FileComponent;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.converter.IOConverter;
 
 /**
- * Unit test to verify that using option setNames and having multi remote directories the
files
- * are stored locally in the same directory layout.
+ * Unit test to verify regexPattern option.
  */
-public class FromFtpSetNamesWithMultiDirectoriesTest extends FtpServerTestSupport {
+public class FromFtpRegexPatternTest extends FtpServerTestSupport {
+
+    private String port = "20097";
 
-    private String port = "20016";
-    
-    // must user "consumer." prefix on the parameters to the file component
-    private String ftpUrl = "ftp://admin@localhost:" + port + "/incoming?password=admin&binary=true"
-        + "&consumer.delay=2000&consumer.recursive=true&consumer.append=false&consumer.setNames=true";
-
-    public void testFtpRoute() throws Exception {
-        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
-        resultEndpoint.expectedMessageCount(2);
-        resultEndpoint.assertIsSatisfied();
-        Exchange ex = resultEndpoint.getExchanges().get(0);
-        byte[] bytes = ex.getIn().getBody(byte[].class);
-        assertTrue("Logo size wrong", bytes.length > 10000);
-
-        // wait until the file producer has written the file
-        Thread.sleep(1000);
-
-        // assert the file
-        File file = new File("target/ftpsetnamestest/data1/logo1.jpeg");
-        assertTrue("The binary file should exists", file.exists());
-        assertTrue("Logo size wrong", file.length() > 10000);
-
-        // assert the file
-        file = new File("target/ftpsetnamestest/data2/logo2.png");
-        assertTrue(" The binary file should exists", file.exists());
-        assertTrue("Logo size wrong", file.length() > 50000);
+    private String ftpUrl = "ftp://admin@localhost:" + port + "/regexp?password=admin&consumer.regexPattern=report.*";
 
-        // let some time pass to let the consumer etc. properly do its business before closing
-        Thread.sleep(1000);
+    public void testFtpRegexPattern() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedBodiesReceived("Report 1", "Report 2");
+        mock.assertIsSatisfied();
     }
 
     public String getPort() {
@@ -74,34 +47,19 @@
     }
 
     private void prepareFtpServer() throws Exception {
-        // prepares the FTP Server by creating a file on the server that we want to unit
+        // prepares the FTP Server by creating files on the server that we want to unit
         // test that we can pool and store as a local file
-        String ftpUrl = "ftp://admin@localhost:" + port + "/incoming/data1/?password=admin&binary=true";
-        Endpoint endpoint = context.getEndpoint(ftpUrl);
-        Exchange exchange = endpoint.createExchange();
-        exchange.getIn().setBody(IOConverter.toFile("src/test/data/ftpbinarytest/logo1.jpeg"));
-        exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, "logo1.jpeg");
-        Producer producer = endpoint.createProducer();
-        producer.start();
-        producer.process(exchange);
-        producer.stop();
-
-        ftpUrl = "ftp://admin@localhost:" + port + "/incoming/data2/?password=admin&binary=true";
-        endpoint = context.getEndpoint(ftpUrl);
-        exchange = endpoint.createExchange();
-        exchange.getIn().setBody(IOConverter.toFile("src/test/data/ftpbinarytest/logo2.png"));
-        exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, "logo2.png");
-        producer = endpoint.createProducer();
-        producer.start();
-        producer.process(exchange);
-        producer.stop();
+        String ftpUrl = "ftp://admin@localhost:" + port + "/regexp/?password=admin";
+        template.sendBodyAndHeader(ftpUrl, "Hello World", FileComponent.HEADER_FILE_NAME,
"hello.txt");
+        template.sendBodyAndHeader(ftpUrl, "Report 1", FileComponent.HEADER_FILE_NAME, "report1.txt");
+        template.sendBodyAndHeader(ftpUrl, "Bye World", FileComponent.HEADER_FILE_NAME, "bye.txt");
+        template.sendBodyAndHeader(ftpUrl, "Report 2", FileComponent.HEADER_FILE_NAME, "report2.txt");
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                String fileUrl = "file:target/ftpsetnamestest/?noop=true";
-                from(ftpUrl).to(fileUrl, "mock:result");
+                from(ftpUrl).to("mock:result");
             }
         };
     }

Modified: activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java
Sun Jul 27 02:49:54 2008
@@ -27,7 +27,7 @@
  */
 public class FtpProducerBuildDirectoryTest extends FtpServerTestSupport {
 
-    private String port = "20026";
+    private String port = "20098";
     private String ftpUrl = "ftp://admin@localhost:" + port + "/upload/user/claus?binary=false&password=admin";
 
     public String getPort() {

Modified: activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpServerTestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpServerTestSupport.java?rev=680083&r1=680082&r2=680083&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpServerTestSupport.java
(original)
+++ activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpServerTestSupport.java
Sun Jul 27 02:49:54 2008
@@ -36,14 +36,20 @@
     protected void setUp() throws Exception {
         super.setUp();
         initFtpServer();
-        ftpServer.start();
+        if (ftpServer.isStopped()) {
+            ftpServer.start();
+        }
     }
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        // must stop server after super to let the clients stop correctly (CAMEL-444)
-        ftpServer.getServerContext().dispose();
-        ftpServer.stop();
+        if (!ftpServer.isStopped()) {
+            // must stop server after super to let the clients stop correctly (CAMEL-444)
+            ftpServer.getServerContext().dispose();
+            ftpServer.stop();
+        }
+        // give it time to properly stop the server
+        Thread.sleep(3000);
     }
 
     protected void initFtpServer() throws Exception {



Mime
View raw message