You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by ma...@apache.org on 2014/09/05 23:49:24 UTC

svn commit: r1622807 - in /oodt/trunk/protocol/ftp: pom.xml src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java src/test/org/apache/oodt/cas/protocol/ftp/TestCogJGlobusFtpProtocol.java

Author: magicaltrout
Date: Fri Sep  5 21:49:24 2014
New Revision: 1622807

URL: http://svn.apache.org/r1622807
Log:
OODT-644 Move some of the FTP communications to new methods so we can test the non FTP parts of the codebase without loosing the logic

Modified:
    oodt/trunk/protocol/ftp/pom.xml
    oodt/trunk/protocol/ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java
    oodt/trunk/protocol/ftp/src/test/org/apache/oodt/cas/protocol/ftp/TestCogJGlobusFtpProtocol.java

Modified: oodt/trunk/protocol/ftp/pom.xml
URL: http://svn.apache.org/viewvc/oodt/trunk/protocol/ftp/pom.xml?rev=1622807&r1=1622806&r2=1622807&view=diff
==============================================================================
--- oodt/trunk/protocol/ftp/pom.xml (original)
+++ oodt/trunk/protocol/ftp/pom.xml Fri Sep  5 21:49:24 2014
@@ -99,5 +99,11 @@
 			<version>3.8.2</version>
 			<scope>test</scope>
 		</dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
 	</dependencies>
 </project>

Modified: oodt/trunk/protocol/ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java
URL: http://svn.apache.org/viewvc/oodt/trunk/protocol/ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java?rev=1622807&r1=1622806&r2=1622807&view=diff
==============================================================================
--- oodt/trunk/protocol/ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java (original)
+++ oodt/trunk/protocol/ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java Fri Sep  5 21:49:24 2014
@@ -18,6 +18,7 @@ package org.apache.oodt.cas.protocol.ftp
 
 //JDK imports
 import java.io.File;
+import java.io.IOException;
 import java.util.List;
 import java.util.Vector;
 
@@ -31,6 +32,9 @@ import org.apache.oodt.cas.protocol.exce
 import org.apache.oodt.cas.protocol.util.ProtocolFileFilter;
 import org.globus.ftp.FTPClient;
 import org.globus.ftp.FileInfo;
+import org.globus.ftp.HostPort;
+import org.globus.ftp.exception.ClientException;
+import org.globus.ftp.exception.ServerException;
 
 /**
  * FTP implementation of a {@link Protocol}
@@ -116,12 +120,44 @@ public class CogJGlobusFtpProtocol imple
 		  throw new ProtocolException("Failed to put file '" + fromFile + "' : " + e.getMessage(), e);
 	  }
   }
-  
-  public List<ProtocolFile> ls() throws ProtocolException {
+
+    /**
+     * SetActive
+     * Sets the FTP Active Protocol
+     * Package protected so we can test the non FTP server parts of the codebase.
+     * @throws ProtocolException
+     */
+  protected void setActive() throws ProtocolException {
       try {
           ftp.setActive(ftp.setLocalPassive());
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to set ftp active : "
+                  + e.getMessage());
+      }
+  }
+
+    /**
+     * ftpList
+     * Gets a file list from the FTP Server
+     * Package protected so we can test the non server portion of the code.
+     * @param filter
+     * @param modifier
+     * @return
+     */
+  protected Vector ftpList(String filter, String modifier) throws ProtocolException {
+      try {
+          return ftp.list(filter, modifier);
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to get list of files : "
+                  + e.getMessage());
+      }
+
+  }
+  public List<ProtocolFile> ls() throws ProtocolException {
+      try {
+          setActive();
           @SuppressWarnings("unchecked")
-          Vector<FileInfo> fileList = (Vector<FileInfo>) ftp.list("*", null);
+          Vector<FileInfo> fileList = (Vector<FileInfo>) ftpList("*", null);
           Vector<ProtocolFile> returnList = new Vector<ProtocolFile>();
           for (FileInfo file : fileList) {
               returnList.add(new ProtocolFile(this.pwd(), file.getName(), file.isDirectory()));
@@ -151,10 +187,27 @@ public class CogJGlobusFtpProtocol imple
 	              + e.getMessage());
 	  }
   }
+
+    /**
+     * Returns the current Directory.
+     * Package protected so we can test the non ftp parts of the codebase.
+     * @return
+     * @throws ProtocolException
+     */
+  protected String getCurentDir() throws ProtocolException {
+      try {
+          return ftp.getCurrentDir();
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to get current directory : "
+                  + e.getMessage());
+      }
+
+
+  }
 	
   public ProtocolFile pwd() throws ProtocolException {
       try {
-          return new ProtocolFile(ftp.getCurrentDir(), true);
+          return new ProtocolFile(getCurentDir(), true);
       } catch (Exception e) {
           throw new ProtocolException("Failed to pwd : " + e.getMessage());
       }

Modified: oodt/trunk/protocol/ftp/src/test/org/apache/oodt/cas/protocol/ftp/TestCogJGlobusFtpProtocol.java
URL: http://svn.apache.org/viewvc/oodt/trunk/protocol/ftp/src/test/org/apache/oodt/cas/protocol/ftp/TestCogJGlobusFtpProtocol.java?rev=1622807&r1=1622806&r2=1622807&view=diff
==============================================================================
--- oodt/trunk/protocol/ftp/src/test/org/apache/oodt/cas/protocol/ftp/TestCogJGlobusFtpProtocol.java (original)
+++ oodt/trunk/protocol/ftp/src/test/org/apache/oodt/cas/protocol/ftp/TestCogJGlobusFtpProtocol.java Fri Sep  5 21:49:24 2014
@@ -18,7 +18,9 @@ package org.apache.oodt.cas.protocol.ftp
 
 //JUnit imports
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Vector;
 
 //APACHE imports
 import org.apache.ftpserver.ConnectionConfigFactory;
@@ -36,6 +38,12 @@ import org.apache.oodt.cas.protocol.exce
 
 //JUnit imports
 import junit.framework.TestCase;
+import org.globus.ftp.FileInfo;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import static org.mockito.Mockito.spy;
 
 /**
  * Test class for {@link CogJGlobusFtpProtocol}.
@@ -75,7 +83,7 @@ public class TestCogJGlobusFtpProtocol e
 		serverFactory.setUserManager(umFactory.createUserManager());
 
 		server = serverFactory.createServer();
-		server.start();
+		//server.start();
 	}
 	
 	@Override
@@ -84,9 +92,45 @@ public class TestCogJGlobusFtpProtocol e
 	}
 	
 	public void testLSandCDandPWD() throws ProtocolException {
-		CogJGlobusFtpProtocol ftpProtocol = new CogJGlobusFtpProtocol(PORT);
-		ftpProtocol.connect("localhost", new BasicAuthentication("anonymous", "password"));
+
+        CogJGlobusFtpProtocol ftpProtocol = spy(new CogJGlobusFtpProtocol(PORT));
+        BasicAuthentication auth = new BasicAuthentication("anonymous", "password");
+
+        /** Mocking server responses to prevent server failure **/
+
+        Mockito.doReturn("testdata").when(ftpProtocol).getCurentDir();
+
+        Vector<FileInfo> vector = new Vector<FileInfo>();
+        FileInfo file = new FileInfo();
+        file.setName("users.properties");
+        byte b = 1;
+        file.setFileType(b);
+        vector.add(file);
+
+        Mockito.doAnswer(new Answer() {
+            public Object answer(InvocationOnMock invocation) {
+                return null;
+            }}).when(ftpProtocol).setActive();
+
+        Mockito.doReturn(vector).when(ftpProtocol).ftpList("*", null);
+
+        Mockito.doAnswer(new Answer() {
+            public Object answer(InvocationOnMock invocation) {
+                return null;
+            }}).when(ftpProtocol).connect("localhost", auth);
+
+        Mockito.doAnswer(new Answer() {
+            public Object answer(InvocationOnMock invocation) {
+                return null;
+            }}).when(ftpProtocol).cd(new ProtocolFile("testdata", true));
+
+
+
+        ftpProtocol.connect("localhost", auth);
+
+
 		ftpProtocol.cd(new ProtocolFile("testdata", true));
+
 		List<ProtocolFile> lsResults = ftpProtocol.ls();
 		assertTrue(lsResults.contains(new ProtocolFile(ftpProtocol.pwd(), "users.properties", false)));
 	}