From 795989445ed62f4d2b28772fc0a869cfc6ed0eca Mon Sep 17 00:00:00 2001 From: mguessan Date: Fri, 2 Jul 2010 06:37:18 +0000 Subject: [PATCH] IMAP: new unit tests and fix $Forwarded flag removal git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1122 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/imap/ImapConnection.java | 12 +- src/test/davmail/imap/TestImap.java | 131 ++++++++++++++++++++++ 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 3655a98e..53c3db9b 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -251,7 +251,7 @@ public class ImapConnection extends AbstractConnection { String folderName = BASE64MailboxDecoder.decode(tokens.nextToken()); try { session.deleteFolder(folderName); - sendClient(commandId + " OK delete completed"); + sendClient(commandId + " OK folder deleted"); } catch (HttpException e) { sendClient(commandId + " NO " + e.getMessage()); } @@ -770,8 +770,7 @@ public class ImapConnection extends AbstractConnection { } while (iterator.hasNext()) { ExchangeSession.Message message = iterator.next(); - if ((conditions.deleted == null || message.deleted == conditions.deleted) - && (conditions.flagged == null || message.flagged == conditions.flagged) + if ((conditions.flagged == null || message.flagged == conditions.flagged) && (conditions.answered == null || message.answered == conditions.answered)) { uidList.add(message.getImapUid()); } @@ -985,7 +984,6 @@ public class ImapConnection extends AbstractConnection { static final class SearchConditions { Boolean flagged; Boolean answered; - Boolean deleted; String indexRange; String uidRange; } @@ -1145,6 +1143,12 @@ public class ImapConnection extends AbstractConnection { } else if ("Junk".equals(flag) && message.junk) { properties.put("junk", "0"); message.junk = false; + } else if ("$Forwarded".equals(flag) && message.forwarded) { + properties.put("forwarded", null); + message.forwarded = false; + } else if ("\\Answered".equals(flag) && message.answered) { + properties.put("answered", null); + message.answered = false; } } } else if ("+Flags".equalsIgnoreCase(action) || "+FLAGS.SILENT".equalsIgnoreCase(action)) { diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index 3bc49b11..25aab9ec 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -22,6 +22,9 @@ import davmail.AbstractDavMailTestCase; import davmail.DavGateway; import davmail.Settings; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; import java.io.*; import java.net.Socket; @@ -33,12 +36,23 @@ public class TestImap extends AbstractDavMailTestCase { static BufferedWriter socketWriter; static BufferedReader socketReader; + static String messageUid = null; + + protected void write(String line) throws IOException { + socketWriter.write(line); + socketWriter.flush(); + } + protected void writeLine(String line) throws IOException { socketWriter.write(line); socketWriter.newLine(); socketWriter.flush(); } + protected String readLine() throws IOException { + return socketReader.readLine(); + } + protected String readFullAnswer(String prefix) throws IOException { String line = socketReader.readLine(); while (!line.startsWith(prefix)) { @@ -99,6 +113,123 @@ public class TestImap extends AbstractDavMailTestCase { readFullAnswer("."); } + public void testCreateFolder() throws IOException { + writeLine(". DELETE testfolder"); + readFullAnswer("."); + writeLine(". CREATE testfolder"); + assertEquals(". OK folder created", readFullAnswer(".")); + } + + public void testSelectFolder() throws IOException { + writeLine(". SELECT testfolder"); + assertEquals(". OK [READ-WRITE] SELECT completed", readFullAnswer(".")); + } + + public void testCreateMessage() throws IOException, MessagingException { + MimeMessage mimeMessage = new MimeMessage((Session) null); + mimeMessage.addHeader("To", "test@test.local"); + mimeMessage.setText("Test message"); + mimeMessage.setSubject("Test subject"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mimeMessage.writeTo(baos); + byte[] content = baos.toByteArray(); + writeLine(". APPEND testfolder (\\Draft) {" + content.length + "}"); + assertEquals("+ send literal data", readLine()); + writeLine(new String(content)); + assertEquals(". OK APPEND completed", readFullAnswer(".")); + writeLine(". NOOP"); + assertEquals(". OK NOOP completed", readFullAnswer(".")); + + // fetch message uid + writeLine(". UID FETCH 1:* (FLAGS)"); + String messageLine = readLine(); + int uidIndex = messageLine.indexOf("UID ") + 4; + messageUid = messageLine.substring(uidIndex, messageLine.indexOf(' ', uidIndex)); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + assertNotNull(messageUid); + } + + public void testUidStoreDeletedFlag() throws IOException { + + // test deleted flag + writeLine(". UID STORE "+messageUid+" +FLAGS (\\Deleted)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Seen \\Deleted \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + + // remove deleted flag + writeLine(". UID STORE "+messageUid+" -FLAGS (\\Deleted)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Seen \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + + } + + public void testUidStoreSeenFlag() throws IOException { + // remove seen flag + writeLine(". UID STORE "+messageUid+" FLAGS (\\Draft)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + } + + public void testUidStoreForwardedFlag() throws IOException { + // add forwarded flag + writeLine(". UID STORE "+messageUid+" +FLAGS ($Forwarded)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft $Forwarded))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + + // remove forwarded flag + writeLine(". UID STORE "+messageUid+" -FLAGS ($Forwarded)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + } + + public void testUidStoreAnsweredFlag() throws IOException { + // add answered flag + writeLine(". UID STORE "+messageUid+" +FLAGS (\\Answered)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft \\Answered))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + + // remove answered flag + writeLine(". UID STORE "+messageUid+" -FLAGS (\\Answered)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + } + + public void testUidStoreJunkFlag() throws IOException { + // add Junk flag + writeLine(". UID STORE "+messageUid+" +FLAGS (Junk)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (Junk \\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + + // remove Junk flag + writeLine(". UID STORE "+messageUid+" -FLAGS (Junk)"); + assertEquals(". OK STORE completed",readFullAnswer(".")); + writeLine(". UID FETCH "+messageUid+" (FLAGS)"); + assertEquals("* 1 FETCH (UID "+messageUid+" FLAGS (\\Draft))", readLine()); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + } + + + public void testDeleteFolder() throws IOException { + writeLine(". DELETE testfolder"); + assertEquals(". OK folder deleted",readFullAnswer(".")); + } + public void testLogout() throws IOException { writeLine(". LOGOUT"); assertEquals("* BYE Closing connection", socketReader.readLine());