forked from WorldStarHipHopX/playforia
-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from pehala/tests
Add tests + small improvements to the CLI parsing
- Loading branch information
Showing
14 changed files
with
417 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
client/src/test/java/org/moparforia/client/LauncherCLITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.moparforia.client; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import picocli.CommandLine; | ||
|
||
import javax.swing.*; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.text.ParseException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Tests that CLI parsing works as expected, it doesn't test the main method, but it tests the picocli annotations | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class LauncherCLITest { | ||
|
||
private Launcher launcher; | ||
|
||
private CommandLine cmd; | ||
private StringWriter stdOut; | ||
private StringWriter stdErr; | ||
|
||
@BeforeEach | ||
void setUp() throws ParseException, IOException { | ||
// Mock game | ||
launcher = spy(new Launcher()); | ||
lenient().doReturn(mock(Game.class)).when(launcher).launchGame(any(JFrame.class), anyString(), anyInt(), any(), anyBoolean()); | ||
|
||
// Mock creating JFrame | ||
lenient().doReturn(mock(JFrame.class)).when(launcher).createFrame(); | ||
|
||
// Mock settings dialog | ||
lenient().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(); | ||
stdErr = new StringWriter(); | ||
|
||
cmd.setOut(new PrintWriter(stdOut)); | ||
cmd.setErr(new PrintWriter(stdErr)); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
clearInvocations(launcher); | ||
} | ||
|
||
@Test | ||
void testInvalidPort() { | ||
assertNotEquals(0, cmd.execute("-p", "test")); | ||
assertNotEquals(0, cmd.execute("--port=test")); | ||
assertNotEquals(0, cmd.execute("-p")); | ||
} | ||
|
||
@Test | ||
void testInvalidLang() { | ||
assertNotEquals(0, cmd.execute("-l", "cs_CZ")); | ||
assertNotEquals(0, cmd.execute("-l", "en")); | ||
} | ||
|
||
@Test | ||
void testValidLang() { | ||
assertEquals(0, cmd.execute("-l", "en_US")); | ||
verify(launcher).launchGame(any(), | ||
eq(Launcher.DEFAULT_SERVER), | ||
eq(Launcher.DEFAULT_PORT), | ||
eq(Launcher.Language.EN_US), | ||
anyBoolean()); | ||
|
||
assertEquals(0, cmd.execute("--lang=Fi_fI")); | ||
verify(launcher).launchGame(any(), | ||
eq(Launcher.DEFAULT_SERVER), | ||
eq(Launcher.DEFAULT_PORT), | ||
eq(Launcher.Language.FI_FI), | ||
anyBoolean()); | ||
} | ||
|
||
@Test | ||
void testValidPortAndHostname() { | ||
assertEquals(0, cmd.execute("-p", "1111", "-ip", "128.128.128.128")); | ||
verify(launcher).launchGame(any(), eq("128.128.128.128"), eq(1111), any(), anyBoolean()); | ||
|
||
assertEquals(0, cmd.execute("-p=2222", "-ip=127.127.127.127")); | ||
verify(launcher).launchGame(any(), eq("127.127.127.127"), eq(2222), any(), anyBoolean()); | ||
|
||
assertEquals(0, cmd.execute("-p=3333", "-ip=126.126.126.126")); | ||
verify(launcher).launchGame(any(), eq("126.126.126.126"), eq(3333), any(), anyBoolean()); | ||
} | ||
|
||
@Test | ||
void testOnlyPort() { | ||
assertEquals(0, cmd.execute("-p", "1111")); | ||
verify(launcher).launchGame(any(), eq(Launcher.DEFAULT_SERVER), eq(1111), any(), anyBoolean()); | ||
} | ||
|
||
@Test | ||
void testOnlyHostname() { | ||
assertEquals(0, cmd.execute("-ip", "127.127.127.127")); | ||
verify(launcher).launchGame(any(), eq("127.127.127.127"), eq(Launcher.DEFAULT_PORT), any(), anyBoolean()); | ||
} | ||
|
||
@Test | ||
void testDefaultValues() { | ||
assertEquals(0, cmd.execute()); | ||
verify(launcher).launchGame(any(), | ||
eq(Launcher.DEFAULT_SERVER), | ||
eq(Launcher.DEFAULT_PORT), | ||
eq(Launcher.Language.EN_US), | ||
eq(false)); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
client/src/test/java/org/moparforia/client/ResourceLoadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.moparforia.client; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
||
|
||
/** | ||
* Tests that resources can be loaded | ||
*/ | ||
class ResourceLoadTest { | ||
|
||
/** | ||
* Tests that Launcher icon can be loaded | ||
*/ | ||
@Test | ||
void testLoadIcon() { | ||
Launcher launcher = new Launcher(); | ||
assertDoesNotThrow(launcher::loadIcon); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.