--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch;
+
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Project;
+import org.sonar.api.rules.ActiveRule;
+import org.sonar.jpa.dao.ProfilesDao;
+
+public class DefaultProfileLoader implements ProfileLoader {
+ static final String PARAM_PROFILE = "sonar.profile";
+
+ private RulesProfile profile;
+
+ private ProfilesDao dao;
+
+ public DefaultProfileLoader(ProfilesDao dao) {
+ this.dao = dao;
+ }
+
+ public RulesProfile load(Project project) {
+ String profileName = (String) project.getProperty(PARAM_PROFILE);
+ if (profileName == null) {
+ Project root = project.getRoot();
+ profile = dao.getActiveProfile(root.getLanguageKey(), root.getKey());
+ if (profile == null) {
+ throw new RuntimeException("Quality profile not found for " + root.getKey() + ", language " + root.getLanguageKey());
+ }
+
+ } else {
+ profile = dao.getProfile(project.getLanguageKey(), profileName);
+ if (profile == null) {
+ throw new RuntimeException("Quality profile not found : " + profileName + ", language " + project.getLanguageKey());
+ }
+ }
+
+ // hack to lazy initialize the profile collections
+ profile.getActiveRules().size();
+ for (ActiveRule activeRule : profile.getActiveRules()) {
+ activeRule.getActiveRuleParams().size();
+ activeRule.getRule().getParams().size();
+ }
+ profile.getAlerts().size();
+
+ return profile;
+ }
+
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch;
+
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Project;
+
+public interface ProfileLoader {
+
+ /**
+ * Loads quality profile for specified project.
+ */
+ RulesProfile load(Project project);
+
+}
import org.picocontainer.injectors.ProviderAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.jpa.dao.ProfilesDao;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
-import org.sonar.api.rules.ActiveRule;
public class ProfileProvider extends ProviderAdapter {
- public static final String PARAM_PROFILE = "sonar.profile";
-
private static final Logger LOG = LoggerFactory.getLogger(ProfileProvider.class);
+
private RulesProfile profile;
- public RulesProfile provide(Project project, ProfilesDao dao) {
+ public RulesProfile provide(Project project, ProfileLoader profileLoader) {
if (profile == null) {
- String profileName = (String) project.getProperty(PARAM_PROFILE);
- if (profileName == null) {
- Project root = project.getRoot();
- profile = dao.getActiveProfile(root.getLanguageKey(), root.getKey());
- if (profile == null) {
- throw new RuntimeException("Quality profile not found for " + root.getKey() + ", language " + root.getLanguageKey());
- }
-
- } else {
- profile = dao.getProfile(project.getLanguageKey(), profileName);
- if (profile == null) {
- throw new RuntimeException("Quality profile not found : " + profileName + ", language " + project.getLanguageKey());
- }
- }
-
- // hack to lazy initialize the profile collections
- profile.getActiveRules().size();
- for (ActiveRule activeRule : profile.getActiveRules()) {
- activeRule.getActiveRuleParams().size();
- activeRule.getRule().getParams().size();
- }
- profile.getAlerts().size();
-
+ profile = profileLoader.load(project);
LOG.info("Selected quality profile : {}", profile);
}
return profile;
}
-}
\ No newline at end of file
+}
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.rules.DefaultRulesManager;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.bootstrap.BatchPluginRepository;
import org.sonar.batch.index.DefaultResourcePersister;
import org.sonar.batch.phases.Phases;
import org.sonar.core.components.DefaultModelFinder;
-import org.sonar.jpa.dao.*;
+import org.sonar.jpa.dao.AsyncMeasuresDao;
+import org.sonar.jpa.dao.AsyncMeasuresService;
+import org.sonar.jpa.dao.DaoFacade;
+import org.sonar.jpa.dao.ProfilesDao;
+import org.sonar.jpa.dao.RulesDao;
public class ProjectBatch {
addComponent(DefaultModelFinder.class);
addComponent(TimeMachineConfiguration.class);
addComponent(PastViolationsLoader.class);
+ addComponent(ProfileLoader.class, DefaultProfileLoader.class);
addAdapter(new ProfileProvider());
addAdapter(new CheckProfileProvider());
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.HashMap;
+
+import org.sonar.batch.DefaultProfileLoader;
+
+import org.apache.commons.configuration.MapConfiguration;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.profiles.Alert;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
+import org.sonar.api.rules.ActiveRule;
+import org.sonar.batch.ProfileLoader;
+import org.sonar.jpa.dao.ProfilesDao;
+
+public class DefaultProfileLoaderTest {
+
+ private ProfilesDao dao;
+ private ProfileLoader loader;
+
+ @Before
+ public void setUp() {
+ dao = mock(ProfilesDao.class);
+ loader = new DefaultProfileLoader(dao);
+ }
+
+ @Test
+ public void shouldGetProjectProfile() {
+ Project project = new Project("project").setLanguageKey(Java.KEY);
+ Project module = new Project("module").setParent(project).setLanguageKey(Java.KEY);
+
+ when(dao.getActiveProfile(Java.KEY, "project")).thenReturn(newProfile());
+
+ assertNotNull(loader.load(module));
+
+ verify(dao, never()).getActiveProfile(Java.KEY, "module");
+ verify(dao).getActiveProfile(Java.KEY, "project");
+ }
+
+ private RulesProfile newProfile() {
+ RulesProfile profile = new RulesProfile();
+ profile.setAlerts(Collections.<Alert> emptyList());
+ profile.setActiveRules(Collections.<ActiveRule> emptyList());
+ return profile;
+ }
+
+ @Test
+ public void mavenPropertyShouldOverrideProfile() {
+ Project project = new Project("project").setLanguageKey(Java.KEY);
+
+ MapConfiguration conf = new MapConfiguration(new HashMap());
+ conf.addProperty(DefaultProfileLoader.PARAM_PROFILE, "profile1");
+ project.setConfiguration(conf);
+
+ when(dao.getProfile(Java.KEY, "profile1")).thenReturn(newProfile());
+
+ loader.load(project);
+
+ verify(dao).getProfile(Java.KEY, "profile1");
+ verify(dao, never()).getActiveProfile(Java.KEY, "project");
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void shouldFailIfProfileIsNotFound() {
+ Project project = new Project("project").setLanguageKey(Java.KEY);
+
+ MapConfiguration conf = new MapConfiguration(new HashMap());
+ conf.addProperty(DefaultProfileLoader.PARAM_PROFILE, "unknown");
+ project.setConfiguration(conf);
+
+ when(dao.getProfile(Java.KEY, "profile1")).thenReturn(null);
+
+ loader.load(project);
+ }
+
+}
*/
package org.sonar.batch;
-import org.apache.commons.configuration.MapConfiguration;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
import org.junit.Test;
-import org.sonar.jpa.dao.ProfilesDao;
-import org.sonar.api.profiles.Alert;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
-import org.sonar.api.rules.ActiveRule;
-
-import java.util.Collections;
-import java.util.HashMap;
-
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.*;
public class ProfileProviderTest {
-
@Test
- public void shouldGetProjectProfile() {
+ public void shouldProvideProfile() {
ProfileProvider provider = new ProfileProvider();
- Project project = new Project("project").setLanguageKey(Java.KEY);
- Project module = new Project("module").setParent(project).setLanguageKey(Java.KEY);
- ProfilesDao dao = mock(ProfilesDao.class);
-
- when(dao.getActiveProfile(Java.KEY, "project")).thenReturn(newProfile());
-
- assertNotNull(provider.provide(module, dao));
-
- verify(dao, never()).getActiveProfile(Java.KEY, "module");
- verify(dao).getActiveProfile(Java.KEY, "project");
+ ProfileLoader loader = mock(ProfileLoader.class);
+ Project project = new Project("project");
+ RulesProfile profile = RulesProfile.create();
+ when(loader.load(project)).thenReturn(profile);
+
+ assertThat(provider.provide(project, loader), is(profile));
+ assertThat(provider.provide(project, loader), is(profile));
+ verify(loader).load(project);
+ verifyNoMoreInteractions(loader);
}
-
- private RulesProfile newProfile() {
- RulesProfile profile = new RulesProfile();
- profile.setAlerts(Collections.<Alert>emptyList());
- profile.setActiveRules(Collections.<ActiveRule>emptyList());
- return profile;
- }
-
- @Test
- public void mavenPropertyShouldOverrideProfile() {
- ProfileProvider provider = new ProfileProvider();
- ProfilesDao dao = mock(ProfilesDao.class);
- Project project = new Project("project").setLanguageKey(Java.KEY);
-
- MapConfiguration conf = new MapConfiguration(new HashMap());
- conf.addProperty(ProfileProvider.PARAM_PROFILE, "profile1");
- project.setConfiguration(conf);
-
- when(dao.getProfile(Java.KEY, "profile1")).thenReturn(newProfile());
-
- provider.provide(project, dao);
-
- verify(dao).getProfile(Java.KEY, "profile1");
- verify(dao, never()).getActiveProfile(Java.KEY, "project");
- }
-
- @Test(expected = RuntimeException.class)
- public void shouldFailIfProfileIsNotFound() {
- ProfileProvider provider = new ProfileProvider();
- Project project = new Project("project").setLanguageKey(Java.KEY);
- ProfilesDao dao = mock(ProfilesDao.class);
-
- MapConfiguration conf = new MapConfiguration(new HashMap());
- conf.addProperty(ProfileProvider.PARAM_PROFILE, "unknown");
- project.setConfiguration(conf);
-
-
- when(dao.getProfile(Java.KEY, "profile1")).thenReturn(null);
-
- provider.provide(project, dao);
- }
-
}