Author: norman
Date: Thu Aug 27 23:58:36 2009
New Revision: 808683

URL: http://svn.apache.org/viewvc?rev=808683&view=rev
Log:
Add DeleteMessageHandlerTest 
Implement more mocking methods

Added:
    
labs/hupa/src/test/java/org/apache/hupa/server/handler/DeleteMessageHandlerTest.java
Modified:
    labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java

Added: 
labs/hupa/src/test/java/org/apache/hupa/server/handler/DeleteMessageHandlerTest.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/test/java/org/apache/hupa/server/handler/DeleteMessageHandlerTest.java?rev=808683&view=auto
==============================================================================
--- 
labs/hupa/src/test/java/org/apache/hupa/server/handler/DeleteMessageHandlerTest.java
 (added)
+++ 
labs/hupa/src/test/java/org/apache/hupa/server/handler/DeleteMessageHandlerTest.java
 Thu Aug 27 23:58:36 2009
@@ -0,0 +1,134 @@
+/****************************************************************
+ * 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.hupa.server.handler;
+
+import java.util.ArrayList;
+import java.util.Properties;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+
+import net.customware.gwt.dispatch.shared.ActionException;
+
+import org.apache.hupa.server.mock.MockIMAPFolder;
+import org.apache.hupa.server.mock.MockIMAPStore;
+import org.apache.hupa.server.mock.MockLog;
+import org.apache.hupa.shared.data.IMAPFolder;
+import org.apache.hupa.shared.data.User;
+import org.apache.hupa.shared.rpc.DeleteMessage;
+import org.apache.hupa.shared.rpc.DeleteMessageResult;
+
+public class DeleteMessageHandlerTest extends AbstractHandlerTest{
+
+       
+       public void testDeleteFolderNotExists() throws MessagingException {
+               DeleteMessageHandler handler = new 
DeleteMessageHandler(storeCache,new MockLog(),sessionProvider);
+       
+               User user = createUser();
+               storeCache.addValidUser(user.getName(), user.getPassword());
+               session.setAttribute("user", user);
+               IMAPFolder folder = new IMAPFolder();
+               folder.setFullName("NOT_EXISTS");
+               DeleteMessage action = new DeleteMessage(VALID_ID,folder,new 
ArrayList<Long>());
+
+               try {
+                       handler.execute(action, null);
+                       fail("Folder should not exists!");
+               } catch (ActionException e) {
+                       e.printStackTrace();
+               }
+       }
+       
+       public void testDeleteFolderExistsAndNotTrash() throws 
MessagingException {
+               Session s = Session.getInstance(new Properties());
+               DeleteMessageHandler handler = new 
DeleteMessageHandler(storeCache,new MockLog(),sessionProvider);
+       
+               User user = createUser();
+               storeCache.addValidUser(user.getName(), user.getPassword());
+               session.setAttribute("user", user);
+               IMAPFolder folder = new IMAPFolder();
+               folder.setFullName("EXISTS");
+               MockIMAPStore store = (MockIMAPStore) storeCache.get(user);
+               store.clear();
+
+               MockIMAPFolder f = 
(MockIMAPFolder)store.getFolder(folder.getFullName());
+               f.create(Folder.HOLDS_FOLDERS);
+               f.addMessages(new Message[] { new MimeMessage(s), new 
MimeMessage(s), new MimeMessage(s)});
+               ArrayList<Long> uids = new ArrayList<Long>();
+               uids.add(new Long(1));
+               uids.add(new Long(3));
+               DeleteMessage action = new DeleteMessage(VALID_ID, folder, 
uids);
+
+               MockIMAPFolder f3 = (MockIMAPFolder) 
store.getFolder(IMAPFolder.DEFAULT_TRASH);
+               assertFalse("Trash folder not exists yet",f3.exists());
+               
+               try {
+                       DeleteMessageResult result = handler.execute(action, 
null);
+                       ArrayList<Long> dUids = result.getMessageUids();
+                       assertEquals("Delete message with uid 1",new Long(1), 
dUids.get(0));
+                       assertEquals("Delete message with uid 2", new Long(3), 
dUids.get(1));
+
+                       assertEquals("Only 1 message left", 1, 
f.getMessageCount());
+                       
+                       MockIMAPFolder f2 = (MockIMAPFolder) 
store.getFolder(IMAPFolder.DEFAULT_TRASH);
+                       assertTrue("Trash folder created",f2.exists());
+                       assertEquals("2 messages moved", 2, 
f2.getMessageCount());
+               } catch (ActionException e) {
+                       e.printStackTrace();
+                       fail();
+               }
+       }
+       
+       public void testDeleteFolderExistsAndIsTrash() throws 
MessagingException {
+               Session s = Session.getInstance(new Properties());
+               DeleteMessageHandler handler = new 
DeleteMessageHandler(storeCache,new MockLog(),sessionProvider);
+       
+               User user = createUser();
+               storeCache.addValidUser(user.getName(), user.getPassword());
+               session.setAttribute("user", user);
+               IMAPFolder folder = new IMAPFolder();
+               folder.setFullName(IMAPFolder.DEFAULT_TRASH);
+               MockIMAPStore store = (MockIMAPStore) storeCache.get(user);
+               store.clear();
+               
+               MockIMAPFolder f = 
(MockIMAPFolder)store.getFolder(folder.getFullName());
+               f.create(Folder.HOLDS_FOLDERS);
+               f.addMessages(new Message[] { new MimeMessage(s), new 
MimeMessage(s), new MimeMessage(s)});
+               ArrayList<Long> uids = new ArrayList<Long>();
+               uids.add(new Long(1));
+               uids.add(new Long(3));
+               DeleteMessage action = new DeleteMessage(VALID_ID, folder, 
uids);
+
+               try {
+                       DeleteMessageResult result = handler.execute(action, 
null);
+                       ArrayList<Long> dUids = result.getMessageUids();
+                       assertEquals("Delete message with uid 1",new Long(1), 
dUids.get(0));
+                       assertEquals("Delete message with uid 2", new Long(3), 
dUids.get(1));
+
+                       assertEquals("Only 1 message left", 1, 
f.getMessageCount());
+               } catch (ActionException e) {
+                       e.printStackTrace();
+                       fail();
+               }
+       }
+}

Modified: 
labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java?rev=808683&r1=808682&r2=808683&view=diff
==============================================================================
--- labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java 
(original)
+++ labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java Thu 
Aug 27 23:58:36 2009
@@ -29,54 +29,66 @@
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Store;
+import javax.mail.Flags.Flag;
 import javax.mail.search.SearchTerm;
 
 import com.sun.mail.imap.IMAPFolder;
 import com.sun.mail.imap.IMAPStore;
 
-public class MockIMAPFolder extends IMAPFolder{
+public class MockIMAPFolder extends IMAPFolder {
 
        private boolean closed;
        private boolean exists;
        public final static char SEPERATOR = '.';
+
        public MockIMAPFolder(String fullName, IMAPStore store) {
                super(fullName, SEPERATOR, store);
        }
 
        public List<Message> messages = new ArrayList<Message>();
+
        @Override
        public synchronized Message[] addMessages(Message[] mArray)
                        throws MessagingException {
+               checkExists();
                messages.addAll(Arrays.asList(mArray));
                return mArray;
        }
 
        @Override
        public synchronized void close(boolean expunge) throws 
MessagingException {
+               checkExists();
                closed = true;
        }
 
        @Override
        public Folder[] list() throws MessagingException {
-               List<MockIMAPFolder> folders = 
((MockIMAPStore)store).getChilds(this);
+               List<MockIMAPFolder> folders = ((MockIMAPStore) 
store).getChilds(this);
                return folders.toArray(new Folder[folders.size()]);
        }
 
        @Override
        public synchronized void copyMessages(Message[] messages, Folder folder)
                        throws MessagingException {
+               checkExists();
+               ((MockIMAPFolder) folder).addMessages(messages);
+
        }
 
        @Override
        public synchronized boolean create(int type) throws MessagingException {
+               if (exists()) {
+                       throw new MessagingException("Folder already exists!");
+               }
                exists = true;
-               return ((MockIMAPStore)store).save(this);
+               return ((MockIMAPStore) store).save(this);
        }
 
        @Override
-       public synchronized boolean delete(boolean recursive) throws 
MessagingException {
+       public synchronized boolean delete(boolean recursive)
+                       throws MessagingException {
                exists = false;
-               return ((MockIMAPStore)store).delete(this,recursive);
+               return ((MockIMAPStore) store).delete(this, recursive);
        }
 
        @Override
@@ -87,7 +99,8 @@
        @Override
        public synchronized void fetch(Message[] msgs, FetchProfile fp)
                        throws MessagingException {
-               //nothing todo
+               // nothing todo
+               checkExists();
        }
 
        @Override
@@ -108,32 +121,38 @@
        @Override
        public synchronized Message getMessage(int msgnum)
                        throws MessagingException {
+               checkExists();
                if (messages.size() < msgnum) {
                        throw new MessagingException();
-               } 
-               return messages.get(msgnum -1); 
+               }
+               return messages.get(msgnum - 1);
        }
 
        @Override
        public synchronized Message getMessageByUID(long uid)
                        throws MessagingException {
+               checkExists();
                return getMessage(new Long(uid).intValue());
        }
 
        @Override
        public synchronized int getMessageCount() throws MessagingException {
+               checkExists();
                return messages.size();
        }
 
        @Override
        public synchronized Message[] getMessagesByUID(long uidstart, long 
uidend)
                        throws MessagingException {
-               return getMessages(new Long(uidstart).intValue(), new 
Long(uidend).intValue());
+               checkExists();
+               return getMessages(new Long(uidstart).intValue(), new 
Long(uidend)
+                               .intValue());
        }
 
        @Override
        public synchronized Message[] getMessagesByUID(long[] uids)
                        throws MessagingException {
+               checkExists();
                int ints[] = new int[uids.length];
                for (int i = 0; i < uids.length; i++) {
                        ints[i] = new Long(uids[i]).intValue();
@@ -148,17 +167,18 @@
 
        @Override
        public synchronized int getNewMessageCount() throws MessagingException {
+               checkExists();
                return 0;
        }
 
        @Override
        public synchronized Folder getParent() throws MessagingException {
-               return ((MockIMAPStore)store).getParent(this);
+               return ((MockIMAPStore) store).getParent(this);
        }
 
        @Override
        public void idle() throws MessagingException {
-
+               checkExists();
        }
 
        @Override
@@ -173,34 +193,71 @@
 
        @Override
        public synchronized void open(int arg0) throws MessagingException {
-               closed = true;
+               checkExists();
+               closed = false;
        }
 
        @Override
        public synchronized boolean renameTo(Folder f) throws 
MessagingException {
+               checkExists();
                return false;
        }
 
        @Override
        public synchronized Message[] search(SearchTerm arg0, Message[] arg1)
                        throws MessagingException {
+               checkExists();
                return arg1;
        }
 
        @Override
+       public synchronized Message[] expunge() throws MessagingException {
+               checkExists();
+               return expunge(messages.toArray(new Message[messages.size()]));
+       }
+
+       @Override
+       public synchronized Message[] expunge(Message[] msgs)
+                       throws MessagingException {
+               checkExists();
+               List<Message> mList = new ArrayList<Message>();
+               for (int i = 0; i < msgs.length; i++) {
+                       Message m = msgs[i];
+                       if (m.getFlags().contains(Flag.DELETED)) {
+                               if (messages.remove(m)) {
+                                       mList.add(m);
+                               }
+                       }
+               }
+               return mList.toArray(new Message[mList.size()]);
+       }
+
+       @Override
        public synchronized Message[] search(SearchTerm arg0)
                        throws MessagingException {
+               checkExists();
                return getMessages();
        }
 
        @Override
-       public synchronized void setFlags(Message[] arg0, Flags arg1, boolean 
arg2)
-                       throws MessagingException {
-               
+       public synchronized void setFlags(Message[] mArray, Flags flags,
+                       boolean value) throws MessagingException {
+               checkExists();
+               for (int i = 0; i < mArray.length; i++) {
+                       Message m = mArray[i];
+                       for (int a = 0; a < messages.size(); a++) {
+                               Message m2 = messages.get(a);
+                               if (m2.equals(m)) {
+                                       m2.setFlags(flags, value);
+                                       break;
+                               }
+                       }
+               }
        }
 
        @Override
        public synchronized Message[] getMessages() throws MessagingException {
+               checkExists();
                return messages.toArray(new Message[messages.size()]);
 
        }
@@ -208,8 +265,9 @@
        @Override
        public synchronized Message[] getMessages(int start, int end)
                        throws MessagingException {
-               int realStart = start -1;
-               int realEnd = end -1;
+               checkExists();
+               int realStart = start - 1;
+               int realEnd = end - 1;
                int range = realEnd - realStart;
                int count = 0;
                Message[] array = new Message[range];
@@ -224,10 +282,11 @@
        @Override
        public synchronized Message[] getMessages(int[] ints)
                        throws MessagingException {
+               checkExists();
                Message[] array = new Message[ints.length];
 
-               for (int i = 0; i  < ints.length; i++) {
-                       int mInt = ints[i] -1;
+               for (int i = 0; i < ints.length; i++) {
+                       int mInt = ints[i] - 1;
                        if (mInt > messages.size() || mInt < messages.size()) {
                                throw new MessagingException();
                        }
@@ -244,17 +303,18 @@
        @Override
        public synchronized void setFlags(int arg0, int arg1, Flags arg2,
                        boolean arg3) throws MessagingException {
-       
+               checkExists();
        }
 
        @Override
        public synchronized void setFlags(int[] arg0, Flags arg1, boolean arg2)
                        throws MessagingException {
-       
+               checkExists();
        }
 
        @Override
        public synchronized long getUID(Message message) throws 
MessagingException {
+               checkExists();
                return messages.indexOf(message);
        }
 
@@ -262,7 +322,11 @@
        public synchronized int getUnreadMessageCount() throws 
MessagingException {
                return 1900;
        }
-       
-       
+
+       private void checkExists() throws MessagingException {
+               if (exists() == false) {
+                       throw new MessagingException("Folder not exists");
+               }
+       }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to