diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-10-05 09:53:10 +0200 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-10-05 12:19:36 +0200 |
commit | 4603796ea1fbd7b9410c000bd738331a65dc4481 (patch) | |
tree | bb51a21d75d514e1c41f16adad837d58911e3b03 /plugins | |
parent | 1bd03f2eebdfa1b56c1ce410081223b2b646754f (diff) | |
download | sonarqube-4603796ea1fbd7b9410c000bd738331a65dc4481.tar.gz sonarqube-4603796ea1fbd7b9410c000bd738331a65dc4481.zip |
SONAR-3676 Create properties to define project links
=> even for non-Maven projects. The new properties are:
- sonar.links.homepage
- sonar.links.ci
- sonar.links.issue
- sonar.links.scm
- sonar.links.scm_dev
Diffstat (limited to 'plugins')
3 files changed, 161 insertions, 62 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index c32b14de1c7..890a0d74e78 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -24,6 +24,7 @@ import org.sonar.api.CoreProperties; import org.sonar.api.Extension; import org.sonar.api.Properties; import org.sonar.api.Property; +import org.sonar.api.PropertyField; import org.sonar.api.PropertyType; import org.sonar.api.SonarPlugin; import org.sonar.api.checks.NoSonarFilter; @@ -127,6 +128,117 @@ import java.util.List; global = true, category = CoreProperties.CATEGORY_GENERAL), @Property( + key = CoreProperties.LINKS_HOME_PAGE, + defaultValue = "", + name = "Project Home Page", + description = "HTTP URL of the home page of the project.", + project = false, + global = false, + category = CoreProperties.CATEGORY_GENERAL), + @Property( + key = CoreProperties.LINKS_CI, + defaultValue = "", + name = "CI server", + description = "HTTP URL of the continuous integration server.", + project = false, + global = false, + category = CoreProperties.CATEGORY_GENERAL), + @Property( + key = CoreProperties.LINKS_ISSUE_TRACKER, + defaultValue = "", + name = "Issue Tracker", + description = "HTTP URL of the issue tracker.", + project = false, + global = false, + category = CoreProperties.CATEGORY_GENERAL), + @Property( + key = CoreProperties.LINKS_SOURCES, + defaultValue = "", + name = "SCM server", + description = "HTTP URL of the server which hosts the sources of the project.", + project = false, + global = false, + category = CoreProperties.CATEGORY_GENERAL), + @Property( + key = CoreProperties.LINKS_SOURCES_DEV, + defaultValue = "", + name = "SCM connection for developers", + description = "HTTP URL used by developers to connect to the SCM server for the project.", + project = false, + global = false, + category = CoreProperties.CATEGORY_GENERAL), + @Property( + key = "sonar.test.jira.servers", + name = "Jira Servers", + description = "List of jira server definitions", + global = true, + project = true, + category = "DEV", + fields = { + @PropertyField( + key = "key", + name = "Key", + type = PropertyType.STRING, + indicativeSize = 10), + @PropertyField( + key = "url", + name = "Url", + description = "l'url du serveur jira", + type = PropertyType.STRING, + indicativeSize = 20), + @PropertyField( + key = "port", + name = "Port", + type = PropertyType.INTEGER, + indicativeSize = 5)}), + @Property( + key = "sonar.demo", + name = "Demo", + global = true, + project = true, + category = "DEV", + fields = { + @PropertyField( + key = "text", + name = "text", + type = PropertyType.TEXT), + @PropertyField( + key = "boolean", + name = "boolean", + type = PropertyType.BOOLEAN), + @PropertyField( + key = "float", + name = "float", + type = PropertyType.FLOAT), + @PropertyField( + key = "license", + name = "license", + type = PropertyType.LICENSE), + @PropertyField( + key = "metric", + name = "metric", + type = PropertyType.METRIC), + @PropertyField( + key = "password", + name = "password", + type = PropertyType.PASSWORD), + @PropertyField( + key = "regexp", + name = "regexp", + type = PropertyType.REGULAR_EXPRESSION), + @PropertyField( + key = "list", + name = "list", + type = PropertyType.SINGLE_SELECT_LIST, + options = {"AAA", "BBB"})}), + @Property( + key = "sonar.test.jira", + name = "Jira", + project = true, + category = "DEV", + type = PropertyType.PROPERTY_SET, + propertySetKey = "sonar.test.jira.servers"), + @Property( key = CoreProperties.PROJECT_LANGUAGE_PROPERTY, defaultValue = Java.KEY, name = "Default language", diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProjectLinksSensor.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProjectLinksSensor.java index a5aed475704..406b067ab28 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProjectLinksSensor.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ProjectLinksSensor.java @@ -20,29 +20,24 @@ package org.sonar.plugins.core.sensors; import org.apache.commons.lang.StringUtils; -import org.apache.maven.model.CiManagement; -import org.apache.maven.model.IssueManagement; -import org.apache.maven.model.Scm; -import org.apache.maven.project.MavenProject; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectLink; +import org.sonar.core.i18n.I18nManager; -@SupportedEnvironment("maven") -public class ProjectLinksSensor implements Sensor { +import java.util.Locale; - public static final String KEY_HOME = "homepage"; - public static final String KEY_CONTINUOUS_INTEGRATION = "ci"; - public static final String KEY_ISSUE_TRACKER = "issue"; - public static final String KEY_SCM = "scm"; - public static final String KEY_SCM_DEVELOPER_CONNECTION = "scm_dev"; +public class ProjectLinksSensor implements Sensor { - private MavenProject pom; + private Settings settings; + private I18nManager i18nManager; - public ProjectLinksSensor(MavenProject pom) { - this.pom = pom; + public ProjectLinksSensor(Settings settings, I18nManager i18nManager) { + this.settings = settings; + this.i18nManager = i18nManager; } public boolean shouldExecuteOnProject(Project project) { @@ -50,26 +45,18 @@ public class ProjectLinksSensor implements Sensor { } public void analyse(Project project, SensorContext context) { - updateLink(context, KEY_HOME, "Home", pom.getUrl()); - - Scm scm = pom.getScm(); - if (scm == null) { - scm = new Scm(); - } - updateLink(context, KEY_SCM, "Sources", scm.getUrl()); - updateLink(context, KEY_SCM_DEVELOPER_CONNECTION, "Developer connection", scm.getDeveloperConnection()); - - CiManagement ci = pom.getCiManagement(); - if (ci == null) { - ci = new CiManagement(); - } - updateLink(context, KEY_CONTINUOUS_INTEGRATION, "Continuous integration", ci.getUrl()); + handleLink(context, CoreProperties.LINKS_HOME_PAGE); + handleLink(context, CoreProperties.LINKS_CI); + handleLink(context, CoreProperties.LINKS_ISSUE_TRACKER); + handleLink(context, CoreProperties.LINKS_SOURCES); + handleLink(context, CoreProperties.LINKS_SOURCES_DEV); + } - IssueManagement issues = pom.getIssueManagement(); - if (issues == null) { - issues = new IssueManagement(); - } - updateLink(context, KEY_ISSUE_TRACKER, "Issues", issues.getUrl()); + private void handleLink(SensorContext context, String linkProperty) { + String home = settings.getString(linkProperty); + String linkType = StringUtils.substringAfterLast(linkProperty, "."); + String name = i18nManager.message(Locale.getDefault(), "project_links." + linkType, linkProperty); + updateLink(context, linkType, name, home); } private void updateLink(SensorContext context, String key, String name, String url) { diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java index 0fd9f9505fb..f87147e55e2 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java @@ -20,64 +20,64 @@ package org.sonar.plugins.core.sensors; import org.apache.commons.lang.StringUtils; -import org.apache.maven.project.MavenProject; import org.junit.Test; import org.mockito.ArgumentMatcher; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.SensorContext; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectLink; -import org.sonar.api.test.MavenTestUtils; +import org.sonar.core.i18n.I18nManager; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import java.util.Locale; + +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class ProjectLinksSensorTest { @Test + public void testToString() { + assertThat(new ProjectLinksSensor(null, null).toString()).isEqualTo("ProjectLinksSensor"); + } + + @Test public void shouldExecuteOnlyForLatestAnalysis() { - MavenProject pom = mock(MavenProject.class); Project project = mock(Project.class); when(project.isLatestAnalysis()).thenReturn(true).thenReturn(false); - assertThat(new ProjectLinksSensor(pom).shouldExecuteOnProject(project), is(true)); - assertThat(new ProjectLinksSensor(pom).shouldExecuteOnProject(project), is(false)); - verify(project, times(2)).isLatestAnalysis(); - verifyNoMoreInteractions(project); + assertThat(new ProjectLinksSensor(null, null).shouldExecuteOnProject(project)).isTrue(); + assertThat(new ProjectLinksSensor(null, null).shouldExecuteOnProject(project)).isFalse(); } @Test public void shouldSaveLinks() { - SensorContext context = mock(SensorContext.class); - MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/core/sensors/ProjectLinksSensorTest/shouldSaveLinks.xml"); + Settings settings = new Settings(); + settings.setProperty(CoreProperties.LINKS_HOME_PAGE, "http://home"); + I18nManager i18nManager = mock(I18nManager.class); + when(i18nManager.message(Locale.getDefault(), "project_links.homepage", CoreProperties.LINKS_HOME_PAGE)).thenReturn("HOME"); Project project = mock(Project.class); + SensorContext context = mock(SensorContext.class); - new ProjectLinksSensor(pom).analyse(project, context); + new ProjectLinksSensor(settings, i18nManager).analyse(project, context); - verify(context).saveLink(argThat(new MatchLink(ProjectLinksSensor.KEY_HOME, "Home", "http://sonar.codehaus.org"))); - verify(context).saveLink(argThat(new MatchLink(ProjectLinksSensor.KEY_ISSUE_TRACKER, "Issues", "http://jira.codehaus.org/browse/SONAR"))); - verify(context).saveLink(argThat(new MatchLink(ProjectLinksSensor.KEY_CONTINUOUS_INTEGRATION, "Continuous integration", "http://bamboo.ci.codehaus.org/browse/SONAR/"))); - verify(context).saveLink(argThat(new MatchLink(ProjectLinksSensor.KEY_SCM, "Sources", "http://svn.sonar.codehaus.org"))); - verify(context).saveLink(argThat(new MatchLink(ProjectLinksSensor.KEY_SCM_DEVELOPER_CONNECTION, "Developer connection", "scm:svn:https://svn.codehaus.org/sonar/trunk"))); + verify(context).saveLink(argThat(new MatchLink("homepage", "HOME", "http://home"))); } @Test - public void shouldDeleteMissingLinks() { - SensorContext context = mock(SensorContext.class); - MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/core/sensors/ProjectLinksSensorTest/shouldDeleteMissingLinks.xml"); + public void shouldDeleteLink() { + Settings settings = new Settings(); + settings.setProperty(CoreProperties.LINKS_HOME_PAGE, ""); + I18nManager i18nManager = mock(I18nManager.class); + when(i18nManager.message(Locale.getDefault(), "project_links.homepage", CoreProperties.LINKS_HOME_PAGE)).thenReturn("HOME"); Project project = mock(Project.class); + SensorContext context = mock(SensorContext.class); - new ProjectLinksSensor(pom).analyse(project, context); + new ProjectLinksSensor(settings, i18nManager).analyse(project, context); - verify(context).deleteLink(ProjectLinksSensor.KEY_HOME); - verify(context).deleteLink(ProjectLinksSensor.KEY_ISSUE_TRACKER); - verify(context).deleteLink(ProjectLinksSensor.KEY_CONTINUOUS_INTEGRATION); - verify(context).deleteLink(ProjectLinksSensor.KEY_SCM); - verify(context).deleteLink(ProjectLinksSensor.KEY_SCM_DEVELOPER_CONNECTION); + verify(context).deleteLink("homepage"); } private class MatchLink extends ArgumentMatcher<ProjectLink> { |