Mercurial > hg > release > thermostat-1.0
changeset 404:1c031363ed80
Sort the list of commands displayed by help
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-June/001940.html
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Wed, 20 Jun 2012 16:23:48 -0400 |
parents | 97b9c07ea0d7 |
children | 2d05f43e292f |
files | common/core/src/main/java/com/redhat/thermostat/common/cli/HelpCommand.java common/core/src/test/java/com/redhat/thermostat/common/cli/HelpCommandTest.java |
diffstat | 2 files changed, 63 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/cli/HelpCommand.java Wed Jun 20 15:46:31 2012 -0400 +++ b/common/core/src/main/java/com/redhat/thermostat/common/cli/HelpCommand.java Wed Jun 20 16:23:48 2012 -0400 @@ -36,8 +36,10 @@ package com.redhat.thermostat.common.cli; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.List; public class HelpCommand implements Command { @@ -47,6 +49,8 @@ private static final String DESCRIPTION = "show help for a given command or help overview"; private static final String USAGE = DESCRIPTION; + private static final CommandComparator comparator = new CommandComparator(); + @Override public void run(CommandContext ctx) { Arguments args = ctx.getArguments(); @@ -65,7 +69,10 @@ out.append("list of commands:\n\n"); Collection<Command> commands = cmdRegistry.getRegisteredCommands(); - for (Command cmd : commands) { + List<Command> sortedCommands = new ArrayList<>(commands); + + Collections.sort(sortedCommands, comparator); + for (Command cmd : sortedCommands) { printCommandSummary(out, cmd); } ctx.getConsole().getOutput().print(out); @@ -119,4 +126,24 @@ return false; } + private static class CommandComparator implements Comparator<Command> { + + @Override + public int compare(Command o1, Command o2) { + // this command ('help') is always listed first + if (o1.getName().equals(o2.getName())) { + return 0; + } + if (o1.getName().equals(NAME)) { + return -1; + } + if (o2.getName().equals(NAME)) { + return 1; + } + + return o1.getName().compareTo(o2.getName()); + } + + } + }
--- a/common/core/src/test/java/com/redhat/thermostat/common/cli/HelpCommandTest.java Wed Jun 20 15:46:31 2012 -0400 +++ b/common/core/src/test/java/com/redhat/thermostat/common/cli/HelpCommandTest.java Wed Jun 20 16:23:48 2012 -0400 @@ -40,6 +40,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.ArrayList; import java.util.Arrays; import org.junit.After; @@ -118,6 +119,40 @@ } @Test + public void verifyHelpKnownCmdPrintsCommandUsageSorted() { + TestCommand cmd1 = new TestCommand("test1"); + String description1 = "test command 1"; + cmd1.setDescription(description1); + + TestCommand cmd2 = new TestCommand("test2"); + String description2 = "test command 2"; + cmd2.setDescription(description2); + + TestCommand cmd3 = new TestCommand("test3"); + String description3 = "test command 3"; + cmd3.setDescription(description3); + + TestCommand cmd4 = new TestCommand("test4"); + String description4 = "test command 4"; + cmd4.setDescription(description4); + + ctxFactory.getCommandRegistry().registerCommands(Arrays.asList(cmd3, cmd1, cmd2, cmd4)); + + HelpCommand cmd = new HelpCommand(); + Arguments args = mock(Arguments.class); + when(args.getNonOptionArguments()).thenReturn(new ArrayList<String>()); + cmd.run(ctxFactory.createContext(args)); + + String actual = ctxFactory.getOutput(); + String expected = "list of commands:\n\n" + + " test1 test command 1\n" + + " test2 test command 2\n" + + " test3 test command 3\n" + + " test4 test command 4\n"; + assertEquals(expected, actual); + } + + @Test public void verifyHelpKnownStorageCmdPrintsCommandUsageWithDbUrl() { TestCommand cmd1 = new TestCommand("test1"); String usage = "test usage command 1";