aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java17
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java4
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml12
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java12
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/technical-project.xml9
-rw-r--r--sonar-server/src/main/java/org/sonar/server/component/DefaultComponentFinder.java9
-rw-r--r--sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java66
7 files changed, 89 insertions, 40 deletions
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<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())
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<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);
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 @@
</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>
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<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
index 00000000000..ba8298a2ae9
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/technical-project.xml
@@ -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>
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<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();
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<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);
+ }
+
}