Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
Add Mojo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradeep Thomas committed Oct 4, 2016
1 parent fd5168a commit e52a7f6
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/main/java/uk/gov/justice/plugin/StartServerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class StartServerMojo extends AbstractMojo {
*/
@Parameter(property = "serverClass", required = true)
protected String serverClass;

@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository local;

Expand Down Expand Up @@ -93,12 +93,12 @@ public void setServerClass(String serverClass) {
private Thread waitThread;

private File java;

@Override
public void execute() throws MojoExecutionException {
try {
java = new File(new File(System.getProperty("java.home"), "bin"), "java");

final List<String> args = new ArrayList<String>();
args.add(java.getAbsolutePath());
args.add("-cp");
Expand Down Expand Up @@ -146,7 +146,7 @@ private String buildClasspath() throws DependencyResolutionRequiredException {
return String.join(separator, runtimeLocs);
}

public void waitOnStopCommand(final Process process) throws IOException {
private void waitOnStopCommand(final Process process) throws IOException {
// !!! cannot write lambdas in plugins it fails
// java.lang.ArrayIndexOutOfBoundsException: 5377
waitThread = new Thread(new Runnable() {
Expand Down Expand Up @@ -210,5 +210,5 @@ public File getJava() {
public void setJava(File java) {
this.java = java;
}

}
53 changes: 52 additions & 1 deletion src/test/java/uk/gov/justice/plugin/StartServerMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.plugin;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -16,7 +17,6 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;


@RunWith(MockitoJUnitRunner.class)
public class StartServerMojoTest {

Expand Down Expand Up @@ -53,4 +53,55 @@ public void shouldExecuteMojo() throws Exception {

}

@Test
public void testGetProject() {
startServerMojo.setProject(project);
assertEquals(project, startServerMojo.getProject());
}

@Test
public void testSetProject() {
startServerMojo.setProject(project);
assertEquals(project, startServerMojo.getProject());
}

@Test
public void testGetPort() {
startServerMojo.setPort(9000);
assertEquals(9000, startServerMojo.getPort());
}

@Test
public void testSetPort() {
startServerMojo.setPort(9000);
assertEquals(9000, startServerMojo.getPort());
}

@Test
public void testGetServerClass() {
startServerMojo.setServerClass(this.getClass().getName());
assertEquals(this.getClass().getName(), startServerMojo.getServerClass());
}

@Test
public void testSetServerClass() {
startServerMojo.setServerClass(this.getClass().getName());
assertEquals(this.getClass().getName(), startServerMojo.getServerClass());
}


@Test
public void testGetJava() {
File f = mock(File.class);
startServerMojo.setJava(f);
assertEquals(f, startServerMojo.getJava());
}

@Test
public void testSetJava() {
File f = mock(File.class);
startServerMojo.setJava(f);
assertEquals(f, startServerMojo.getJava());
}

}
71 changes: 71 additions & 0 deletions src/test/java/uk/gov/justice/plugin/StopServerMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package uk.gov.justice.plugin;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class StopServerMojoTest {

@InjectMocks
StopServerMojo stopServerMojo;

@Test
public void shouldGetProject() {
MavenProject mp = mock(MavenProject.class);
stopServerMojo.setProject(mp);
assertEquals(mp, stopServerMojo.getProject());
}

@Test
public void shouldSetProject() {
MavenProject mp = mock(MavenProject.class);
stopServerMojo.setProject(mp);
assertEquals(mp, stopServerMojo.getProject());
}

@Test
public void shouldGetPort() {
stopServerMojo.setPort(8000);
assertEquals(8000, stopServerMojo.getPort());
}

@Test
public void shouldSetPort() {
stopServerMojo.setPort(8000);
assertEquals(8000, stopServerMojo.getPort());
}

@Test
public void shouldExecuteMojo() throws IOException {
try (final ServerSocket socket = new ServerSocket(0)) {
int port = socket.getLocalPort();
stopServerMojo.setPort(port);
stopServerMojo.execute();
} catch (MojoExecutionException e) {
fail(e.getMessage());
}
}

@Test(expected = RuntimeException.class)
public void shouldThrowRuntimeException() throws MojoExecutionException, IOException {
try (final ServerSocket socket = new ServerSocket(0)) {
int port = socket.getLocalPort();
stopServerMojo.setPort(port - 1);
stopServerMojo.execute();
}
}

}

0 comments on commit e52a7f6

Please sign in to comment.