diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-28 17:41:48 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-28 17:41:48 +0100 |
commit | db7da381fb19ff5356988a815e9bbfeb14879356 (patch) | |
tree | d29f5aea4ab4c4fa17ac5476abcd985b0015f920 | |
parent | a743db9c4104a4ab1f56dd061314f4dc96d24e6c (diff) | |
download | sonarqube-db7da381fb19ff5356988a815e9bbfeb14879356.tar.gz sonarqube-db7da381fb19ff5356988a815e9bbfeb14879356.zip |
SONAR-5029 Mix between quality profiles of each languages on project profiles page
10 files changed, 41 insertions, 31 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java index 2f8ab8a38b5..b069a9598c8 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java @@ -27,6 +27,7 @@ import org.sonar.core.component.ComponentDto; import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; + import java.util.List; public class QualityProfileDao implements ServerComponent { @@ -101,10 +102,11 @@ public class QualityProfileDao implements ServerComponent { } } - public List<QualityProfileDto> selectByProject(long projectId, String propKeyPrefix) { + + public QualityProfileDto selectByProjectAndLanguage(long projectId, String language, String key) { SqlSession session = mybatis.openSession(); try { - return session.getMapper(QualityProfileMapper.class).selectByProject(projectId, propKeyPrefix); + return session.getMapper(QualityProfileMapper.class).selectByProjectAndLanguage(projectId, language, key); } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java index 5ddf7e3b98f..fb4ace51624 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java @@ -63,7 +63,7 @@ public interface QualityProfileMapper { int countProjects(@Param("value") String propertyValue, @Param("key") String propertyKey); - List<QualityProfileDto> selectByProject(@Param("projectId") Long projectId, @Param("key") String propertyKeyPrefix); + QualityProfileDto selectByProjectAndLanguage(@Param("projectId") Long projectId, @Param("language") String language, @Param("key") String propertyKeyPrefix); void updatedUsedColumn(@Param("id") int profileId, @Param("used") boolean used); } diff --git a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml index 876fc40df5c..78eff7dd4e2 100644 --- a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml @@ -121,12 +121,15 @@ </where> </select> - <select id="selectByProject" parameterType="map" resultType="QualityProfile"> - SELECT DISTINCT <include refid="profilesColumns"/> + <select id="selectByProjectAndLanguage" parameterType="map" resultType="QualityProfile"> + SELECT <include refid="profilesColumns"/> FROM rules_profiles p INNER JOIN properties prop ON prop.resource_id=#{projectId} AND prop.prop_key LIKE #{key} AND prop.text_value LIKE p.name + <where> + AND p.language=#{language} + </where> </select> <update id="updatedUsedColumn" parameterType="map"> diff --git a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java index f01091d9963..3044732c8cd 100644 --- a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java @@ -217,10 +217,11 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase { } @Test - public void select_by_project() { + public void select_by_project_and_language() { setupData("projects"); - assertThat(dao.selectByProject(1L, "sonar.profile.%")).hasSize(2); + QualityProfileDto dto = dao.selectByProjectAndLanguage(1L, "java", "sonar.profile.java"); + assertThat(dto.getId()).isEqualTo(1); } @Test diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java index a88e94f6f72..677528d962f 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectLookup.java @@ -20,8 +20,6 @@ package org.sonar.server.qualityprofile; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.apache.ibatis.session.SqlSession; import org.sonar.api.ServerComponent; @@ -31,9 +29,9 @@ import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; -import java.util.List; +import javax.annotation.CheckForNull; -import static com.google.common.collect.Lists.newArrayList; +import java.util.List; public class QProfileProjectLookup implements ServerComponent { @@ -62,14 +60,13 @@ public class QProfileProjectLookup implements ServerComponent { return qualityProfileDao.countProjects(profile.name(), QProfileOperations.PROFILE_PROPERTY_PREFIX + profile.language()); } - public List<QProfile> profiles(long projectId) { - List<QualityProfileDto> dtos = qualityProfileDao.selectByProject(projectId, QProfileOperations.PROFILE_PROPERTY_PREFIX + "%"); - return newArrayList(Iterables.transform(dtos, new Function<QualityProfileDto, QProfile>() { - @Override - public QProfile apply(QualityProfileDto dto) { - return QProfile.from(dto); - } - })); + @CheckForNull + public QProfile findProfileByProjectAndLanguage(long projectId, String language) { + QualityProfileDto dto = qualityProfileDao.selectByProjectAndLanguage(projectId, language, QProfileOperations.PROFILE_PROPERTY_PREFIX + language); + if (dto != null) { + return QProfile.from(dto); + } + return null; } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java index c6970e6ffdb..4442553a8c3 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java @@ -29,6 +29,7 @@ import org.sonar.server.util.Validation; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -141,8 +142,9 @@ public class QProfiles implements ServerComponent { /** * Used in /project/profile */ - public List<QProfile> profiles(int projectId) { - return projectLookup.profiles(projectId); + @CheckForNull + public QProfile findProfileByProjectAndLanguage(long projectId, String language) { + return projectLookup.findProfileByProjectAndLanguage(projectId, language); } public void addProject(int profileId, long projectId) { diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb index 9b799d6565f..156f2222505 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb @@ -84,7 +84,6 @@ class ProjectController < ApplicationController @project = Project.by_key(@project_id) call_backend do - @project_quality_profiles = Internal.quality_profiles.profiles(@project_id.to_i).to_a @all_quality_profiles = Internal.quality_profiles.allProfiles().to_a end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/project/profile.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/project/profile.html.erb index ce0a01c912a..f52b97485b1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/project/profile.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/project/profile.html.erb @@ -11,7 +11,7 @@ <tbody> <% Api::Utils.languages.each do |language| - selected_profile = @project_quality_profiles.find {|profile| profile.language == language.getKey()} + selected_profile = Internal.quality_profiles.findProfileByProjectAndLanguage(@project_id, language.getKey()) %> <tr class="<%= cycle 'even', 'odd' -%>"> <td class="thin" nowrap><%= h language.getName() -%></td> diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectLookupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectLookupTest.java index 1e4e5e746b0..d0a52da2728 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectLookupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectLookupTest.java @@ -33,8 +33,6 @@ import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.server.exceptions.NotFoundException; -import java.util.List; - import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -94,10 +92,18 @@ public class QProfileProjectLookupTest { @Test public void search_profiles_from_project() throws Exception { QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - when(qualityProfileDao.selectByProject(1L, "sonar.profile.%")).thenReturn(newArrayList(qualityProfile)); + when(qualityProfileDao.selectByProjectAndLanguage(1L, "java", "sonar.profile.java")).thenReturn(qualityProfile); + + QProfile result = lookup.findProfileByProjectAndLanguage(1L, "java"); + assertThat(result).isNotNull(); + } + + @Test + public void return_null_when_no_profile_when_searching_for_profiles_from_project() throws Exception { + when(qualityProfileDao.selectByProjectAndLanguage(1L, "java", "sonar.profile.java")).thenReturn(null); - List<QProfile> result = lookup.profiles(1L); - assertThat(result).hasSize(1); + QProfile result = lookup.findProfileByProjectAndLanguage(1L, "java"); + assertThat(result).isNull(); } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java index 915777799de..56e569d3c89 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java @@ -186,9 +186,9 @@ public class QProfilesTest { } @Test - public void get_profiles_from_project_id() throws Exception { - qProfiles.profiles(1); - verify(projectLookup).profiles(1); + public void get_profiles_from_project_and_language() throws Exception { + qProfiles.findProfileByProjectAndLanguage(1, "java"); + verify(projectLookup).findProfileByProjectAndLanguage(1, "java"); } @Test |