aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-08-01 16:55:39 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-08-01 16:56:35 +0200
commiteb948a5d6043bbc73fa7449d912c7ad7cb0b0d94 (patch)
tree7d0ecec6ae72fa2a8bd9816015ae0e973fedece3 /sonar-batch
parent51027719280543a9fc554911122a553e856882b2 (diff)
downloadsonarqube-eb948a5d6043bbc73fa7449d912c7ad7cb0b0d94.tar.gz
sonarqube-eb948a5d6043bbc73fa7449d912c7ad7cb0b0d94.zip
SONAR-4515 Moved check to another place to not break views
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java15
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java11
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java10
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java61
5 files changed, 78 insertions, 23 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java
index 6554b60c029..035d6209063 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java
@@ -19,11 +19,9 @@
*/
package org.sonar.batch;
-import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.utils.SonarException;
@@ -36,17 +34,18 @@ public class DefaultProfileLoader implements ProfileLoader {
this.dao = dao;
}
- public RulesProfile load(Project project, Settings settings, Languages languages) {
- if (!languages.allKey().contains(project.getLanguageKey())) {
- String languageList = Joiner.on(", ").join(languages.allKey());
- throw new SonarException("You must install a plugin that supports the language '" + project.getLanguageKey() +
- "'. Supported language keys are: " + languageList);
- }
+ public RulesProfile load(Project project, Settings settings) {
String profileName = StringUtils.defaultIfBlank(
settings.getString("sonar.profile"),
settings.getString("sonar.profile." + project.getLanguageKey()));
+ if (StringUtils.isBlank(profileName)) {
+ // This means that the current language is not supported by any installed plugin, otherwise at least a
+ // "Default <Language Name>" profile would have been created by ActivateDefaultProfiles class.
+ throw new SonarException("You must install a plugin that supports the language '" + project.getLanguageKey() + "'");
+ }
+
RulesProfile profile = dao.getProfile(project.getLanguageKey(), profileName);
if (profile == null) {
throw new SonarException("Quality profile not found : " + profileName + ", language " + project.getLanguageKey());
diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java
index 10479d7d21c..c844cef918f 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java
@@ -21,15 +21,13 @@ package org.sonar.batch;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
public interface ProfileLoader {
/**
* Loads quality profile for specified project.
- * @param languages remove this parameter when Languages is no more in scope ModuleScanContainer
*/
- RulesProfile load(Project project, Settings settings, Languages languages);
+ RulesProfile load(Project project, Settings settings);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java
index e93a61b100c..a645c9e99a1 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java
@@ -19,6 +19,7 @@
*/
package org.sonar.batch;
+import com.google.common.base.Joiner;
import org.picocontainer.injectors.ProviderAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -26,6 +27,7 @@ import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.SonarException;
public class ProfileProvider extends ProviderAdapter {
@@ -35,7 +37,14 @@ public class ProfileProvider extends ProviderAdapter {
public RulesProfile provide(Project project, ProfileLoader profileLoader, Settings settings, Languages languages) {
if (profile == null) {
- profile = profileLoader.load(project, settings, languages);
+ // TODO Move this check to ProjectReactorValidator when Languages is available as TaskComponent
+ if (!languages.allKey().contains(project.getLanguageKey())) {
+ String languageList = Joiner.on(", ").join(languages.allKey());
+ throw new SonarException("You must install a plugin that supports the language '" + project.getLanguageKey() +
+ "'. Supported language keys are: " + languageList);
+ }
+
+ profile = profileLoader.load(project, settings);
LOG.info("Quality profile : {}", profile);
}
return profile;
diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java
index 836effb1b03..98004894633 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java
@@ -69,7 +69,7 @@ public class DefaultProfileLoaderTest {
settings.setProperty("sonar.profile.java", "legacy profile");
when(dao.getProfile(Java.KEY, "legacy profile")).thenReturn(RulesProfile.create("legacy profile", "java"));
- RulesProfile profile = new DefaultProfileLoader(dao).load(javaProject, settings, languages);
+ RulesProfile profile = new DefaultProfileLoader(dao).load(javaProject, settings);
assertThat(profile.getName()).isEqualTo("legacy profile");
}
@@ -81,19 +81,19 @@ public class DefaultProfileLoaderTest {
thrown.expect(SonarException.class);
thrown.expectMessage("Quality profile not found : unknown, language java");
- new DefaultProfileLoader(dao).load(javaProject, settings, languages);
+ new DefaultProfileLoader(dao).load(javaProject, settings);
}
/**
- * SONAR-3125, SONAR-4515
+ * SONAR-3125
*/
@Test
public void should_give_explicit_message_if_default_profile_not_found() {
Project cobolProject = newProject("cobol");
thrown.expect(SonarException.class);
- thrown.expectMessage("You must install a plugin that supports the language 'cobol'. Supported language keys are: java, js");
- new DefaultProfileLoader(dao).load(cobolProject, new Settings(), languages);
+ thrown.expectMessage("You must install a plugin that supports the language 'cobol'");
+ new DefaultProfileLoader(dao).load(cobolProject, new Settings());
}
private Project newProject(String language) {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java
index 7cc8ecddf80..4276ee3b404 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java
@@ -19,11 +19,18 @@
*/
package org.sonar.batch;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.AbstractLanguage;
+import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.SonarException;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@@ -35,18 +42,60 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
public class ProfileProviderTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private Project javaProject;
+ private Languages languages;
+
+ @Before
+ public void setUp() {
+
+ Language java = new AbstractLanguage("java", "Java") {
+ public String[] getFileSuffixes() {
+ return null;
+ };
+ };
+ Language cobol = new AbstractLanguage("js", "JavaScript") {
+ public String[] getFileSuffixes() {
+ return null;
+ };
+ };
+ languages = new Languages(java, cobol);
+ javaProject = newProject("java");
+ }
+
@Test
public void shouldProvideProfile() {
ProfileProvider provider = new ProfileProvider();
ProfileLoader loader = mock(ProfileLoader.class);
- Project project = new Project("project");
RulesProfile profile = RulesProfile.create();
- Languages languages = mock(Languages.class);
- when(loader.load(eq(project), any(Settings.class), eq(languages))).thenReturn(profile);
+ when(loader.load(eq(javaProject), any(Settings.class))).thenReturn(profile);
- assertThat(provider.provide(project, loader, new Settings(), languages), is(profile));
- assertThat(provider.provide(project, loader, new Settings(), languages), is(profile));
- verify(loader).load(eq(project), any(Settings.class), eq(languages));
+ assertThat(provider.provide(javaProject, loader, new Settings(), languages), is(profile));
+ assertThat(provider.provide(javaProject, loader, new Settings(), languages), is(profile));
+ verify(loader).load(eq(javaProject), any(Settings.class));
verifyNoMoreInteractions(loader);
}
+
+ /**
+ * SONAR-4515
+ */
+ @Test
+ public void should_give_explicit_message_if_language_not_found() {
+ Project cobolProject = newProject("cobol");
+
+ ProfileProvider provider = new ProfileProvider();
+
+ thrown.expect(SonarException.class);
+ thrown.expectMessage("You must install a plugin that supports the language 'cobol'. Supported language keys are: java, js");
+ provider.provide(cobolProject, mock(ProfileLoader.class), new Settings(), languages);
+ }
+
+ private Project newProject(String language) {
+ PropertiesConfiguration configuration = new PropertiesConfiguration();
+ configuration.setProperty("sonar.language", language);
+ return new Project("project").setConfiguration(configuration);
+ }
}