Skip to content

Commit

Permalink
Merge pull request #50 from pehala/tests
Browse files Browse the repository at this point in the history
Add tests + small improvements to the CLI parsing
  • Loading branch information
PhilippvK authored Sep 6, 2020
2 parents 46f610f + f60348f commit 4e0e8c9
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 28 deletions.
27 changes: 26 additions & 1 deletion client/client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.5.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:3.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.5.10" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty:3.6.6.Final" level="project" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.1" level="project" />
<orderEntry type="module" module-name="shared" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.5.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:3.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.5.10" level="project" />
</component>
</module>
21 changes: 21 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
</dependency>
<dependency>
<groupId>org.moparforia</groupId>
<artifactId>shared</artifactId>
</dependency>

<!-- Test dependencies-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
56 changes: 39 additions & 17 deletions client/src/main/java/org/moparforia/client/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import picocli.CommandLine;

import org.moparforia.shared.ManifestVersionProvider;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.text.ParseException;
import java.util.concurrent.Callable;

/**
* Playforia
* 28.5.2013
*/

@CommandLine.Command(
description = "Starts Minigolf Client",
name = "client", mixinStandardHelpOptions = true
name = "client",
mixinStandardHelpOptions = true,
versionProvider = ManifestVersionProvider.class
)
public class Launcher implements Callable<Void> {

private static final String DEFAULT_SERVER = "127.0.0.1";
private static final int DEFAULT_PORT = 4242;
public static final String DEFAULT_SERVER = "127.0.0.1";
public static final int DEFAULT_PORT = 4242;

// CLI options
@CommandLine.Option(names = {"--hostname", "-ip"},
Expand All @@ -33,9 +34,8 @@ public class Launcher implements Callable<Void> {
private int port;

@CommandLine.Option(names = {"--lang", "-l"},
description = "Sets language of the game, available values: ${COMPLETION-CANDIDATES}",
defaultValue = "EN_US")
private Language lang;
description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}")
private Language lang = Language.EN_US;

@CommandLine.Option(names = {"--verbose", "-v"}, description = "Set if you want verbose information")
private static boolean verbose = false;
Expand All @@ -51,7 +51,9 @@ public static boolean isUsingCustomServer() {
public static void main(String... args) throws Exception {
Launcher launcher = new Launcher();
try {
CommandLine.ParseResult parseResult = new CommandLine(launcher).parseArgs(args);
CommandLine.ParseResult parseResult = new CommandLine(launcher)
.setCaseInsensitiveEnumValuesAllowed(true)
.parseArgs(args);
if (!CommandLine.printHelpIfRequested(parseResult)) {
launcher.call();
}
Expand All @@ -61,7 +63,7 @@ public static void main(String... args) throws Exception {
}
}

private boolean showSettingDialog(JFrame frame, String server, int port) throws ParseException {
public boolean showSettingDialog(JFrame frame, String server, int port) throws ParseException {
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(4, 1));

Expand Down Expand Up @@ -117,10 +119,7 @@ private String[] login(JFrame frame) {

@Override
public Void call() throws Exception{
JFrame frame = new JFrame();
frame.setTitle("Minigolf");
Image img = ImageIO.read(getClass().getResource("/icons/playforia.png"));
frame.setIconImage(img);
JFrame frame = createFrame();
if (hostname.isEmpty() || port == 0) {
// Determine which of these was actually false
String temp_hostname = hostname.isEmpty() ? DEFAULT_SERVER : hostname;
Expand All @@ -130,10 +129,33 @@ public Void call() throws Exception{
}
}

new Game(frame, hostname, port, lang.toString(), verbose);
launchGame(frame, hostname, port, lang, verbose);
return null;
}

public JFrame createFrame() throws IOException {
JFrame frame = new JFrame();
frame.setTitle("Minigolf");
Image img = loadIcon();
frame.setIconImage(img);
return frame;
}

public Game launchGame(JFrame frame, String hostname, int port, Language lang, boolean verbose) {
return new Game(frame, hostname, port, lang.toString(), verbose);
}

public Image loadIcon() throws IOException {
return ImageIO.read(getClass().getResource("/icons/playforia.png"));
}

public void setHostname(String hostname) {
this.hostname = hostname;
}

public void setPort(int port) {
this.port = port;
}

enum Language {
EN_US("en_US"),
Expand Down
126 changes: 126 additions & 0 deletions client/src/test/java/org/moparforia/client/LauncherCLITest.java
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 client/src/test/java/org/moparforia/client/ResourceLoadTest.java
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);
}

}
1 change: 0 additions & 1 deletion editor/editor.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
Expand Down
31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${project.mainClass}</mainClass>
<manifestEntries>
<Specification-Title>${project.artifactId}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
</manifestEntries>
</transformer>
</transformers>
</configuration>
Expand All @@ -74,6 +81,10 @@
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down Expand Up @@ -106,6 +117,26 @@
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>

<!-- Test dependencies-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.5.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Loading

0 comments on commit 4e0e8c9

Please sign in to comment.