]> source.dussan.org Git - sonarqube.git/commitdiff
Add ProfileLoader
authorEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 11 Mar 2011 09:21:25 +0000 (12:21 +0300)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 11 Mar 2011 10:20:01 +0000 (13:20 +0300)
sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/ProfileLoader.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/ProfileProvider.java
sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/ProfileProviderTest.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 (file)
index 0000000..648676e
--- /dev/null
@@ -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 (file)
index 0000000..04b432b
--- /dev/null
@@ -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);
+
+}
index cb814196efea33d6d60db6d1e5be4f1587570793..fdf9c0702491668b2fa73ef8ec05d277038799f3 100644 (file)
@@ -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
+}
index 575be37f35abaf20f67b14b152b898b38ba991af..8614891b8b8889c44db216741a3992036bef7786 100644 (file)
@@ -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 (file)
index 0000000..a409144
--- /dev/null
@@ -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.<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);
+  }
+
+}
index 3fd5b097cdfc2b66cb462f5247eda8458d1f45e5..906f00512fe38301417586d2d4ed23387c4164c3 100644 (file)
  */
 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);
-  }
-
 }