JAMES-1983 Write CLI integration tests on the printed output
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c994be04 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c994be04 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c994be04 Branch: refs/heads/master Commit: c994be049e91bb3e9a6ca0fa31dec94fb2d23860 Parents: e854211 Author: benwa <btell...@linagora.com> Authored: Sat Apr 1 18:24:01 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Mon Apr 3 18:09:33 2017 +0700 ---------------------------------------------------------------------- .../james/cli/DataCommandsIntegrationTest.java | 97 +++++++++++++++----- .../cli/MailboxCommandsIntegrationTest.java | 25 +++-- .../james/cli/QuotaCommandsIntegrationTest.java | 83 ++++++++++++++--- .../cli/SieveQuotaCommandsIntegrationTest.java | 31 +++++-- .../apache/james/cli/util/OutputCapture.java | 39 ++++++++ .../james/cli/util/OutputCaptureTest.java | 58 ++++++++++++ .../java/org/apache/james/cli/ServerCmd.java | 63 +++++++------ 7 files changed, 315 insertions(+), 81 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli-integration/src/test/java/org/apache/james/cli/DataCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/DataCommandsIntegrationTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/DataCommandsIntegrationTest.java index 1e3a6d3..9c46c2a 100644 --- a/server/container/cli-integration/src/test/java/org/apache/james/cli/DataCommandsIntegrationTest.java +++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/DataCommandsIntegrationTest.java @@ -24,32 +24,27 @@ import static org.mockito.Mockito.mock; import java.util.AbstractMap; -import javax.inject.Singleton; - import org.apache.james.GuiceJamesServer; import org.apache.james.MemoryJmapTestRule; -import org.apache.james.mailbox.inmemory.InMemoryMailboxManager; +import org.apache.james.cli.util.OutputCapture; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.modules.server.JMXServerModule; import org.apache.james.rrt.lib.MappingImpl; import org.apache.james.rrt.lib.Mappings; import org.apache.james.rrt.lib.MappingsImpl; import org.apache.james.utils.DataProbeImpl; -import org.apache.james.utils.MailboxManagerDefinition; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import com.google.inject.Inject; -import com.google.inject.multibindings.Multibinder; - public class DataCommandsIntegrationTest { - public static final String DOMAIN_COM = "domain.com"; - public static final String USER = "user"; - public static final String MAIL_ADDRESS = USER + "@" + DOMAIN_COM; + public static final String DOMAIN = "domain.com"; + public static final String USER = "chibenwa"; + public static final String MAIL_ADDRESS = USER + "@" + DOMAIN; public static final String PASSWORD = "12345"; + private OutputCapture outputCapture; @Rule public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule(); @@ -62,6 +57,7 @@ public class DataCommandsIntegrationTest { binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class))); guiceJamesServer.start(); dataProbe = guiceJamesServer.getProbe(DataProbeImpl.class); + outputCapture = new OutputCapture(); } @After @@ -71,23 +67,43 @@ public class DataCommandsIntegrationTest { @Test public void addDomainShouldWork() throws Exception { - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "ADDDOMAIN", DOMAIN_COM}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "ADDDOMAIN", DOMAIN}); - assertThat(dataProbe.containsDomain(DOMAIN_COM)).isTrue(); + assertThat(dataProbe.containsDomain(DOMAIN)).isTrue(); } @Test public void removeDomainShouldWork() throws Exception { - dataProbe.addDomain(DOMAIN_COM); + dataProbe.addDomain(DOMAIN); + + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "REMOVEDOMAIN", DOMAIN}); + + assertThat(dataProbe.containsDomain(DOMAIN)).isFalse(); + } + + @Test + public void listDomainsShouldWork() throws Exception { + dataProbe.addDomain(DOMAIN); - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "REMOVEDOMAIN", DOMAIN_COM}); + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listdomains"}, outputCapture.getPrintStream()); - assertThat(dataProbe.containsDomain(DOMAIN_COM)).isFalse(); + assertThat(outputCapture.getContent()).contains(DOMAIN); + } + + @Test + public void containsDomainShouldWork() throws Exception { + dataProbe.addDomain(DOMAIN); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "containsdomain", DOMAIN}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce(DOMAIN + " exists"); } @Test public void addUserShouldWork() throws Exception { - dataProbe.addDomain(DOMAIN_COM); + dataProbe.addDomain(DOMAIN); ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "ADDUSER", MAIL_ADDRESS, PASSWORD}); @@ -96,7 +112,7 @@ public class DataCommandsIntegrationTest { @Test public void removeUserShouldWork() throws Exception { - dataProbe.addDomain(DOMAIN_COM); + dataProbe.addDomain(DOMAIN); dataProbe.addUser(MAIL_ADDRESS, PASSWORD); ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "REMOVEUSER", MAIL_ADDRESS}); @@ -105,9 +121,20 @@ public class DataCommandsIntegrationTest { } @Test + public void listUsersShouldWork() throws Exception { + dataProbe.addDomain(DOMAIN); + dataProbe.addUser(MAIL_ADDRESS, PASSWORD); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listusers"}, outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce(USER); + } + + @Test public void addAddressMappingShouldWork() throws Exception { String redirectionAddress = "redir...@apache.org"; - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN_COM, redirectionAddress}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress}); assertThat(dataProbe.listMappings()) .containsOnly( @@ -119,11 +146,35 @@ public class DataCommandsIntegrationTest { } @Test + public void listMappingsShouldWork() throws Exception { + String redirectionAddress = "redir...@apache.org"; + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listmappings"}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("chibe...@domain.com=redir...@apache.org"); + } + + @Test + public void listUsersDomainMappingShouldWork() throws Exception { + String redirectionAddress = "redir...@apache.org"; + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listuserdomainmappings", USER, DOMAIN}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("redir...@apache.org"); + } + + @Test public void removeAddressMappingShouldWork() throws Exception { String redirectionAddress = "redir...@apache.org"; - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN_COM, redirectionAddress}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress}); - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeaddressmapping", USER, DOMAIN_COM, redirectionAddress}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeaddressmapping", USER, DOMAIN, redirectionAddress}); assertThat(dataProbe.listMappings()) .isNull(); @@ -132,7 +183,7 @@ public class DataCommandsIntegrationTest { @Test public void addRegexMappingShouldWork() throws Exception { String regex = "regex"; - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN_COM, regex}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN, regex}); assertThat(dataProbe.listMappings()) .containsOnly( @@ -146,9 +197,9 @@ public class DataCommandsIntegrationTest { @Test public void removeRegexMappingShouldWork() throws Exception { String regex = "regex"; - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN_COM, regex}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN, regex}); - ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeregexmapping", USER, DOMAIN_COM, regex}); + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeregexmapping", USER, DOMAIN, regex}); assertThat(dataProbe.listMappings()) .isNull(); http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli-integration/src/test/java/org/apache/james/cli/MailboxCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/MailboxCommandsIntegrationTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/MailboxCommandsIntegrationTest.java index 074a22a..9e131bb 100644 --- a/server/container/cli-integration/src/test/java/org/apache/james/cli/MailboxCommandsIntegrationTest.java +++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/MailboxCommandsIntegrationTest.java @@ -22,40 +22,34 @@ package org.apache.james.cli; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import javax.inject.Singleton; - import org.apache.james.GuiceJamesServer; import org.apache.james.MemoryJmapTestRule; -import org.apache.james.mailbox.inmemory.InMemoryMailboxManager; +import org.apache.james.cli.util.OutputCapture; import org.apache.james.mailbox.model.MailboxConstants; -import org.apache.james.mailbox.model.QuotaRoot; -import org.apache.james.mailbox.store.quota.QuotaRootImpl; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.modules.MailboxProbeImpl; import org.apache.james.modules.server.JMXServerModule; -import org.apache.james.utils.MailboxManagerDefinition; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import com.google.inject.Inject; -import com.google.inject.multibindings.Multibinder; - public class MailboxCommandsIntegrationTest { public static final String USER = "user"; - public static final String MAILBOX = "mailbox"; + public static final String MAILBOX = "mailboxExampleName"; @Rule public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule(); private GuiceJamesServer guiceJamesServer; private MailboxProbeImpl mailboxProbe; + private OutputCapture outputCapture; @Before public void setUp() throws Exception { guiceJamesServer = memoryJmap.jmapServer(new JMXServerModule(), binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class))); guiceJamesServer.start(); + outputCapture = new OutputCapture(); mailboxProbe = guiceJamesServer.getProbe(MailboxProbeImpl.class); } @@ -81,6 +75,17 @@ public class MailboxCommandsIntegrationTest { } @Test + public void listUserMailboxesShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "createmailbox", MailboxConstants.USER_NAMESPACE, USER, MAILBOX}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listusermailboxes", USER}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce(MAILBOX); + } + + @Test public void deleteMailboxeShouldWork() throws Exception { ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "createmailbox", MailboxConstants.USER_NAMESPACE, USER, MAILBOX}); http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java index 517457d..551a5f0 100644 --- a/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java +++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/QuotaCommandsIntegrationTest.java @@ -22,36 +22,23 @@ package org.apache.james.cli; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import javax.inject.Singleton; - import org.apache.james.GuiceJamesServer; import org.apache.james.MemoryJmapTestRule; -import org.apache.james.mailbox.inmemory.InMemoryMailboxManager; +import org.apache.james.cli.util.OutputCapture; import org.apache.james.mailbox.model.QuotaRoot; import org.apache.james.mailbox.store.quota.QuotaRootImpl; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.modules.QuotaProbesImpl; import org.apache.james.modules.server.JMXServerModule; -import org.apache.james.utils.MailboxManagerDefinition; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import com.google.inject.Inject; -import com.google.inject.multibindings.Multibinder; - public class QuotaCommandsIntegrationTest { public static final String USER = "user"; public static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("#private&" + USER); - - @Singleton - private static class MemoryMailboxManagerDefinition extends MailboxManagerDefinition { - @Inject - private MemoryMailboxManagerDefinition(InMemoryMailboxManager manager) { - super("memory-mailboxmanager", manager); - } - } + private OutputCapture outputCapture; @Rule public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule(); @@ -64,6 +51,7 @@ public class QuotaCommandsIntegrationTest { binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class))); guiceJamesServer.start(); quotaProbe = guiceJamesServer.getProbe(QuotaProbesImpl.class); + outputCapture = new OutputCapture(); } @After @@ -79,6 +67,17 @@ public class QuotaCommandsIntegrationTest { } @Test + public void getDefaultMaxStorageShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setdefaultmaxstoragequota", "36M"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getdefaultmaxstoragequota"}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("Default Maximum Storage Quota: 36 MB"); + } + + @Test public void setDefaultMaxMessageCountShouldWork() throws Exception { ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setdefaultmaxmessagecountquota", "36"}); @@ -86,6 +85,17 @@ public class QuotaCommandsIntegrationTest { } @Test + public void getDefaultMaxMessageCountShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setdefaultmaxmessagecountquota", "36"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getdefaultmaxmessagecountquota"}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("Default Maximum message count Quota: 36"); + } + + @Test public void setMaxStorageShouldWork() throws Exception { ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxstoragequota", QUOTA_ROOT.getValue(), "36"}); @@ -93,10 +103,53 @@ public class QuotaCommandsIntegrationTest { } @Test + public void getMaxStorageShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxstoragequota", QUOTA_ROOT.getValue(), "1g"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getmaxstoragequota", QUOTA_ROOT.getValue()}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("Storage space allowed for Quota Root #private&user: 1 GB"); + } + + @Test public void setMaxMessageCountShouldWork() throws Exception { ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxmessagecountquota", QUOTA_ROOT.getValue(), "36"}); assertThat(quotaProbe.getMaxMessageCount(QUOTA_ROOT.getValue())).isEqualTo(36); } + @Test + public void getMaxMessageCountShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxmessagecountquota", QUOTA_ROOT.getValue(), "36"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getmaxmessagecountquota", QUOTA_ROOT.getValue()}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("MailboxMessage count allowed for Quota Root #private&user: 36"); + } + + @Test + public void getStorageQuotaShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxstoragequota", QUOTA_ROOT.getValue(), "36"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getstoragequota", QUOTA_ROOT.getValue()}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("Storage quota for #private&user is: 0 bytes / 36 bytes"); + } + + @Test + public void getMessageCountQuotaShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxmessagecountquota", QUOTA_ROOT.getValue(), "36"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getmessagecountquota", QUOTA_ROOT.getValue()}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("MailboxMessage count quota for #private&user is: 0 / 36"); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli-integration/src/test/java/org/apache/james/cli/SieveQuotaCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/SieveQuotaCommandsIntegrationTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/SieveQuotaCommandsIntegrationTest.java index bc2fb30..4e086ab 100644 --- a/server/container/cli-integration/src/test/java/org/apache/james/cli/SieveQuotaCommandsIntegrationTest.java +++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/SieveQuotaCommandsIntegrationTest.java @@ -22,17 +22,14 @@ package org.apache.james.cli; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import javax.inject.Singleton; - import org.apache.james.GuiceJamesServer; import org.apache.james.MemoryJmapTestRule; -import org.apache.james.mailbox.inmemory.InMemoryMailboxManager; +import org.apache.james.cli.util.OutputCapture; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.modules.TestFilesystemModule; import org.apache.james.modules.protocols.SieveProbeImpl; import org.apache.james.modules.server.JMXServerModule; import org.apache.james.sieverepository.api.exception.QuotaNotFoundException; -import org.apache.james.utils.MailboxManagerDefinition; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -40,9 +37,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import com.google.inject.Inject; -import com.google.inject.multibindings.Multibinder; - public class SieveQuotaCommandsIntegrationTest { public static final String USER = "user"; @@ -54,6 +48,7 @@ public class SieveQuotaCommandsIntegrationTest { public TemporaryFolder temporaryFolder = new TemporaryFolder(); private GuiceJamesServer guiceJamesServer; private SieveProbeImpl sieveProbe; + private OutputCapture outputCapture; @Before public void setUp() throws Exception { @@ -61,6 +56,7 @@ public class SieveQuotaCommandsIntegrationTest { binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class))) .overrideWith(new TestFilesystemModule(temporaryFolder)); guiceJamesServer.start(); + outputCapture = new OutputCapture(); sieveProbe = guiceJamesServer.getProbe(SieveProbeImpl.class); } @@ -77,12 +73,33 @@ public class SieveQuotaCommandsIntegrationTest { } @Test + public void getSieveUserQuotaShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setsieveuserquota", USER, "36"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getsieveuserquota", USER}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("Storage space allowed for user Sieve scripts: 36 bytes"); + } + + @Test public void setSieveQuotaShouldWork() throws Exception { ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setsievequota", "36"}); assertThat(sieveProbe.getSieveQuota()).isEqualTo(36); } + @Test + public void getSieveQuotaShouldWork() throws Exception { + ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setsievequota", "36"}); + + ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getsievequota"}, + outputCapture.getPrintStream()); + + assertThat(outputCapture.getContent()) + .containsOnlyOnce("Storage space allowed for Sieve scripts by default: 36 bytes"); + } @Test public void removeSieveUserQuotaShouldWork() throws Exception { http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCapture.java ---------------------------------------------------------------------- diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCapture.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCapture.java new file mode 100644 index 0000000..20fef6a --- /dev/null +++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCapture.java @@ -0,0 +1,39 @@ +/**************************************************************** + * 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.cli.util; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import com.google.common.base.Charsets; + +public class OutputCapture { + + private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + public PrintStream getPrintStream() { + return new PrintStream(byteArrayOutputStream); + } + + public String getContent() { + return new String(byteArrayOutputStream.toByteArray(), Charsets.UTF_8); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCaptureTest.java ---------------------------------------------------------------------- diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCaptureTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCaptureTest.java new file mode 100644 index 0000000..a76c2fb --- /dev/null +++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/util/OutputCaptureTest.java @@ -0,0 +1,58 @@ +/**************************************************************** + * 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.cli.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +import com.google.common.base.Charsets; + +public class OutputCaptureTest { + + @Test + public void contentShouldBeEmptyByDefault() { + assertThat(new OutputCapture().getContent()).isEmpty(); + } + + @Test + public void contentShouldReturnOutputStreamInput() throws Exception { + OutputCapture outputCapture = new OutputCapture(); + + String message = "Hello world!\n"; + outputCapture.getPrintStream().write(message.getBytes(Charsets.UTF_8)); + + assertThat(outputCapture.getContent()).isEqualTo(message); + } + + + @Test + public void mixingReadsAndWritesShouldWork() throws Exception { + OutputCapture outputCapture = new OutputCapture(); + String message = "Hello world!\n"; + outputCapture.getPrintStream().write(message.getBytes(Charsets.UTF_8)); + outputCapture.getContent(); + + String additionalMessage = "Additional message!\n"; + outputCapture.getPrintStream().write(additionalMessage.getBytes(Charsets.UTF_8)); + + assertThat(outputCapture.getContent()).isEqualTo(message + additionalMessage); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c994be04/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java ---------------------------------------------------------------------- diff --git a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java index af882ed..edf86fa 100644 --- a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java +++ b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java @@ -102,6 +102,11 @@ public class ServerCmd { } public static void doMain(String[] args) throws Exception { + PrintStream printStream = System.out; + executeAndOutputToStream(args, printStream); + } + + public static void executeAndOutputToStream(String[] args, PrintStream printStream) throws Exception { StopWatch stopWatch = new StopWatch(); stopWatch.start(); CommandLine cmd = parseCommandLine(args); @@ -112,11 +117,11 @@ public class ServerCmd { new JmxQuotaProbe().connect(jmxConnection), new JmxSieveProbe().connect(jmxConnection) ) - .executeCommandLine(cmd); + .executeCommandLine(cmd, printStream); stopWatch.split(); print(new String[] { Joiner.on(' ') .join(cmdType.getCommand(), "command executed sucessfully in", stopWatch.getSplitTime(), "ms.")}, - System.out); + printStream); stopWatch.stop(); } @@ -169,8 +174,8 @@ public class ServerCmd { } @VisibleForTesting - CmdType executeCommandLine(CommandLine cmd) throws Exception { - String[] arguments = cmd.getArgs(); + private CmdType executeCommandLine(CommandLine commandLine, PrintStream printStream) throws Exception { + String[] arguments = commandLine.getArgs(); String cmdName = arguments[0]; CmdType cmdType = CmdType.lookup(cmdName); if (cmdType == null) { @@ -179,11 +184,17 @@ public class ServerCmd { if (! cmdType.hasCorrectArguments(arguments.length)) { throw new InvalidArgumentNumberException(cmdType, arguments.length); } - executeCommand(arguments, cmdType); + executeCommand(arguments, cmdType, printStream); return cmdType; } - private void executeCommand(String[] arguments, CmdType cmdType) throws Exception { + + @VisibleForTesting + CmdType executeCommandLine(CommandLine commandLine) throws Exception { + return executeCommandLine(commandLine, new PrintStream(System.out)); + } + + private void executeCommand(String[] arguments, CmdType cmdType, PrintStream printStream) throws Exception { switch (cmdType) { case ADDUSER: probe.addUser(arguments[1], arguments[2]); @@ -192,7 +203,7 @@ public class ServerCmd { probe.removeUser(arguments[1]); break; case LISTUSERS: - print(probe.listUsers(), System.out); + print(probe.listUsers(), printStream); break; case ADDDOMAIN: probe.addDomain(arguments[1]); @@ -202,20 +213,20 @@ public class ServerCmd { break; case CONTAINSDOMAIN: if (probe.containsDomain(arguments[1])) { - System.out.println(arguments[1] + " exists"); + printStream.println(arguments[1] + " exists"); } else { - System.out.println(arguments[1] + " does not exists"); + printStream.println(arguments[1] + " does not exists"); } break; case LISTDOMAINS: - print(probe.listDomains(), System.out); + print(probe.listDomains(), printStream); break; case LISTMAPPINGS: - print(probe.listMappings(), System.out); + print(probe.listMappings(), printStream); break; case LISTUSERDOMAINMAPPINGS: Mappings userDomainMappings = probe.listUserDomainMappings(arguments[1], arguments[2]); - print(userDomainMappings.asStrings(), System.out); + print(userDomainMappings.asStrings(), printStream); break; case ADDADDRESSMAPPING: probe.addAddressMapping(arguments[1], arguments[2], arguments[3]); @@ -243,28 +254,28 @@ public class ServerCmd { break; case LISTUSERMAILBOXES: Collection<String> mailboxes = mailboxProbe.listUserMailboxes(arguments[1]); - print(mailboxes.toArray(new String[0]), System.out); + print(mailboxes.toArray(new String[0]), printStream); break; case DELETEMAILBOX: mailboxProbe.deleteMailbox(arguments[1], arguments[2], arguments[3]); break; case GETSTORAGEQUOTA: - printStorageQuota(arguments[1], quotaProbe.getStorageQuota(arguments[1])); + printStorageQuota(arguments[1], quotaProbe.getStorageQuota(arguments[1]), printStream); break; case GETMESSAGECOUNTQUOTA: - printMessageQuota(arguments[1], quotaProbe.getMessageCountQuota(arguments[1])); + printMessageQuota(arguments[1], quotaProbe.getMessageCountQuota(arguments[1]), printStream); break; case GETQUOTAROOT: - System.out.println("Quota Root : " + quotaProbe.getQuotaRoot(arguments[1], arguments[2], arguments[3])); + printStream.println("Quota Root : " + quotaProbe.getQuotaRoot(arguments[1], arguments[2], arguments[3])); break; case GETMAXSTORAGEQUOTA: - System.out.println("Storage space allowed for Quota Root " + printStream.println("Storage space allowed for Quota Root " + arguments[1] + " : " + formatStorageValue(quotaProbe.getMaxStorage(arguments[1]))); break; case GETMAXMESSAGECOUNTQUOTA: - System.out.println("MailboxMessage count allowed for Quota Root " + arguments[1] + " : " + formatMessageValue(quotaProbe.getMaxMessageCount(arguments[1]))); + printStream.println("MailboxMessage count allowed for Quota Root " + arguments[1] + " : " + formatMessageValue(quotaProbe.getMaxMessageCount(arguments[1]))); break; case SETMAXSTORAGEQUOTA: quotaProbe.setMaxStorage(arguments[1], ValueWithUnit.parse(arguments[2]).getConvertedValue()); @@ -279,10 +290,10 @@ public class ServerCmd { quotaProbe.setDefaultMaxMessageCount(Long.parseLong(arguments[1])); break; case GETDEFAULTMAXSTORAGEQUOTA: - System.out.println("Default Maximum Storage Quota : " + formatStorageValue(quotaProbe.getDefaultMaxStorage())); + printStream.println("Default Maximum Storage Quota : " + formatStorageValue(quotaProbe.getDefaultMaxStorage())); break; case GETDEFAULTMAXMESSAGECOUNTQUOTA: - System.out.println("Default Maximum message count Quota : " + formatMessageValue(quotaProbe.getDefaultMaxMessageCount())); + printStream.println("Default Maximum message count Quota : " + formatMessageValue(quotaProbe.getDefaultMaxMessageCount())); break; case REINDEXMAILBOX: mailboxProbe.reIndexMailbox(arguments[1], arguments[2], arguments[3]); @@ -297,11 +308,11 @@ public class ServerCmd { sieveProbe.setSieveQuota(arguments[1], ValueWithUnit.parse(arguments[2]).getConvertedValue()); break; case GETSIEVEQUOTA: - System.out.println("Storage space allowed for Sieve scripts by default : " + printStream.println("Storage space allowed for Sieve scripts by default : " + formatStorageValue(sieveProbe.getSieveQuota())); break; case GETSIEVEUSERQUOTA: - System.out.println("Storage space allowed for " + printStream.println("Storage space allowed for " + arguments[1] + " Sieve scripts : " + formatStorageValue(sieveProbe.getSieveQuota(arguments[1]))); @@ -327,15 +338,15 @@ public class ServerCmd { } } - private void printStorageQuota(String quotaRootString, SerializableQuota quota) { - System.out.println(String.format("Storage quota for %s is : %s / %s", + private void printStorageQuota(String quotaRootString, SerializableQuota quota, PrintStream printStream) { + printStream.println(String.format("Storage quota for %s is : %s / %s", quotaRootString, formatStorageValue(quota.getUsed()), formatStorageValue(quota.getMax()))); } - private void printMessageQuota(String quotaRootString, SerializableQuota quota) { - System.out.println(String.format("MailboxMessage count quota for %s is : %s / %s", + private void printMessageQuota(String quotaRootString, SerializableQuota quota, PrintStream printStream) { + printStream.println(String.format("MailboxMessage count quota for %s is : %s / %s", quotaRootString, formatMessageValue(quota.getUsed()), formatMessageValue(quota.getMax()))); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org