DAV: add unit tests, move buildCalendarPath logic to getFolderPath

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1099 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-06-20 22:12:36 +00:00
parent d552bc2993
commit 878f30818e
4 changed files with 176 additions and 10 deletions

View File

@ -89,7 +89,9 @@ public abstract class ExchangeSession {
protected static final String PUBLIC_ROOT = "/public";
protected static final String CALENDAR = "calendar";
protected static final String CONTACTS = "contacts";
protected static final String ADDRESSBOOK = "addressbook";
protected static final String INBOX = "INBOX";
protected static final String LOWER_CASE_INBOX = "inbox";
protected static final String SENT = "Sent";
protected static final String DRAFTS = "Drafts";
protected static final String TRASH = "Trash";
@ -102,6 +104,7 @@ public abstract class ExchangeSession {
* Base user mailboxes path (used to select folder)
*/
protected String mailPath;
protected String rootPath;
protected String email;
protected String alias;
protected final HttpClient httpClient;

View File

@ -86,6 +86,16 @@ public class DavExchangeSession extends ExchangeSession {
protected String contactsUrl;
protected String outboxUrl;
protected String inboxName;
protected String deleteditemsName;
protected String sentitemsName;
protected String draftsName;
protected String calendarName;
protected String contactsName;
protected String outboxName;
protected static final String USERS = "/users/";
/**
* Convert logical or relative folder path to absolute folder path.
*
@ -94,20 +104,49 @@ public class DavExchangeSession extends ExchangeSession {
*/
public String getFolderPath(String folderName) {
String folderPath;
// IMAP path
if (folderName.startsWith(INBOX)) {
folderPath = folderName.replaceFirst(INBOX, inboxUrl);
folderPath = mailPath + inboxName + folderName.substring(INBOX.length());
} else if (folderName.startsWith(TRASH)) {
folderPath = folderName.replaceFirst(TRASH, deleteditemsUrl);
folderPath = mailPath + deleteditemsName + folderName.substring(TRASH.length());
} else if (folderName.startsWith(DRAFTS)) {
folderPath = folderName.replaceFirst(DRAFTS, draftsUrl);
folderPath = mailPath + draftsName + folderName.substring(DRAFTS.length());
} else if (folderName.startsWith(SENT)) {
folderPath = folderName.replaceFirst(SENT, sentitemsUrl);
} else if (folderName.startsWith(CALENDAR)) {
folderPath = folderName.replaceFirst(CALENDAR, calendarUrl);
} else if (folderName.startsWith(CONTACTS)) {
folderPath = folderName.replaceFirst(CONTACTS, contactsUrl);
folderPath = mailPath + sentitemsName + folderName.substring(SENT.length());
} else if (folderName.startsWith("public")) {
folderPath = publicFolderUrl + folderName.substring("public".length());
// caldav path
} else if (folderName.startsWith(USERS)) {
// get requested principal
String principal = null;
String localPath;
int principalIndex = folderName.indexOf('/', USERS.length());
if (principalIndex >= 0) {
principal = folderName.substring(USERS.length(), principalIndex);
localPath = folderName.substring(USERS.length() + principal.length() + 1);
if (localPath.startsWith(LOWER_CASE_INBOX)) {
localPath = inboxName + localPath.substring(LOWER_CASE_INBOX.length());
} else if (localPath.startsWith(CALENDAR)) {
localPath = calendarName + localPath.substring(CALENDAR.length());
} else if (localPath.startsWith(CONTACTS) ) {
localPath = contactsName + localPath.substring(CONTACTS.length());
} else if (localPath.startsWith(ADDRESSBOOK) ) {
localPath = contactsName + localPath.substring(ADDRESSBOOK.length());
}
} else {
principal = folderName.substring(USERS.length());
localPath = "";
}
if (principal.length() == 0) {
folderPath = rootPath;
} else if (alias.equalsIgnoreCase(principal) || email.equalsIgnoreCase(principal)) {
folderPath = mailPath + localPath;
} else {
LOGGER.debug("Detected shared path for principal " + principal + ", user principal is " + email);
folderPath = rootPath + principal + '/' + localPath;
}
// absolute folder path
} else if (folderName.startsWith("/")) {
folderPath = folderName;
@ -127,6 +166,7 @@ public class DavExchangeSession extends ExchangeSession {
* @param folderName requested folder name
* @return Exchange folder path
* @throws IOException on error
* @deprecated user getFolderPath instead
*/
public String buildCalendarPath(String principal, String folderName) throws IOException {
StringBuilder buffer = new StringBuilder();
@ -213,6 +253,7 @@ public class DavExchangeSession extends ExchangeSession {
mailPath = "/exchange/" + email + '/';
LOGGER.debug("Current user email is " + email + ", mailPath is " + mailPath);
}
rootPath = mailPath.substring(0, mailPath.lastIndexOf('/', mailPath.length() - 2) + 1);
} catch (IOException e) {
LOGGER.error("Error parsing main page at " + method.getPath(), e);
} finally {
@ -241,6 +282,22 @@ public class DavExchangeSession extends ExchangeSession {
}
}
// return last folder name from url
protected String getFolderName(String url) {
if (url != null) {
if (url.endsWith("/")) {
return url.substring(url.lastIndexOf('/', url.length() - 2) + 1);
} else if (url.indexOf('/') > 0) {
return url.substring(url.lastIndexOf('/') + 1);
} else {
return null;
}
} else {
return null;
}
}
protected void getWellKnownFolders() throws DavMailException {
// Retrieve well known URLs
MultiStatusResponse[] responses;
@ -252,13 +309,20 @@ public class DavExchangeSession extends ExchangeSession {
}
DavPropertySet properties = responses[0].getProperties(HttpStatus.SC_OK);
inboxUrl = getURIPropertyIfExists(properties, "inbox");
inboxName = getFolderName(inboxUrl);
deleteditemsUrl = getURIPropertyIfExists(properties, "deleteditems");
deleteditemsName = getFolderName(deleteditemsUrl);
sentitemsUrl = getURIPropertyIfExists(properties, "sentitems");
sentitemsName = getFolderName(sentitemsUrl);
sendmsgUrl = getURIPropertyIfExists(properties, "sendmsg");
draftsUrl = getURIPropertyIfExists(properties, "drafts");
draftsName = getFolderName(draftsUrl);
calendarUrl = getURIPropertyIfExists(properties, "calendar");
calendarName = getFolderName(calendarUrl);
contactsUrl = getURIPropertyIfExists(properties, "contacts");
contactsName = getFolderName(contactsUrl);
outboxUrl = getURIPropertyIfExists(properties, "outbox");
outboxName = getFolderName(outboxUrl);
// junk folder not available over webdav
// default public folder path

View File

@ -19,8 +19,10 @@
package davmail.exchange;
import davmail.exchange.dav.DavExchangeSession;
import davmail.exchange.ews.EwsExchangeSession;
import junit.framework.TestCase;
import java.io.File;
import java.io.IOException;
import davmail.Settings;
@ -41,8 +43,13 @@ public class AbstractExchangeSessionTestCase extends TestCase {
@Override
public void setUp() throws IOException {
if (session == null) {
// Load current user settings
if (url == null) {
// try to load settings from current folder davmail.properties
File file = new File("davmail.properties");
if (file.exists()) {
Settings.setConfigFilePath("davmail.properties");
}
// Load current settings
Settings.load();
} else {
Settings.setDefaultSettings();
@ -60,7 +67,11 @@ public class AbstractExchangeSessionTestCase extends TestCase {
// open session, get username and password from davmail.properties
// Note: those properties should *not* exist in normal production mode,
// they are not used by DavMail, just by this test case
session = new DavExchangeSession(Settings.getProperty("davmail.url"), Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password"));
if (Settings.getBooleanProperty("davmail.enableEws")) {
session = new EwsExchangeSession(Settings.getProperty("davmail.url"), Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password"));
} else {
session = new DavExchangeSession(Settings.getProperty("davmail.url"), Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password"));
}
}
}

View File

@ -0,0 +1,88 @@
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2010 Mickael Guessant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package davmail.exchange.dav;
import davmail.exchange.AbstractExchangeSessionTestCase;
import java.io.IOException;
/**
* Webdav specific unit tests
*/
public class TestDavExchangeSession extends AbstractExchangeSessionTestCase {
DavExchangeSession davSession;
public void setUp() throws IOException {
super.setUp();
davSession = ((DavExchangeSession) session);
}
public void testGetFolderPath() {
String mailPath = davSession.getFolderPath("");
String rootPath = davSession.getFolderPath("/users/");
assertEquals(mailPath + davSession.inboxName, davSession.getFolderPath("INBOX"));
assertEquals(mailPath + davSession.deleteditemsName, davSession.getFolderPath("Trash"));
assertEquals(mailPath + davSession.sentitemsName, davSession.getFolderPath("Sent"));
assertEquals(mailPath + davSession.draftsName, davSession.getFolderPath("Drafts"));
assertEquals(mailPath + davSession.inboxName + "/test", davSession.getFolderPath("INBOX/test"));
assertEquals(mailPath + davSession.deleteditemsName + "/test", davSession.getFolderPath("Trash/test"));
assertEquals(mailPath + davSession.sentitemsName + "/test", davSession.getFolderPath("Sent/test"));
assertEquals(mailPath + davSession.draftsName + "/test", davSession.getFolderPath("Drafts/test"));
// TODO: may be wrong, should return full url, public folders may be located on another server
assertEquals("/public", davSession.getFolderPath("/public"));
assertEquals("/public/test", davSession.getFolderPath("/public/test"));
// caldav folder paths
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getEmail()));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getEmail()+ '/'));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getAlias()));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getAlias()+ '/'));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getEmail().toUpperCase()));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getEmail().toLowerCase()));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getAlias().toUpperCase()));
assertEquals(mailPath, davSession.getFolderPath("/users/"+davSession.getAlias().toLowerCase()));
assertEquals(mailPath+"subfolder", davSession.getFolderPath("/users/"+davSession.getAlias()+ "/subfolder"));
assertEquals(mailPath+"subfolder/", davSession.getFolderPath("/users/"+davSession.getAlias()+ "/subfolder/"));
assertEquals(rootPath+"anotheruser/", davSession.getFolderPath("/users/anotheruser"));
assertEquals(rootPath+"anotheruser/subfolder", davSession.getFolderPath("/users/anotheruser/subfolder"));
assertEquals(mailPath+davSession.inboxName, davSession.getFolderPath("/users/"+davSession.getEmail()+"/inbox"));
assertEquals(mailPath+davSession.inboxName+"/subfolder", davSession.getFolderPath("/users/"+davSession.getEmail()+"/inbox/subfolder"));
assertEquals(mailPath+davSession.calendarName, davSession.getFolderPath("/users/"+davSession.getEmail()+"/calendar"));
assertEquals(mailPath+davSession.contactsName, davSession.getFolderPath("/users/"+davSession.getEmail()+"/contacts"));
assertEquals(mailPath+davSession.contactsName, davSession.getFolderPath("/users/"+davSession.getEmail()+"/addressbook"));
assertEquals(rootPath+"anotherUser/"+davSession.inboxName, davSession.getFolderPath("/users/anotherUser/inbox"));
assertEquals(rootPath+"anotherUser/"+davSession.calendarName, davSession.getFolderPath("/users/anotherUser/calendar"));
assertEquals(rootPath+"anotherUser/"+davSession.contactsName, davSession.getFolderPath("/users/anotherUser/contacts"));
// do not replace i18n names
assertEquals(mailPath+"Inbox", davSession.getFolderPath("/users/"+davSession.getEmail()+"/Inbox"));
assertEquals(mailPath+"Calendar", davSession.getFolderPath("/users/"+davSession.getEmail()+"/Calendar"));
assertEquals(mailPath+"Contacts", davSession.getFolderPath("/users/"+davSession.getEmail()+"/Contacts"));
}
}