From 1038fa5d1b853be7cde0440755e1ee28152ec960 Mon Sep 17 00:00:00 2001 From: mguessan Date: Thu, 1 Jul 2010 14:23:31 +0000 Subject: [PATCH] Dav: fix bug 3022451 in new search filter implementation with empty sub conditions git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1117 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 33 ++++- .../exchange/dav/DavExchangeSession.java | 2 +- .../exchange/ews/EwsExchangeSession.java | 5 + .../exchange/ExchangeSessionContactTest.java | 81 ++++++++++++ .../davmail/exchange/TestExchangeAdapter.java | 122 ++++++++++++++++++ src/test/davmail/imap/TestImap.java | 9 +- 6 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 src/test/davmail/exchange/ExchangeSessionContactTest.java create mode 100644 src/test/davmail/exchange/TestExchangeAdapter.java diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 14e7578e..c802c821 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -600,7 +600,14 @@ public abstract class ExchangeSession { * * @param buffer search filter buffer */ - public abstract void appendTo(StringBuilder buffer); + public void appendTo(StringBuilder buffer); + + /** + * True if condition is empty. + * + * @return true if condition is empty + */ + public boolean isEmpty(); } /** @@ -616,6 +623,10 @@ public abstract class ExchangeSession { this.operator = operator; this.value = value; } + + public boolean isEmpty() { + return false; + } } /** @@ -641,6 +652,17 @@ public abstract class ExchangeSession { conditions.add(condition); } } + + public boolean isEmpty() { + boolean isEmpty = true; + for (Condition condition: conditions) { + if (!condition.isEmpty()) { + isEmpty = false; + break; + } + } + return isEmpty; + } } /** @@ -652,6 +674,11 @@ public abstract class ExchangeSession { protected NotCondition(Condition condition) { this.condition = condition; } + + public boolean isEmpty() { + return condition.isEmpty(); + } + } /** @@ -665,6 +692,10 @@ public abstract class ExchangeSession { this.attributeName = attributeName; this.operator = operator; } + + public boolean isEmpty() { + return false; + } } /** diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index f2be0fc7..a41be869 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -364,7 +364,7 @@ public class DavExchangeSession extends ExchangeSession { boolean first = true; for (Condition condition : conditions) { - if (condition != null) { + if (condition != null && !condition.isEmpty()) { if (first) { buffer.append('('); first = false; diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 6e6c101c..8a24f328 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -244,6 +244,11 @@ public class EwsExchangeSession extends ExchangeSession { attributeMap.get(attributeName).appendTo(buffer); buffer.append(""); } + + public boolean isEmpty() { + return false; + } + } @Override diff --git a/src/test/davmail/exchange/ExchangeSessionContactTest.java b/src/test/davmail/exchange/ExchangeSessionContactTest.java new file mode 100644 index 00000000..2fa0c327 --- /dev/null +++ b/src/test/davmail/exchange/ExchangeSessionContactTest.java @@ -0,0 +1,81 @@ +/* + * 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; + +import davmail.Settings; +import davmail.http.DavGatewayHttpClientFacade; +import junit.framework.TestCase; +import org.apache.commons.httpclient.util.URIUtil; +import org.apache.jackrabbit.webdav.client.methods.PropPatchMethod; +import org.apache.jackrabbit.webdav.property.DavProperty; +import org.apache.jackrabbit.webdav.property.DavPropertyName; +import org.apache.jackrabbit.webdav.property.DefaultDavProperty; +import org.apache.jackrabbit.webdav.xml.Namespace; +import org.apache.log4j.Level; + +import java.io.IOException; +import java.util.ArrayList; + +/** + * Test contact search + */ +public class ExchangeSessionContactTest extends AbstractExchangeSessionTestCase { + @Override + public void setUp() throws IOException { + super.setUp(); + } + /* + public void testSearchPrivateFlag() throws IOException { + ExchangeSession.MessageList messageList = session.searchMessages("Contacts", " AND \"http://schemas.microsoft.com/mapi/proptag/0x360003\" = 2"); + assertEquals(1, messageList.size()); + } + + public void testSearchPrivateFlag2() throws IOException { + ExchangeSession.MessageList messageList = session.searchMessages("Contacts", " AND \"http://schemas.microsoft.com/mapi/sensitivity\" = 2"); + assertEquals(1, messageList.size()); + } + public void testSearchPrivateFlag3() throws IOException { + ExchangeSession.MessageList messageList = session.searchMessages("Contacts", " AND \"http://schemas.microsoft.com/exchange/sensitivity\" = 2"); + assertEquals(1, messageList.size()); + } + + public void testSearchPrivateFlag4() throws IOException { + ExchangeSession.MessageList messageList = session.searchMessages("Contacts", " AND Cast(\"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/0x8506\" as \"boolean\") = true"); + assertEquals(1, messageList.size()); + } + // + public void testUnsetPrivateFlag() throws IOException { + String messageUrl = URIUtil.encodePathQuery(session.getFolderPath("Contacts") + '/' + "test test" + ".EML"); + ArrayList list = new ArrayList(); + list.add(new DefaultDavProperty(DavPropertyName.create("sensitivity", Namespace.getNamespace("http://schemas.microsoft.com/mapi/")), "0")); + PropPatchMethod propPatchMethod = new PropPatchMethod(messageUrl, list); + DavGatewayHttpClientFacade.executeMethod(session.getHttpClient(), propPatchMethod); + } + + public void testSetPrivateFlag() throws IOException { + String messageUrl = URIUtil.encodePathQuery(session.getFolderPath("Contacts") + '/' + "test test" + ".EML"); + ArrayList list = new ArrayList(); + list.add(new DefaultDavProperty(DavPropertyName.create("sensitivity", Namespace.getNamespace("http://schemas.microsoft.com/mapi/")), "2")); + PropPatchMethod propPatchMethod = new PropPatchMethod(messageUrl, list); + //DavGatewayHttpClientFacade.executeMethod(session.getHttpClient(), propPatchMethod); + + ExchangeSession.MessageList messageList = session.searchMessages("Contacts", " AND Cast(\"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/0x8506\" as \"boolean\") = true"); + assertEquals(1, messageList.size()); + } */ +} diff --git a/src/test/davmail/exchange/TestExchangeAdapter.java b/src/test/davmail/exchange/TestExchangeAdapter.java new file mode 100644 index 00000000..478d0965 --- /dev/null +++ b/src/test/davmail/exchange/TestExchangeAdapter.java @@ -0,0 +1,122 @@ +/* + * 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; + +import davmail.Settings; +import davmail.exchange.dav.DavExchangeSession; +import davmail.exchange.ews.EwsExchangeSession; +import davmail.http.DavGatewaySSLProtocolSocketFactory; +import junit.framework.TestCase; +import org.apache.log4j.Level; + +import java.io.IOException; +import java.util.List; + +/** + * Test Exchange adapter methods. + */ +public class TestExchangeAdapter extends TestCase { + ExchangeSession davSession; + ExchangeSession ewsSession; + + @Override + public void setUp() throws IOException { + if (davSession == null) { + Settings.setConfigFilePath("davmail.properties"); + Settings.load(); + DavGatewaySSLProtocolSocketFactory.register(); + davSession = new DavExchangeSession(Settings.getProperty("davmail.url"), + Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password")); + ewsSession = new EwsExchangeSession(Settings.getProperty("davmail.url"), + Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password")); + Settings.setLoggingLevel("httpclient.wire", Level.INFO); + } + } + + public void assertEquals(ExchangeSession.Folder davFolder, ExchangeSession.Folder ewsFolder) { + assertNotNull(ewsFolder); + assertEquals(davFolder.folderPath, ewsFolder.folderPath); + assertEquals(davFolder.folderClass, ewsFolder.folderClass); + assertEquals(davFolder.hasChildren, ewsFolder.hasChildren); + assertEquals(davFolder.unreadCount, ewsFolder.unreadCount); + + assertEquals(davFolder.isCalendar(), false); + assertEquals(ewsFolder.isCalendar(), false); + + assertEquals(davFolder.isContact(), false); + assertEquals(ewsFolder.isContact(), false); + + assertEquals(davFolder.noInferiors, false); + assertEquals(ewsFolder.noInferiors, false); + + assertEquals(davFolder.getFlags(), ewsFolder.getFlags()); + assertEquals(davFolder.etag.substring(0, ewsFolder.ctag.length()-1)+ 'Z', ewsFolder.etag); + + + assertNotNull(davFolder.ctag); + assertNotNull(ewsFolder.ctag); + // dav and ews ctags are still different: dav contentag has milliseconds info + assertEquals(davFolder.ctag.substring(0, ewsFolder.ctag.length()-1)+ 'Z', ewsFolder.ctag); + + } + + public void testGetInbox() throws IOException { + ExchangeSession.Folder davFolder = davSession.getFolder("INBOX"); + ExchangeSession.Folder ewsFolder = ewsSession.getFolder("INBOX"); + assertEquals(davFolder, ewsFolder); + } + + public void testGetSubFolder() throws IOException { + ExchangeSession.Folder ewsFolder = ewsSession.getFolder("INBOX/bbbb"); + } + + public void testFindFolder() throws IOException { + List davFolders = davSession.getSubFolders("", false); + Settings.setLoggingLevel("httpclient.wire", Level.DEBUG); + List ewsFolders = ewsSession.getSubFolders("", false); + assertEquals(davFolders.size(), ewsFolders.size()); + } + + public void testFindPublicFolder() throws IOException { + List davFolders = davSession.getSubFolders("/public", false); + Settings.setLoggingLevel("httpclient.wire", Level.DEBUG); + List ewsFolders = ewsSession.getSubFolders("/public", false); + assertEquals(davFolders.size(), ewsFolders.size()); + } + + public void testFindFolders() throws IOException { + List davFolders = davSession.getSubFolders("/public", null, true); + System.out.println(davFolders); + } + + public void testSearchMessages() throws IOException { + ExchangeSession.MessageList messages = davSession.searchMessages("INBOX"); + for (ExchangeSession.Message message:messages) { + System.out.println(message); + } + } + + public void testSearchEvents() throws IOException { + List events = davSession.getAllEvents("calendar"); + for (ExchangeSession.Event event:events) { + System.out.println(event); + } + } + +} diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index ffd95c62..117c711e 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -71,9 +71,14 @@ public class TestImap extends AbstractExchangeSessionTestCase { assertEquals(". OK [READ-WRITE] SELECT completed", readFullAnswer(".")); } - public void testUidSearchUndeleted() throws IOException { + public void testUidSearchDeleted() throws IOException { writeLine(". UID SEARCH UNDELETED"); - assertEquals(". OK", readFullAnswer(".")); + assertEquals(". OK SEARCH completed", readFullAnswer(".")); + } + + public void testUidSearchUndeleted() throws IOException { + writeLine(". UID SEARCH DELETED"); + assertEquals(". OK SEARCH completed", readFullAnswer(".")); } public void testLogout() throws IOException {