]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5438 Add "Official Distribution" information to System Info
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 8 Apr 2015 11:47:54 +0000 (13:47 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 8 Apr 2015 12:45:03 +0000 (14:45 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeMonitor.java
server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeMonitorTest.java

index 930a32c36b63d6b71c810aad288c17b6525a7563..0f99dbac1955e24f7856ea12c16c081e356d8c0d 100644 (file)
@@ -27,10 +27,13 @@ import org.sonar.api.security.SecurityRealm;
 import org.sonar.process.ProcessProperties;
 import org.sonar.server.user.SecurityRealmFactory;
 
+import java.io.File;
 import java.util.LinkedHashMap;
 
 public class SonarQubeMonitor extends BaseMonitorMBean implements SonarQubeMonitorMBean {
 
+  static final String BRANDING_FILE_PATH = "web/WEB-INF/classes/com/sonarsource/branding";
+
   private final Settings settings;
   private final SecurityRealmFactory securityRealmFactory;
   private final Server server;
@@ -72,6 +75,14 @@ public class SonarQubeMonitor extends BaseMonitorMBean implements SonarQubeMonit
     return settings.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY);
   }
 
+  public boolean isOfficialDistribution() {
+    // the dependency com.sonarsource:sonarsource-branding is shaded to webapp
+    // during release (see sonar-web pom)
+    File brandingFile = new File(server.getRootDir(), BRANDING_FILE_PATH);
+    // no need to check that the file exists. java.io.File#length() returns zero in this case.
+    return brandingFile.length() > 0L;
+  }
+
   @Override
   public String name() {
     return "SonarQube";
@@ -86,6 +97,7 @@ public class SonarQubeMonitor extends BaseMonitorMBean implements SonarQubeMonit
     attributes.put("Automatic User Creation", getAutomaticUserCreation());
     attributes.put("Allow Users to Sign Up", getAllowUsersToSignUp());
     attributes.put("Force authentication", getForceAuthentication());
+    attributes.put("Official Distribution", isOfficialDistribution());
     attributes.put("Home Dir", settings.getString(ProcessProperties.PATH_HOME));
     attributes.put("Data Dir", settings.getString(ProcessProperties.PATH_DATA));
     attributes.put("Logs Dir", settings.getString(ProcessProperties.PATH_LOGS));
index cf19f567eba59770400014f852cb22a2259ca7ae..30ecd2207a725d7ccd75d9ba7d12b0db735e400e 100644 (file)
  */
 package org.sonar.server.platform.monitoring;
 
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.sonar.api.config.Settings;
 import org.sonar.api.platform.Server;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.server.user.SecurityRealmFactory;
 
+import java.io.File;
 import java.util.LinkedHashMap;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -33,14 +37,41 @@ import static org.mockito.Mockito.when;
 
 public class SonarQubeMonitorTest {
 
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  Settings settings = new Settings();
+  Server server = mock(Server.class);
+
   @Test
   public void getServerId() throws Exception {
-    Settings settings = new Settings();
-    Server server = mock(Server.class);
     when(server.getStartedAt()).thenReturn(DateUtils.parseDate("2015-01-01"));
     SonarQubeMonitor monitor = new SonarQubeMonitor(settings, new SecurityRealmFactory(settings), server);
 
     LinkedHashMap<String, Object> attributes = monitor.attributes();
     assertThat(attributes).containsKeys("Server ID", "Version");
   }
+
+  @Test
+  public void official_distribution() throws Exception {
+    File rootDir = temp.newFolder();
+    FileUtils.write(new File(rootDir, SonarQubeMonitor.BRANDING_FILE_PATH), "1.2");
+
+    when(server.getRootDir()).thenReturn(rootDir);
+    SonarQubeMonitor monitor = new SonarQubeMonitor(settings, new SecurityRealmFactory(settings), server);
+
+    LinkedHashMap<String, Object> attributes = monitor.attributes();
+    assertThat(attributes).containsEntry("Official Distribution", Boolean.TRUE);
+  }
+
+  @Test
+  public void not_an_official_distribution() throws Exception {
+    File rootDir = temp.newFolder();
+    // branding file is missing
+    when(server.getRootDir()).thenReturn(rootDir);
+    SonarQubeMonitor monitor = new SonarQubeMonitor(settings, new SecurityRealmFactory(settings), server);
+
+    LinkedHashMap<String, Object> attributes = monitor.attributes();
+    assertThat(attributes).containsEntry("Official Distribution", Boolean.FALSE);
+  }
 }