Author: norman Date: Mon Jan 10 19:14:49 2011 New Revision: 1057322 URL: http://svn.apache.org/viewvc?rev=1057322&view=rev Log: Fix NPE when try to create INBOX mailbox after other mailbox was created. See MAILBOX-31
Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirFolder.java james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java james/mailbox/trunk/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirFolder.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirFolder.java?rev=1057322&r1=1057321&r2=1057322&view=diff ============================================================================== --- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirFolder.java (original) +++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirFolder.java Mon Jan 10 19:14:49 2011 @@ -108,7 +108,7 @@ public class MaildirFolder { * @return true if the directory belonging to this {...@link MaildirFolder} exists ; false otherwise */ public boolean exists() { - return rootFolder.isDirectory(); + return rootFolder.isDirectory() && curFolder.isDirectory() && newFolder.isDirectory() && tmpFolder.isDirectory(); } /** Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java?rev=1057322&r1=1057321&r2=1057322&view=diff ============================================================================== --- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java (original) +++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/MaildirStore.java Mon Jan 10 19:14:49 2011 @@ -91,11 +91,11 @@ public class MaildirStore implements Uid */ public Mailbox<Integer> loadMailbox(MailboxPath mailboxPath) throws MailboxNotFoundException, MailboxException { - String folder = getFolderName(mailboxPath); - File f = new File(folder); - if (!f.isDirectory()) + MaildirFolder folder = new MaildirFolder(getFolderName(mailboxPath)); + + if (!folder.exists()) throw new MailboxNotFoundException(mailboxPath); - return loadMailbox(f, mailboxPath); + return loadMailbox(folder.getRootFile(), mailboxPath); } /** @@ -105,7 +105,7 @@ public class MaildirStore implements Uid * @return The Mailbox object populated with data from the file system * @throws MailboxException If the mailbox folder doesn't exist or can't be read */ - public Mailbox<Integer> loadMailbox(File mailboxFile, MailboxPath mailboxPath) throws MailboxException { + private Mailbox<Integer> loadMailbox(File mailboxFile, MailboxPath mailboxPath) throws MailboxException { long uidValidity; long lastUid; MaildirFolder folder = new MaildirFolder(mailboxFile.getAbsolutePath()); Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java?rev=1057322&r1=1057321&r2=1057322&view=diff ============================================================================== --- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java (original) +++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMailboxMapper.java Mon Jan 10 19:14:49 2011 @@ -80,12 +80,8 @@ public class MaildirMailboxMapper extend * @see org.apache.james.mailbox.store.mail.MailboxMapper#findMailboxByPath(org.apache.james.imap.api.MailboxPath) */ public Mailbox<Integer> findMailboxByPath(MailboxPath mailboxPath) - throws MailboxException, MailboxNotFoundException { - String folder = maildirStore.getFolderName(mailboxPath); - File f = new File(folder); - if (!f.isDirectory()) - throw new MailboxNotFoundException(mailboxPath); - Mailbox<Integer> mailbox = maildirStore.loadMailbox(f, mailboxPath); + throws MailboxException, MailboxNotFoundException { + Mailbox<Integer> mailbox = maildirStore.loadMailbox(mailboxPath); return cacheMailbox(mailbox); } @@ -167,7 +163,8 @@ public class MaildirMailboxMapper extend // it cannot be found and is thus new MaildirFolder folder = maildirStore.createMaildirFolder(mailbox); if (!folder.exists()) { - boolean success = folder.getRootFile().mkdirs(); + boolean success = folder.getRootFile().exists(); + if (!success) success = folder.getRootFile().mkdirs(); if (!success) throw new MailboxException("Failed to save Mailbox " + mailbox); success = folder.getCurFolder().mkdir(); @@ -259,7 +256,7 @@ public class MaildirMailboxMapper extend // Special case for INBOX: Let's use the user's folder. MailboxPath inboxMailboxPath = MailboxPath.inbox(user.getName() + "@" + domain.getName()); - mailboxList.add(maildirStore.loadMailbox(user, inboxMailboxPath)); + mailboxList.add(maildirStore.loadMailbox(inboxMailboxPath)); // List all INBOX sub folders. @@ -281,7 +278,7 @@ public class MaildirMailboxMapper extend MailboxPath mailboxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, userName, mailbox.getName().substring(1)); - mailboxList.add(maildirStore.loadMailbox(mailbox, mailboxPath)); + mailboxList.add(maildirStore.loadMailbox(mailboxPath)); } Modified: james/mailbox/trunk/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java?rev=1057322&r1=1057321&r2=1057322&view=diff ============================================================================== --- james/mailbox/trunk/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java (original) +++ james/mailbox/trunk/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java Mon Jan 10 19:14:49 2011 @@ -18,15 +18,23 @@ ****************************************************************/ package org.apache.james.mailbox.maildir; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.util.Date; + +import javax.mail.Flags; import junit.framework.Assert; import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.impl.SimpleLog; +import org.apache.james.mailbox.MailboxConstants; import org.apache.james.mailbox.MailboxException; import org.apache.james.mailbox.MailboxManagerTest; +import org.apache.james.mailbox.MailboxPath; +import org.apache.james.mailbox.MailboxSession; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -119,5 +127,22 @@ public class MaildirMailboxManagerTest e private void deleteMaildirTestDirectory() throws IOException { FileUtils.deleteDirectory(new File(MAILDIR_HOME)); } + + // See MAILBOX-31 + @Test + public void testCreateSubFolder() throws MailboxException { + MaildirStore store = new MaildirStore("target/maildir" + "/%domain/%user"); + MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(store); + MaildirMailboxManager manager = new MaildirMailboxManager(mf, null, store); + manager.init(); + + String user = "t...@localhost"; + MailboxSession session = manager.createSystemSession(user, new SimpleLog("Test")); + manager.createMailbox(new MailboxPath(MailboxConstants.USER_NAMESPACE, user, "Trash"), session); + manager.createMailbox(new MailboxPath(MailboxConstants.USER_NAMESPACE, user, "INBOX.testfolder"), session); + + // this threw NPE + manager.getMailbox(MailboxPath.inbox(user), session).appendMessage(new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), session, false, new Flags()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org