Skip to content

Commit

Permalink
Add basic tests
Browse files Browse the repository at this point in the history
* Currently tests only CLI + icon loading.
  • Loading branch information
pehala committed Sep 6, 2020
1 parent 43ec325 commit f60348f
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 4 deletions.
24 changes: 24 additions & 0 deletions client/client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,35 @@
<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 @@ -29,6 +29,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
13 changes: 9 additions & 4 deletions client/src/main/java/org/moparforia/client/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ private String[] login(JFrame frame) {

@Override
public Void call() throws Exception{
JFrame frame = new JFrame();
frame.setTitle("Minigolf");
Image img = loadIcon();
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 @@ -136,6 +133,14 @@ public Void call() throws Exception{
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);
}
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);
}

}
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,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 @@ -113,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>
17 changes: 17 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@
<groupId>info.picocli</groupId>
<artifactId>picocli</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>
<plugins>
Expand Down
13 changes: 13 additions & 0 deletions server/server.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<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" />
Expand All @@ -17,5 +18,17 @@
<orderEntry type="library" name="Maven: com.thoughtworks.proxytoys:proxytoys:1.0" level="project" />
<orderEntry type="module" module-name="shared" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.1" level="project" />
<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="module" module-name="client" scope="TEST" />
</component>
</module>
Loading

0 comments on commit f60348f

Please sign in to comment.