diff --git a/client/src/main/java/org/moparforia/client/Launcher.java b/client/src/main/java/org/moparforia/client/Launcher.java index ddee877c..17d89a52 100644 --- a/client/src/main/java/org/moparforia/client/Launcher.java +++ b/client/src/main/java/org/moparforia/client/Launcher.java @@ -34,8 +34,9 @@ public class Launcher implements Callable { private int port; @CommandLine.Option(names = {"--lang", "-l"}, - description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}") - private Language lang = Language.EN_US; + description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}", + defaultValue = "en_us") + private Language lang; @CommandLine.Option(names = {"--verbose", "-v"}, description = "Set if you want verbose information") private static boolean verbose = false; @@ -48,19 +49,11 @@ public static boolean isUsingCustomServer() { return true;//instance.serverBox.isSelected(); } - public static void main(String... args) throws Exception { + public static void main(String... args) { Launcher launcher = new Launcher(); - try { - CommandLine.ParseResult parseResult = new CommandLine(launcher) - .setCaseInsensitiveEnumValuesAllowed(true) - .parseArgs(args); - if (!CommandLine.printHelpIfRequested(parseResult)) { - launcher.call(); - } - } catch (CommandLine.ParameterException ex) { // command line arguments could not be parsed - System.err.println(ex.getMessage()); - ex.getCommandLine().usage(System.err); - } + new CommandLine(launcher) + .setCaseInsensitiveEnumValuesAllowed(true) + .execute(args); } public boolean showSettingDialog(JFrame frame, String server, int port) throws ParseException { diff --git a/client/src/test/java/org/moparforia/client/LauncherCLITest.java b/client/src/test/java/org/moparforia/client/LauncherCLITest.java index 79aad260..db73192a 100644 --- a/client/src/test/java/org/moparforia/client/LauncherCLITest.java +++ b/client/src/test/java/org/moparforia/client/LauncherCLITest.java @@ -31,21 +31,25 @@ class LauncherCLITest { private StringWriter stdErr; @BeforeEach - void setUp() throws ParseException, IOException { + void setUp() throws Exception { // Mock game - launcher = spy(new Launcher()); - lenient().doReturn(mock(Game.class)).when(launcher).launchGame(any(JFrame.class), anyString(), anyInt(), any(), anyBoolean()); + launcher = mock(Launcher.class, withSettings() + .lenient() + .withoutAnnotations()); - // Mock creating JFrame - lenient().doReturn(mock(JFrame.class)).when(launcher).createFrame(); + // Use real methods + doCallRealMethod().when(launcher).call(); + doCallRealMethod().when(launcher).setPort(anyInt()); + doCallRealMethod().when(launcher).setHostname(anyString()); - // Mock settings dialog - lenient().doAnswer((invocaton) -> { + doReturn(mock(JFrame.class)).when(launcher).createFrame(); + doAnswer((invocaton) -> { launcher.setPort(invocaton.getArgument(2)); launcher.setHostname(invocaton.getArgument(1)); return true; }).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt()); + cmd = new CommandLine(launcher).setCaseInsensitiveEnumValuesAllowed(true); stdOut = new StringWriter(); @@ -117,10 +121,12 @@ void testOnlyHostname() { @Test void testDefaultValues() { assertEquals(0, cmd.execute()); - verify(launcher).launchGame(any(), + verify(launcher).launchGame( + any(), eq(Launcher.DEFAULT_SERVER), eq(Launcher.DEFAULT_PORT), eq(Launcher.Language.EN_US), - eq(false)); + eq(false) + ); } } \ No newline at end of file diff --git a/server/src/main/java/org/moparforia/server/Launcher.java b/server/src/main/java/org/moparforia/server/Launcher.java index 45247727..4fdbf2bc 100644 --- a/server/src/main/java/org/moparforia/server/Launcher.java +++ b/server/src/main/java/org/moparforia/server/Launcher.java @@ -14,18 +14,24 @@ Converter.class } ) -public class Launcher implements Callable { +public class Launcher implements Callable { public static final String DEFAULT_HOST = "0.0.0.0"; - public static final int DEFAULT_PORT = 4242; - - @CommandLine.Option(names = {"--hostname", "-ip"}, - description = "Sets server hostname") - private String host = DEFAULT_HOST; - - @CommandLine.Option(names = {"--port", "-p"}, - description = "Sets server port") - private int port = DEFAULT_PORT; + public static final String DEFAULT_PORT = "4242"; + + @CommandLine.Option( + names = {"--hostname", "-ip"}, + description = "Sets server hostname", + defaultValue = DEFAULT_HOST + ) + private String host; + + @CommandLine.Option( + names = {"--port", "-p"}, + description = "Sets server port", + defaultValue = DEFAULT_PORT + ) + private int port; public static void main(String... args) { Launcher launcher = new Launcher(); @@ -35,9 +41,9 @@ public static void main(String... args) { } @Override - public Void call() { + public Integer call() { getServer(host, port).start(); - return null; + return 0; } public Server getServer(String host, int port) { diff --git a/server/src/test/java/org/moparforia/server/LauncherCLITest.java b/server/src/test/java/org/moparforia/server/LauncherCLITest.java index 6516c103..848c6cf3 100644 --- a/server/src/test/java/org/moparforia/server/LauncherCLITest.java +++ b/server/src/test/java/org/moparforia/server/LauncherCLITest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.jupiter.MockitoExtension; import picocli.CommandLine; @@ -20,6 +21,7 @@ */ @ExtendWith(MockitoExtension.class) class LauncherCLITest { + private static final int DEFAULT_PORT = Integer.parseInt(Launcher.DEFAULT_PORT); private Launcher launcher; private CommandLine cmd; @@ -29,8 +31,12 @@ class LauncherCLITest { @BeforeEach void setUp() { // Mock Launcher instance - launcher = spy(new Launcher()); - lenient().doReturn(mock(Server.class)).when(launcher).getServer(anyString(), anyInt()); + launcher = mock(Launcher.class, withSettings() + .lenient() + .withoutAnnotations()); + + doReturn(mock(Server.class)).when(launcher).getServer(anyString(), anyInt()); + when(launcher.call()).thenCallRealMethod(); cmd = new CommandLine(launcher); cmd.setCaseInsensitiveEnumValuesAllowed(true); @@ -77,12 +83,12 @@ void testOnlyPort() { @Test void testOnlyHostname() { assertEquals(0, cmd.execute("-ip", "127.127.127.127")); - verify(launcher).getServer(eq("127.127.127.127"), eq(Launcher.DEFAULT_PORT)); + verify(launcher).getServer(eq("127.127.127.127"), eq(DEFAULT_PORT)); } @Test void testDefaultValues() { assertEquals(0, cmd.execute()); - verify(launcher).getServer(eq(Launcher.DEFAULT_HOST), eq(Launcher.DEFAULT_PORT)); + verify(launcher).getServer(eq(Launcher.DEFAULT_HOST), eq(DEFAULT_PORT)); } } \ No newline at end of file