Mercurial > hg > release > thermostat-0.9
view launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java @ 1067:f0f0027e0724
Hide CommandInfo and reduce Command interface
Reviewed-by: vanaltj
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-April/006349.html
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Wed, 17 Apr 2013 14:34:38 -0400 |
parents | 77cd25fad5bf |
children |
line wrap: on
line source
/* * Copyright 2012, 2013 Red Hat, Inc. * * This file is part of Thermostat. * * Thermostat is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or (at your * option) any later version. * * Thermostat is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Thermostat; see the file COPYING. If not see * <http://www.gnu.org/licenses/>. * * Linking this code with other modules is making a combined work * based on this code. Thus, the terms and conditions of the GNU * General Public License cover the whole combination. * * As a special exception, the copyright holders of this code give * you permission to link this code with independent modules to * produce an executable, regardless of the license terms of these * independent modules, and to copy and distribute the resulting * executable under terms of your choice, provided that you also * meet, for each linked independent module, the terms and conditions * of the license of that module. An independent module is a module * which is not derived from or based on this code. If you modify * this code, you may extend this exception to your version of the * library, but you are not obligated to do so. If you do not wish * to do so, delete this exception statement from your version. */ package com.redhat.thermostat.launcher.internal; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import org.apache.commons.cli.Options; import org.junit.Before; import org.junit.Test; import com.redhat.thermostat.common.cli.Arguments; import com.redhat.thermostat.common.cli.SimpleArguments; import com.redhat.thermostat.test.TestCommandContextFactory; public class HelpCommandTest { private TestCommandContextFactory ctxFactory; private CommandInfoSource infos; @Before public void setUp() { ctxFactory = new TestCommandContextFactory(); infos = mock(CommandInfoSource.class); } @Test public void verifyCommandIsAvailableEverywhere() { HelpCommand cmd = new HelpCommand(); assertTrue(cmd.isAvailableInShell()); assertTrue(cmd.isAvailableOutsideShell()); } @Test public void verifyStorageIsNotRequired() { HelpCommand cmd = new HelpCommand(); assertFalse(cmd.isStorageRequired()); } @Test public void verifyHelpFailsWithoutCommandInfoSource() { HelpCommand cmd = new HelpCommand(); Arguments args = mock(Arguments.class); when(args.getNonOptionArguments()).thenReturn(Arrays.asList("test1")); cmd.run(ctxFactory.createContext(args)); assertEquals("no information about commands", ctxFactory.getError()); assertEquals("", ctxFactory.getOutput()); } @Test public void verifyHelpNoArgPrintsListOfCommandsNoCommands() { HelpCommand cmd = new HelpCommand(); cmd.setCommandInfoSource(infos); Arguments args = mock(Arguments.class); cmd.run(ctxFactory.createContext(args)); String expected = "list of commands:\n\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); } @Test public void verifyHelpNoArgPrintsListOfCommands2Commands() { Collection<CommandInfo> infoList = new ArrayList<CommandInfo>(); CommandInfo info1 = mock(CommandInfo.class); when(info1.getName()).thenReturn("test1"); when(info1.getDescription()).thenReturn("test command 1"); infoList.add(info1); CommandInfo info2 = mock(CommandInfo.class); when(info2.getName()).thenReturn("test2longname"); when(info2.getDescription()).thenReturn("test command 2"); infoList.add(info2); when(infos.getCommandInfos()).thenReturn(infoList); HelpCommand cmd = new HelpCommand(); cmd.setCommandInfoSource(infos); Arguments args = mock(Arguments.class); cmd.run(ctxFactory.createContext(args)); String expected = "list of commands:\n\n" + " test1 test command 1\n" + " test2longname test command 2\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); } @Test public void verifyHelpKnownCmdPrintsCommandUsage() { CommandInfo testCommandInfo = mock(CommandInfo.class); when(testCommandInfo.getName()).thenReturn("test1"); when(testCommandInfo.getUsage()).thenReturn("usage of test command"); when(testCommandInfo.getDescription()).thenReturn("description of test command"); when(testCommandInfo.getOptions()).thenReturn(new Options()); when(infos.getCommandInfo("test1")).thenReturn(testCommandInfo); HelpCommand cmd = new HelpCommand(); cmd.setCommandInfoSource(infos); Arguments args = mock(Arguments.class); when(args.getNonOptionArguments()).thenReturn(Arrays.asList("test1")); cmd.run(ctxFactory.createContext(args)); String actual = ctxFactory.getOutput(); assertEquals("usage: thermostat usage of test command\n" + " description of test command\n" + "thermostat test1\n\n", actual); } @Test public void verifyHelpKnownCmdPrintsCommandUsageSorted() { CommandInfo helpInfo = mock(CommandInfo.class); when(helpInfo.getName()).thenReturn("help"); when(helpInfo.getDescription()).thenReturn("show help"); CommandInfo info1 = mock(CommandInfo.class); when(info1.getName()).thenReturn("test1"); when(info1.getDescription()).thenReturn("test command 1"); CommandInfo info2 = mock(CommandInfo.class); when(info2.getName()).thenReturn("test2"); when(info2.getDescription()).thenReturn("test command 2"); CommandInfo info3 = mock(CommandInfo.class); when(info3.getName()).thenReturn("test3"); when(info3.getDescription()).thenReturn("test command 3"); CommandInfo info4 = mock(CommandInfo.class); when(info4.getName()).thenReturn("test4"); when(info4.getDescription()).thenReturn("test command 4"); when(infos.getCommandInfos()).thenReturn(Arrays.asList(info2, helpInfo, info4, info3, info1)); HelpCommand cmd = new HelpCommand(); cmd.setCommandInfoSource(infos); 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" + " help show help\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 verifyHelpUnknownCmdPrintsSummaries() { when(infos.getCommandInfo("test1")).thenThrow(new CommandInfoNotFoundException("test1")); HelpCommand cmd = new HelpCommand(); cmd.setCommandInfoSource(infos); SimpleArguments args = new SimpleArguments(); args.addNonOptionArgument("test1"); cmd.run(ctxFactory.createContext(args)); String expected = "unknown command 'test1'\n" + "list of commands:\n\n"; String actual = ctxFactory.getOutput(); assertEquals(expected, actual); } }