/****************************************************************
 * 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.james.mailbox.functional.maildir;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import junit.framework.Assert;

import org.apache.commons.io.FileUtils;
import org.apache.james.mailbox.MailboxException;
import org.apache.james.mailbox.MailboxManagerTest;
import org.apache.james.mailbox.maildir.MaildirMailboxManager;
import org.apache.james.mailbox.maildir.MaildirMailboxSessionMapperFactory;
import org.apache.james.mailbox.maildir.MaildirStore;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * MaildirMailboxManagerTest that extends the StoreMailboxManagerTest.
 */
public class MaildirMailboxManagerTest extends MailboxManagerTest {
    
    private static final String MAILDIR_HOME = "target/Maildir";

    /**
     * Setup the mailboxManager.
     * 
     * @throws Exception
     */
    @Before
    public void setup() throws Exception {
        FileUtils.deleteDirectory(new File(MAILDIR_HOME));
    }
    
    /**
     * Delete Maildir directory after test.
     * 
     * @throws IOException 
     */
    @After
    public void tearDown() throws IOException {
        FileUtils.deleteDirectory(new File(MAILDIR_HOME));
    }

    /* (non-Javadoc)
     * @see org.apache.james.mailbox.MailboxManagerTest#testList()
     */
    @Test
    public void testList() throws MailboxException, UnsupportedEncodingException {
        
        if (OsDetector.isWindows()) {
            System.out.println("Maildir tests work only on non-windows systems. So skip the test");
        } else {

            doTestListWithMaildirStoreConfiguration("/%domain/%user");
            
            // TODO Tests fail with /%user and /%fulluser configuration
//            doTestListWithMaildirStoreConfiguration("/%user");
//            doTestListWithMaildirStoreConfiguration("/%fulluser");

        }
            
    }
    
    private void doTestListWithMaildirStoreConfiguration(String maildirStoreConfiguration) throws MailboxException, UnsupportedEncodingException {
        MaildirStore store = new MaildirStore(MAILDIR_HOME + maildirStoreConfiguration);
        MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(store);
        MaildirMailboxManager manager = new MaildirMailboxManager(mf, null, store);
        manager.init();
        setMailboxManager(manager);
        super.testList();
        try {
            tearDown();
        } catch (IOException e) {
            Assert.fail();
            e.printStackTrace();
        }
    }

    @Override
    protected void createMailboxManager() {
        // Do nothing, the maildir mailboxManager is created in the test method.
    }

}

@@ -2188,56 +2188,35 @@
     
-FileUtils.deleteDirectory(new File(MAILDIR_HOME)
+deleteMaildirTestDirectory(
 );%0D%0A
@@ -2402,56 +2402,35 @@
     
-FileUtils.deleteDirectory(new File(MAILDIR_HOME)
+deleteMaildirTestDirectory(
 );%0D%0A
@@ -2935,23 +2935,8 @@
 ser 
-and /%25fulluser 
 conf
@@ -2946,18 +2946,17 @@
 ration%0D%0A
-//
+ 
         
@@ -2950,32 +2950,34 @@
 on%0D%0A            
+//
  doTestListWithM
@@ -3017,21 +3017,80 @@
 );%0D%0A
-//   
+%0D%0A            // TODO Tests fail with /%25fulluser configuration%0D%0A
         
  doT
@@ -3077,32 +3077,38 @@
 ration%0D%0A        
+    //
  doTestListWithM
@@ -3180,32 +3180,313 @@
  %0D%0A    %7D%0D%0A    %0D%0A
+    /**%0D%0A     * Create the maildirStore with the provided configuration and executes the list() tests.%0D%0A     * Cleans the generated artifacts.%0D%0A     * %0D%0A     * @param maildirStoreConfiguration%0D%0A     * @throws MailboxException%0D%0A     * @throws UnsupportedEncodingException%0D%0A     */%0D%0A
     private void
@@ -4004,24 +4004,42 @@
         
-tearDown
+deleteMaildirTestDirectory
 ();%0D%0A   
@@ -4148,24 +4148,135 @@
 %7D%0D%0A    %7D%0D%0A%0D%0A
+    /* (non-Javadoc)%0D%0A     * @see org.apache.james.mailbox.MailboxManagerTest#createMailboxManager()%0D%0A     */%0D%0A
     @Overrid
@@ -4411,14 +4411,270 @@
 %0D%0A    %7D%0D
+%0A   %0D%0A    /**%0D%0A     * Utility method to delete the test Maildir Directory.%0D%0A     * %0D%0A     * @throws IOException%0D%0A     */%0D%0A    private void deleteMaildirTestDirectory() throws IOException %7B%0D%0A        FileUtils.deleteDirectory(new File(MAILDIR_HOME));%0D%0A    %7D%0D
 %0A%0D%0A%7D%0D%0A

Add utility private method and javadoc.