You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by bf...@apache.org on 2011/06/08 00:05:43 UTC

svn commit: r1133174 - in /oodt/branches/protocol/protocol-imaps: ./ src/main/java/org/apache/oodt/cas/protocol/imaps/ src/test/ src/test/org/ src/test/org/apache/ src/test/org/apache/oodt/ src/test/org/apache/oodt/cas/ src/test/org/apache/oodt/cas/pro...

Author: bfoster
Date: Tue Jun  7 22:05:42 2011
New Revision: 1133174

URL: http://svn.apache.org/viewvc?rev=1133174&view=rev
Log:

- unit-test updates 

---------------
OODT-194

Added:
    oodt/branches/protocol/protocol-imaps/src/test/
    oodt/branches/protocol/protocol-imaps/src/test/org/
    oodt/branches/protocol/protocol-imaps/src/test/org/apache/
    oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/
    oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/
    oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/
    oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/
    oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/TestImapsProtocol.java   (with props)
Modified:
    oodt/branches/protocol/protocol-imaps/pom.xml
    oodt/branches/protocol/protocol-imaps/src/main/java/org/apache/oodt/cas/protocol/imaps/ImapsProtocol.java

Modified: oodt/branches/protocol/protocol-imaps/pom.xml
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-imaps/pom.xml?rev=1133174&r1=1133173&r2=1133174&view=diff
==============================================================================
--- oodt/branches/protocol/protocol-imaps/pom.xml (original)
+++ oodt/branches/protocol/protocol-imaps/pom.xml Tue Jun  7 22:05:42 2011
@@ -99,6 +99,12 @@
 			<version>0.8</version>
 		</dependency>
 		<dependency>
+			<groupId>com.icegreen</groupId>
+			<artifactId>greenmail</artifactId>
+			<version>1.3.1b</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>3.8.2</version>

Modified: oodt/branches/protocol/protocol-imaps/src/main/java/org/apache/oodt/cas/protocol/imaps/ImapsProtocol.java
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-imaps/src/main/java/org/apache/oodt/cas/protocol/imaps/ImapsProtocol.java?rev=1133174&r1=1133173&r2=1133174&view=diff
==============================================================================
--- oodt/branches/protocol/protocol-imaps/src/main/java/org/apache/oodt/cas/protocol/imaps/ImapsProtocol.java (original)
+++ oodt/branches/protocol/protocol-imaps/src/main/java/org/apache/oodt/cas/protocol/imaps/ImapsProtocol.java Tue Jun  7 22:05:42 2011
@@ -56,17 +56,19 @@ import org.apache.tika.sax.BodyContentHa
 import org.apache.tika.sax.TextContentHandler;
 
 /**
+ * IMAP Secure {@link Protocol} implementation
  * 
  * @author bfoster
  * @version $Revision$
- * 
  */
 public class ImapsProtocol implements Protocol {
 
   static Store store;
 
   static Folder currentFolder;
-
+  
+  static Folder homeFolder;
+  
   static int port = 993;
 
   static Session session;
@@ -74,24 +76,35 @@ public class ImapsProtocol implements Pr
   static int openCalls = 0;
 
   static int connectCalls = 0;
-
+  
   public synchronized void cd(ProtocolFile file) throws ProtocolException {
     try {
       String remotePath = file.getPath();
-      // System.out.println("cd to " + remotePath);
       if (remotePath.startsWith("/"))
         remotePath = remotePath.substring(1);
       if (remotePath.trim().equals(""))
-        currentFolder = store.getDefaultFolder();
+        homeFolder = currentFolder = store.getDefaultFolder();
       else {
-        currentFolder = store.getFolder(remotePath);
+        homeFolder = currentFolder = store.getFolder(remotePath);
       }
     } catch (Exception e) {
-      e.printStackTrace();
+      throw new ProtocolException("Failed to change directory to '" 
+      		+ file + "' : " + e.getMessage(), e);
     }
-    // changedDir = true;
   }
 
+  public synchronized void cdRoot() throws ProtocolException {
+    try {
+      cd(new ProtocolFile("/", true));
+    } catch (Exception e) {
+      throw new ProtocolException("Failed to cd to root : " + e.getMessage(), e);
+    }
+  }
+  
+  public synchronized void cdHome() throws ProtocolException {
+  	currentFolder = homeFolder;
+  }
+  
   public synchronized void connect(String host, Authentication auth)
       throws ProtocolException {
     try {
@@ -103,31 +116,30 @@ public class ImapsProtocol implements Pr
       }
       this.incrementConnections();
     } catch (Exception e) {
-      e.printStackTrace();
       throw new ProtocolException("Failed to connected to IMAPS server " + host
-          + " with username " + auth.getUser() + " : " + e.getMessage());
+          + " with username " + auth.getUser() + " : " + e.getMessage(), e);
     }
   }
 
   public synchronized void close() throws ProtocolException {
     decrementConnections();
     if (connectCalls <= 0) {
-      // changedDir = true;
       try {
-        if (!currentFolder.isOpen()) {
-          try {
-            currentFolder.open(Folder.READ_WRITE);
-          } catch (Exception e) {
-            try {
-              currentFolder.open(Folder.READ_ONLY);
-            } catch (Exception e2) {
-            }
-          }
-        }
-        currentFolder.close(true);
+//        if (!currentFolder.isOpen()) {
+//          try {
+//            currentFolder.open(Folder.READ_WRITE);
+//          } catch (Exception e) {
+//            try {
+//              currentFolder.open(Folder.READ_ONLY);
+//            } catch (Exception e2) {
+//            }
+//          }
+//        }
+//        currentFolder.close(true);
         store.close();
       } catch (Exception e) {
-        e.printStackTrace();
+      	e.printStackTrace();
+        throw new ProtocolException("Failed to close connection : " + e.getMessage(), e);
       } finally {
         store = null;
       }
@@ -156,9 +168,8 @@ public class ImapsProtocol implements Pr
         }
       }
     } catch (Exception e) {
-      e.printStackTrace();
       throw new ProtocolException("Failed to download " + fromFile + " to "
-          + toFile + " : " + e.getMessage());
+          + toFile + " : " + e.getMessage(), e);
     } finally {
       try {
         closeFolder(currentFolder);
@@ -219,7 +230,7 @@ public class ImapsProtocol implements Pr
       // changedDir = false;
     } catch (Exception e) {
       if (!currentFolder.getFullName().equals(""))
-        throw new ProtocolException("Failed to ls");
+        throw new ProtocolException("Failed to ls : " + e.getMessage(), e);
     } finally {
       try {
         closeFolder(currentFolder);
@@ -233,12 +244,12 @@ public class ImapsProtocol implements Pr
   public synchronized ProtocolFile pwd()
       throws ProtocolException {
     try {
-      String pwd = this.currentFolder.getFullName();
+      String pwd = currentFolder.getFullName();
       if (!pwd.equals("") && !pwd.startsWith("/"))
         pwd = "/" + pwd;
       return new ProtocolFile(pwd, true);
     } catch (Exception e) {
-      throw new ProtocolException("Failed to pwd : " + e.getMessage());
+      throw new ProtocolException("Failed to pwd : " + e.getMessage(), e);
     }
   }
 

Added: oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/TestImapsProtocol.java
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/TestImapsProtocol.java?rev=1133174&view=auto
==============================================================================
--- oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/TestImapsProtocol.java (added)
+++ oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/TestImapsProtocol.java Tue Jun  7 22:05:42 2011
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.imaps;
+
+//OODT imports
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.oodt.cas.protocol.ProtocolFile;
+import org.apache.oodt.cas.protocol.auth.BasicAuthentication;
+import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+
+//GreenMail imports
+import com.icegreen.greenmail.util.DummySSLSocketFactory;
+import com.icegreen.greenmail.util.GreenMail;
+import com.icegreen.greenmail.util.GreenMailUtil;
+
+//JUnit imports
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link ImapsProtocol}.
+ * 
+ * @author bfoster
+ */
+public class TestImapsProtocol extends TestCase {
+
+	private GreenMail gMail;
+	private ImapsProtocol imapsProtocol;
+	
+	@Override
+	public void setUp() {
+  	System.setProperty("mail.imaps.socketFactory.class", DummySSLSocketFactory.class.getCanonicalName());
+		System.setProperty("mail.imaps.socketFactory.fallback", "false");
+		gMail = new GreenMail();
+		gMail.setUser("bfoster@google.com", "password");
+		gMail.start();
+		ImapsProtocol.port = gMail.getImaps().getPort();
+		imapsProtocol = new ImapsProtocol();
+		try {
+			imapsProtocol.connect("localhost", new BasicAuthentication("bfoster@google.com", "password"));
+		} catch (ProtocolException e) {
+			fail("Failed to connect to GreenMail IMAPS server : " + e.getMessage());
+		}
+		assertEquals(1, ImapsProtocol.connectCalls);
+	}
+	
+	@Override
+	public void tearDown() {
+		gMail.stop();
+		try {
+			imapsProtocol.close();
+		} catch (ProtocolException e) {
+			fail("Failed to disconnect from GreenMail IMAPS server : " + e.getMessage());
+		}
+		assertEquals(0, ImapsProtocol.connectCalls);
+	}
+	
+	public void testCDAndPWD() throws ProtocolException {
+		assertEquals("", imapsProtocol.pwd().getPath());
+		imapsProtocol.cd(new ProtocolFile("INBOX", true));
+		assertEquals("/INBOX", imapsProtocol.pwd().getPath());
+	}
+	
+	public void testLSandGET() throws ProtocolException, IOException {
+		GreenMailUtil.sendTextEmailSecureTest("bfoster@google.com", "tom@bumster.org", "Test Subject", "Test Body");
+		imapsProtocol.cd(new ProtocolFile("INBOX", true));
+		List<ProtocolFile> emails = imapsProtocol.ls();
+		assertEquals(1, emails.size());
+		File bogusFile = File.createTempFile("bogus", "bogus");
+		File tmpDir = new File(bogusFile.getParentFile(), "TestImapsProtocol");
+		bogusFile.delete();
+		tmpDir.mkdirs();
+		
+		File email = new File(tmpDir, "test-email");
+		imapsProtocol.get(emails.get(0), email);
+		String[] splitEmail = FileUtils.readFileToString(email, "UTF-8").split("\n");
+		assertEquals("From: tom@bumster.org", splitEmail[0]);
+		assertEquals("To: bfoster@google.com", splitEmail[1]);
+		assertEquals("Subject: Test Subject", splitEmail[2]);
+		// 3 is divider text (i.e. ----- ~ Message ~ -----)
+		assertEquals("Test Body", splitEmail[4]);
+	}
+	
+	public void testDelete() throws ProtocolException {
+		GreenMailUtil.sendTextEmailSecureTest("bfoster@google.com", "tom@bumster.org", "Test Subject", "Test Body");
+		imapsProtocol.cd(new ProtocolFile("INBOX", true));
+		List<ProtocolFile> emails = imapsProtocol.ls();
+		assertEquals(1, emails.size());
+		imapsProtocol.delete(emails.get(0));
+		emails = imapsProtocol.ls();
+		assertEquals(0, emails.size());
+	}
+}

Propchange: oodt/branches/protocol/protocol-imaps/src/test/org/apache/oodt/cas/protocol/imaps/TestImapsProtocol.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain