From 53d3da7dfa26eb14d33c72f6deb7d2894e2f2dec Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 11 Mar 2011 12:21:25 +0300 Subject: [PATCH] Add ProfileLoader --- .../org/sonar/batch/DefaultProfileLoader.java | 65 +++++++++++ .../java/org/sonar/batch/ProfileLoader.java | 32 ++++++ .../java/org/sonar/batch/ProfileProvider.java | 33 +----- .../java/org/sonar/batch/ProjectBatch.java | 13 ++- .../sonar/batch/DefaultProfileLoaderTest.java | 104 ++++++++++++++++++ .../org/sonar/batch/ProfileProviderTest.java | 82 +++----------- 6 files changed, 233 insertions(+), 96 deletions(-) create mode 100644 sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java new file mode 100644 index 00000000000..648676eb7f2 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java @@ -0,0 +1,65 @@ +/* + * 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; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java new file mode 100644 index 00000000000..04b432b960d --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java @@ -0,0 +1,32 @@ +/* + * 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); + +} 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 cb814196efe..fdf9c070249 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java @@ -22,45 +22,20 @@ package org.sonar.batch; 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 +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java index 575be37f35a..8614891b8b8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java @@ -25,7 +25,11 @@ import org.sonar.api.measures.CoreMetrics; 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; @@ -35,7 +39,11 @@ import org.sonar.batch.index.DefaultIndex; 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 { @@ -128,6 +136,7 @@ public class ProjectBatch { addComponent(DefaultModelFinder.class); addComponent(TimeMachineConfiguration.class); addComponent(PastViolationsLoader.class); + addComponent(ProfileLoader.class, DefaultProfileLoader.class); addAdapter(new ProfileProvider()); addAdapter(new CheckProfileProvider()); diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java new file mode 100644 index 00000000000..a4091445671 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java @@ -0,0 +1,104 @@ +/* + * 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. emptyList()); + profile.setActiveRules(Collections. 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); + } + +} 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 3fd5b097cdf..906f00512fe 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.java @@ -19,77 +19,29 @@ */ 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.emptyList()); - profile.setActiveRules(Collections.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); - } - } -- 2.39.5