]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4419 when searching for projects, the technical projects should not be returned
authorJulien Lancelot <julien.lancelot@gmail.com>
Wed, 17 Jul 2013 16:26:31 +0000 (18:26 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Wed, 17 Jul 2013 16:26:31 +0000 (18:26 +0200)
sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/technical-project.xml [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/component/DefaultComponentFinder.java
sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java

index b6a5e8c113f7ceaa7fc0745c4190d48db1af2edb..18bb0ec788c0d919b2799fea8d785db88ae99d59 100644 (file)
@@ -201,6 +201,23 @@ public class ResourceDao {
     }
   }
 
+  public List<Component> selectComponentsByQualifiers(Collection<String> qualifiers) {
+    if (qualifiers.isEmpty()) {
+      return Collections.emptyList();
+    }
+    SqlSession session = mybatis.openSession();
+    try {
+      List<ResourceDto> resourceDtos = session.getMapper(ResourceMapper.class).selectComponentsByQualifiers(qualifiers);
+      List<Component> 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())
index 2632a0d19291457ba72759b645c3c10a73eb122e..c8a811080fb5a31f7e674e508a018e1edd4a9f4a 100644 (file)
@@ -74,6 +74,10 @@ public interface ResourceMapper {
   List<Integer> selectAuthorizedChildrenComponentIds(@Param("componentRootKeys") Collection<String> componentRootKeys,
                                                      @Param("userId") @Nullable Integer userId, @Param("role") String role);
 
+  /**
+   * @since 3.7
+   */
+  List<ResourceDto> selectComponentsByQualifiers(@Param("qualifiers") Collection<String> qualifier);
 
   void insert(ResourceDto resource);
 
index 16c5c68f3baed9c4fbccac84353c024e0e50cd7e..3d0319a0e04e0c853b8d273ab24054a13a8303f4 100644 (file)
     </where>
   </select>
 
+  <select id="selectComponentsByQualifiers" parameterType="map" resultMap="resourceResultMap">
+    select * from projects p
+    <where>
+      <if test="qualifiers != null and qualifiers.size() > 0">
+        and p.qualifier in
+        <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
+      </if>
+      and p.enabled=${_true}
+      and p.copy_resource_id is null
+    </where>
+  </select>
+
   <select id="selectAuthorizedChildrenComponentIds" parameterType="map" resultType="int">
     <include refid="selectAuthorizedChildrenComponentIdsQuery" />
   </select>
index 1855d41a178750bdaa6ed247b7c3fd647886d587..11a7edcd71ed1111ae4e497049ec18f75cfe44ae 100644 (file)
@@ -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<Component> 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.<String>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 (file)
index 0000000..ba8298a
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <!-- technical project -->
+  <projects id="5" root_id="[null]" scope="PRJ" qualifier="TRK" kee="COPYorg.struts:struts" name="Struts"
+            description="the description" long_name="Apache Struts"
+            enabled="[true]" language="java" copy_resource_id="1" person_id="[null]"/>
+
+
+</dataset>
index ff5164e31cb7136fbedd3abd2a0dd244b9e5fc81..6bd4e9291de0f731bc5f464f17cce6f931b19e4f 100644 (file)
@@ -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<ComponentDto> dtos = ResourceDao.toComponents(resourceDao.getResources(resourceQuery));
-      Collection<Component> foundComponents = search(query, dtos);
+      // 1. Search components for selected qualifiers
+      List<Component> components = resourceDao.selectComponentsByQualifiers(query.qualifiers());
+      Collection<Component> foundComponents = search(query, components);
 
       // 2. Sort components
       Collection<? extends Component> sortedComponents = new ComponentsFinderSort(foundComponents, query).sort();
index bba0fb77fa9e8e043f1f7883118b407b315d56e4..9aa014331a56e0e24c90f9660e03d269cfdf2949 100644 (file)
@@ -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<ResourceDto> 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<ResourceDto> 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<ResourceDto> 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<ResourceDto> 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<ResourceDto> 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);
+  }
+
 }