Fix output of stats command for missing joined or forced channels.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Oct 2014 13:27:02 +0000 (15:27 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Oct 2014 13:27:02 +0000 (15:27 +0200)
src/main/java/net/pterodactylus/xdcc/ui/stdin/StatsCommand.java
src/test/java/net/pterodactylus/xdcc/ui/stdin/StatsCommandTest.java

index 07f2288..d40cc82 100644 (file)
@@ -65,16 +65,35 @@ public class StatsCommand implements Command {
                return null;
        }
 
-       private void dumpNetworkStats(Writer outputWriter, ConnectedNetwork network) throws IOException {
-               outputWriter.write(format("Connected to %s via %s:%d.\n", network.getNetwork().name(), network.getHostname(), network.getPort(), network.getNickname()));
+       private void dumpNetworkStats(Writer outputWriter,
+                       ConnectedNetwork network) throws IOException {
+               outputWriter.write(format("Connected to %s via %s:%d.\n",
+                               network.getNetwork().name(), network.getHostname(),
+                               network.getPort(), network.getNickname()));
+               String joinedChannels = joinChannelNames(network.getChannels());
+               String forcedChannels = joinChannelNames(network.getForcedChannels());
+               if (forcedChannels.isEmpty()) {
+                       if (!joinedChannels.isEmpty()) {
+                               outputWriter.write(format("  Joined %s.\n", joinedChannels));
+                       }
+               } else {
+                       if (joinedChannels.isEmpty()) {
+                               outputWriter.write(
+                                               format("  Force-joined %s.\n", forcedChannels));
+                       } else {
+                               outputWriter.write(format("  Joined %s, force-joined %s.\n",
+                                               joinedChannels, forcedChannels));
+                       }
+               }
+               outputWriter.write(
+                               format("  %d bots serving %d packs.\n", network.getBotCount(),
+                                               network.getPackCount()));
+       }
+
+       private String joinChannelNames(Collection<String> channels) {
                StringJoiner joinedChannels = new StringJoiner(", ");
-               network.getChannels().stream().forEach(
-                               (channel) -> joinedChannels.add(channel));
-               StringJoiner forceJoinedChannels = new StringJoiner(", ");
-               network.getForcedChannels().stream().forEach((channel) -> forceJoinedChannels.add(channel));
-               outputWriter.write(format("  Joined %s, force-joined %s.\n",
-                               joinedChannels, forceJoinedChannels));
-               outputWriter.write(format("  %d bots serving %d packs.\n", network.getBotCount(), network.getPackCount()));
+               channels.stream().forEach((channel) -> joinedChannels.add(channel));
+               return joinedChannels.toString();
        }
 
 }
index 338fe27..c1f0abc 100644 (file)
@@ -36,7 +36,7 @@ public class StatsCommandTest {
                                6667,
                                "Nickname1",
                                asList("#channel1", "#channel2"),
-                               asList("#forced"),
+                               emptyList(),
                                17,
                                445
                );
@@ -50,7 +50,27 @@ public class StatsCommandTest {
                                65,
                                234
                );
-               when(core.connectedNetworks()).thenReturn(asList(network1, network2));
+               ConnectedNetwork network3 = new ConnectedNetwork(
+                               builder("FooNet").build(),
+                               "irc.foo.net",
+                               7000,
+                               "Nickname3",
+                               emptyList(),
+                               emptyList(),
+                               118,
+                               1234
+               );
+               ConnectedNetwork network4 = new ConnectedNetwork(
+                               builder("BarNet").build(),
+                               "irc.bar.net",
+                               7001,
+                               "Nickname4",
+                               emptyList(),
+                               asList("#foo", "#bar"),
+                               4,
+                               21
+               );
+               when(core.connectedNetworks()).thenReturn(asList(network1, network2, network3, network4));
        }
 
        @Test
@@ -60,12 +80,17 @@ public class StatsCommandTest {
                String result = writer.toString();
                assertThat(result, is(
                                "Connected to TestNet via irc.test.net:6667.\n" +
-                               "  Joined #channel1, #channel2, force-joined #forced.\n" +
+                               "  Joined #channel1, #channel2.\n" +
                                "  17 bots serving 445 packs.\n" +
                                "Connected to DummyNet via irc.dummy.net:6789.\n" +
                                "  Joined #dummy1, #dummy2, force-joined #foo, #bar.\n" +
                                "  65 bots serving 234 packs.\n" +
-                               "Total: 82 bots, 679 packs.\n"
+                               "Connected to FooNet via irc.foo.net:7000.\n" +
+                               "  118 bots serving 1234 packs.\n" +
+                               "Connected to BarNet via irc.bar.net:7001.\n" +
+                               "  Force-joined #foo, #bar.\n" +
+                               "  4 bots serving 21 packs.\n" +
+                               "Total: 204 bots, 1934 packs.\n"
                ));
        }