diff --git a/pom.xml b/pom.xml index a0f80cf..1fd29e9 100644 --- a/pom.xml +++ b/pom.xml @@ -16,13 +16,15 @@ UTF-8 - 3.8.1 + 4.12 3.2.2 3.5 1.8 3.5.1 3.2.1 3.2 + 1.10.19 + @@ -102,6 +104,12 @@ + + org.mockito + mockito-core + ${mockito.version} + test + diff --git a/src/main/java/uk/gov/justice/plugin/StartServerMojo.java b/src/main/java/uk/gov/justice/plugin/StartServerMojo.java index ff7a665..278c778 100644 --- a/src/main/java/uk/gov/justice/plugin/StartServerMojo.java +++ b/src/main/java/uk/gov/justice/plugin/StartServerMojo.java @@ -92,10 +92,12 @@ 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 args = new ArrayList(); args.add(java.getAbsolutePath()); @@ -103,11 +105,11 @@ public void execute() throws MojoExecutionException { 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); @@ -166,7 +168,6 @@ public void run() { } private void addShutdownHook(final Process process) { - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { try { @@ -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(); } @@ -201,4 +202,13 @@ public void run() { } }).start(); } + + public File getJava() { + return java; + } + + public void setJava(File java) { + this.java = java; + } + } diff --git a/src/main/java/uk/gov/justice/plugin/StopServerMojo.java b/src/main/java/uk/gov/justice/plugin/StopServerMojo.java index 9c27170..4b669b3 100644 --- a/src/main/java/uk/gov/justice/plugin/StopServerMojo.java +++ b/src/main/java/uk/gov/justice/plugin/StopServerMojo.java @@ -20,6 +20,7 @@ public class StopServerMojo extends AbstractMojo { @Parameter(defaultValue = "${project}", readonly = true) protected MavenProject project; + public MavenProject getProject() { return project; } @@ -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())) { diff --git a/src/test/java/uk/gov/justice/plugin/StartServerMojoTest.java b/src/test/java/uk/gov/justice/plugin/StartServerMojoTest.java new file mode 100644 index 0000000..3439319 --- /dev/null +++ b/src/test/java/uk/gov/justice/plugin/StartServerMojoTest.java @@ -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()); + + startServerMojo.execute(); + + } + +}