JAMES-2024 Rely on Mockito where JMOCK upgrade made tests fails
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/33a6d55e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/33a6d55e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/33a6d55e Branch: refs/heads/master Commit: 33a6d55e02cea5e724525b0bd8ccdcabf19d54b4 Parents: ab4aec5 Author: benwa <[email protected]> Authored: Sat Aug 19 14:03:04 2017 +0700 Committer: benwa <[email protected]> Committed: Fri Aug 25 18:17:05 2017 +0700 ---------------------------------------------------------------------- protocols/imap/pom.xml | 1 - .../imap/processor/GetQuotaProcessorTest.java | 184 ++++++++----------- .../imap/processor/SetQuotaProcessorTest.java | 54 +++--- .../processor/StatusResponseTypeMatcher.java | 6 + 4 files changed, 101 insertions(+), 144 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/33a6d55e/protocols/imap/pom.xml ---------------------------------------------------------------------- diff --git a/protocols/imap/pom.xml b/protocols/imap/pom.xml index 4ce0ca7..54e7dde 100644 --- a/protocols/imap/pom.xml +++ b/protocols/imap/pom.xml @@ -41,7 +41,6 @@ <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> - <version>1.1.7</version> <scope>test</scope> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/33a6d55e/protocols/imap/src/test/java/org/apache/james/imap/processor/GetQuotaProcessorTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/GetQuotaProcessorTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/GetQuotaProcessorTest.java index 162ea9d..f96e9a9 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/GetQuotaProcessorTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/GetQuotaProcessorTest.java @@ -19,10 +19,17 @@ package org.apache.james.imap.processor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.ImapSessionUtils; -import org.apache.james.imap.api.message.response.StatusResponse; +import org.apache.james.imap.api.message.response.ImapResponseMessage; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapSession; import org.apache.james.imap.message.request.GetQuotaRequest; @@ -41,17 +48,12 @@ import org.apache.james.mailbox.quota.QuotaRootResolver; import org.apache.james.mailbox.store.quota.QuotaImpl; import org.apache.james.mailbox.store.quota.QuotaRootImpl; import org.apache.james.metrics.api.NoopMetricFactory; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; -import com.google.common.collect.Lists; +import com.google.common.collect.ImmutableList; -@RunWith(JMock.class) public class GetQuotaProcessorTest { private static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("plop"); @@ -60,7 +62,6 @@ public class GetQuotaProcessorTest { public static final Quota STORAGE_QUOTA = QuotaImpl.quota(240, 15890); private GetQuotaProcessor testee; - private Mockery mockery; private ImapSession mockedImapSession; private ImapProcessor.Responder mockedResponder; private QuotaManager mockedQuotaManager; @@ -71,137 +72,98 @@ public class GetQuotaProcessorTest { @Before public void setUp() { mailboxSession = new MockMailboxSession("plop"); - mockery = new JUnit4Mockery(); UnpooledStatusResponseFactory statusResponseFactory = new UnpooledStatusResponseFactory(); - mockedImapSession = mockery.mock(ImapSession.class); - mockedQuotaManager = mockery.mock(QuotaManager.class); - mockedQuotaRootResolver = mockery.mock(QuotaRootResolver.class); - mockedResponder = mockery.mock(ImapProcessor.Responder.class); - mockedMailboxManager = mockery.mock(MailboxManager.class); - testee = new GetQuotaProcessor(mockery.mock(ImapProcessor.class), mockedMailboxManager, + mockedImapSession = mock(ImapSession.class); + mockedQuotaManager = mock(QuotaManager.class); + mockedQuotaRootResolver = mock(QuotaRootResolver.class); + mockedResponder = mock(ImapProcessor.Responder.class); + mockedMailboxManager = mock(MailboxManager.class); + testee = new GetQuotaProcessor(mock(ImapProcessor.class), mockedMailboxManager, statusResponseFactory, mockedQuotaManager, mockedQuotaRootResolver, new NoopMetricFactory()); } @Test public void processorShouldWorkOnValidRights() throws Exception { GetQuotaRequest getQuotaRequest = new GetQuotaRequest("A004", ImapCommand.anyStateCommand("Name"), "quotaRoot"); - Expectations expectations = new Expectations(); - - expectations.allowing(mockedImapSession).getState(); - expectations.will(Expectations.returnValue(ImapSessionState.AUTHENTICATED)); - - expectations.allowing(mockedImapSession).getAttribute(expectations.with(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)); - expectations.will(Expectations.returnValue(mailboxSession)); - - expectations.allowing(mockedQuotaRootResolver).createQuotaRoot(expectations.with("quotaRoot")); - expectations.will(Expectations.returnValue(QUOTA_ROOT)); - - expectations.allowing(mockedQuotaRootResolver).retrieveAssociatedMailboxes(expectations.with(QUOTA_ROOT), expectations.with(mailboxSession)); - expectations.will(Expectations.returnValue(Lists.newArrayList(MAILBOX_PATH))); - - expectations.allowing(mockedMailboxManager).hasRight(expectations.with(MAILBOX_PATH), - expectations.with(SimpleMailboxACL.Rfc4314Rights.r_Read_RIGHT), expectations.with(mailboxSession)); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mockedQuotaManager).getMessageQuota(expectations.with(QUOTA_ROOT)); - expectations.will(Expectations.returnValue(MESSAGE_QUOTA)); - - expectations.allowing(mockedQuotaManager).getStorageQuota(expectations.with(QUOTA_ROOT)); - expectations.will(Expectations.returnValue(STORAGE_QUOTA)); - expectations.allowing(mockedMailboxManager).startProcessingRequest(expectations.with(mailboxSession)); + when(mockedImapSession.getState()).thenReturn(ImapSessionState.AUTHENTICATED); + when(mockedImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(mockedQuotaRootResolver.createQuotaRoot("quotaRoot")) + .thenReturn(QUOTA_ROOT); + when(mockedQuotaRootResolver.retrieveAssociatedMailboxes(QUOTA_ROOT, mailboxSession)) + .thenReturn(ImmutableList.of(MAILBOX_PATH)); + when(mockedMailboxManager.hasRight(MAILBOX_PATH, SimpleMailboxACL.Rfc4314Rights.r_Read_RIGHT, mailboxSession)) + .thenReturn(true); + when(mockedQuotaManager.getMessageQuota(QUOTA_ROOT)).thenReturn(MESSAGE_QUOTA); + when(mockedQuotaManager.getStorageQuota(QUOTA_ROOT)).thenReturn(STORAGE_QUOTA); + + QuotaResponse storageQuotaResponse = new QuotaResponse("STORAGE", "plop", STORAGE_QUOTA); + QuotaResponse messageQuotaResponse = new QuotaResponse("MESSAGE", "plop", MESSAGE_QUOTA); - expectations.allowing(mockedMailboxManager).endProcessingRequest(expectations.with(mailboxSession)); - - final QuotaResponse storageQuotaResponse = new QuotaResponse("STORAGE", "plop", STORAGE_QUOTA); - final QuotaResponse messageQuotaResponse = new QuotaResponse("MESSAGE", "plop", MESSAGE_QUOTA); - - mockery.checking(expectations); + testee.doProcess(getQuotaRequest, mockedResponder, mockedImapSession); - mockery.checking(new Expectations() { - { - oneOf(mockedResponder).respond(with(equal(storageQuotaResponse))); - oneOf(mockedResponder).respond(with(equal(messageQuotaResponse))); - oneOf(mockedResponder).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.OK))); - } - }); + ArgumentCaptor<ImapResponseMessage> argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); + verify(mockedResponder, times(3)).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(mockedResponder); - testee.doProcess(getQuotaRequest, mockedResponder, mockedImapSession); + assertThat(argumentCaptor.getAllValues()) + .hasSize(3) + .contains(storageQuotaResponse, messageQuotaResponse); + assertThat(argumentCaptor.getAllValues().get(2)) + .matches(StatusResponseTypeMatcher.OK_RESPONSE_MATCHER::matches); } @Test public void processorShouldWorkOnExceptionThrown() throws Exception { GetQuotaRequest getQuotaRequest = new GetQuotaRequest("A004", ImapCommand.anyStateCommand("Name"), "quotaRoot"); - Expectations expectations = new Expectations(); - - expectations.allowing(mockedImapSession).getState(); - expectations.will(Expectations.returnValue(ImapSessionState.AUTHENTICATED)); - - expectations.allowing(mockedImapSession).getAttribute(expectations.with(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)); - expectations.will(Expectations.returnValue(mailboxSession)); - - expectations.allowing(mockedQuotaRootResolver).createQuotaRoot(expectations.with("quotaRoot")); - expectations.will(Expectations.returnValue(QUOTA_ROOT)); - expectations.allowing(mockedQuotaRootResolver).retrieveAssociatedMailboxes(expectations.with(QUOTA_ROOT), expectations.with(mailboxSession)); - expectations.will(Expectations.returnValue(Lists.newArrayList(MAILBOX_PATH))); + when(mockedImapSession.getState()).thenReturn(ImapSessionState.AUTHENTICATED); + when(mockedImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(mockedQuotaRootResolver.createQuotaRoot("quotaRoot")) + .thenReturn(QUOTA_ROOT); + when(mockedQuotaRootResolver.retrieveAssociatedMailboxes(QUOTA_ROOT, mailboxSession)) + .thenReturn(ImmutableList.of(MAILBOX_PATH)); + when(mockedMailboxManager.hasRight(MAILBOX_PATH, SimpleMailboxACL.Rfc4314Rights.r_Read_RIGHT, mailboxSession)) + .thenReturn(true); + when(mockedQuotaManager.getMessageQuota(QUOTA_ROOT)).thenThrow(new MailboxException()); + when(mockedQuotaManager.getStorageQuota(QUOTA_ROOT)).thenReturn(STORAGE_QUOTA); - expectations.allowing(mockedMailboxManager).hasRight(expectations.with(MAILBOX_PATH), - expectations.with(SimpleMailboxACL.Rfc4314Rights.r_Read_RIGHT), expectations.with(mailboxSession)); - expectations.will(Expectations.returnValue(true)); - - expectations.allowing(mockedQuotaManager).getMessageQuota(expectations.with(QUOTA_ROOT)); - expectations.will(Expectations.throwException(new MailboxException())); - - expectations.allowing(mockedMailboxManager).startProcessingRequest(expectations.with(mailboxSession)); - - expectations.allowing(mockedMailboxManager).endProcessingRequest(expectations.with(mailboxSession)); - - mockery.checking(expectations); + testee.doProcess(getQuotaRequest, mockedResponder, mockedImapSession); - mockery.checking(new Expectations() { - { - oneOf(mockedResponder).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.BAD))); - } - }); + ArgumentCaptor<ImapResponseMessage> argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); + verify(mockedResponder).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(mockedResponder); - testee.doProcess(getQuotaRequest, mockedResponder, mockedImapSession); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1) + .allMatch(StatusResponseTypeMatcher.BAD_RESPONSE_MATCHER::matches); } @Test public void processorShouldWorkOnNoRights() throws Exception { GetQuotaRequest getQuotaRequest = new GetQuotaRequest("A004", ImapCommand.anyStateCommand("Name"), "quotaRoot"); - Expectations expectations = new Expectations(); - - expectations.allowing(mockedImapSession).getState(); - expectations.will(Expectations.returnValue(ImapSessionState.AUTHENTICATED)); - expectations.allowing(mockedImapSession).getAttribute(expectations.with(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)); - expectations.will(Expectations.returnValue(mailboxSession)); + when(mockedImapSession.getState()).thenReturn(ImapSessionState.AUTHENTICATED); + when(mockedImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); + when(mockedQuotaRootResolver.createQuotaRoot("quotaRoot")) + .thenReturn(QUOTA_ROOT); + when(mockedQuotaRootResolver.retrieveAssociatedMailboxes(QUOTA_ROOT, mailboxSession)) + .thenReturn(ImmutableList.of(MAILBOX_PATH)); + when(mockedMailboxManager.hasRight(MAILBOX_PATH, SimpleMailboxACL.Rfc4314Rights.r_Read_RIGHT, mailboxSession)) + .thenReturn(false); - expectations.allowing(mockedQuotaRootResolver).createQuotaRoot(expectations.with("quotaRoot")); - expectations.will(Expectations.returnValue(QUOTA_ROOT)); - - expectations.allowing(mockedQuotaRootResolver).retrieveAssociatedMailboxes(expectations.with(QUOTA_ROOT), expectations.with(mailboxSession)); - expectations.will(Expectations.returnValue(Lists.newArrayList(MAILBOX_PATH))); - - expectations.allowing(mockedMailboxManager).hasRight(expectations.with(MAILBOX_PATH), - expectations.with(SimpleMailboxACL.Rfc4314Rights.r_Read_RIGHT), expectations.with(mailboxSession)); - expectations.will(Expectations.returnValue(false)); - - expectations.allowing(mockedMailboxManager).startProcessingRequest(expectations.with(mailboxSession)); - - expectations.allowing(mockedMailboxManager).endProcessingRequest(expectations.with(mailboxSession)); - - mockery.checking(expectations); + testee.doProcess(getQuotaRequest, mockedResponder, mockedImapSession); - mockery.checking(new Expectations() { - { - oneOf(mockedResponder).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + ArgumentCaptor<ImapResponseMessage> argumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); + verify(mockedResponder).respond(argumentCaptor.capture()); + verifyNoMoreInteractions(mockedResponder); - testee.doProcess(getQuotaRequest, mockedResponder, mockedImapSession); + assertThat(argumentCaptor.getAllValues()) + .hasSize(1) + .allMatch(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/33a6d55e/protocols/imap/src/test/java/org/apache/james/imap/processor/SetQuotaProcessorTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/SetQuotaProcessorTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/SetQuotaProcessorTest.java index 80e749e..91337c6 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/SetQuotaProcessorTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/SetQuotaProcessorTest.java @@ -19,10 +19,16 @@ package org.apache.james.imap.processor; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapSessionState; import org.apache.james.imap.api.ImapSessionUtils; -import org.apache.james.imap.api.message.response.StatusResponse; +import org.apache.james.imap.api.message.response.ImapResponseMessage; import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.api.process.ImapSession; import org.apache.james.imap.message.request.SetQuotaRequest; @@ -31,19 +37,12 @@ import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.mock.MockMailboxSession; import org.apache.james.metrics.api.NoopMetricFactory; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.integration.junit4.JMock; -import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; -@RunWith(JMock.class) public class SetQuotaProcessorTest { - private SetQuotaProcessor testee; - private Mockery mockery; private ImapSession mockedImapSession; private ImapProcessor.Responder mockedResponder; private MailboxManager mockedMailboxManager; @@ -52,39 +51,30 @@ public class SetQuotaProcessorTest { @Before public void setUp() { mailboxSession = new MockMailboxSession("plop"); - mockery = new JUnit4Mockery(); UnpooledStatusResponseFactory statusResponseFactory = new UnpooledStatusResponseFactory(); - mockedImapSession = mockery.mock(ImapSession.class); - mockedResponder = mockery.mock(ImapProcessor.Responder.class); - mockedMailboxManager = mockery.mock(MailboxManager.class); - testee = new SetQuotaProcessor(mockery.mock(ImapProcessor.class), mockedMailboxManager, + mockedImapSession = mock(ImapSession.class); + mockedResponder = mock(ImapProcessor.Responder.class); + mockedMailboxManager = mock(MailboxManager.class); + testee = new SetQuotaProcessor(mock(ImapProcessor.class), mockedMailboxManager, statusResponseFactory, new NoopMetricFactory()); } @Test public void processorShouldWorkOnNoRights() throws Exception { SetQuotaRequest setQuotaRequest = new SetQuotaRequest("A004", ImapCommand.anyStateCommand("Name"), "quotaRoot"); - Expectations expectations = new Expectations(); - - expectations.allowing(mockedImapSession).getState(); - expectations.will(Expectations.returnValue(ImapSessionState.AUTHENTICATED)); - - expectations.allowing(mockedImapSession).getAttribute(expectations.with(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)); - expectations.will(Expectations.returnValue(mailboxSession)); - expectations.allowing(mockedMailboxManager).startProcessingRequest(expectations.with(mailboxSession)); - - expectations.allowing(mockedMailboxManager).endProcessingRequest(expectations.with(mailboxSession)); - - mockery.checking(expectations); - - mockery.checking(new Expectations() { - { - oneOf(mockedResponder).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO))); - } - }); + when(mockedImapSession.getState()).thenReturn(ImapSessionState.AUTHENTICATED); + when(mockedImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)) + .thenReturn(mailboxSession); testee.doProcess(setQuotaRequest, mockedResponder, mockedImapSession); + + ArgumentCaptor<ImapResponseMessage> imapResponseMessageArgumentCaptor = ArgumentCaptor.forClass(ImapResponseMessage.class); + verify(mockedResponder).respond(imapResponseMessageArgumentCaptor.capture()); + assertThat(imapResponseMessageArgumentCaptor.getAllValues()) + .hasSize(1) + .allMatch(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); + verifyNoMoreInteractions(mockedResponder); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/33a6d55e/protocols/imap/src/test/java/org/apache/james/imap/processor/StatusResponseTypeMatcher.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/StatusResponseTypeMatcher.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/StatusResponseTypeMatcher.java index bfca14d..0cdc650 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/StatusResponseTypeMatcher.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/StatusResponseTypeMatcher.java @@ -30,6 +30,12 @@ import org.hamcrest.Description; * */ public class StatusResponseTypeMatcher extends BaseMatcher<StatusResponse> { + + public static final StatusResponseTypeMatcher OK_RESPONSE_MATCHER = new StatusResponseTypeMatcher(StatusResponse.Type.OK); + public static final StatusResponseTypeMatcher BAD_RESPONSE_MATCHER = new StatusResponseTypeMatcher(StatusResponse.Type.BAD); + public static final StatusResponseTypeMatcher NO_RESPONSE_MATCHER = new StatusResponseTypeMatcher(StatusResponse.Type.NO); + + private final org.apache.james.imap.api.message.response.StatusResponse.Type serverResponseType; public StatusResponseTypeMatcher(org.apache.james.imap.api.message.response.StatusResponse.Type responseCode) { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
