Author: btellier
Date: Thu Jul 9 07:44:34 2015
New Revision: 1690005
URL: http://svn.apache.org/r1690005
Log:
JAMES-1584 Improve error handling in CLI
Added:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidArgumentNumberException.java
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/JamesCliException.java
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/MissingCommandException.java
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/UnrecognizedCommandException.java
Modified:
james/server/trunk/container/cli/pom.xml
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/type/CmdType.java
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java
james/server/trunk/container/mailbox-adapter/src/main/java/org/apache/james/adapter/mailbox/MailboxManagerManagement.java
james/server/trunk/container/mailbox-adapter/src/test/java/org/apache/james/adapter/mailbox/MailboxManagementTest.java
Modified: james/server/trunk/container/cli/pom.xml
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/pom.xml?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
--- james/server/trunk/container/cli/pom.xml (original)
+++ james/server/trunk/container/cli/pom.xml Thu Jul 9 07:44:34 2015
@@ -28,7 +28,6 @@
<relativePath>../../pom.xml</relativePath>
</parent>
- <groupId>org.apache.james</groupId>
<artifactId>james-server-cli</artifactId>
<packaging>bundle</packaging>
@@ -52,6 +51,11 @@
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
Modified:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
(original)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
Thu Jul 9 07:44:34 2015
@@ -19,6 +19,7 @@
package org.apache.james.cli;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
@@ -26,13 +27,18 @@ import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
+import org.apache.commons.lang.time.StopWatch;
+import org.apache.james.cli.exceptions.InvalidArgumentNumberException;
+import org.apache.james.cli.exceptions.InvalidPortException;
+import org.apache.james.cli.exceptions.JamesCliException;
+import org.apache.james.cli.exceptions.MissingCommandException;
+import org.apache.james.cli.exceptions.UnrecognizedCommandException;
import org.apache.james.cli.probe.ServerProbe;
import org.apache.james.cli.probe.impl.JmxServerProbe;
import org.apache.james.cli.type.CmdType;
import java.io.IOException;
import java.io.PrintStream;
-import java.util.Calendar;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
@@ -41,22 +47,24 @@ import java.util.Map.Entry;
* Command line utility for managing various aspect of the James server.
*/
public class ServerCmd {
- private static final String HOST_OPT_LONG = "host";
- private static final String HOST_OPT_SHORT = "h";
- private static final String PORT_OPT_LONG = "port";
- private static final String PORT_OPT_SHORT = "p";
- private static final int DEFAULT_PORT = 9999;
- private static final Options OPTIONS = new Options();
+ public static final String HOST_OPT_LONG = "host";
+ public static final String HOST_OPT_SHORT = "h";
+ public static final String PORT_OPT_LONG = "port";
+ public static final String PORT_OPT_SHORT = "p";
- private ServerProbe probe;
+ private static final int DEFAULT_PORT = 9999;
- static {
+ private static Options createOptions() {
+ Options options = new Options();
Option optHost = new Option(HOST_OPT_SHORT, HOST_OPT_LONG, true, "node
hostname or ip address");
optHost.setRequired(true);
- OPTIONS.addOption(optHost);
- OPTIONS.addOption(PORT_OPT_SHORT, PORT_OPT_LONG, true, "remote jmx
agent port number");
+ options.addOption(optHost);
+ options.addOption(PORT_OPT_SHORT, PORT_OPT_LONG, true, "remote jmx
agent port number");
+ return options;
}
+ private final ServerProbe probe;
+
public ServerCmd(ServerProbe probe) {
this.probe = probe;
}
@@ -65,48 +73,48 @@ public class ServerCmd {
* Main method to initialize the class.
*
* @param args Command-line arguments.
- * @throws IOException
- * @throws InterruptedException
- * @throws ParseException
*/
- public static void main(String[] args) throws IOException,
InterruptedException, ParseException {
- long start = Calendar.getInstance().getTimeInMillis();
- CommandLine cmd = parseCommandLine(args);
- if (cmd.getArgs().length < 1) {
- failWithMessage("Missing argument for command.");
- }
+ public static void main(String[] args) {
+
try {
- new ServerCmd(new
JmxServerProbe(cmd.getOptionValue(HOST_OPT_LONG), getPort(cmd)))
- .executeCommandLine(start, cmd);
+ StopWatch stopWatch = new StopWatch();
+ stopWatch.start();
+ CommandLine cmd = parseCommandLine(args);
+ CmdType cmdType =new ServerCmd(new
JmxServerProbe(cmd.getOptionValue(HOST_OPT_LONG), getPort(cmd)))
+ .executeCommandLine(cmd);
+ stopWatch.stop();
+ print(new String[] { Joiner.on(' ')
+ .join(cmdType.getCommand(), "command executed sucessfully
in", stopWatch.getSplitTime(), "ms.")},
+ System.out);
+ System.exit(0);
+ } catch (JamesCliException e) {
+ failWithMessage(e.getMessage());
+ } catch (ParseException e) {
+ failWithMessage("Error parsing command line : " + e.getMessage());
} catch (IOException ioe) {
- System.err.println("Error connecting to remote JMX agent!");
- ioe.printStackTrace();
- System.exit(3);
+ failWithMessage("Error connecting to remote JMX agent : " +
ioe.getMessage());
} catch (Exception e) {
failWithMessage("Error while executing command:" + e.getMessage());
}
- System.exit(0);
+
}
@VisibleForTesting
- static CommandLine parseCommandLine(String[] args) {
- try {
- CommandLineParser parser = new PosixParser();
- return parser.parse(OPTIONS, args);
- } catch (ParseException parseExcep) {
- System.err.println(parseExcep.getMessage());
- printUsage();
- parseExcep.printStackTrace(System.err);
- System.exit(1);
- return null;
+ static CommandLine parseCommandLine(String[] args) throws ParseException {
+ CommandLineParser parser = new PosixParser();
+ CommandLine commandLine = parser.parse(createOptions(), args);
+ if (commandLine.getArgs().length < 1) {
+ throw new MissingCommandException();
}
+ return commandLine;
}
- private static int getPort(CommandLine cmd) throws ParseException {
+ @VisibleForTesting
+ static int getPort(CommandLine cmd) throws ParseException {
String portNum = cmd.getOptionValue(PORT_OPT_LONG);
if (portNum != null) {
try {
- return Integer.parseInt(portNum);
+ return validatePortNumber(Integer.parseInt(portNum));
} catch (NumberFormatException e) {
throw new ParseException("Port must be a number");
}
@@ -114,6 +122,13 @@ public class ServerCmd {
return DEFAULT_PORT;
}
+ private static int validatePortNumber(int portNumber) {
+ if (portNumber < 1 || portNumber > 65535) {
+ throw new InvalidPortException(portNumber);
+ }
+ return portNumber;
+ }
+
private static void failWithMessage(String s) {
System.err.println(s);
printUsage();
@@ -121,23 +136,21 @@ public class ServerCmd {
}
@VisibleForTesting
- void executeCommandLine(long start, CommandLine cmd) throws Exception {
+ CmdType executeCommandLine(CommandLine cmd) throws Exception {
String[] arguments = cmd.getArgs();
String cmdName = arguments[0];
CmdType cmdType = CmdType.lookup(cmdName);
-
+ if (cmdType == null) {
+ throw new UnrecognizedCommandException(cmdName);
+ }
if (! cmdType.hasCorrectArguments(arguments.length)) {
- throw new Exception(String.format("%s is expecting %d arguments
but got %d",
- cmdType.getCommand(),
- cmdType.getArguments(),
- arguments.length));
+ throw new InvalidArgumentNumberException(cmdType,
arguments.length);
}
- executeCommand(arguments, cmdName, cmdType);
-
- this.print(new String[] { cmdType.getCommand() + " command executed
sucessfully in " + (Calendar.getInstance().getTimeInMillis() - start) + " ms."
}, System.out);
+ executeCommand(arguments, cmdType);
+ return cmdType;
}
- private void executeCommand(String[] arguments, String cmdName, CmdType
cmdType) throws Exception {
+ private void executeCommand(String[] arguments, CmdType cmdType) throws
Exception {
switch (cmdType) {
case ADDUSER:
probe.addUser(arguments[1], arguments[2]);
@@ -165,7 +178,7 @@ public class ServerCmd {
break;
case LISTUSERDOMAINMAPPINGS:
Collection<String> userDomainMappings =
probe.listUserDomainMappings(arguments[1], arguments[2]);
- this.print(userDomainMappings.toArray(new
String[userDomainMappings.size()]), System.out);
+ print(userDomainMappings.toArray(new String[0]), System.out);
break;
case ADDADDRESSMAPPING:
probe.addAddressMapping(arguments[1], arguments[2], arguments[3]);
@@ -193,75 +206,48 @@ public class ServerCmd {
break;
case LISTUSERMAILBOXES:
Collection<String> mailboxes =
probe.listUserMailboxes(arguments[1]);
- this.print(mailboxes.toArray(new String[mailboxes.size()]),
System.out);
+ print(mailboxes.toArray(new String[0]), System.out);
break;
case DELETEMAILBOX:
probe.deleteMailbox(arguments[1], arguments[2], arguments[3]);
break;
default:
- throw new Exception("Unrecognized command: " + cmdName + ".");
+ throw new UnrecognizedCommandException(cmdType.getCommand());
}
}
- /**
- * Print data to an output stream.
- *
- * @param data The data to print, each element representing a line.
- * @param out The output stream to which printing should occur.
- */
- public void print(String[] data, PrintStream out) {
- if (data == null)
- return;
- for (String u : data) {
- out.println(u);
- }
- out.println();
- }
-
- public void print(Map<String, Collection<String>> map, PrintStream out) {
- if (map == null)
- return;
- for (Entry<String, Collection<String>> entry : map.entrySet()) {
- out.println(entry.getKey() + '=' + collectionToString(entry));
+ private static void print(String[] data, PrintStream out) {
+ if (data != null) {
+ for (String u : data) {
+ out.println(u);
+ }
+ out.println(Joiner.on('\n').join(data));
}
- out.println();
}
- private String collectionToString(Entry<String, Collection<String>> entry)
{
- StringBuilder stringBuilder = new StringBuilder();
- for (String value : entry.getValue()) {
- stringBuilder.append(value).append(',');
+ private void print(Map<String, Collection<String>> map, PrintStream out) {
+ if (map != null) {
+ for (Entry<String, Collection<String>> entry : map.entrySet()) {
+ out.println(entry.getKey() + '=' + collectionToString(entry));
+ }
+ out.println();
}
- return stringBuilder.toString();
}
- /**
- * Prints usage information to stdout.
- */
+ private String collectionToString(Entry<String, Collection<String>> entry)
{
+ return Joiner.on(',').join(entry.getValue());
+ }
+
private static void printUsage() {
- HelpFormatter hf = new HelpFormatter();
- String footer = String.format("%nAvailable commands:%n" +
- "adduser <username> <password>%n" +
- "setpassword <username> <password>%n" +
- "removeuser <username>%n" + "listusers%n" +
- "adddomain <domainname>%n" +
- "containsdomain <domainname>%n" +
- "removedomain <domainname>%n" +
- "listdomains%n" +
- "addaddressmapping <user> <domain> <fromaddress>%n" +
- "removeaddressmapping <user> <domain> <fromaddress>%n" +
- "addregexmapping <user> <domain> <regex>%n" +
- "removeregexmapping <user> <domain> <regex>%n" +
- "listuserdomainmappings <user> <domain>%n" +
- "listmappings%n" +
- "copymailbox <srcbean> <dstbean>%n" +
- "deleteusermailboxes <user>%n" +
- "createmailbox <namespace> <user> <name>%n" +
- "listusermailboxes <user>%n" +
- "deletemailbox <namespace> <user> <name>%n"
- );
- String usage = String.format("java %s --host <arg> <command>%n",
ServerCmd.class.getName());
- hf.printHelp(usage, "", OPTIONS, footer);
+ StringBuilder footerBuilder = new StringBuilder();
+ for (CmdType cmdType : CmdType.values()) {
+ footerBuilder.append(cmdType.getUsage()).append("\n");
+ }
+ new HelpFormatter().printHelp(
+ String.format("java %s --host <arg> <command>%n",
ServerCmd.class.getName()),
+ "",
+ createOptions(),
+ footerBuilder.toString());
}
}
Added:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidArgumentNumberException.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidArgumentNumberException.java?rev=1690005&view=auto
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidArgumentNumberException.java
(added)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidArgumentNumberException.java
Thu Jul 9 07:44:34 2015
@@ -0,0 +1,34 @@
+/****************************************************************
+ * 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.exceptions;
+
+import org.apache.james.cli.type.CmdType;
+
+public class InvalidArgumentNumberException extends JamesCliException {
+
+ public InvalidArgumentNumberException(CmdType cmdType, int
providedArgumentCount) {
+ super(String.format("%s should be used with %d arguments but only %d
were provided%n%s%n",
+ cmdType.getCommand(),
+ cmdType.getArgumentCount(),
+ providedArgumentCount,
+ cmdType.getUsage()));
+ }
+
+}
Added:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java?rev=1690005&view=auto
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
(added)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
Thu Jul 9 07:44:34 2015
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.exceptions;
+
+public class InvalidPortException extends JamesCliException {
+
+ public InvalidPortException(int invalidPortNumber) {
+ super( invalidPortNumber + " is not a valid port number. Please
provide one between 1 and 65535");
+ }
+
+}
Added:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/JamesCliException.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/JamesCliException.java?rev=1690005&view=auto
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/JamesCliException.java
(added)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/JamesCliException.java
Thu Jul 9 07:44:34 2015
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.exceptions;
+
+public class JamesCliException extends RuntimeException{
+
+ public JamesCliException(String s) {
+ super(s);
+ }
+
+}
Added:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/MissingCommandException.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/MissingCommandException.java?rev=1690005&view=auto
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/MissingCommandException.java
(added)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/MissingCommandException.java
Thu Jul 9 07:44:34 2015
@@ -0,0 +1,27 @@
+/****************************************************************
+ * 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.exceptions;
+
+public class MissingCommandException extends JamesCliException {
+
+ public MissingCommandException() {
+ super("You must specify a command.");
+ }
+}
Added:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/UnrecognizedCommandException.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/UnrecognizedCommandException.java?rev=1690005&view=auto
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/UnrecognizedCommandException.java
(added)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/exceptions/UnrecognizedCommandException.java
Thu Jul 9 07:44:34 2015
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.exceptions;
+
+public class UnrecognizedCommandException extends JamesCliException {
+
+ public UnrecognizedCommandException(String providedCommandName) {
+ super(providedCommandName + " does not exist");
+ }
+
+}
Modified:
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/type/CmdType.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/main/java/org/apache/james/cli/type/CmdType.java?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
---
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/type/CmdType.java
(original)
+++
james/server/trunk/container/cli/src/main/java/org/apache/james/cli/type/CmdType.java
Thu Jul 9 07:44:34 2015
@@ -22,30 +22,30 @@ package org.apache.james.cli.type;
* Enumeration of valid command types.
*/
public enum CmdType {
- ADDUSER("adduser", 3), //
- REMOVEUSER("removeuser", 2), //
- LISTUSERS("listusers", 1), //
- ADDDOMAIN("adddomain", 2), //
- REMOVEDOMAIN("removedomain", 2), //
- CONTAINSDOMAIN("containsdomain", 2), //
- LISTDOMAINS("listdomains", 1), //
- LISTMAPPINGS("listmappings", 1), //
- LISTUSERDOMAINMAPPINGS("listuserdomainmappings", 3), //
- ADDADDRESSMAPPING("addaddressmapping", 4), //
- REMOVEADDRESSMAPPING("removeaddressmapping", 4), //
- ADDREGEXMAPPING("addregexmapping", 4), //
- REMOVEREGEXMAPPING("removeregexmapping", 4), //
- SETPASSWORD("setpassword", 3), //
- COPYMAILBOX("copymailbox", 3), //
- DELETEUSERMAILBOXES("deleteusermailboxes", 2), //
- CREATEMAILBOX("createmailbox", 4), //
- LISTUSERMAILBOXES("listusermailboxes",2), //
- DELETEMAILBOX("deletemailbox", 4) //
- ;
+ ADDUSER("adduser", "username","password"),
+ REMOVEUSER("removeuser", "username"),
+ LISTUSERS("listusers"),
+ ADDDOMAIN("adddomain", "domainname"),
+ REMOVEDOMAIN("removedomain", "domainname"),
+ CONTAINSDOMAIN("containsdomain", "domainname"),
+ LISTDOMAINS("listdomains"),
+ LISTMAPPINGS("listmappings"),
+ LISTUSERDOMAINMAPPINGS("listuserdomainmappings", "user","domain"),
+ ADDADDRESSMAPPING("addaddressmapping", "user","domain", "fromaddress"),
+ REMOVEADDRESSMAPPING("removeaddressmapping", "user","domain",
"fromaddress"),
+ ADDREGEXMAPPING("addregexmapping", "user","domain", "regex"),
+ REMOVEREGEXMAPPING("removeregexmapping", "user","domain", "regex"),
+ SETPASSWORD("setpassword", "username","password"),
+ COPYMAILBOX("copymailbox", "srcbean","dstbean"),
+ DELETEUSERMAILBOXES("deleteusermailboxes", "user"),
+ CREATEMAILBOX("createmailbox", "namespace", "user", "name"),
+ LISTUSERMAILBOXES("listusermailboxes", "user"),
+ DELETEMAILBOX("deletemailbox", "namespace", "user", "name");
+
private final String command;
- private final int arguments;
+ private final String[] arguments;
- private CmdType(String command, int arguments) {
+ CmdType(String command, String... arguments) {
this.command = command;
this.arguments = arguments;
}
@@ -58,7 +58,7 @@ public enum CmdType {
* @return true if values match, false otherwise.
*/
public boolean hasCorrectArguments(int arguments) {
- return this.arguments == arguments;
+ return this.arguments.length + 1 == arguments;
}
@@ -72,9 +72,11 @@ public enum CmdType {
*/
public static CmdType lookup(String command) {
if (command != null) {
- for (CmdType cmd : values())
- if (cmd.getCommand().equalsIgnoreCase(command))
- return cmd;
+ for (CmdType cmd : values()) {
+ if (cmd.getCommand().equalsIgnoreCase(command)) {
+ return cmd;
+ }
+ }
}
return null;
}
@@ -93,7 +95,15 @@ public enum CmdType {
*
* @return the value of arguments.
*/
- public int getArguments() {
- return this.arguments;
+ public int getArgumentCount() {
+ return this.arguments.length + 1;
}
+
+ public String getUsage() {
+ StringBuilder stringBuilder = new StringBuilder();
+ for(String argument : arguments) {
+ stringBuilder.append(" <" + argument + ">");
+ }
+ return stringBuilder.toString();
+ }
}
Modified:
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
---
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
(original)
+++
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
Thu Jul 9 07:44:34 2015
@@ -19,6 +19,8 @@
package org.apache.james.cli;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
import static org.easymock.EasyMock.createControl;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
@@ -29,13 +31,17 @@ import java.util.Collection;
import java.util.HashMap;
import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.ParseException;
+import org.apache.james.cli.exceptions.InvalidArgumentNumberException;
+import org.apache.james.cli.exceptions.InvalidPortException;
+import org.apache.james.cli.exceptions.MissingCommandException;
+import org.apache.james.cli.exceptions.UnrecognizedCommandException;
import org.apache.james.cli.probe.ServerProbe;
import org.apache.james.cli.type.CmdType;
import org.easymock.IMocksControl;
import org.junit.Before;
import org.junit.Test;
-
public class ServerCmdTest {
public static final String ADDITIONAL_ARGUMENT = "additionalArgument";
@@ -62,7 +68,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -76,7 +82,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -89,7 +95,7 @@ public class ServerCmdTest {
expect(serverProbe.containsDomain(domain)).andReturn(true);
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -102,7 +108,7 @@ public class ServerCmdTest {
expect(serverProbe.listDomains()).andReturn(res);
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -117,7 +123,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -131,7 +137,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -144,7 +150,7 @@ public class ServerCmdTest {
expect(serverProbe.listUsers()).andReturn(res);
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -156,7 +162,7 @@ public class ServerCmdTest {
expect(serverProbe.listMappings()).andReturn(new HashMap<String,
Collection<String>>());
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -170,7 +176,7 @@ public class ServerCmdTest {
expect(serverProbe.listUserDomainMappings(user, domain)).andReturn(new
ArrayList<String>());
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -186,7 +192,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -202,7 +208,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -218,7 +224,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -234,7 +240,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -249,7 +255,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -264,7 +270,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -278,7 +284,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -294,7 +300,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -310,7 +316,7 @@ public class ServerCmdTest {
expectLastCall();
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
@@ -323,50 +329,50 @@ public class ServerCmdTest {
expect(serverProbe.listUserMailboxes(user)).andReturn(new
ArrayList<String>());
control.replay();
- testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
control.verify();
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addDomainCommandShouldThrowOnMissingArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.ADDDOMAIN.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeDomainCommandShouldThrowOnMissingArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.REMOVEDOMAIN.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void containsDomainCommandShouldThrowOnMissingArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.CONTAINSDOMAIN.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addUserCommandShouldThrowOnMissingArguments() throws Exception
{
String user = "user@domain";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.ADDUSER.getCommand(), user};
@@ -374,26 +380,26 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeUserCommandShouldThrowOnMissingArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.REMOVEUSER.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void listUserDomainMappingsCommandShouldThrowOnMissingArguments()
throws Exception {
String user = "user@domain";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.LISTUSERDOMAINMAPPINGS.getCommand(), user};
@@ -401,13 +407,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addAddressCommandShouldThrowOnMissingArguments() throws
Exception {
String user = "user@domain";
String domain = "domain";
@@ -416,13 +422,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeAddressCommandShouldThrowOnMissingArguments() throws
Exception {
String user = "user@domain";
String domain = "domain";
@@ -431,13 +437,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addRegexMappingCommandShouldThrowOnMissingArguments() throws
Exception {
String user = "user@domain";
String domain = "domain";
@@ -446,13 +452,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeRegexMappingCommandShouldThrowOnMissingArguments()
throws Exception {
String user = "user@domain";
String domain = "domain";
@@ -461,13 +467,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void setPasswordMappingCommandShouldThrowOnMissingArguments()
throws Exception {
String user = "user@domain";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.SETPASSWORD.getCommand(), user};
@@ -475,13 +481,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void copyMailboxMappingCommandShouldThrowOnMissingArguments()
throws Exception {
String srcBean = "srcBean";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.COPYMAILBOX.getCommand(), srcBean};
@@ -489,26 +495,26 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void
deleteUserMailboxesMappingCommandShouldThrowOnMissingArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.DELETEUSERMAILBOXES.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void createMailboxMappingCommandShouldThrowOnMissingArguments()
throws Exception {
String user = "user@domain";
String namespace = "#private";
@@ -517,13 +523,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void deleteMailboxMappingCommandShouldThrowOnMissingArguments()
throws Exception {
String user = "user@domain";
String namespace = "#private";
@@ -532,26 +538,26 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void
listUserMailboxesMappingsCommandShouldThrowOnMissingArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.LISTUSERMAILBOXES.getCommand()};
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addDomainCommandShouldThrowOnAdditionalArguments() throws
Exception {
String domain = "example.com";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.ADDDOMAIN.getCommand(), domain, ADDITIONAL_ARGUMENT };
@@ -559,13 +565,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeDomainCommandShouldThrowOnAdditionalArguments() throws
Exception {
String domain = "example.com";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.REMOVEDOMAIN.getCommand(), domain, ADDITIONAL_ARGUMENT };
@@ -573,13 +579,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void containsDomainCommandShouldThrowOnAdditionalArguments() throws
Exception {
String domain = "example.com";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.CONTAINSDOMAIN.getCommand(), domain, ADDITIONAL_ARGUMENT };
@@ -587,26 +593,26 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void listDomainsCommandShouldThrowOnAdditionalArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.LISTDOMAINS.getCommand(), ADDITIONAL_ARGUMENT };
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addUserCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String password = "password";
@@ -615,13 +621,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeUserCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.REMOVEUSER.getCommand(), user, ADDITIONAL_ARGUMENT };
@@ -629,39 +635,39 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void listUsersCommandShouldThrowOnAdditionalArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.LISTUSERS.getCommand(), ADDITIONAL_ARGUMENT };
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void listMappingsCommandShouldThrowOnAdditionalArguments() throws
Exception {
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.LISTMAPPINGS.getCommand(), ADDITIONAL_ARGUMENT };
CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void
listUserDomainMappingsCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String domain = "domain";
@@ -670,13 +676,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addAddressCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String domain = "domain";
@@ -686,13 +692,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeAddressCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String domain = "domain";
@@ -702,13 +708,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void addRegexMappingCommandShouldThrowOnAdditionalArguments()
throws Exception {
String user = "user@domain";
String domain = "domain";
@@ -718,13 +724,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void removeRegexMappingCommandShouldThrowOnAdditionalArguments()
throws Exception {
String user = "user@domain";
String domain = "domain";
@@ -734,13 +740,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void setPasswordMappingCommandShouldThrowOnAdditionalArguments()
throws Exception {
String user = "user@domain";
String password = "pass";
@@ -749,13 +755,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void copyMailboxMappingCommandShouldThrowOnAdditionalArguments()
throws Exception {
String srcBean = "srcBean";
String dstBean = "dstBean";
@@ -764,13 +770,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void
deleteUserMailboxesMappingCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.DELETEUSERMAILBOXES.getCommand(), user, ADDITIONAL_ARGUMENT };
@@ -778,13 +784,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void createMailboxMappingCommandShouldThrowOnAdditionalArguments()
throws Exception {
String user = "user@domain";
String namespace = "#private";
@@ -794,13 +800,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void deleteMailboxMappingCommandShouldThrowOnAdditionalArguments()
throws Exception {
String user = "user@domain";
String namespace = "#private";
@@ -810,13 +816,13 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
- @Test(expected = Exception.class)
+ @Test(expected = InvalidArgumentNumberException.class)
public void
listUserMailboxesMappingsCommandShouldThrowOnAdditionalArguments() throws
Exception {
String user = "user@domain";
String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
CmdType.LISTUSERMAILBOXES.getCommand(), user, ADDITIONAL_ARGUMENT };
@@ -824,9 +830,108 @@ public class ServerCmdTest {
control.replay();
try {
-
testee.executeCommandLine(Calendar.getInstance().getTimeInMillis(),
commandLine);
+ testee.executeCommandLine(commandLine);
+ } finally {
+ control.verify();
+ }
+ }
+
+ @Test(expected = UnrecognizedCommandException.class)
+ public void executeCommandLineShouldThrowOnUnrecognizedCommands() throws
Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "9999",
"wrongCommand"};
+ CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
+
+ control.replay();
+ try {
+ testee.executeCommandLine(commandLine);
} finally {
control.verify();
}
}
+
+ @Test(expected = MissingCommandException.class)
+ public void parseCommandLineShouldThrowWhenOnlyOptionAreProvided() throws
Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "9999" };
+ ServerCmd.parseCommandLine(arguments);
+ }
+
+ @Test(expected = ParseException.class)
+ public void parseCommandLineShouldThrowWhenInvalidOptionIsProvided()
throws Exception {
+ String[] arguments = { "-v", "-h", "127.0.0.1", "-p", "9999" };
+ ServerCmd.parseCommandLine(arguments);
+ }
+
+ @Test(expected = ParseException.class)
+ public void parseCommandLineShouldThrowWhenMandatoryOptionIsMissing()
throws Exception {
+ String[] arguments = { "-v", "-h", "127.0.0.1", "-p", "9999" };
+ ServerCmd.parseCommandLine(arguments);
+ }
+
+ @Test
+ public void parseCommandLineShouldReturnACommandLineWithCorrectArguments()
throws Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "9999", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
+ assertThat(commandLine.getArgs()).containsExactly("command", "arg1",
"arg2", "arg3");
+ }
+
+ @Test
+ public void
parseCommandLineShouldReturnACommandLineWithCorrectPortOption() throws
Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "9999", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
+
assertThat(commandLine.getOptionValue(ServerCmd.PORT_OPT_LONG)).isEqualTo("9999");
+ }
+
+ @Test
+ public void
parseCommandLineShouldReturnACommandLineWithCorrectHostOption() throws
Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "9999", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
+
assertThat(commandLine.getOptionValue(ServerCmd.HOST_OPT_LONG)).isEqualTo("127.0.0.1");
+ }
+
+ @Test
+ public void getPortShouldRetrievePort() throws Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "9999", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
+ assertThat(ServerCmd.getPort(commandLine)).isEqualTo(9999);
+ }
+
+ @Test(expected = InvalidPortException.class)
+ public void getPortShouldThrowOnNullPortValueOption() throws Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "0", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine;
+ try {
+ commandLine = ServerCmd.parseCommandLine(arguments);
+ } catch (Exception e) {
+ fail("Exception received", e);
+ return;
+ }
+ ServerCmd.getPort(commandLine);
+ }
+
+ @Test(expected = InvalidPortException.class)
+ public void getPortShouldThrowOnNegativePortValueOption() throws Exception
{
+ String[] arguments = { "-h", "127.0.0.1", "-p", "-1", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine;
+ try {
+ commandLine = ServerCmd.parseCommandLine(arguments);
+ } catch (Exception e) {
+ fail("Exception received", e);
+ return;
+ }
+ ServerCmd.getPort(commandLine);
+ }
+
+ @Test(expected = InvalidPortException.class)
+ public void getPortShouldThrowOnTooHighPortValueOption() throws Exception {
+ String[] arguments = { "-h", "127.0.0.1", "-p", "99999", "command",
"arg1", "arg2", "arg3" };
+ CommandLine commandLine;
+ try {
+ commandLine = ServerCmd.parseCommandLine(arguments);
+ } catch (Exception e) {
+ fail("Exception received", e);
+ return;
+ }
+ ServerCmd.getPort(commandLine);
+ }
+
}
Modified:
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
---
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java
(original)
+++
james/server/trunk/container/cli/src/test/java/org/apache/james/cli/type/CmdTypeTest.java
Thu Jul 9 07:44:34 2015
@@ -41,11 +41,11 @@ public class CmdTypeTest {
assertEquals(false, result);
// Test actual number
- result = cmd.hasCorrectArguments(cmd.getArguments());
+ result = cmd.hasCorrectArguments(cmd.getArgumentCount());
assertEquals(true, result);
// Test known bad number
- result = cmd.hasCorrectArguments(cmd.getArguments() - 1);
+ result = cmd.hasCorrectArguments(cmd.getArgumentCount() - 1);
assertEquals(false, result);
}
Modified:
james/server/trunk/container/mailbox-adapter/src/main/java/org/apache/james/adapter/mailbox/MailboxManagerManagement.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/mailbox-adapter/src/main/java/org/apache/james/adapter/mailbox/MailboxManagerManagement.java?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
---
james/server/trunk/container/mailbox-adapter/src/main/java/org/apache/james/adapter/mailbox/MailboxManagerManagement.java
(original)
+++
james/server/trunk/container/mailbox-adapter/src/main/java/org/apache/james/adapter/mailbox/MailboxManagerManagement.java
Thu Jul 9 07:44:34 2015
@@ -29,7 +29,6 @@ import javax.management.NotCompliantMBea
import javax.management.StandardMBean;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import org.apache.james.lifecycle.api.LogEnabled;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
@@ -63,7 +62,7 @@ public class MailboxManagerManagement ex
*/
@Override
public boolean deleteMailboxes(String username) {
- Preconditions.checkArgument(Strings.isNullOrEmpty(username), "Username
should not be null");
+ checkString(username, "Username");
MailboxSession session = null;
try {
session = mailboxManager.createSystemSession(username, log);
@@ -96,7 +95,7 @@ public class MailboxManagerManagement ex
*/
@Override
public List<String> listMailboxes(String username) {
- Preconditions.checkArgument(Strings.isNullOrEmpty(username), "Username
should not be null");
+ checkString(username, "Username");
List<String> boxes = new ArrayList<String>();
MailboxSession session = null;
try {
@@ -145,12 +144,6 @@ public class MailboxManagerManagement ex
}
}
- private void checkMailboxArguments(String namespace, String user, String
name) {
- Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace),
"Provided mailbox path components should not be null or empty");
- Preconditions.checkArgument(!Strings.isNullOrEmpty(user), "Provided
mailbox path components should not be null or empty");
- Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Provided
mailbox name components should not be null or empty");
- }
-
private void closeSession(MailboxSession session) {
if (session != null) {
mailboxManager.endProcessingRequest(session);
@@ -169,4 +162,15 @@ public class MailboxManagerManagement ex
session.getPathDelimiter()),
session);
}
+
+ private void checkMailboxArguments(String namespace, String user, String
name) {
+ checkString(namespace, "mailbox path namespace");
+ checkString(user, "mailbox path user");
+ checkString(name, "mailbox name");
+ }
+
+ private void checkString(String argument, String role) {
+ Preconditions.checkNotNull(argument, "Provided " + role + " should not
be null.");
+ Preconditions.checkArgument(!argument.equals(""), "Provided " + role +
" should not be empty.");
+ }
}
\ No newline at end of file
Modified:
james/server/trunk/container/mailbox-adapter/src/test/java/org/apache/james/adapter/mailbox/MailboxManagementTest.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/container/mailbox-adapter/src/test/java/org/apache/james/adapter/mailbox/MailboxManagementTest.java?rev=1690005&r1=1690004&r2=1690005&view=diff
==============================================================================
---
james/server/trunk/container/mailbox-adapter/src/test/java/org/apache/james/adapter/mailbox/MailboxManagementTest.java
(original)
+++
james/server/trunk/container/mailbox-adapter/src/test/java/org/apache/james/adapter/mailbox/MailboxManagementTest.java
Thu Jul 9 07:44:34 2015
@@ -108,7 +108,7 @@ public class MailboxManagementTest {
assertThat(inMemoryMapperFactory.createMailboxMapper(session).list()).isEmpty();
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void deleteMailboxesShouldThrowOnNullUserName() throws Exception {
mailboxManagerManagement.deleteMailboxes(null);
}
@@ -143,17 +143,17 @@ public class MailboxManagementTest {
assertThat(inMemoryMapperFactory.createMailboxMapper(session).list()).containsExactly(mailbox);
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void createMailboxShouldThrowOnNullNamespace() {
mailboxManagerManagement.createMailbox(null, "a", "a");
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void createMailboxShouldThrowOnNullUser() {
mailboxManagerManagement.createMailbox("a", null, "a");
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void createMailboxShouldThrowOnNullName() {
mailboxManagerManagement.createMailbox("a", "a", null);
}
@@ -190,7 +190,7 @@ public class MailboxManagementTest {
assertThat(mailboxManagerManagement.listMailboxes(USER)).containsOnly("name2",
"name4", "INBOX", "INBOX.toto");
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void listMailboxesShouldThrowOnNullUserName() {
mailboxManagerManagement.listMailboxes(null);
}
@@ -231,17 +231,17 @@ public class MailboxManagementTest {
assertThat(inMemoryMapperFactory.createMailboxMapper(session).list()).containsOnly(mailbox);
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void deleteMailboxShouldThrowOnNullNamespace() {
mailboxManagerManagement.deleteMailbox(null, "a", "a");
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void deleteMailboxShouldThrowOnNullUser() {
mailboxManagerManagement.deleteMailbox("a", null, "a");
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void deleteMailboxShouldThrowOnNullName() {
mailboxManagerManagement.deleteMailbox("a", "a", null);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]