Author: dejanb
Date: Thu Aug  6 09:12:10 2009
New Revision: 801551

URL: http://svn.apache.org/viewvc?rev=801551&view=rev
Log:
fix blob tests to work with embedded ftp server

Modified:
    
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java
    
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobTest.java
    
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java

Modified: 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java
URL: 
http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java?rev=801551&r1=801550&r2=801551&view=diff
==============================================================================
--- 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java
 (original)
+++ 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java
 Thu Aug  6 09:12:10 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.activemq.blob;
 
+import java.io.File;
+import java.io.FileWriter;
 import java.io.InputStream;
 import java.net.URL;
 
@@ -25,20 +27,68 @@
 import junit.framework.TestCase;
 
 import org.apache.activemq.command.ActiveMQBlobMessage;
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.ftplet.UserManager;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
+import org.apache.ftpserver.usermanager.impl.BaseUser;
+import org.jmock.Mockery;
 
-/**
- * To start this test make sure an ftp server is running with
- * user: activemq and password: activemq. 
- * Also a file called test.txt with the content <b>hello world</b> must be in 
the ftptest directory.
- */
 public class FTPBlobDownloadStrategyTest extends TestCase {
        
-       public void xtestDownload() {
+    private static final String ftpServerListenerName = "default";
+    private FtpServer server;
+    final static String userNamePass = "activemq";
+
+       Mockery context = null;
+       int ftpPort;
+       String ftpUrl;
+       
+       protected void setUp() throws Exception {     
+           final File ftpHomeDirFile = new File("target/FTPBlobTest/ftptest");
+               ftpHomeDirFile.mkdirs();
+               ftpHomeDirFile.getParentFile().deleteOnExit();
+
+               FtpServerFactory serverFactory = new FtpServerFactory();
+               ListenerFactory factory = new ListenerFactory();
+
+               PropertiesUserManagerFactory userManagerFactory = new 
PropertiesUserManagerFactory();
+               UserManager userManager = 
userManagerFactory.createUserManager();
+
+               BaseUser user = new BaseUser();
+        user.setName("activemq");
+        user.setPassword("activemq");
+        user.setHomeDirectory(ftpHomeDirFile.getParent());
+        
+        userManager.save(user);
+
+               serverFactory.setUserManager(userManager);
+               factory.setPort(0);
+               serverFactory.addListener(ftpServerListenerName, factory
+                               .createListener());
+               server = serverFactory.createServer();
+               server.start();
+               ftpPort = serverFactory.getListener(ftpServerListenerName)
+                               .getPort();
+
+               ftpUrl = "ftp://"; + userNamePass + ":" + userNamePass + 
"@localhost:"
+                               + ftpPort + "/ftptest/";
+
+           File uploadFile = new File(ftpHomeDirFile, "test.txt");
+           FileWriter wrt = new FileWriter(uploadFile);
+           wrt.write("hello world");
+           wrt.close();
+
+    }
+       
+       public void testDownload() {
                ActiveMQBlobMessage message = new ActiveMQBlobMessage();
                BlobDownloadStrategy strategy = new FTPBlobDownloadStrategy();
                InputStream stream;
                try {
-                       message.setURL(new 
URL("ftp://activemq:activ...@localhost/ftptest/test.txt";));
+                       message.setURL(new URL(ftpUrl + "test.txt"));
                        stream = strategy.getInputStream(message);
                        int i = stream.read();
                        StringBuilder sb = new StringBuilder(10);
@@ -53,11 +103,11 @@
                }
        }
        
-       public void xtestWrongAuthentification() {
+       public void testWrongAuthentification() {
                ActiveMQBlobMessage message = new ActiveMQBlobMessage();
                BlobDownloadStrategy strategy = new FTPBlobDownloadStrategy();
                try {
-                       message.setURL(new 
URL("ftp://activemq:activemq_wr...@localhost/ftptest/test.txt";));
+                       message.setURL(new URL("ftp://"; + userNamePass + 
"_wrong:" + userNamePass + "@localhost:"       + ftpPort + "/ftptest/"));
                        strategy.getInputStream(message);
                } catch(JMSException e) {
                        Assert.assertEquals("Wrong Exception", "Cant 
Authentificate to FTP-Server", e.getMessage());
@@ -71,11 +121,11 @@
                Assert.assertTrue("Expect Exception", false);
        }
        
-       public void xtestWrongFTPPort() {
+       public void testWrongFTPPort() {
                ActiveMQBlobMessage message = new ActiveMQBlobMessage();
                BlobDownloadStrategy strategy = new FTPBlobDownloadStrategy();
                try {
-                       message.setURL(new 
URL("ftp://activemq:activ...@localhost:442/ftptest/test.txt";));
+                       message.setURL(new URL("ftp://"; + userNamePass + ":" + 
userNamePass + "@localhost:"     + 422 + "/ftptest/"));
                        strategy.getInputStream(message);
                } catch(JMSException e) {
                        Assert.assertEquals("Wrong Exception", "Problem 
connecting the FTP-server", e.getMessage());

Modified: 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobTest.java
URL: 
http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobTest.java?rev=801551&r1=801550&r2=801551&view=diff
==============================================================================
--- 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobTest.java
 (original)
+++ 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobTest.java
 Thu Aug  6 09:12:10 2009
@@ -45,10 +45,7 @@
 import org.jmock.api.Invocation;
 import org.jmock.lib.action.CustomAction;
 
-/**
- * To start this test make sure an ftp server is running with user: activemq 
and
- * password: activemq
- */
+
 public class FTPBlobTest extends EmbeddedBrokerTestSupport {
 
     private static final String ftpServerListenerName = "default";

Modified: 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java
URL: 
http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java?rev=801551&r1=801550&r2=801551&view=diff
==============================================================================
--- 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java
 (original)
+++ 
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java
 Thu Aug  6 09:12:10 2009
@@ -33,24 +33,74 @@
 import org.apache.activemq.command.ActiveMQBlobMessage;
 import org.apache.activemq.command.MessageId;
 import org.apache.commons.net.ftp.FTPClient;
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.ftplet.AuthorizationRequest;
+import org.apache.ftpserver.ftplet.User;
+import org.apache.ftpserver.ftplet.UserManager;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
+import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
+import org.apache.ftpserver.usermanager.impl.BaseUser;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.api.Invocation;
+import org.jmock.lib.action.CustomAction;
+
 
-/**
- * To start this test make sure an ftp server is running with
- * user: activemq and password: activemq
- */
 public class FTPBlobUploadStrategyTest extends EmbeddedBrokerTestSupport {
        
-       private Connection connection;
+    private static final String ftpServerListenerName = "default";
+    private Connection connection;
+    private FtpServer server;
+    final static String userNamePass = "activemq";
 
+       Mockery context = null;
+       String ftpUrl;
+       
        protected void setUp() throws Exception {
-               bindAddress = 
"vm://localhost?jms.blobTransferPolicy.defaultUploadUrl=ftp://activemq:activ...@localhost/ftptest/";;
+               
+        final File ftpHomeDirFile = new File("target/FTPBlobTest/ftptest");
+        ftpHomeDirFile.mkdirs();
+        ftpHomeDirFile.getParentFile().deleteOnExit();
+
+        FtpServerFactory serverFactory = new FtpServerFactory();
+        ListenerFactory factory = new ListenerFactory();
+
+               PropertiesUserManagerFactory userManagerFactory = new 
PropertiesUserManagerFactory();
+               UserManager userManager = 
userManagerFactory.createUserManager();
+
+               BaseUser user = new BaseUser();
+        user.setName("activemq");
+        user.setPassword("activemq");
+        user.setHomeDirectory(ftpHomeDirFile.getParent());
+        
+        userManager.save(user);
+
+        serverFactory.setUserManager(userManager);
+        factory.setPort(0);
+        serverFactory.addListener(ftpServerListenerName, factory
+                .createListener());
+        server = serverFactory.createServer();
+        server.start();
+        int ftpPort = serverFactory.getListener(ftpServerListenerName)
+                .getPort();
+               
+        ftpUrl = "ftp://";
+            + userNamePass
+            + ":"
+            + userNamePass
+            + "@localhost:"
+            + ftpPort
+            + "/ftptest/";
+        bindAddress = 
"vm://localhost?jms.blobTransferPolicy.defaultUploadUrl=" + ftpUrl;
         super.setUp();
 
         connection = createConnection();
         connection.start();
         
         // check if file exist and delete it
-        URL url = new URL("ftp://activemq:activ...@localhost/ftptest/";);
+        URL url = new URL(ftpUrl);
         String connectUrl = url.getHost();
                int port = url.getPort() < 1 ? 21 : url.getPort();
                
@@ -80,7 +130,7 @@
         ActiveMQBlobMessage message = (ActiveMQBlobMessage) 
((ActiveMQSession)session).createBlobMessage(file);
         message.setMessageId(new MessageId("testmessage"));
         message.onSend();
-        
Assert.assertEquals("ftp://activemq:activ...@localhost/ftptest/testmessage";, 
message.getURL().toString()); 
+        Assert.assertEquals(ftpUrl + "testmessage", 
message.getURL().toString()); 
        }
 
 }


Reply via email to