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;
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";
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));
*/
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;
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);
+ }
}