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

Commit

Permalink
Add Mojo Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradeep Thomas committed Oct 4, 2016
1 parent 70346d7 commit c1d6563
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- change the versions at your own peril -->
<junit.version>3.8.1</junit.version>
<junit.version>4.12</junit.version>
<maven.version>3.2.2</maven.version>
<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
<java.version>1.8</java.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-compat.version>3.2.1</maven-compat.version>
<maven-plugin-plugin.version>3.2</maven-plugin-plugin.version>
<mockito.version>1.10.19</mockito.version>

</properties>

<scm>
Expand Down Expand Up @@ -102,6 +104,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/uk/gov/justice/plugin/StartServerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,24 @@ public void setServerClass(String serverClass) {

private Thread waitThread;

private File java;

@Override
public void execute() throws MojoExecutionException {
try {
final File java = new File(new File(System.getProperty("java.home"), "bin"), "java");
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");
args.add(buildClasspath());
args.add(getServerClass());

final Process p = new ProcessBuilder(args).start();
dumpStream(p.getInputStream(), System.out);
dumpStream(p.getErrorStream(), System.err);
addShutdownHook(p);
waitOnStopCommand(p);
final Process process = new ProcessBuilder(args).start();
dumpStream(process.getInputStream(), System.out);
dumpStream(process.getErrorStream(), System.err);
addShutdownHook(process);
waitOnStopCommand(process);

} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -166,7 +168,6 @@ public void run() {
}

private void addShutdownHook(final Process process) {

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
try {
Expand All @@ -178,13 +179,13 @@ public void run() {
}));
}

private void destroyIfAlive(final Process process) {
private synchronized void destroyIfAlive(final Process process) {
if (!Objects.isNull(process) && process.isAlive()) {
process.destroy();
}
}

private void destroyForciblyIfAlive(final Process process) {
private synchronized void destroyForciblyIfAlive(final Process process) {
if (!Objects.isNull(process) && process.isAlive()) {
process.destroyForcibly();
}
Expand All @@ -201,4 +202,13 @@ public void run() {
}
}).start();
}

public File getJava() {
return java;
}

public void setJava(File java) {
this.java = java;
}

}
2 changes: 2 additions & 0 deletions src/main/java/uk/gov/justice/plugin/StopServerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class StopServerMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true)
protected MavenProject project;


public MavenProject getProject() {
return project;
}
Expand All @@ -36,6 +37,7 @@ public void setPort(int port) {
this.port = port;
}


@Override
public void execute() throws MojoExecutionException {
try (final Socket clientSocket = new Socket("localhost", getPort())) {
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/uk/gov/justice/plugin/StartServerMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package uk.gov.justice.plugin;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;


@RunWith(MockitoJUnitRunner.class)
public class StartServerMojoTest {

@Mock
MavenProject project;

@Mock
File targetOutputDirectory;

@Mock
File targetTestOutputDirectory;

@Mock
PluginDescriptor plugin;

@InjectMocks
StartServerMojo startServerMojo;

@Test
public void shouldExecuteMojo() throws Exception {

when(project.getRuntimeClasspathElements()).thenReturn(new ArrayList<>());

when(targetOutputDirectory.getAbsolutePath()).thenReturn("");

when(targetTestOutputDirectory.getAbsolutePath()).thenReturn("");

startServerMojo.setServerClass(
mock(StartServerMojoTest.class).getClass().getCanonicalName());

when(plugin.getArtifactMap()).thenReturn(new HashMap<String, Artifact>());

startServerMojo.execute();

}

}

0 comments on commit c1d6563

Please sign in to comment.