Author: norman
Date: Thu Aug 20 16:42:11 2009
New Revision: 806247
URL: http://svn.apache.org/viewvc?rev=806247&view=rev
Log:
Start with creating mock objects for junit tests
Added:
labs/hupa/src/test/
labs/hupa/src/test/java/
labs/hupa/src/test/java/org/
labs/hupa/src/test/java/org/apache/
labs/hupa/src/test/java/org/apache/hupa/
labs/hupa/src/test/java/org/apache/hupa/server/
labs/hupa/src/test/java/org/apache/hupa/server/mock/
labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java
labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPStore.java
Added: 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=806247&view=auto
==============================================================================
--- labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java
(added)
+++ labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPFolder.java Thu
Aug 20 16:42:11 2009
@@ -0,0 +1,253 @@
+/****************************************************************
+ * 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.mock;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.mail.FetchProfile;
+import javax.mail.Flags;
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Store;
+import javax.mail.search.SearchTerm;
+
+import com.sun.mail.imap.IMAPFolder;
+import com.sun.mail.imap.IMAPStore;
+
+public class MockIMAPFolder extends IMAPFolder{
+
+ private boolean closed;
+ private boolean exists;
+
+ public MockIMAPFolder(String fullName, char separator, IMAPStore store)
{
+ super(fullName, separator, store);
+ }
+
+ public List<Message> messages = new ArrayList<Message>();
+ @Override
+ public synchronized Message[] addMessages(Message[] mArray)
+ throws MessagingException {
+ messages.addAll(Arrays.asList(mArray));
+ return mArray;
+ }
+
+ @Override
+ public synchronized void close(boolean expunge) throws
MessagingException {
+ closed = true;
+ }
+
+ @Override
+ public synchronized void copyMessages(Message[] messages, Folder folder)
+ throws MessagingException {
+ }
+
+ @Override
+ public synchronized boolean create(int type) throws MessagingException {
+ exists = true;
+ return true;
+ }
+
+ @Override
+ public synchronized boolean delete(boolean recursive) throws
MessagingException {
+ exists = false;
+ return true;
+ }
+
+ @Override
+ public synchronized boolean exists() throws MessagingException {
+ return exists;
+ }
+
+ @Override
+ public synchronized void fetch(Message[] msgs, FetchProfile fp)
+ throws MessagingException {
+ //nothing todo
+ }
+
+ @Override
+ public synchronized int getDeletedMessageCount() throws
MessagingException {
+ return 0;
+ }
+
+ @Override
+ public Folder getFolder(String name) throws MessagingException {
+ return super.getFolder(name);
+ }
+
+ @Override
+ public synchronized String getFullName() {
+ return fullName;
+ }
+
+ @Override
+ public synchronized Message getMessage(int msgnum)
+ throws MessagingException {
+ if (messages.size() < msgnum) {
+ throw new MessagingException();
+ }
+ return messages.get(msgnum -1);
+ }
+
+ @Override
+ public synchronized Message getMessageByUID(long arg0)
+ throws MessagingException {
+ // TODO Auto-generated method stub
+ return super.getMessageByUID(arg0);
+ }
+
+ @Override
+ public synchronized int getMessageCount() throws MessagingException {
+ return messages.size();
+ }
+
+ @Override
+ public synchronized Message[] getMessagesByUID(long arg0, long arg1)
+ throws MessagingException {
+ // TODO Implement me
+ return super.getMessagesByUID(arg0, arg1);
+ }
+
+ @Override
+ public synchronized Message[] getMessagesByUID(long[] arg0)
+ throws MessagingException {
+ // TODO Auto-generated method stub
+ return super.getMessagesByUID(arg0);
+ }
+
+ @Override
+ public synchronized String getName() {
+ return name;
+ }
+
+ @Override
+ public synchronized int getNewMessageCount() throws MessagingException {
+ return 0;
+ }
+
+ @Override
+ public synchronized Folder getParent() throws MessagingException {
+ return null;
+ }
+
+ @Override
+ public void idle() throws MessagingException {
+
+ }
+
+ @Override
+ public synchronized boolean isOpen() {
+ return closed == false;
+ }
+
+ @Override
+ public synchronized boolean isSubscribed() {
+ return true;
+ }
+
+ @Override
+ public synchronized void open(int arg0) throws MessagingException {
+ closed = true;
+ }
+
+ @Override
+ public synchronized boolean renameTo(Folder f) throws
MessagingException {
+ // TODO Auto-generated method stub
+ return super.renameTo(f);
+ }
+
+ @Override
+ public synchronized Message[] search(SearchTerm arg0, Message[] arg1)
+ throws MessagingException {
+ return arg1;
+ }
+
+ @Override
+ public synchronized Message[] search(SearchTerm arg0)
+ throws MessagingException {
+ return getMessages();
+ }
+
+ @Override
+ public synchronized void setFlags(Message[] arg0, Flags arg1, boolean
arg2)
+ throws MessagingException {
+
+ }
+
+ @Override
+ public synchronized Message[] getMessages() throws MessagingException {
+ return messages.toArray(new Message[messages.size()]);
+
+ }
+
+ @Override
+ public synchronized Message[] getMessages(int start, int end)
+ throws MessagingException {
+ int realStart = start -1;
+ int realEnd = end -1;
+ int range = realEnd - realStart;
+ int count = 0;
+ Message[] array = new Message[range];
+ while (realStart < realEnd) {
+ array[count] = messages.get(realStart);
+ realStart++;
+ count++;
+ }
+ return array;
+ }
+
+ @Override
+ public synchronized Message[] getMessages(int[] ints)
+ throws MessagingException {
+ Message[] array = new Message[ints.length];
+
+ for (int i = 0; i < ints.length; i++) {
+ int mInt = ints[i] -1;
+ if (mInt > messages.size() || mInt < messages.size()) {
+ throw new MessagingException();
+ }
+ array[i] = messages.get(i);
+ }
+ return array;
+ }
+
+ @Override
+ public Store getStore() {
+ // TODO Auto-generated method stub
+ return super.getStore();
+ }
+
+ @Override
+ public synchronized void setFlags(int arg0, int arg1, Flags arg2,
+ boolean arg3) throws MessagingException {
+
+ }
+
+ @Override
+ public synchronized void setFlags(int[] arg0, Flags arg1, boolean arg2)
+ throws MessagingException {
+
+ }
+
+
+
+}
Added: labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPStore.java
URL:
http://svn.apache.org/viewvc/labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPStore.java?rev=806247&view=auto
==============================================================================
--- labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPStore.java
(added)
+++ labs/hupa/src/test/java/org/apache/hupa/server/mock/MockIMAPStore.java Thu
Aug 20 16:42:11 2009
@@ -0,0 +1,111 @@
+package org.apache.hupa.server.mock;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.mail.AuthenticationFailedException;
+import javax.mail.Folder;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.URLName;
+
+/****************************************************************
+ * 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. *
+ ****************************************************************/
+
+import com.sun.mail.imap.IMAPStore;
+
+public class MockIMAPStore extends IMAPStore{
+
+ private Map<String, String> validLogins = new HashMap<String, String>();
+ private Map<String, Integer> validServers = new HashMap<String,
Integer>();
+ private boolean connected = false;
+
+ public MockIMAPStore(Session session, URLName url) {
+ super(session, url);
+ }
+
+ public void setValidLogins(Map<String,String> validLogins) {
+ this.validLogins = validLogins;
+ }
+
+ public void setValidServers(Map<String,Integer> validServers) {
+ this.validServers = validServers;
+ }
+ @Override
+ public synchronized void close() throws MessagingException {
+ connected = false;
+ }
+
+ @Override
+ public synchronized Folder getDefaultFolder() throws MessagingException
{
+ return super.getDefaultFolder();
+ }
+
+ @Override
+ public synchronized Folder getFolder(String name) throws
MessagingException {
+ return super.getFolder(name);
+ }
+
+ @Override
+ public void idle() throws MessagingException {
+ // nothing todo
+ }
+
+ @Override
+ public synchronized boolean isConnected() {
+ return connected;
+ }
+
+ @Override
+ public void connect() throws MessagingException {
+ connected = true;
+ }
+
+ @Override
+ public synchronized void connect(String host, int port, String username,
+ String password) throws MessagingException {
+ Integer myPort = validServers.get(host);
+ if (myPort != null && myPort.intValue() == port) {
+ connect(username,password);
+ }
+ throw new MessagingException("Can't connect to host");
+
+ }
+
+ @Override
+ public void connect(String host, String user, String password)
+ throws MessagingException {
+ if (validServers.containsKey(host) == false) {
+ throw new MessagingException("Can't connect to host");
+ }
+ connect(user,password);
+ }
+
+ @Override
+ public void connect(String user, String password) throws
MessagingException {
+ String pass = validLogins.get(user);
+ if (pass != null && validLogins.get(user).equals(password)) {
+ connect();
+ return;
+ }
+ throw new AuthenticationFailedException("Invalid login");
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]