From: Julien Lancelot Date: Wed, 17 Jul 2013 16:26:31 +0000 (+0200) Subject: SONAR-4419 when searching for projects, the technical projects should not be returned X-Git-Tag: 3.7~70 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=aab6ac481446f5373f662b7c75dc88897d99864d;p=sonarqube.git SONAR-4419 when searching for projects, the technical projects should not be returned --- diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java index b6a5e8c113f..18bb0ec788c 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java @@ -201,6 +201,23 @@ public class ResourceDao { } } + public List selectComponentsByQualifiers(Collection qualifiers) { + if (qualifiers.isEmpty()) { + return Collections.emptyList(); + } + SqlSession session = mybatis.openSession(); + try { + List resourceDtos = session.getMapper(ResourceMapper.class).selectComponentsByQualifiers(qualifiers); + List components = newArrayList(); + for (ResourceDto resourceDto : resourceDtos) { + components.add(toComponent(resourceDto)); + } + return components; + } finally { + MyBatis.closeQuietly(session); + } + } + public static ComponentDto toComponent(ResourceDto resourceDto){ return new ComponentDto() .setKey(resourceDto.getKey()) diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java index 2632a0d1929..c8a811080fb 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java @@ -74,6 +74,10 @@ public interface ResourceMapper { List selectAuthorizedChildrenComponentIds(@Param("componentRootKeys") Collection componentRootKeys, @Param("userId") @Nullable Integer userId, @Param("role") String role); + /** + * @since 3.7 + */ + List selectComponentsByQualifiers(@Param("qualifiers") Collection qualifier); void insert(ResourceDto resource); diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml index 16c5c68f3ba..3d0319a0e04 100644 --- a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml @@ -120,6 +120,18 @@ + + diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java index 1855d41a178..11a7edcd71e 100644 --- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java @@ -308,4 +308,16 @@ public class ResourceDaoTest extends AbstractDaoTestCase { assertThat(dao.findByKey("org.struts:struts:org.struts.RequestContext")).isNotNull(); assertThat(dao.findByKey("unknown")).isNull(); } + + @Test + public void should_select_components_by_qualifiers(){ + setupData("fixture", "technical-project"); + + List components = dao.selectComponentsByQualifiers(newArrayList("TRK")); + assertThat(components).hasSize(1); + assertThat(components.get(0).key()).isEqualTo("org.struts:struts"); + + assertThat(dao.selectComponentsByQualifiers(newArrayList("unknown"))).isEmpty(); + assertThat(dao.selectComponentsByQualifiers(Collections.emptyList())).isEmpty(); + } } diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/technical-project.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/technical-project.xml new file mode 100644 index 00000000000..ba8298a2ae9 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/technical-project.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/sonar-server/src/main/java/org/sonar/server/component/DefaultComponentFinder.java b/sonar-server/src/main/java/org/sonar/server/component/DefaultComponentFinder.java index ff5164e31cb..6bd4e9291de 100644 --- a/sonar-server/src/main/java/org/sonar/server/component/DefaultComponentFinder.java +++ b/sonar-server/src/main/java/org/sonar/server/component/DefaultComponentFinder.java @@ -26,9 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.component.Component; import org.sonar.api.utils.Paging; -import org.sonar.core.component.ComponentDto; import org.sonar.core.resource.ResourceDao; -import org.sonar.core.resource.ResourceQuery; import java.util.Collection; import java.util.List; @@ -52,10 +50,9 @@ public class DefaultComponentFinder { LOG.debug("ComponentQuery : {}", query); long start = System.currentTimeMillis(); try { - // 1. Search components - ResourceQuery resourceQuery = ResourceQuery.create().setQualifiers(query.qualifiers().toArray(new String[]{})); - List dtos = ResourceDao.toComponents(resourceDao.getResources(resourceQuery)); - Collection foundComponents = search(query, dtos); + // 1. Search components for selected qualifiers + List components = resourceDao.selectComponentsByQualifiers(query.qualifiers()); + Collection foundComponents = search(query, components); // 2. Sort components Collection sortedComponents = new ComponentsFinderSort(foundComponents, query).sort(); diff --git a/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java b/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java index bba0fb77fa9..9aa014331a5 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java @@ -22,16 +22,14 @@ package org.sonar.server.component; import org.junit.Test; import org.sonar.api.component.Component; +import org.sonar.api.resources.Project; import org.sonar.core.resource.ResourceDao; -import org.sonar.core.resource.ResourceDto; -import org.sonar.core.resource.ResourceQuery; import java.util.Iterator; -import java.util.List; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyCollection; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -40,14 +38,14 @@ public class DefaultComponentFinderTest { ResourceDao dao = mock(ResourceDao.class); DefaultComponentFinder finder = new DefaultComponentFinder(dao); + @Test public void should_return_all_components_when_no_parameter() { - List dtoList = newArrayList( - new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), - new ResourceDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent").setQualifier("TRK") - ); - when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList( + createProject("org.codehaus.sonar", "Sonar"), + createProject("org.apache.tika:tika", "Apache Tika"), + createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent") + )); ComponentQuery query = ComponentQuery.builder().build(); DefaultComponentQueryResult results = finder.find(query); @@ -61,12 +59,11 @@ public class DefaultComponentFinderTest { @Test public void should_find_components_by_key_pattern() { - List dtoList = newArrayList( - new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.jackrabbit:jackrabbit").setName("Apache Jackrabbit").setQualifier("TRK") - ); - when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList( + createProject("org.codehaus.sonar", "Sonar"), + createProject("org.apache.tika:tika", "Apache Tika"), + createProject("org.apache.jackrabbit:jackrabbit", "Apache Jackrabbit") + )); ComponentQuery query = ComponentQuery.builder().keys(newArrayList("org.apache")).build(); assertThat(finder.find(query).components()).hasSize(2); @@ -74,12 +71,11 @@ public class DefaultComponentFinderTest { @Test public void should_find_components_by_name_pattern() { - List dtoList = newArrayList( - new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.jackrabbit:jackrabbit").setName("Apache Jackrabbit").setQualifier("TRK") - ); - when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList( + createProject("org.codehaus.sonar", "Sonar"), + createProject("org.apache.tika:tika", "Apache Tika"), + createProject("org.apache.jackrabbit:jackrabbit", "Apache Jackrabbit") + )); ComponentQuery query = ComponentQuery.builder().names(newArrayList("Apache")).build(); assertThat(finder.find(query).components()).hasSize(2); @@ -87,12 +83,11 @@ public class DefaultComponentFinderTest { @Test public void should_sort_result_by_name() { - List dtoList = newArrayList( - new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), - new ResourceDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent").setQualifier("TRK") - ); - when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList( + createProject("org.codehaus.sonar", "Sonar"), + createProject("org.apache.tika:tika", "Apache Tika"), + createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent") + )); ComponentQuery query = ComponentQuery.builder().build(); DefaultComponentQueryResult results = finder.find(query); @@ -108,12 +103,11 @@ public class DefaultComponentFinderTest { public void should_find_paginate_result() { ComponentQuery query = ComponentQuery.builder().pageSize(1).pageIndex(1).build(); - List dtoList = newArrayList( - new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), - new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), - new ResourceDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent").setQualifier("TRK") - ); - when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + when(dao.selectComponentsByQualifiers(anyCollection())).thenReturn(newArrayList( + createProject("org.codehaus.sonar", "Sonar"), + createProject("org.apache.tika:tika", "Apache Tika"), + createProject("org.picocontainer:picocontainer-parent", "PicoContainer Parent") + )); DefaultComponentQueryResult results = finder.find(query); assertThat(results.paging().offset()).isEqualTo(0); @@ -121,4 +115,8 @@ public class DefaultComponentFinderTest { assertThat(results.paging().total()).isEqualTo(3); } + private Component createProject(String key, String name){ + return new Project(key, null, name); + } + }