From 728b0375d18f617a0d9d72667b2e41a6edc16310 Mon Sep 17 00:00:00 2001 From: Benoit Tellier Date: Tue, 7 Jun 2016 15:55:48 +0700 Subject: [PATCH] JAMES-1746 MailboxManager tests should be done with junit-contracts --- mailbox/api/pom.xml | 5 + .../mailbox/AbstractMailboxManagerTest.java | 219 ---------------- ...est.java => MailboxManagerStressTest.java} | 50 ++-- .../james/mailbox/MailboxManagerTest.java | 234 ++++++++++++++++++ .../CassandraMailboxManagerTest.java | 89 +++---- mailbox/hbase/pom.xml | 5 + .../hbase/HBaseMailboxManagerTest.java | 133 +++++----- mailbox/jcr/pom.xml | 10 + .../mailbox/jcr/JCRMailboxManagerTest.java | 116 +++++---- .../james/mailbox/jcr/JCRStressTest.java | 88 ------- mailbox/jpa/pom.xml | 10 + .../mailbox/jpa/JPAMailboxManagerTest.java | 131 +++++----- .../james/mailbox/jpa/JPAStressTest.java | 120 --------- mailbox/maildir/pom.xml | 10 + .../maildir/MaildirMailboxManagerTest.java | 97 -------- .../maildir/MaildirMailboxManagerTests.java | 157 ++++++++++++ .../mailbox/maildir/MaildirStressTest.java | 77 ------ .../inmemory/InMemoryMailboxManagerTest.java | 107 +++----- 18 files changed, 720 insertions(+), 938 deletions(-) delete mode 100644 mailbox/api/src/test/java/org/apache/james/mailbox/AbstractMailboxManagerTest.java rename mailbox/api/src/test/java/org/apache/james/mailbox/{AbstractStressTest.java => MailboxManagerStressTest.java} (72%) create mode 100644 mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java delete mode 100644 mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java delete mode 100644 mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java delete mode 100644 mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java create mode 100644 mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTests.java delete mode 100644 mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirStressTest.java diff --git a/mailbox/api/pom.xml b/mailbox/api/pom.xml index c892d7ef270..12999c65b91 100644 --- a/mailbox/api/pom.xml +++ b/mailbox/api/pom.xml @@ -65,6 +65,11 @@ mockito-core test + + org.xenei + junit-contracts + test + diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/AbstractMailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/AbstractMailboxManagerTest.java deleted file mode 100644 index 66b0179eaf0..00000000000 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/AbstractMailboxManagerTest.java +++ /dev/null @@ -1,219 +0,0 @@ -/**************************************************************** - * 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; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.Date; - -import javax.mail.Flags; - -import org.apache.james.mailbox.exception.BadCredentialsException; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.mock.MockMailboxManager; -import org.apache.james.mailbox.model.MailboxConstants; -import org.apache.james.mailbox.model.MailboxPath; -import org.junit.After; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Test the {@link StoreMailboxManager} methods that - * are not covered by the protocol-tester suite. - * - * This class needs to be extended by the different mailbox - * implementations which are responsible to setup and - * implement the test methods. - * - */ -public abstract class AbstractMailboxManagerTest { - - public final static String USER_1 = "USER_1"; - public final static String USER_2 = "USER_2"; - - /** - * The mailboxManager that needs to get instanciated - * by the mailbox implementations. - */ - protected MailboxManager mailboxManager; - private MailboxSession session; - - @After - public void teardown() throws MailboxException { - getMailboxManager().logout(session, false); - getMailboxManager().endProcessingRequest(session); - } - - @Test - public void createUser1SystemSessionShouldReturnValidSession() throws UnsupportedEncodingException, MailboxException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - - assertThat(session.getUser().getUserName()).isEqualTo(USER_1); - } - - @Test - public void user1ShouldNotHaveAnInbox() throws UnsupportedEncodingException, MailboxException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - - MailboxPath inbox = MailboxPath.inbox(session); - assertThat(getMailboxManager().mailboxExists(inbox, session)).isFalse(); - } - - @Test - public void user1ShouldBeAbleToCreateInbox() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - - MailboxPath inbox = MailboxPath.inbox(session); - getMailboxManager().createMailbox(inbox, session); - - assertThat(getMailboxManager().mailboxExists(inbox, session)).isTrue(); - } - - @Test(expected=MailboxException.class) - public void user1ShouldNotBeAbleToCreateInboxTwice() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - MailboxPath inbox = MailboxPath.inbox(session); - getMailboxManager().createMailbox(inbox, session); - getMailboxManager().createMailbox(inbox, session); - } - - @Test - public void user1ShouldNotHaveTestSubmailbox() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - - MailboxPath inbox = MailboxPath.inbox(session); - getMailboxManager().createMailbox(inbox, session); - - assertThat(getMailboxManager().mailboxExists(new MailboxPath(inbox, "INBOX.Test"), session)).isFalse(); - } - - @Test - public void user1ShouldBeAbleToCreateTestSubmailbox() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - MailboxPath inbox = MailboxPath.inbox(session); - getMailboxManager().createMailbox(inbox, session); - - MailboxPath inboxSubMailbox = new MailboxPath(inbox, "INBOX.Test"); - getMailboxManager().createMailbox(inboxSubMailbox, session); - - assertThat(getMailboxManager().mailboxExists(inboxSubMailbox, session)).isTrue(); - } - - @Test - public void user1ShouldBeAbleToDeleteInbox() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - - MailboxPath inbox = MailboxPath.inbox(session); - getMailboxManager().createMailbox(inbox, session); - MailboxPath inboxSubMailbox = new MailboxPath(inbox, "INBOX.Test"); - getMailboxManager().createMailbox(inboxSubMailbox, session); - - getMailboxManager().deleteMailbox(inbox, session); - - assertThat(getMailboxManager().mailboxExists(inbox, session)).isFalse(); - assertThat(getMailboxManager().mailboxExists(inboxSubMailbox, session)).isTrue(); - } - - @Test - public void user1ShouldBeAbleToDeleteSubmailbox() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - - MailboxPath inbox = MailboxPath.inbox(session); - getMailboxManager().createMailbox(inbox, session); - MailboxPath inboxSubMailbox = new MailboxPath(inbox, "INBOX.Test"); - getMailboxManager().createMailbox(inboxSubMailbox, session); - - getMailboxManager().deleteMailbox(inboxSubMailbox, session); - - assertThat(getMailboxManager().mailboxExists(inbox, session)).isTrue(); - assertThat(getMailboxManager().mailboxExists(inboxSubMailbox, session)).isFalse(); - } - - @Test - public void closingSessionShouldWork() throws BadCredentialsException, MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); - getMailboxManager().startProcessingRequest(session); - - getMailboxManager().logout(session, false); - getMailboxManager().endProcessingRequest(session); - - assertThat(session.isOpen()).isFalse(); - } - - @Test - public void listShouldReturnMailboxes() throws MailboxException, UnsupportedEncodingException { - setMailboxManager(new MockMailboxManager(getMailboxManager()).getMockMailboxManager()); - session = getMailboxManager().createSystemSession("manager", LoggerFactory.getLogger("testList")); - getMailboxManager().startProcessingRequest(session); - - assertThat(getMailboxManager().list(session)).hasSize(MockMailboxManager.EXPECTED_MAILBOXES_COUNT); - } - - @Test - public void user2ShouldBeAbleToCreateRootlessFolder() throws BadCredentialsException, MailboxException { - session = getMailboxManager().createSystemSession(USER_2, LoggerFactory.getLogger("Test")); - MailboxPath trash = new MailboxPath(MailboxConstants.USER_NAMESPACE, USER_2, "Trash"); - getMailboxManager().createMailbox(trash, session); - - assertThat(getMailboxManager().mailboxExists(trash, session)).isTrue(); - } - - @Test - public void user2ShouldBeAbleToCreateNestedFoldersWithoutTheirParents() throws BadCredentialsException, MailboxException { - session = getMailboxManager().createSystemSession(USER_2, LoggerFactory.getLogger("Test")); - MailboxPath nestedFolder = new MailboxPath(MailboxConstants.USER_NAMESPACE, USER_2, "INBOX.testfolder"); - getMailboxManager().createMailbox(nestedFolder, session); - - assertThat(getMailboxManager().mailboxExists(nestedFolder, session)).isTrue(); - getMailboxManager().getMailbox(MailboxPath.inbox(session), session).appendMessage(new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), session, false, new Flags()); - } - - protected abstract void createMailboxManager() throws MailboxException, IOException; - - protected void setMailboxManager(MailboxManager mailboxManager) { - this.mailboxManager = mailboxManager; - } - - protected MailboxManager getMailboxManager() { - if (mailboxManager == null) { - throw new IllegalStateException("Please setMailboxManager with a non null value before requesting getMailboxManager()"); - } - return mailboxManager; - } - -} diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/AbstractStressTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerStressTest.java similarity index 72% rename from mailbox/api/src/test/java/org/apache/james/mailbox/AbstractStressTest.java rename to mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerStressTest.java index 6886a6738bf..6f7986aa9ac 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/AbstractStressTest.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerStressTest.java @@ -18,6 +18,7 @@ ****************************************************************/ package org.apache.james.mailbox; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; @@ -36,28 +37,45 @@ import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailbox.model.MailboxPath; -import org.junit.Test; +import org.junit.After; import org.slf4j.LoggerFactory; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractTest; +import org.xenei.junit.contract.IProducer; -public abstract class AbstractStressTest { +import com.google.common.collect.ImmutableSet; + +@Contract(MailboxManager.class) +public class MailboxManagerStressTest { private final static int APPEND_OPERATIONS = 200; + private IProducer producer; + private MailboxManager mailboxManager; + + @Contract.Inject + public final void setProducer(IProducer producer) throws MailboxException { + this.producer = producer; + this.mailboxManager = producer.newInstance(); + } - protected abstract MailboxManager getMailboxManager(); + @After + public void tearDown() { + producer.cleanUp(); + } - @Test - public void testStessTest() throws InterruptedException, MailboxException { + @ContractTest + public void testStressTest() throws InterruptedException, MailboxException { final CountDownLatch latch = new CountDownLatch(APPEND_OPERATIONS); final ExecutorService pool = Executors.newFixedThreadPool(APPEND_OPERATIONS / 2); final List uList = new ArrayList(); final String username = "username"; - MailboxSession session = getMailboxManager().createSystemSession(username, LoggerFactory.getLogger("Test")); - getMailboxManager().startProcessingRequest(session); + MailboxSession session = mailboxManager.createSystemSession(username, LoggerFactory.getLogger("Test")); + mailboxManager.startProcessingRequest(session); final MailboxPath path = new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - getMailboxManager().createMailbox(path, session); - getMailboxManager().addListener(path, new MailboxListener() { + mailboxManager.createMailbox(path, session); + mailboxManager.addListener(path, new MailboxListener() { @Override public ListenerType getType() { @@ -75,8 +93,8 @@ public void event(Event event) { uList.add(u); } }, session); - getMailboxManager().endProcessingRequest(session); - getMailboxManager().logout(session, false); + mailboxManager.endProcessingRequest(session); + mailboxManager.logout(session, false); final AtomicBoolean fail = new AtomicBoolean(false); final ConcurrentHashMap uids = new ConcurrentHashMap(); @@ -93,18 +111,18 @@ public void run() { try { - MailboxSession session = getMailboxManager().createSystemSession(username, LoggerFactory.getLogger("Test")); + MailboxSession session = mailboxManager.createSystemSession(username, LoggerFactory.getLogger("Test")); - getMailboxManager().startProcessingRequest(session); - MessageManager m = getMailboxManager().getMailbox(path, session); + mailboxManager.startProcessingRequest(session); + MessageManager m = mailboxManager.getMailbox(path, session); Long uid = m.appendMessage(new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), session, false, new Flags()); System.out.println("Append message with uid=" + uid); if (uids.put(uid, new Object()) != null) { fail.set(true); } - getMailboxManager().endProcessingRequest(session); - getMailboxManager().logout(session, false); + mailboxManager.endProcessingRequest(session); + mailboxManager.logout(session, false); } catch (MailboxException e) { e.printStackTrace(); fail.set(true); diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java new file mode 100644 index 00000000000..6c9e741d86d --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java @@ -0,0 +1,234 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.List; + +import javax.mail.Flags; + +import org.apache.james.mailbox.exception.BadCredentialsException; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.mock.MockMailboxManager; +import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailbox.model.MailboxMetaData; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.MailboxQuery; +import org.junit.After; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.slf4j.LoggerFactory; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractTest; +import org.xenei.junit.contract.IProducer; + +/** + * Test the {@link StoreMailboxManager} methods that + * are not covered by the protocol-tester suite. + * + * This class needs to be extended by the different mailbox + * implementations which are responsible to setup and + * implement the test methods. + * + */ +@Contract(MailboxManager.class) +public class MailboxManagerTest { + + public final static String USER_1 = "USER_1"; + public final static String USER_2 = "USER_2"; + + @Rule + public ExpectedException expected = ExpectedException.none(); + + private IProducer producer; + private MailboxManager mailboxManager; + private MailboxSession session; + + @Contract.Inject + public final void setProducer(IProducer producer) throws Exception { + this.producer = producer; + this.mailboxManager = new MockMailboxManager(producer.newInstance()).getMockMailboxManager(); + } + + @After + public void tearDown() throws Exception { + mailboxManager.logout(session, false); + mailboxManager.endProcessingRequest(session); + + producer.cleanUp(); + } + + @ContractTest + public void createUser1SystemSessionShouldReturnValidSession() throws UnsupportedEncodingException, MailboxException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + + assertThat(session.getUser().getUserName()).isEqualTo(USER_1); + } + + @ContractTest + public void user1ShouldNotHaveAnInbox() throws UnsupportedEncodingException, MailboxException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + + MailboxPath inbox = MailboxPath.inbox(session); + assertThat(mailboxManager.mailboxExists(inbox, session)).isFalse(); + } + + @ContractTest + public void user1ShouldBeAbleToCreateInbox() throws MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + + MailboxPath inbox = MailboxPath.inbox(session); + mailboxManager.createMailbox(inbox, session); + + assertThat(mailboxManager.mailboxExists(inbox, session)).isTrue(); + } + + @ContractTest + public void user1ShouldNotBeAbleToCreateInboxTwice() throws MailboxException, UnsupportedEncodingException { + expected.expect(MailboxException.class); + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + MailboxPath inbox = MailboxPath.inbox(session); + mailboxManager.createMailbox(inbox, session); + mailboxManager.createMailbox(inbox, session); + } + + @ContractTest + public void user1ShouldNotHaveTestSubmailbox() throws MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + + MailboxPath inbox = MailboxPath.inbox(session); + mailboxManager.createMailbox(inbox, session); + + assertThat(mailboxManager.mailboxExists(new MailboxPath(inbox, "INBOX.Test"), session)).isFalse(); + } + + @ContractTest + public void user1ShouldBeAbleToCreateTestSubmailbox() throws MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + MailboxPath inbox = MailboxPath.inbox(session); + mailboxManager.createMailbox(inbox, session); + + MailboxPath inboxSubMailbox = new MailboxPath(inbox, "INBOX.Test"); + mailboxManager.createMailbox(inboxSubMailbox, session); + + assertThat(mailboxManager.mailboxExists(inboxSubMailbox, session)).isTrue(); + } + + @ContractTest + public void user1ShouldBeAbleToDeleteInbox() throws MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + + MailboxPath inbox = MailboxPath.inbox(session); + mailboxManager.createMailbox(inbox, session); + MailboxPath inboxSubMailbox = new MailboxPath(inbox, "INBOX.Test"); + mailboxManager.createMailbox(inboxSubMailbox, session); + + mailboxManager.deleteMailbox(inbox, session); + + assertThat(mailboxManager.mailboxExists(inbox, session)).isFalse(); + assertThat(mailboxManager.mailboxExists(inboxSubMailbox, session)).isTrue(); + } + + @ContractTest + public void user1ShouldBeAbleToDeleteSubmailbox() throws MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + + MailboxPath inbox = MailboxPath.inbox(session); + mailboxManager.createMailbox(inbox, session); + MailboxPath inboxSubMailbox = new MailboxPath(inbox, "INBOX.Test"); + mailboxManager.createMailbox(inboxSubMailbox, session); + + mailboxManager.deleteMailbox(inboxSubMailbox, session); + + assertThat(mailboxManager.mailboxExists(inbox, session)).isTrue(); + assertThat(mailboxManager.mailboxExists(inboxSubMailbox, session)).isFalse(); + } + + @ContractTest + public void closingSessionShouldWork() throws BadCredentialsException, MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.startProcessingRequest(session); + + mailboxManager.logout(session, false); + mailboxManager.endProcessingRequest(session); + + assertThat(session.isOpen()).isFalse(); + } + + @ContractTest + public void listShouldReturnMailboxes() throws MailboxException, UnsupportedEncodingException { + session = mailboxManager.createSystemSession("manager", LoggerFactory.getLogger("testList")); + mailboxManager.startProcessingRequest(session); + + assertThat(mailboxManager.list(session)).hasSize(MockMailboxManager.EXPECTED_MAILBOXES_COUNT); + } + + @ContractTest + public void user2ShouldBeAbleToCreateRootlessFolder() throws BadCredentialsException, MailboxException { + session = mailboxManager.createSystemSession(USER_2, LoggerFactory.getLogger("Test")); + MailboxPath trash = new MailboxPath(MailboxConstants.USER_NAMESPACE, USER_2, "Trash"); + mailboxManager.createMailbox(trash, session); + + assertThat(mailboxManager.mailboxExists(trash, session)).isTrue(); + } + + @ContractTest + public void user2ShouldBeAbleToCreateNestedFoldersWithoutTheirParents() throws BadCredentialsException, MailboxException { + session = mailboxManager.createSystemSession(USER_2, LoggerFactory.getLogger("Test")); + MailboxPath nestedFolder = new MailboxPath(MailboxConstants.USER_NAMESPACE, USER_2, "INBOX.testfolder"); + mailboxManager.createMailbox(nestedFolder, session); + + assertThat(mailboxManager.mailboxExists(nestedFolder, session)).isTrue(); + mailboxManager.getMailbox(MailboxPath.inbox(session), session).appendMessage(new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), session, false, new Flags()); + } + + @ContractTest + public void searchShouldNotReturnResultsFromOtherNamespaces() throws Exception { + Assume.assumeTrue(mailboxManager.getSupportedMailboxCapabilities().contains(MailboxManager.MailboxCapabilities.Namespace)); + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.createMailbox(new MailboxPath("#namespace", USER_1, "Other"), session); + mailboxManager.createMailbox(MailboxPath.inbox(session), session); + List metaDatas = mailboxManager.search(new MailboxQuery(new MailboxPath("#private", USER_1, ""), "*", '.'), session); + assertThat(metaDatas).hasSize(1); + assertThat(metaDatas.get(0).getPath()).isEqualTo(MailboxPath.inbox(session)); + } + + @ContractTest + public void searchShouldNotReturnResultsFromOtherUsers() throws Exception { + session = mailboxManager.createSystemSession(USER_1, LoggerFactory.getLogger("Mock")); + mailboxManager.createMailbox(new MailboxPath("#namespace", USER_2, "Other"), session); + mailboxManager.createMailbox(MailboxPath.inbox(session), session); + List metaDatas = mailboxManager.search(new MailboxQuery(new MailboxPath("#private", USER_1, ""), "*", '.'), session); + assertThat(metaDatas).hasSize(1); + assertThat(metaDatas.get(0).getPath()).isEqualTo(MailboxPath.inbox(session)); + } + +} diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java index ad61efe2d59..e22595f539d 100644 --- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerTest.java @@ -20,8 +20,7 @@ import org.apache.james.backends.cassandra.CassandraCluster; import org.apache.james.backends.cassandra.init.CassandraModuleComposite; -import org.apache.james.mailbox.AbstractMailboxManagerTest; -import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.cassandra.mail.CassandraModSeqProvider; import org.apache.james.mailbox.cassandra.mail.CassandraUidProvider; import org.apache.james.mailbox.cassandra.modules.CassandraAclModule; @@ -32,19 +31,24 @@ import org.apache.james.mailbox.cassandra.modules.CassandraSubscriptionModule; import org.apache.james.mailbox.cassandra.modules.CassandraUidModule; import org.apache.james.mailbox.cassandra.modules.CassandraModSeqModule; -import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.store.NoMailboxPathLocker; import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.After; -import org.junit.Before; -import org.slf4j.LoggerFactory; +import org.junit.runner.RunWith; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractImpl; +import org.xenei.junit.contract.ContractSuite; +import org.xenei.junit.contract.IProducer; + +import com.google.common.base.Throwables; /** * CassandraMailboxManagerTest that extends the StoreMailboxManagerTest. * */ -public class CassandraMailboxManagerTest extends AbstractMailboxManagerTest { +@RunWith(ContractSuite.class) +@ContractImpl(CassandraMailboxManager.class) +public class CassandraMailboxManagerTest { private static final CassandraCluster CASSANDRA = CassandraCluster.create(new CassandraModuleComposite( new CassandraAclModule(), @@ -56,56 +60,37 @@ public class CassandraMailboxManagerTest extends AbstractMailboxManagerTest { new CassandraSubscriptionModule(), new CassandraAttachmentModule())); - /** - * Setup the mailboxManager. - * - * @throws Exception - */ - @Before - public void setup() throws Exception { - CASSANDRA.ensureAllTables(); - CASSANDRA.clearAllTables(); - createMailboxManager(); - } + private IProducer producer = new IProducer() { - /** - * Close the system session and entityManagerFactory - * - * @throws MailboxException - * @throws BadCredentialsException - */ - @After - public void tearDown() throws Exception { - deleteAllMailboxes(); - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); - session.close(); - } + @Override + public CassandraMailboxManager newInstance() { + CASSANDRA.ensureAllTables(); + CassandraUidProvider uidProvider = new CassandraUidProvider(CASSANDRA.getConf()); + CassandraModSeqProvider modSeqProvider = new CassandraModSeqProvider(CASSANDRA.getConf()); + CassandraMailboxSessionMapperFactory mapperFactory = new CassandraMailboxSessionMapperFactory(uidProvider, + modSeqProvider, + CASSANDRA.getConf(), + CASSANDRA.getTypesProvider()); - /* - * (non-Javadoc)i deve - * - * @see org.apache.james.mailbox.MailboxManagerTest#createMailboxManager() - */ - @Override - protected void createMailboxManager() throws MailboxException { - final CassandraUidProvider uidProvider = new CassandraUidProvider(CASSANDRA.getConf()); - final CassandraModSeqProvider modSeqProvider = new CassandraModSeqProvider(CASSANDRA.getConf()); - final CassandraMailboxSessionMapperFactory mapperFactory = new CassandraMailboxSessionMapperFactory(uidProvider, - modSeqProvider, - CASSANDRA.getConf(), - CASSANDRA.getTypesProvider()); + CassandraMailboxManager manager = new CassandraMailboxManager(mapperFactory, null, new NoMailboxPathLocker(), new MessageParser()); + try { + manager.init(); + } catch (MailboxException e) { + throw Throwables.propagate(e); + } - final CassandraMailboxManager manager = new CassandraMailboxManager(mapperFactory, null, new NoMailboxPathLocker(), new MessageParser()); - manager.init(); + return manager; + } - setMailboxManager(manager); + @Override + public void cleanUp() { + CASSANDRA.clearAllTables(); + } + }; - deleteAllMailboxes(); + @Contract.Inject + public IProducer getProducer() { + return producer; } - private void deleteAllMailboxes() throws BadCredentialsException, MailboxException { - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); - CASSANDRA.clearAllTables(); - session.close(); - } } diff --git a/mailbox/hbase/pom.xml b/mailbox/hbase/pom.xml index ca49c5143b6..f17359421dd 100644 --- a/mailbox/hbase/pom.xml +++ b/mailbox/hbase/pom.xml @@ -133,6 +133,11 @@ + + org.xenei + junit-contracts + test + diff --git a/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/HBaseMailboxManagerTest.java b/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/HBaseMailboxManagerTest.java index c4cdaeb5d78..ae06d588f3c 100644 --- a/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/HBaseMailboxManagerTest.java +++ b/mailbox/hbase/src/test/java/org/apache/james/mailbox/hbase/HBaseMailboxManagerTest.java @@ -18,99 +18,86 @@ ****************************************************************/ package org.apache.james.mailbox.hbase; -import java.io.IOException; -import org.apache.james.mailbox.AbstractMailboxManagerTest; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.acl.GroupMembershipResolver; -import org.apache.james.mailbox.acl.MailboxACLResolver; +import static org.apache.james.mailbox.hbase.HBaseNames.MAILBOXES; +import static org.apache.james.mailbox.hbase.HBaseNames.MAILBOXES_TABLE; +import static org.apache.james.mailbox.hbase.HBaseNames.MAILBOX_CF; +import static org.apache.james.mailbox.hbase.HBaseNames.MESSAGES; +import static org.apache.james.mailbox.hbase.HBaseNames.MESSAGES_META_CF; +import static org.apache.james.mailbox.hbase.HBaseNames.MESSAGES_TABLE; +import static org.apache.james.mailbox.hbase.HBaseNames.MESSAGE_DATA_BODY_CF; +import static org.apache.james.mailbox.hbase.HBaseNames.MESSAGE_DATA_HEADERS_CF; +import static org.apache.james.mailbox.hbase.HBaseNames.SUBSCRIPTIONS; +import static org.apache.james.mailbox.hbase.HBaseNames.SUBSCRIPTIONS_TABLE; +import static org.apache.james.mailbox.hbase.HBaseNames.SUBSCRIPTION_CF; + +import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; -import static org.apache.james.mailbox.hbase.HBaseNames.*; import org.apache.james.mailbox.hbase.mail.HBaseModSeqProvider; import org.apache.james.mailbox.hbase.mail.HBaseUidProvider; import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.After; -import org.junit.Before; -import org.slf4j.LoggerFactory; - -/** - * HBaseMailboxManagerTest that extends the StoreMailboxManagerTest. - * - */ -public class HBaseMailboxManagerTest extends AbstractMailboxManagerTest { +import org.junit.runner.RunWith; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractImpl; +import org.xenei.junit.contract.ContractSuite; +import org.xenei.junit.contract.IProducer; - private static final HBaseClusterSingleton CLUSTER = HBaseClusterSingleton.build(); +import com.google.common.base.Throwables; - /** - * Setup the mailboxManager. +@RunWith(ContractSuite.class) +@ContractImpl(HBaseMailboxManager.class) +public class HBaseMailboxManagerTest { - * @throws Exception - */ - @Before - public void setup() throws Exception { - ensureTables(); - clearTables(); - createMailboxManager(); - } - - private void ensureTables() throws IOException { - CLUSTER.ensureTable(MAILBOXES_TABLE, new byte[][]{MAILBOX_CF}); - CLUSTER.ensureTable(MESSAGES_TABLE, - new byte[][]{MESSAGES_META_CF, MESSAGE_DATA_HEADERS_CF, MESSAGE_DATA_BODY_CF}); - CLUSTER.ensureTable(SUBSCRIPTIONS_TABLE, new byte[][]{SUBSCRIPTION_CF}); - } + private static final HBaseClusterSingleton CLUSTER = HBaseClusterSingleton.build(); - private void clearTables() { - CLUSTER.clearTable(MAILBOXES); - CLUSTER.clearTable(MESSAGES); - CLUSTER.clearTable(SUBSCRIPTIONS); - } + private IProducer producer = new IProducer() { - /** - * Close the system session and entityManagerFactory - * - * @throws MailboxException - * @throws BadCredentialsException - */ - @After - public void tearDown() throws Exception { - deleteAllMailboxes(); - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); - session.close(); - } + @Override + public HBaseMailboxManager newInstance() { + ensureTables(); - /* (non-Javadoc)i deve - * @see org.apache.james.mailbox.MailboxManagerTest#createMailboxManager() - */ - @Override - protected void createMailboxManager() throws MailboxException { - final HBaseUidProvider uidProvider = new HBaseUidProvider(CLUSTER.getConf()); - final HBaseModSeqProvider modSeqProvider = new HBaseModSeqProvider(CLUSTER.getConf()); - final HBaseMailboxSessionMapperFactory mapperFactory = new HBaseMailboxSessionMapperFactory(CLUSTER.getConf(), + HBaseUidProvider uidProvider = new HBaseUidProvider(CLUSTER.getConf()); + HBaseModSeqProvider modSeqProvider = new HBaseModSeqProvider(CLUSTER.getConf()); + HBaseMailboxSessionMapperFactory mapperFactory = new HBaseMailboxSessionMapperFactory(CLUSTER.getConf(), uidProvider, modSeqProvider); - final MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - final GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - final MessageParser messageParser = new MessageParser(); + HBaseMailboxManager manager = new HBaseMailboxManager(mapperFactory, + null, + new UnionMailboxACLResolver(), + new SimpleGroupMembershipResolver(), + new MessageParser()); - final HBaseMailboxManager manager = new HBaseMailboxManager(mapperFactory, null, aclResolver, - groupMembershipResolver, messageParser); - manager.init(); + try { + manager.init(); + } catch (MailboxException e) { + throw Throwables.propagate(e); + } - setMailboxManager(manager); + return manager; + } - deleteAllMailboxes(); + @Override + public void cleanUp() { + CLUSTER.clearTable(MAILBOXES); + CLUSTER.clearTable(MESSAGES); + CLUSTER.clearTable(SUBSCRIPTIONS); + } + }; + + @Contract.Inject + public IProducer getProducer() { + return producer; } - private void deleteAllMailboxes() throws BadCredentialsException, MailboxException { - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); + private void ensureTables() { try { - ((HBaseMailboxManager) mailboxManager).deleteEverything(session); - } catch (MailboxException e) { - e.printStackTrace(); + CLUSTER.ensureTable(MAILBOXES_TABLE, new byte[][]{MAILBOX_CF}); + CLUSTER.ensureTable(MESSAGES_TABLE, + new byte[][]{MESSAGES_META_CF, MESSAGE_DATA_HEADERS_CF, MESSAGE_DATA_BODY_CF}); + CLUSTER.ensureTable(SUBSCRIPTIONS_TABLE, new byte[][]{SUBSCRIPTION_CF}); + } catch (Exception e) { + throw Throwables.propagate(e); } - session.close(); } } diff --git a/mailbox/jcr/pom.xml b/mailbox/jcr/pom.xml index 7a27bb085a4..e6a0c24b379 100644 --- a/mailbox/jcr/pom.xml +++ b/mailbox/jcr/pom.xml @@ -104,5 +104,15 @@ ${assertj-1.version} test + + org.mockito + mockito-core + test + + + org.xenei + junit-contracts + test + diff --git a/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java b/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java index 9abeb324e87..e5a725be5ac 100644 --- a/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java +++ b/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRMailboxManagerTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.fail; import java.io.File; +import java.io.IOException; import javax.jcr.RepositoryException; @@ -28,8 +29,8 @@ import org.apache.jackrabbit.core.RepositoryImpl; import org.apache.jackrabbit.core.config.ConfigurationException; import org.apache.jackrabbit.core.config.RepositoryConfig; -import org.apache.james.mailbox.AbstractMailboxManagerTest; -import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxManagerTest; import org.apache.james.mailbox.acl.GroupMembershipResolver; import org.apache.james.mailbox.acl.MailboxACLResolver; import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; @@ -39,66 +40,79 @@ import org.apache.james.mailbox.jcr.mail.JCRUidProvider; import org.apache.james.mailbox.store.JVMMailboxPathLocker; import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.After; -import org.junit.Before; -import org.slf4j.LoggerFactory; +import org.junit.runner.RunWith; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractImpl; +import org.xenei.junit.contract.ContractSuite; +import org.xenei.junit.contract.IProducer; import org.xml.sax.InputSource; -/** - * JCRMailboxManagerTest that extends the StoreMailboxManagerTest. - */ -public class JCRMailboxManagerTest extends AbstractMailboxManagerTest { +import com.google.common.base.Throwables; - private static final String JACKRABBIT_HOME = "target/jackrabbit"; +@RunWith(ContractSuite.class) +@ContractImpl(JCRMailboxManager.class) +public class JCRMailboxManagerTest extends MailboxManagerTest { - public static final String META_DATA_DIRECTORY = "target/user-meta-data"; + private static final String JACKRABBIT_HOME = "target/jackrabbit"; private static RepositoryImpl repository; - @Before - public void setup() throws Exception { - createMailboxManager(); - } + private IProducer producer = new IProducer() { - @After - public void tearDown() throws Exception { - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); - session.close(); - repository.shutdown(); - FileUtils.forceDelete(new File(JACKRABBIT_HOME)); - } + @Override + public JCRMailboxManager newInstance() { + String user = "user"; + String pass = "pass"; + String workspace = null; + RepositoryConfig config; + try { + config = RepositoryConfig.create(new InputSource(JCRMailboxManagerTest.class.getClassLoader().getResourceAsStream("test-repository.xml")), JACKRABBIT_HOME); + repository = RepositoryImpl.create(config); + } catch (ConfigurationException e) { + e.printStackTrace(); + fail(); + } catch (RepositoryException e) { + e.printStackTrace(); + fail(); + } + + // Register imap cnd file + JCRUtils.registerCnd(repository, workspace, user, pass); + MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass); + JVMMailboxPathLocker locker = new JVMMailboxPathLocker(); + JCRUidProvider uidProvider = new JCRUidProvider(locker, sessionRepos); + JCRModSeqProvider modSeqProvider = new JCRModSeqProvider(locker, sessionRepos); + JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, uidProvider, modSeqProvider); + + MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); + GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); + MessageParser messageParser = new MessageParser(); + + JCRMailboxManager manager = new JCRMailboxManager(mf, null, locker, aclResolver, groupMembershipResolver, messageParser); + + try { + manager.init(); + } catch (MailboxException e) { + throw Throwables.propagate(e); + } + + return manager; + } - protected void createMailboxManager() throws MailboxException { - String user = "user"; - String pass = "pass"; - String workspace = null; - RepositoryConfig config; - try { - config = RepositoryConfig.create(new InputSource(JCRMailboxManagerTest.class.getClassLoader().getResourceAsStream("test-repository.xml")), JACKRABBIT_HOME); - repository = RepositoryImpl.create(config); - } catch (ConfigurationException e) { - e.printStackTrace(); - fail(); - } catch (RepositoryException e) { - e.printStackTrace(); - fail(); + @Override + public void cleanUp() { + repository.shutdown(); + try { + FileUtils.forceDelete(new File(JACKRABBIT_HOME)); + } catch (IOException e) { + throw Throwables.propagate(e); + } } + }; - // Register imap cnd file - JCRUtils.registerCnd(repository, workspace, user, pass); - MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass); - JVMMailboxPathLocker locker = new JVMMailboxPathLocker(); - JCRUidProvider uidProvider = new JCRUidProvider(locker, sessionRepos); - JCRModSeqProvider modSeqProvider = new JCRModSeqProvider(locker, sessionRepos); - JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, uidProvider, modSeqProvider); - - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); - - JCRMailboxManager manager = new JCRMailboxManager(mf, null, locker, aclResolver, groupMembershipResolver, messageParser); - manager.init(); - setMailboxManager(manager); + @Contract.Inject + public IProducer getProducer() { + return producer; } } diff --git a/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java b/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java deleted file mode 100644 index 50bd9556eb8..00000000000 --- a/mailbox/jcr/src/test/java/org/apache/james/mailbox/jcr/JCRStressTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/**************************************************************** - * 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.jcr; - -import java.io.File; -import java.io.IOException; - -import javax.jcr.RepositoryException; - -import org.apache.commons.io.FileUtils; -import org.apache.jackrabbit.core.RepositoryImpl; -import org.apache.jackrabbit.core.config.RepositoryConfig; -import org.apache.james.mailbox.AbstractStressTest; -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.acl.GroupMembershipResolver; -import org.apache.james.mailbox.acl.MailboxACLResolver; -import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; -import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.jcr.mail.JCRModSeqProvider; -import org.apache.james.mailbox.jcr.mail.JCRUidProvider; -import org.apache.james.mailbox.store.JVMMailboxPathLocker; -import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.After; -import org.junit.Before; -import org.slf4j.LoggerFactory; -import org.xml.sax.InputSource; - -public class JCRStressTest extends AbstractStressTest { - - private JCRMailboxManager mailboxManager; - private RepositoryImpl repository; - private static final String JACKRABBIT_HOME = "target/jackrabbit"; - - @Before - public void setUp() throws RepositoryException, MailboxException { - String user = "user"; - String pass = "pass"; - String workspace = null; - RepositoryConfig config = RepositoryConfig.create(new InputSource(this.getClass().getClassLoader().getResourceAsStream("test-repository.xml")), JACKRABBIT_HOME); - repository = RepositoryImpl.create(config); - - // Register imap cnd file - JCRUtils.registerCnd(repository, workspace, user, pass); - MailboxSessionJCRRepository sessionRepos = new GlobalMailboxSessionJCRRepository(repository, workspace, user, pass); - JVMMailboxPathLocker locker = new JVMMailboxPathLocker(); - JCRUidProvider uidProvider = new JCRUidProvider(locker, sessionRepos); - JCRModSeqProvider modSeqProvider = new JCRModSeqProvider(locker, sessionRepos); - JCRMailboxSessionMapperFactory mf = new JCRMailboxSessionMapperFactory(sessionRepos, uidProvider, modSeqProvider); - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); - - mailboxManager = new JCRMailboxManager(mf, null, locker, aclResolver, groupMembershipResolver, messageParser); - mailboxManager.init(); - } - - @After - public void tearDown() throws IOException { - MailboxSession session = mailboxManager.createSystemSession("test", LoggerFactory.getLogger(JCRStressTest.class)); - session.close(); - repository.shutdown(); - FileUtils.forceDelete(new File(JACKRABBIT_HOME)); - } - - @Override - protected MailboxManager getMailboxManager() { - return mailboxManager; - } - -} diff --git a/mailbox/jpa/pom.xml b/mailbox/jpa/pom.xml index 1495cd81de3..b2627ea92bd 100644 --- a/mailbox/jpa/pom.xml +++ b/mailbox/jpa/pom.xml @@ -79,6 +79,11 @@ ${assertj-1.version} test + + org.mockito + mockito-core + test + com.h2database h2 @@ -95,6 +100,11 @@ slf4j-simple test + + org.xenei + junit-contracts + test + diff --git a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java index aa7ab4291dc..fb43b12e2ed 100644 --- a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java +++ b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAMailboxManagerTest.java @@ -22,13 +22,12 @@ import javax.persistence.EntityManagerFactory; -import org.apache.james.mailbox.AbstractMailboxManagerTest; +import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.acl.GroupMembershipResolver; import org.apache.james.mailbox.acl.MailboxACLResolver; import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.jpa.mail.JPAModSeqProvider; import org.apache.james.mailbox.jpa.mail.JPAUidProvider; @@ -42,89 +41,79 @@ import org.apache.james.mailbox.store.JVMMailboxPathLocker; import org.apache.james.mailbox.store.mail.model.impl.MessageParser; import org.apache.openjpa.persistence.OpenJPAPersistence; -import org.junit.After; -import org.junit.Before; +import org.junit.runner.RunWith; import org.slf4j.LoggerFactory; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractImpl; +import org.xenei.junit.contract.ContractSuite; +import org.xenei.junit.contract.IProducer; + +import com.google.common.base.Throwables; + +@RunWith(ContractSuite.class) +@ContractImpl(OpenJPAMailboxManager.class) +public class JPAMailboxManagerTest { -/** - * JPAMailboxManagerTest that extends the StoreMailboxManagerTest. - */ -public class JPAMailboxManagerTest extends AbstractMailboxManagerTest { - /** * The entity manager factory. */ private static EntityManagerFactory entityManagerFactory; - - /** - * Setup the mailboxManager. - * - * @throws Exception - */ - @Before - public void setup() throws Exception { - createMailboxManager(); - } - - /** - * Close the system session and entityManagerFactory - * - * @throws MailboxException - * @throws BadCredentialsException - */ - @After - public void tearDown() throws BadCredentialsException, MailboxException { - deleteAllMailboxes(); - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); - session.close(); - entityManagerFactory.close(); - } - /* (non-Javadoc) - * @see org.apache.james.mailbox.MailboxManagerTest#createMailboxManager() - */ - @Override - protected void createMailboxManager() throws MailboxException { - - HashMap properties = new HashMap(); - properties.put("openjpa.ConnectionDriverName", "org.h2.Driver"); - properties.put("openjpa.ConnectionURL", "jdbc:h2:mem:imap;DB_CLOSE_DELAY=-1"); - properties.put("openjpa.Log", "JDBC=WARN, SQL=WARN, Runtime=WARN"); - properties.put("openjpa.ConnectionFactoryProperties", "PrettyPrint=true, PrettyPrintLineLength=72"); - properties.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); - properties.put("openjpa.MetaDataFactory", "jpa(Types=" + + private IProducer producer = new IProducer() { + + private OpenJPAMailboxManager openJPAMailboxManager; + + @Override + public OpenJPAMailboxManager newInstance() { + HashMap properties = new HashMap(); + properties.put("openjpa.ConnectionDriverName", "org.h2.Driver"); + properties.put("openjpa.ConnectionURL", "jdbc:h2:mem:imap;DB_CLOSE_DELAY=-1"); + properties.put("openjpa.Log", "JDBC=WARN, SQL=WARN, Runtime=WARN"); + properties.put("openjpa.ConnectionFactoryProperties", "PrettyPrint=true, PrettyPrintLineLength=72"); + properties.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); + properties.put("openjpa.MetaDataFactory", "jpa(Types=" + JPAMailbox.class.getName() + ";" + AbstractJPAMailboxMessage.class.getName() + ";" + JPAMailboxMessage.class.getName() + ";" + JPAProperty.class.getName() + ";" + JPAUserFlag.class.getName() + ";" + JPASubscription.class.getName() + ")"); - - entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties); - JVMMailboxPathLocker locker = new JVMMailboxPathLocker(); - JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, new JPAUidProvider(locker, entityManagerFactory), new JPAModSeqProvider(locker, entityManagerFactory)); - - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); - - JPAMailboxManager mailboxManager = new OpenJPAMailboxManager(mf, null, aclResolver, groupMembershipResolver, messageParser); - mailboxManager.init(); - - setMailboxManager(mailboxManager); - - deleteAllMailboxes(); - - } - - private void deleteAllMailboxes() throws BadCredentialsException, MailboxException { - MailboxSession session = getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")); - try { - ((OpenJPAMailboxManager) mailboxManager).deleteEverything(session); - } catch (MailboxException e) { - e.printStackTrace(); + + entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties); + JVMMailboxPathLocker locker = new JVMMailboxPathLocker(); + JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, new JPAUidProvider(locker, entityManagerFactory), new JPAModSeqProvider(locker, entityManagerFactory)); + + MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); + GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); + MessageParser messageParser = new MessageParser(); + + openJPAMailboxManager = new OpenJPAMailboxManager(mf, null, aclResolver, groupMembershipResolver, messageParser); + + try { + openJPAMailboxManager.init(); + } catch (MailboxException e) { + throw Throwables.propagate(e); + } + + return openJPAMailboxManager; } - session.close(); + + @Override + public void cleanUp() { + MailboxSession session = openJPAMailboxManager.createSystemSession("test", LoggerFactory.getLogger("Test")); + try { + openJPAMailboxManager.deleteEverything(session); + } catch (MailboxException e) { + e.printStackTrace(); + } + session.close(); + entityManagerFactory.close(); + } + }; + + @Contract.Inject + public IProducer getProducer() { + return producer; } } diff --git a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java b/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java deleted file mode 100644 index 7eab4eac9d4..00000000000 --- a/mailbox/jpa/src/test/java/org/apache/james/mailbox/jpa/JPAStressTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/**************************************************************** - * 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.jpa; - -import java.util.HashMap; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -import org.apache.james.mailbox.AbstractStressTest; -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.acl.GroupMembershipResolver; -import org.apache.james.mailbox.acl.MailboxACLResolver; -import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; -import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.jpa.mail.JPAModSeqProvider; -import org.apache.james.mailbox.jpa.mail.JPAUidProvider; -import org.apache.james.mailbox.jpa.mail.model.JPAMailbox; -import org.apache.james.mailbox.jpa.mail.model.JPAProperty; -import org.apache.james.mailbox.jpa.mail.model.JPAUserFlag; -import org.apache.james.mailbox.jpa.mail.model.openjpa.AbstractJPAMailboxMessage; -import org.apache.james.mailbox.jpa.mail.model.openjpa.JPAMailboxMessage; -import org.apache.james.mailbox.jpa.openjpa.OpenJPAMailboxManager; -import org.apache.james.mailbox.jpa.user.model.JPASubscription; -import org.apache.james.mailbox.store.JVMMailboxPathLocker; -import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.apache.openjpa.persistence.OpenJPAPersistence; -import org.junit.After; -import org.junit.Before; -import org.slf4j.LoggerFactory; - -/** - * Proof of bug https://issues.apache.org/jira/browse/IMAP-137 - */ -public class JPAStressTest extends AbstractStressTest { - - private OpenJPAMailboxManager mailboxManager; - - private long locktimeout = 60000; - - private EntityManagerFactory entityManagerFactory; - - @Before - public void setUp() throws MailboxException { - - HashMap properties = new HashMap(); - properties.put("openjpa.ConnectionDriverName", org.h2.Driver.class.getName()); - properties.put("openjpa.ConnectionURL", "jdbc:h2:mem:mailboxintegrationstress;DB_CLOSE_DELAY=-1"); - properties.put("openjpa.Log", "JDBC=WARN, SQL=WARN, Runtime=WARN"); - properties.put("openjpa.ConnectionFactoryProperties", "PrettyPrint=true, PrettyPrintLineLength=72"); - properties.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); - properties.put("openjpa.MetaDataFactory", "jpa(Types=" + - JPAMailbox.class.getName() + ";" + - AbstractJPAMailboxMessage.class.getName() + ";" + - JPAMailboxMessage.class.getName() + ";" + - JPAProperty.class.getName() + ";" + - JPAUserFlag.class.getName() + ";" + - JPASubscription.class.getName() + ")"); - properties.put("openjpa.LockTimeout", locktimeout + ""); - - entityManagerFactory = OpenJPAPersistence.getEntityManagerFactory(properties); - JVMMailboxPathLocker locker = new JVMMailboxPathLocker(); - JPAMailboxSessionMapperFactory mf = new JPAMailboxSessionMapperFactory(entityManagerFactory, new JPAUidProvider(locker, entityManagerFactory), new JPAModSeqProvider(locker, entityManagerFactory)); - - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); - - mailboxManager = new OpenJPAMailboxManager(mf, null, aclResolver, groupMembershipResolver, messageParser); - mailboxManager.init(); - - // Set the lock timeout via SQL because of a bug in openJPA - // https://issues.apache.org/jira/browse/OPENJPA-1656 - setH2LockTimeout(); - - } - - private void setH2LockTimeout() { - EntityManager manager = entityManagerFactory.createEntityManager(); - manager.getTransaction().begin(); - manager.createNativeQuery("SET DEFAULT_LOCK_TIMEOUT " + locktimeout).executeUpdate(); - manager.getTransaction().commit(); - manager.close(); - } - - @After - public void tearDown() { - MailboxSession session = mailboxManager.createSystemSession("test", LoggerFactory.getLogger("Test")); - try { - mailboxManager.deleteEverything(session); - } catch (MailboxException e) { - e.printStackTrace(); - } - session.close(); - } - - @Override - protected MailboxManager getMailboxManager() { - return mailboxManager; - } - -} diff --git a/mailbox/maildir/pom.xml b/mailbox/maildir/pom.xml index bbb3ec5bdaa..699abef5ccb 100644 --- a/mailbox/maildir/pom.xml +++ b/mailbox/maildir/pom.xml @@ -92,5 +92,15 @@ ${assertj-1.version} test + + org.mockito + mockito-core + test + + + org.xenei + junit-contracts + test + diff --git a/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java b/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java deleted file mode 100644 index 8203a403e67..00000000000 --- a/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/**************************************************************** - * 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.maildir; - -import java.io.IOException; - -import org.apache.james.mailbox.AbstractMailboxManagerTest; -import org.apache.james.mailbox.acl.GroupMembershipResolver; -import org.apache.james.mailbox.acl.MailboxACLResolver; -import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; -import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.store.JVMMailboxPathLocker; -import org.apache.james.mailbox.store.StoreMailboxManager; -import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; -import org.junit.runners.Suite.SuiteClasses; - -@SuiteClasses({ - MaildirMailboxManagerTest.DomainUser.class, - MaildirMailboxManagerTest.User.class, - MaildirMailboxManagerTest.FullUser.class}) - -public class MaildirMailboxManagerTest { - - public static abstract class AbstractMaildirMailboxManagerTest extends AbstractMailboxManagerTest { - @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); - - @Before - public void setup() throws Exception { - if (OsDetector.isWindows()) { - System.out.println("Maildir tests work only on non-windows systems. So skip the test"); - } else { - createMailboxManager(); - } - } - - protected void createMailboxManager(String configuration) throws MailboxException, IOException { - MaildirStore store = new MaildirStore(tmpFolder.newFolder().getPath() + configuration, new JVMMailboxPathLocker()); - MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(store); - - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); - - StoreMailboxManager manager = new StoreMailboxManager(mf, null, new JVMMailboxPathLocker(), aclResolver, groupMembershipResolver, messageParser); - manager.init(); - setMailboxManager(manager); - } - - } - - public static class DomainUser extends AbstractMaildirMailboxManagerTest { - @Override - protected void createMailboxManager() throws MailboxException, - IOException { - createMailboxManager("/%domain/%user"); - } - } - - @Ignore - public static class User extends AbstractMaildirMailboxManagerTest { - @Override - protected void createMailboxManager() throws MailboxException, - IOException { - createMailboxManager("/%user"); - } - } - - public static class FullUser extends AbstractMaildirMailboxManagerTest { - @Override - protected void createMailboxManager() throws MailboxException, - IOException { - createMailboxManager("/%fulluser"); - } - } - -} diff --git a/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTests.java b/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTests.java new file mode 100644 index 00000000000..9a6a4a8952b --- /dev/null +++ b/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirMailboxManagerTests.java @@ -0,0 +1,157 @@ +/**************************************************************** + * 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.maildir; + +import java.io.IOException; + +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxManagerTest; +import org.apache.james.mailbox.acl.GroupMembershipResolver; +import org.apache.james.mailbox.acl.MailboxACLResolver; +import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; +import org.apache.james.mailbox.acl.UnionMailboxACLResolver; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.store.JVMMailboxPathLocker; +import org.apache.james.mailbox.store.StoreMailboxManager; +import org.apache.james.mailbox.store.mail.model.impl.MessageParser; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractImpl; +import org.xenei.junit.contract.ContractSuite; +import org.xenei.junit.contract.IProducer; + +import com.google.common.base.Throwables; + +@SuiteClasses({ + MaildirMailboxManagerTests.DomainUser.class, + MaildirMailboxManagerTests.User.class, + MaildirMailboxManagerTests.FullUser.class}) + +public class MaildirMailboxManagerTests { + + public static abstract class MaildirMailboxManagerTest extends MailboxManagerTest { + protected StoreMailboxManager createMailboxManager(String configuration, TemporaryFolder temporaryFolder) throws MailboxException, IOException { + MaildirStore store = new MaildirStore(temporaryFolder.newFolder().getPath() + configuration, new JVMMailboxPathLocker()); + MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(store); + + MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); + GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); + MessageParser messageParser = new MessageParser(); + + StoreMailboxManager manager = new StoreMailboxManager(mf, null, new JVMMailboxPathLocker(), aclResolver, groupMembershipResolver, messageParser); + manager.init(); + + return manager; + } + + } + + @RunWith(ContractSuite.class) + @ContractImpl(StoreMailboxManager.class) + public static class DomainUser extends MaildirMailboxManagerTest { + @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); + + private IProducer producer = new IProducer() { + + @Override + public StoreMailboxManager newInstance() { + try { + tmpFolder.create(); + return createMailboxManager("/%domain/%user", tmpFolder); + } catch (Exception e) { + throw Throwables.propagate(e); + } + } + + @Override + public void cleanUp() { + tmpFolder.delete(); + } + }; + + @Contract.Inject + public IProducer getProducer() { + return producer; + } + } + + @Ignore + @RunWith(ContractSuite.class) + @ContractImpl(StoreMailboxManager.class) + public static class User extends MaildirMailboxManagerTest { + @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); + + private IProducer producer = new IProducer() { + + @Override + public StoreMailboxManager newInstance() { + try { + tmpFolder.create(); + return createMailboxManager("/%user", tmpFolder); + } catch (Exception e) { + throw Throwables.propagate(e); + } + } + + @Override + public void cleanUp() { + tmpFolder.delete(); + } + }; + + @Contract.Inject + public IProducer getProducer() { + return producer; + } + } + + @RunWith(ContractSuite.class) + @ContractImpl(StoreMailboxManager.class) + public static class FullUser extends MaildirMailboxManagerTest { + @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); + + private IProducer producer = new IProducer() { + + @Override + public StoreMailboxManager newInstance() { + try { + tmpFolder.create(); + return createMailboxManager("/%fulluser", tmpFolder); + } catch (Exception e) { + throw Throwables.propagate(e); + } + } + + @Override + public void cleanUp() { + tmpFolder.delete(); + } + }; + + @Contract.Inject + public IProducer getProducer() { + return producer; + } + } + +} diff --git a/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirStressTest.java b/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirStressTest.java deleted file mode 100644 index ea56353b4e0..00000000000 --- a/mailbox/maildir/src/test/java/org/apache/james/mailbox/maildir/MaildirStressTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/**************************************************************** - * 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.maildir; - -import java.io.File; -import java.io.IOException; - -import org.apache.commons.io.FileUtils; -import org.apache.james.mailbox.AbstractStressTest; -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.acl.GroupMembershipResolver; -import org.apache.james.mailbox.acl.MailboxACLResolver; -import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; -import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.store.JVMMailboxPathLocker; -import org.apache.james.mailbox.store.StoreMailboxManager; -import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.After; -import org.junit.Before; - -public class MaildirStressTest extends AbstractStressTest { - - private static final String MAILDIR_HOME = "target/Maildir"; - - private StoreMailboxManager mailboxManager; - - @Before - public void setUp() throws MailboxException { - MaildirStore store = new MaildirStore(MAILDIR_HOME + "/%user", new JVMMailboxPathLocker()); - - MaildirMailboxSessionMapperFactory mf = new MaildirMailboxSessionMapperFactory(store); - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); - - mailboxManager = new StoreMailboxManager(mf, null, new JVMMailboxPathLocker(), aclResolver, groupMembershipResolver, messageParser); - mailboxManager.init(); - - } - - @After - public void tearDown() throws IOException { - FileUtils.deleteDirectory(new File(MAILDIR_HOME)); - } - - @Override - public void testStessTest() throws InterruptedException, MailboxException { - if (OsDetector.isWindows()) { - System.out.println("Maildir tests work only on non-windows systems. So skip the test"); - } else { - super.testStessTest(); - } - } - - @Override - protected MailboxManager getMailboxManager() { - return mailboxManager; - } - -} diff --git a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManagerTest.java b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManagerTest.java index b876f442a46..7a9d8366b98 100644 --- a/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManagerTest.java +++ b/mailbox/memory/src/test/java/org/apache/james/mailbox/inmemory/InMemoryMailboxManagerTest.java @@ -16,99 +16,58 @@ * specific language governing permissions and limitations * * under the License. * ****************************************************************/ -package org.apache.james.mailbox.inmemory; - -import static org.assertj.core.api.Assertions.assertThat; -import java.util.List; +package org.apache.james.mailbox.inmemory; -import org.apache.james.mailbox.AbstractMailboxManagerTest; -import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.acl.GroupMembershipResolver; import org.apache.james.mailbox.acl.MailboxACLResolver; import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver; import org.apache.james.mailbox.acl.UnionMailboxACLResolver; -import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.model.MailboxMetaData; -import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.model.MailboxQuery; import org.apache.james.mailbox.store.JVMMailboxPathLocker; import org.apache.james.mailbox.store.MockAuthenticator; -import org.apache.james.mailbox.store.StoreMailboxManager; import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.LoggerFactory; +import org.junit.runner.RunWith; +import org.xenei.junit.contract.Contract; +import org.xenei.junit.contract.ContractImpl; +import org.xenei.junit.contract.ContractSuite; +import org.xenei.junit.contract.IProducer; -/** - * InMemoryMailboxManagerTest that extends the MailboxManagerTest. - */ -public class InMemoryMailboxManagerTest extends AbstractMailboxManagerTest { +import com.google.common.base.Throwables; - private MailboxSession session; +@RunWith(ContractSuite.class) +@ContractImpl(InMemoryMailboxManager.class) +public class InMemoryMailboxManagerTest { - /** - * Setup the mailboxManager. - * - * @throws Exception - */ - @Before - public void setup() throws Exception { - createMailboxManager(); + private IProducer producer = new IProducer() { - session = getMailboxManager().createSystemSession(USER_1, LoggerFactory.getLogger("Test")); - getMailboxManager().startProcessingRequest(session); - } - - /** - * Close the system session and entityManagerFactory - * - * @throws MailboxException - * @throws BadCredentialsException - */ - @After - public void tearDown() throws MailboxException { - getMailboxManager().logout(session, true); - getMailboxManager().endProcessingRequest(session); - getMailboxManager().createSystemSession("test", LoggerFactory.getLogger("Test")).close(); - } - - /* (non-Javadoc) - * @see org.apache.james.mailbox.MailboxManagerTest#createMailboxManager() - */ - @Override - protected void createMailboxManager() throws MailboxException { + @Override + public InMemoryMailboxManager newInstance() { + MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); + GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); + MessageParser messageParser = new MessageParser(); - MailboxACLResolver aclResolver = new UnionMailboxACLResolver(); - GroupMembershipResolver groupMembershipResolver = new SimpleGroupMembershipResolver(); - MessageParser messageParser = new MessageParser(); + InMemoryMailboxSessionMapperFactory mailboxSessionMapperFactory = new InMemoryMailboxSessionMapperFactory(); + InMemoryMailboxManager mailboxManager = new InMemoryMailboxManager(mailboxSessionMapperFactory, new MockAuthenticator(), new JVMMailboxPathLocker(), aclResolver, groupMembershipResolver, messageParser); - InMemoryMailboxSessionMapperFactory mailboxSessionMapperFactory = new InMemoryMailboxSessionMapperFactory(); - StoreMailboxManager mailboxManager = new InMemoryMailboxManager(mailboxSessionMapperFactory, new MockAuthenticator(), new JVMMailboxPathLocker(), aclResolver, groupMembershipResolver, messageParser); - mailboxManager.init(); - - setMailboxManager(mailboxManager); + try { + mailboxManager.init(); + } catch (MailboxException e) { + throw Throwables.propagate(e); + } - } + return mailboxManager; + } - @Test - public void searchShouldNotReturnResultsFromOtherNamespaces() throws Exception { - getMailboxManager().createMailbox(new MailboxPath("#namespace", USER_1, "Other"), session); - getMailboxManager().createMailbox(MailboxPath.inbox(session), session); - List metaDatas = getMailboxManager().search(new MailboxQuery(new MailboxPath("#private", USER_1, ""), "*", '.'), session); - assertThat(metaDatas).hasSize(1); - assertThat(metaDatas.get(0).getPath()).isEqualTo(MailboxPath.inbox(session)); - } + @Override + public void cleanUp() { + } + }; - @Test - public void searchShouldNotReturnResultsFromOtherUsers() throws Exception { - getMailboxManager().createMailbox(new MailboxPath("#namespace", USER_2, "Other"), session); - getMailboxManager().createMailbox(MailboxPath.inbox(session), session); - List metaDatas = getMailboxManager().search(new MailboxQuery(new MailboxPath("#private", USER_1, ""), "*", '.'), session); - assertThat(metaDatas).hasSize(1); - assertThat(metaDatas.get(0).getPath()).isEqualTo(MailboxPath.inbox(session)); + @Contract.Inject + public IProducer getProducer() { + return producer; } }