CommandDispatcher { public int dispatchCommand(Object pVm,VirtualMachineHandler pHandler) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { String commandName = options.getCommand(); AbstractBaseCommand command = COMMANDS.get(commandName); if (command == null) { throw new IllegalArgumentException("Unknown command '" + commandName + "'"); } return command.execute(options,pVm,pHandler); } CommandDispatcher(OptionsAndArgs pOptions); int dispatchCommand(Object pVm,VirtualMachineHandler pHandler); static Set<String> getAvailableCommands(); static void printHelp();  }
@Test public void version() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { CommandDispatcher d = new CommandDispatcher(opts("--version")); assertEquals(d.dispatchCommand(null, null), 0); assertTrue(outStream.toString().contains(Version.getAgentVersion())); assertFalse(outStream.toString().contains(Version.getProtocolVersion())); }
@Test public void versionVerbose() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { CommandDispatcher d = new CommandDispatcher(opts("--version","--verbose")); assertEquals(d.dispatchCommand(null, null), 0); assertTrue(outStream.toString().contains(Version.getAgentVersion())); assertTrue(outStream.toString().contains(Version.getProtocolVersion())); }
@Test public void list() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { CommandDispatcher d = new CommandDispatcher(opts("list")); VirtualMachineHandler vmh = createMock(VirtualMachineHandler.class); List<ProcessDescription> ret = new ArrayList<ProcessDescription>(); ret.add(new ProcessDescription("12","TestProcess")); expect(vmh.listProcesses()).andReturn(ret); replay(vmh); assertEquals(d.dispatchCommand(null, vmh), 0); verify(vmh); }
@Test public void descriptionWithPattern() throws AgentInitializationException, InvocationTargetException, IOException, NoSuchMethodException, AgentLoadException, IllegalAccessException { CommandDispatcher d = new CommandDispatcher(opts("start","blub")); VirtualMachineHandler vmh = createMock(VirtualMachineHandler.class); VirtualMachine vm = createMock(VirtualMachine.class); expect(vm.getSystemProperties()).andReturn(getProperties(false)); expect(vm.getSystemProperties()).andReturn(getProperties(true)); vm.loadAgent(EasyMock.<String>anyObject(), EasyMock.<String>anyObject()); expect(vmh.findProcess(patternMatcher("blub"))).andReturn(new ProcessDescription("18", "bla blub blie")); replay(vm, vmh); int rc = d.dispatchCommand(vm, vmh); assertEquals(rc, 0); verify(vm, vmh); }
@Test(expectedExceptions = InvocationTargetException.class) public void throwException() throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException { CommandDispatcher d = new CommandDispatcher(opts("start", "42")); VirtualMachine vm = createMock(VirtualMachine.class); expect(vm.getSystemProperties()).andThrow(new IOException()); replay(vm); d.dispatchCommand(vm,null); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*Unknown.*") public void unknownCommand() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { CommandDispatcher d = new CommandDispatcher(opts("blub", "42")); d.dispatchCommand(null,null); }