DefaultCommandManager implements CommandManager { @Override public ManagedCommand lookupByType(Class<? extends Command> commandType) { ManagedCommand match = commandsByType().get(commandType); if (match == null) { throw new IllegalArgumentException("Unknown command type: " + commandType.getName()); } return match; } DefaultCommandManager(Map<String, ManagedCommand> commands); @Override Map<String, ManagedCommand> getAllCommands(); @Override ManagedCommand lookupByType(Class<? extends Command> commandType);  }
@Test public void testLookupByType_Missing() { Map<String, ManagedCommand> map = new HashMap<>(); map.put("x", ManagedCommand.builder(c1).build()); DefaultCommandManager cm = new DefaultCommandManager(map); assertThrows(IllegalArgumentException.class, () -> cm.lookupByType(C2.class)); }
@Test public void testLookupByType() { Map<String, ManagedCommand> map = new HashMap<>(); map.put("x", ManagedCommand.forCommand(c1)); map.put("y", ManagedCommand.forCommand(c2)); map.put("z", ManagedCommand.builder(c3).asDefault().build()); DefaultCommandManager cm = new DefaultCommandManager(map); assertSame(c1, cm.lookupByType(C1.class).getCommand()); assertSame(c2, cm.lookupByType(C2.class).getCommand()); assertSame(c3, cm.lookupByType(C3.class).getCommand(), "No default command included in lookup"); }