From 6ca63e5aac717ef91781360b5c15ccde8abafd5c Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 8 Apr 2015 13:47:54 +0200 Subject: [PATCH] SONAR-5438 Add "Official Distribution" information to System Info --- .../platform/monitoring/SonarQubeMonitor.java | 12 +++++++ .../monitoring/SonarQubeMonitorTest.java | 35 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeMonitor.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeMonitor.java index 930a32c36b6..0f99dbac195 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeMonitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeMonitor.java @@ -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)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeMonitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeMonitorTest.java index cf19f567eba..30ecd2207a7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeMonitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeMonitorTest.java @@ -19,12 +19,16 @@ */ 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 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 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 attributes = monitor.attributes(); + assertThat(attributes).containsEntry("Official Distribution", Boolean.FALSE); + } } -- 2.39.5