]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5408 - FIxed test for SONAR_HOME in process
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 08:58:43 +0000 (10:58 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 09:28:00 +0000 (11:28 +0200)
server/sonar-process/src/main/java/org/sonar/process/Process.java
server/sonar-process/src/test/java/org/sonar/process/ProcessTest.java

index 44d7b4f028a626d98278f037e87e6a3e2f464382..df605f8d172a81717bc84ed3d0c89a2faaa56c43 100644 (file)
@@ -44,6 +44,9 @@ public abstract class Process implements ProcessMXBean {
   public static final String PORT_PROPERTY = "pPort";
 
   public static final String MISSING_NAME_ARGUMENT = "Missing Name argument";
+  public static final String SONAR_HOME_IS_NOT_SET = "variable SONAR_HOME is not set.";
+  public static final String SONAR_HOME_DOES_NOT_EXIST = "Directory SONAR_HOME does not exist";
+  public static final String SONAR_HOME_IS_NOT_WRITABLE = "Directory SONAR_HOME is not writable";
 
 
   private final static Logger LOGGER = LoggerFactory.getLogger(Process.class);
@@ -164,18 +167,18 @@ public abstract class Process implements ProcessMXBean {
     // check that we have a SONAR_HOME either in props or in env.
     String sonarHome = props.of(SONAR_HOME, System.getenv(SONAR_HOME));
     if (StringUtils.isEmpty(sonarHome)) {
-      throw new IllegalStateException("variable SONAR_HOME is not set.");
+      throw new IllegalStateException(SONAR_HOME_IS_NOT_SET);
     }
 
     // check that SONAR_HOME exists
     File home = new File(sonarHome);
     if(!home.exists()) {
-      throw new IllegalStateException("Directory SONAR_HOME '" + sonarHome + "' is not set");
+      throw new IllegalStateException(SONAR_HOME_DOES_NOT_EXIST);
     }
 
     // check that SONAR_HOME is writable
     if (!home.canWrite()) {
-      throw new IllegalStateException("Directory SONAR_HOME '" + sonarHome + "' is not writable");
+      throw new IllegalStateException(SONAR_HOME_IS_NOT_WRITABLE);
     }
   }
 }
\ No newline at end of file
index d7b390654d4264f6fb2d3810f07e79d52cebfa8b..0a0bb26337dbe72d979a2944afd094abd80375ba 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.process;
 
+import org.apache.commons.io.FileUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -67,6 +68,22 @@ public class ProcessTest {
   @Test
   public void fail_missing_properties() {
     Properties properties = new Properties();
+    try {
+      new TestProcess(Props.create(properties));
+    } catch (Exception e) {
+      assertThat(e.getMessage()).isEqualTo(Process.SONAR_HOME_IS_NOT_SET);
+    }
+
+    properties = new Properties();
+    properties.setProperty(Process.SONAR_HOME, "lahdslahdslf");
+    try {
+      new TestProcess(Props.create(properties));
+    } catch (Exception e) {
+      assertThat(e.getMessage()).isEqualTo(Process.SONAR_HOME_DOES_NOT_EXIST);
+    }
+
+    properties = new Properties();
+    properties.setProperty(Process.SONAR_HOME, FileUtils.getTempDirectoryPath());
     try {
       new TestProcess(Props.create(properties));
     } catch (Exception e) {
@@ -80,6 +97,7 @@ public class ProcessTest {
     MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
 
     Properties properties = new Properties();
+    properties.setProperty(Process.SONAR_HOME, FileUtils.getTempDirectoryPath());
     properties.setProperty(Process.NAME_PROPERTY, "TEST");
     Props props = Props.create(properties);
     process = new TestProcess(props);
@@ -102,6 +120,7 @@ public class ProcessTest {
   @Test(timeout = 5000L)
   public void should_stop_explicit() throws Exception {
     Properties properties = new Properties();
+    properties.setProperty(Process.SONAR_HOME, FileUtils.getTempDirectoryPath());
     properties.setProperty(Process.NAME_PROPERTY, "TEST");
     Props props = Props.create(properties);
     process = new TestProcess(props);
@@ -133,6 +152,7 @@ public class ProcessTest {
   @Test(timeout = 15000L)
   public void should_stop_implicit() throws Exception {
     Properties properties = new Properties();
+    properties.setProperty(Process.SONAR_HOME, FileUtils.getTempDirectoryPath());
     properties.setProperty(Process.NAME_PROPERTY, "TEST");
     properties.setProperty(Process.PORT_PROPERTY, Integer.toString(freePort));
     Props props = Props.create(properties);