aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-09-30 16:17:20 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-10-02 09:23:01 +0200
commit4e337f119df5ee05515f85a6f26075d3667d7707 (patch)
treeaa60d1e5bb9fc150080544a61d697cabb803f5dd
parent770be1eac18e8a7f99f0596938f38054e2abfb40 (diff)
downloadsonarqube-4e337f119df5ee05515f85a6f26075d3667d7707.tar.gz
sonarqube-4e337f119df5ee05515f85a6f26075d3667d7707.zip
SONAR-6857 ComponentDao with a generic way to search for components
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java20
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java8
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java22
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java10
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java8
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java10
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java5
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java61
-rw-r--r--sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java40
-rw-r--r--sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java6
-rw-r--r--sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java25
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml37
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java66
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java54
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java (renamed from server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java)8
-rw-r--r--sonar-db/src/test/java/org/sonar/db/dialect/DialectTest.java53
24 files changed, 397 insertions, 72 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java
index a072dfd9a89..1b74b2c70ca 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java
@@ -46,8 +46,8 @@ import org.sonar.server.computation.period.PeriodsHolderRule;
import org.sonar.test.DbTests;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.server.component.SnapshotTesting.createForComponent;
-import static org.sonar.server.component.SnapshotTesting.createForProject;
+import static org.sonar.db.component.SnapshotTesting.createForComponent;
+import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
@Category(DbTests.class)
public class ReportComputeMeasureVariationsStepTest {
@@ -100,7 +100,7 @@ public class ReportComputeMeasureVariationsStepTest {
@Test
public void do_nothing_when_no_raw_measure() {
- SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO);
+ SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
session.commit();
@@ -128,7 +128,7 @@ public class ReportComputeMeasureVariationsStepTest {
@Test
public void set_variation() {
// Project
- SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO);
+ SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
@@ -157,11 +157,11 @@ public class ReportComputeMeasureVariationsStepTest {
@Test
public void set_variations_on_all_periods() {
- SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
- SnapshotDto period2ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
- SnapshotDto period3ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
- SnapshotDto period4ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
- SnapshotDto period5ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false);
+ SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
+ SnapshotDto period2ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
+ SnapshotDto period3ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
+ SnapshotDto period4ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
+ SnapshotDto period5ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
dbClient.snapshotDao().insert(session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot);
dbClient.measureDao().insert(session,
@@ -197,7 +197,7 @@ public class ReportComputeMeasureVariationsStepTest {
@Test
public void set_variation_on_all_numeric_metrics() {
- SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO);
+ SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
dbClient.measureDao().insert(session,
newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d),
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java
index e9d0ced4fd6..84bebad7585 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java
@@ -35,7 +35,7 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.component.SnapshotQuery;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.Component;
@@ -282,7 +282,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest {
public void persist_snapshots_with_periods() {
ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
dbClient.componentDao().insert(dbTester.getSession(), projectDto);
- SnapshotDto snapshotDto = SnapshotTesting.createForProject(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
+ SnapshotDto snapshotDto = SnapshotTesting.newSnapshotForProject(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
dbTester.getSession().commit();
periodsHolder.setPeriods(new Period(1, CoreProperties.TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, 123L));
@@ -305,7 +305,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest {
ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
dbClient.componentDao().insert(dbTester.getSession(), projectDto);
- SnapshotDto projectSnapshot = SnapshotTesting.createForProject(projectDto);
+ SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(projectDto);
dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
@@ -355,7 +355,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest {
public void set_no_period_on_snapshots_when_no_period() {
ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
dbClient.componentDao().insert(dbTester.getSession(), projectDto);
- SnapshotDto snapshotDto = SnapshotTesting.createForProject(projectDto);
+ SnapshotDto snapshotDto = SnapshotTesting.newSnapshotForProject(projectDto);
dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
dbTester.getSession().commit();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java
index 7a32cee3485..bc24a015cae 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java
@@ -33,7 +33,7 @@ import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.computation.batch.BatchReportReaderRule;
import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.Component;
@@ -247,7 +247,7 @@ public class ValidateProjectStepTest {
ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY);
dbClient.componentDao().insert(dbTester.getSession(), project);
- dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project).setCreatedAt(1420088400000L)); // 2015-01-01
+ dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project).setCreatedAt(1420088400000L)); // 2015-01-01
dbTester.getSession().commit();
treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
@@ -274,7 +274,7 @@ public class ValidateProjectStepTest {
ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY);
dbClient.componentDao().insert(dbTester.getSession(), project);
- dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project).setCreatedAt(1433131200000L)); // 2015-06-01
+ dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project).setCreatedAt(1433131200000L)); // 2015-06-01
dbTester.getSession().commit();
treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java
index 90408c55709..f498a21c092 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java
@@ -46,9 +46,9 @@ import org.sonar.server.computation.period.PeriodsHolderRule;
import org.sonar.test.DbTests;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.server.component.SnapshotTesting.createForComponent;
-import static org.sonar.server.component.SnapshotTesting.createForProject;
-import static org.sonar.server.component.SnapshotTesting.createForView;
+import static org.sonar.db.component.SnapshotTesting.createForComponent;
+import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
+import static org.sonar.db.component.SnapshotTesting.newSnapshotForView;
@Category(DbTests.class)
public class ViewsComputeMeasureVariationsStepTest {
@@ -101,7 +101,7 @@ public class ViewsComputeMeasureVariationsStepTest {
@Test
public void do_nothing_when_no_raw_measure() {
- SnapshotDto period1ViewSnapshot = createForView(VIEW_DTO);
+ SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO);
dbClient.snapshotDao().insert(session, period1ViewSnapshot);
dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d));
session.commit();
@@ -129,7 +129,7 @@ public class ViewsComputeMeasureVariationsStepTest {
@Test
public void set_variation() {
// View
- SnapshotDto period1ViewSnapshot = createForView(VIEW_DTO);
+ SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO);
dbClient.snapshotDao().insert(session, period1ViewSnapshot);
dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d));
@@ -158,11 +158,11 @@ public class ViewsComputeMeasureVariationsStepTest {
@Test
public void set_variations_on_all_periods() {
- SnapshotDto period1ViewSnapshot = createForProject(VIEW_DTO).setLast(false);
- SnapshotDto period2ViewSnapshot = createForProject(VIEW_DTO).setLast(false);
- SnapshotDto period3ViewSnapshot = createForProject(VIEW_DTO).setLast(false);
- SnapshotDto period4ViewSnapshot = createForProject(VIEW_DTO).setLast(false);
- SnapshotDto period5ViewSnapshot = createForProject(VIEW_DTO).setLast(false);
+ SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
+ SnapshotDto period2ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
+ SnapshotDto period3ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
+ SnapshotDto period4ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
+ SnapshotDto period5ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
dbClient.snapshotDao().insert(session, period1ViewSnapshot, period2ViewSnapshot, period3ViewSnapshot, period4ViewSnapshot, period5ViewSnapshot);
dbClient.measureDao().insert(session,
@@ -198,7 +198,7 @@ public class ViewsComputeMeasureVariationsStepTest {
@Test
public void set_variation_on_all_numeric_metrics() {
- SnapshotDto period1ViewSnapshot = createForProject(VIEW_DTO);
+ SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO);
dbClient.snapshotDao().insert(session, period1ViewSnapshot);
dbClient.measureDao().insert(session,
newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d),
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java
index 432f5cc8777..fb517d68bf2 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java
@@ -51,7 +51,7 @@ import static org.sonar.db.component.ComponentTesting.newProjectCopy;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.ComponentTesting.newSubView;
import static org.sonar.db.component.ComponentTesting.newView;
-import static org.sonar.server.component.SnapshotTesting.createForProject;
+import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
import static org.sonar.server.computation.component.Component.Type.VIEW;
@@ -182,8 +182,8 @@ public class ViewsPersistSnapshotsStepTest extends BaseStepTest {
public void persist_snapshots_with_periods() {
ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project"));
ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2"));
- SnapshotDto viewSnapshotDto = save(createForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
- SnapshotDto subViewSnapshotDto = save(createForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
+ SnapshotDto viewSnapshotDto = save(newSnapshotForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
+ SnapshotDto subViewSnapshotDto = save(newSnapshotForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
dbTester.getSession().commit();
Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("ABCD").build();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java
index c68780b4fc6..0facac5f24b 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java
@@ -45,7 +45,7 @@ import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.db.user.UserDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.issue.index.IssueIndexer;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
@@ -88,7 +88,7 @@ public class IssueBulkChangeServiceMediumTest {
project = ComponentTesting.newProjectDto().setKey("MyProject");
tester.get(ComponentDao.class).insert(session, project);
- SnapshotDto projectSnapshot = SnapshotTesting.createForProject(project);
+ SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
tester.get(SnapshotDao.class).insert(session, projectSnapshot);
file = ComponentTesting.newFileDto(project).setKey("MyComponent");
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java
index d9f4c802d0c..920f3da5950 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java
@@ -44,7 +44,7 @@ import org.sonar.db.issue.IssueDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.issue.index.IssueIndexer;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
@@ -85,7 +85,7 @@ public class IssueCommentServiceMediumTest {
project = ComponentTesting.newProjectDto();
tester.get(ComponentDao.class).insert(session, project);
- SnapshotDto projectSnapshot = SnapshotTesting.createForProject(project);
+ SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
tester.get(SnapshotDao.class).insert(session, projectSnapshot);
file = ComponentTesting.newFileDto(project);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java
index 282de27bafe..71fa47a68b5 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java
@@ -45,7 +45,7 @@ import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.ForbiddenException;
@@ -208,7 +208,7 @@ public class SearchActionTest {
dbClient.customMeasureDao().insert(dbSession, newCustomMeasure(1, metric)
.setCreatedAt(yesterday)
.setUpdatedAt(yesterday));
- dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(defaultProject));
+ dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newSnapshotForProject(defaultProject));
dbSession.commit();
String response = newRequest()
diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java
index 71cf3db4433..4238a605471 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java
@@ -52,7 +52,7 @@ import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.component.ComponentCleanerService;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.ForbiddenException;
@@ -226,7 +226,7 @@ public class BulkDeleteActionTest {
dbClient.deprecatedRuleDao().insert(dbSession, rule);
IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix);
dbClient.componentDao().insert(dbSession, project);
- SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project));
+ SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newSnapshotForProject(project));
dbClient.issueDao().insert(dbSession, issue);
dbSession.commit();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java
index 1a590fd3df5..4f4bcef3f87 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java
@@ -50,7 +50,7 @@ import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.component.ComponentCleanerService;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.ForbiddenException;
@@ -248,7 +248,7 @@ public class DeleteActionTest {
dbClient.deprecatedRuleDao().insert(dbSession, rule);
IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix);
dbClient.componentDao().insert(dbSession, project);
- SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project));
+ SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newSnapshotForProject(project));
dbClient.issueDao().insert(dbSession, issue);
dbSession.commit();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java
index 124513278f6..0f91139b583 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java
@@ -35,7 +35,7 @@ import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
@@ -142,14 +142,14 @@ public class GhostsActionTest {
.setName("HBase")
.setCreatedAt(DateUtils.parseDateTime("2015-03-04T23:03:44+0100"));
dbClient.componentDao().insert(db.getSession(), hBaseProject);
- dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.createForProject(hBaseProject)
+ dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newSnapshotForProject(hBaseProject)
.setStatus(SnapshotDto.STATUS_UNPROCESSED));
ComponentDto roslynProject = ComponentTesting.newProjectDto("c526ef20-131b-4486-9357-063fa64b5079")
.setKey("com.microsoft.roslyn:roslyn")
.setName("Roslyn")
.setCreatedAt(DateUtils.parseDateTime("2013-03-04T23:03:44+0100"));
dbClient.componentDao().insert(db.getSession(), roslynProject);
- dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.createForProject(roslynProject)
+ dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newSnapshotForProject(roslynProject)
.setStatus(SnapshotDto.STATUS_UNPROCESSED));
db.getSession().commit();
@@ -171,7 +171,7 @@ public class GhostsActionTest {
.setName("ghost-name-" + id)
.setKey("ghost-key-" + id);
dbClient.componentDao().insert(db.getSession(), project);
- SnapshotDto snapshot = SnapshotTesting.createForProject(project)
+ SnapshotDto snapshot = SnapshotTesting.newSnapshotForProject(project)
.setStatus(SnapshotDto.STATUS_UNPROCESSED);
dbClient.snapshotDao().insert(db.getSession(), snapshot);
db.getSession().commit();
@@ -183,7 +183,7 @@ public class GhostsActionTest {
.setName("analyzed-name-" + id)
.setKey("analyzed-key-" + id);
dbClient.componentDao().insert(db.getSession(), project);
- SnapshotDto snapshot = SnapshotTesting.createForProject(project);
+ SnapshotDto snapshot = SnapshotTesting.newSnapshotForProject(project);
dbClient.snapshotDao().insert(db.getSession(), snapshot);
db.getSession().commit();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java
index 18a14f58e2b..c0cc0420755 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java
@@ -37,7 +37,7 @@ import org.sonar.db.component.ComponentDao;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
@@ -74,7 +74,7 @@ public class ProvisionedActionTest {
userSessionRule.setGlobalPermissions(GlobalPermissions.PROVISIONING);
ComponentDto analyzedProject = ComponentTesting.newProjectDto("analyzed-uuid-1");
componentDao.insert(db.getSession(), newProvisionedProject("1"), newProvisionedProject("2"), analyzedProject);
- SnapshotDto snapshot = SnapshotTesting.createForProject(analyzedProject);
+ SnapshotDto snapshot = SnapshotTesting.newSnapshotForProject(analyzedProject);
dbClient.snapshotDao().insert(db.getSession(), snapshot);
db.getSession().commit();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java
index 1e7bc2c2715..75867abb12e 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java
@@ -50,7 +50,7 @@ import org.sonar.db.dashboard.DashboardDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.component.SnapshotTesting;
+import org.sonar.db.component.SnapshotTesting;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
@@ -194,7 +194,7 @@ public class ComponentNavigationActionTest {
ComponentDto project = ComponentTesting.newProjectDto("abcd")
.setKey("polop").setName("Polop").setLanguage("xoo");
dbClient.componentDao().insert(dbTester.getSession(), project);
- dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project));
+ dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project));
dbTester.getSession().commit();
userSessionRule.addProjectUuidPermissions(UserRole.USER, "abcd");
@@ -208,7 +208,7 @@ public class ComponentNavigationActionTest {
ComponentDto project = ComponentTesting.newProjectDto("abcd")
.setKey("polop").setName("Polop").setLanguage("xoo");
dbClient.componentDao().insert(dbTester.getSession(), project);
- dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project));
+ dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project));
dbTester.getSession().commit();
userSessionRule
@@ -354,7 +354,7 @@ public class ComponentNavigationActionTest {
.setPath(directory.path());
dbClient.componentDao().insert(dbTester.getSession(), project, module, directory, file);
- SnapshotDto projectSnapshot = SnapshotTesting.createForProject(project);
+ SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project);
dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
SnapshotDto moduleSnapshot = SnapshotTesting.createForComponent(module, projectSnapshot);
dbClient.snapshotDao().insert(dbTester.getSession(), moduleSnapshot);
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
index 96199463731..4f4a9cc49ca 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
@@ -66,6 +66,14 @@ public class ComponentDao implements Dao {
return componentDto.get();
}
+ public List<ComponentDto> selectByQuery(DbSession session, ComponentQuery query, int offset, int limit) {
+ return mapper(session).selectByQuery(query, new RowBounds(offset, limit));
+ }
+
+ public int countByQuery(DbSession session, ComponentQuery query) {
+ return mapper(session).countByQuery(query);
+ }
+
public boolean existsById(Long id, DbSession session) {
return mapper(session).countById(id) > 0;
}
@@ -186,7 +194,7 @@ public class ComponentDao implements Dao {
* Does not return component copies
*/
public List<ComponentDto> selectComponents(DbSession session, Collection<String> qualifiers, int offset, int limit, @Nullable String query) {
- Map<String, Object> parameters = newHashMapWithExpectedSize(2);
+ Map<String, Object> parameters = newHashMapWithExpectedSize(3);
addProjectQualifier(parameters);
addPartialQueryParameterIfNotNull(parameters, query);
addQualifiers(parameters, qualifiers);
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
index dc2ab91b60b..774c14a378d 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
@@ -57,6 +57,10 @@ public interface ComponentMapper {
List<String> selectExistingUuids(@Param("uuids") Collection<String> uuids);
+ List<ComponentDto> selectByQuery(ComponentQuery query, RowBounds rowBounds);
+
+ int countByQuery(ComponentQuery query);
+
/**
* Return all project (PRJ/TRK) uuids
*/
@@ -127,5 +131,4 @@ public interface ComponentMapper {
void update(ComponentDto componentDto);
void delete(long componentId);
-
}
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java
new file mode 100644
index 00000000000..6c4e361ddfc
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java
@@ -0,0 +1,61 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.db.component;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+import org.sonar.db.Database;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.db.dialect.WildcardPosition.AFTER;
+
+public class ComponentQuery {
+ private final Database database;
+ private final String nameOrKeyQuery;
+ private final String[] qualifiers;
+
+ public ComponentQuery(Database database, @Nullable String nameOrKeyQuery, String... qualifiers) {
+ checkArgument(qualifiers.length > 0, "At least one qualifier must be provided");
+
+ this.database = database;
+ this.nameOrKeyQuery = nameOrKeyQuery;
+ this.qualifiers = qualifiers;
+ }
+
+ public String[] getQualifiers() {
+ return qualifiers;
+ }
+
+ @CheckForNull
+ public String getNameOrKeyQuery() {
+ return nameOrKeyQuery;
+ }
+
+ @CheckForNull
+ public String getNameOrKeyQueryToSqlForResourceIndex() {
+ return database.getDialect().buildLikeValue(nameOrKeyQuery, AFTER).toLowerCase();
+ }
+
+ @CheckForNull
+ public String getNameOrKeyQueryToSqlForProjectKey() {
+ return database.getDialect().buildLikeValue(nameOrKeyQuery, AFTER);
+ }
+}
diff --git a/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java b/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java
index 236c90acd1b..5aeb289f495 100644
--- a/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java
+++ b/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java
@@ -87,4 +87,44 @@ abstract class AbstractDialect implements Dialect {
public int getScrollSingleRowFetchSize() {
return 1;
}
+
+ @Override
+ public String buildLikeValue(String value, WildcardPosition wildcardPosition) {
+ String escapedValue = escapePercentAndUnderscore(value);
+ String wildcard = "%";
+ switch (wildcardPosition) {
+ case BEFORE:
+ escapedValue = wildcard + escapedValue;
+ break;
+ case AFTER:
+ escapedValue += wildcard;
+ break;
+ case BEFORE_AND_AFTER:
+ escapedValue = wildcard + escapedValue + wildcard;
+ break;
+ default:
+ throw new UnsupportedOperationException("Unhandled WildcardPosition: " + wildcardPosition);
+ }
+
+ return appendEscapeBackslachForSomeDb(escapedValue);
+ }
+
+ /**
+ * Replace escape percent and underscore by adding a slash just before
+ */
+ private static String escapePercentAndUnderscore(String value) {
+ return value
+ .replaceAll("%", "\\\\%")
+ .replaceAll("_", "\\\\_");
+ }
+
+ private String appendEscapeBackslachForSomeDb(String value) {
+ return isOracleOrMsSql()
+ ? (value + " ESCAPE '\\'")
+ : value;
+ }
+
+ private boolean isOracleOrMsSql() {
+ return getId().equals(Oracle.ID) || getId().equals(MsSql.ID);
+ }
}
diff --git a/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java b/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java
index 3d0a0f6e730..a6fba169d91 100644
--- a/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java
+++ b/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java
@@ -89,4 +89,10 @@ public interface Dialect {
* @return a boolean
*/
boolean supportsMigration();
+
+ /**
+ * Returns an escaped value in parameter, with the desired wildcards.
+ * Suitable to be used in a like sql query
+ */
+ String buildLikeValue(String value, WildcardPosition wildcardPosition);
}
diff --git a/sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java b/sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java
new file mode 100644
index 00000000000..0b8780b58e7
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java
@@ -0,0 +1,25 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.db.dialect;
+
+public enum WildcardPosition {
+ BEFORE, AFTER, BEFORE_AND_AFTER
+}
diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
index da674300494..8d846a61e39 100644
--- a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
@@ -250,6 +250,43 @@
ORDER BY UPPER(p.name), p.name
</select>
+ <select id="selectByQuery" resultType="Component">
+ select
+ <include refid="componentColumns"/>
+ <include refid="sqlSelectByQuery"/>
+ ORDER BY LOWER(p.name), p.name
+ </select>
+
+ <select id="countByQuery" resultType="int">
+ select count(p.id)
+ <include refid="sqlSelectByQuery"/>
+ </select>
+
+ <sql id="sqlSelectByQuery">
+ from projects p
+ <where>
+ AND p.enabled=${_true}
+ AND p.copy_resource_id is null
+ AND p.qualifier in
+ <foreach collection="qualifiers" item="qualifier" open="(" close=")" separator=",">
+ #{qualifier}
+ </foreach>
+ <if test="nameOrKeyQuery!=null">
+ AND (exists (
+ select 1
+ from resource_index ri
+ where
+ ri.resource_id=p.id
+ AND ri.qualifier in
+ <foreach collection="qualifiers" item="qualifier" open="(" close=")" separator=",">
+ #{qualifier}
+ </foreach>
+ AND ri.kee like #{nameOrKeyQueryToSqlForResourceIndex})
+ OR p.kee like #{nameOrKeyQueryToSqlForProjectKey})
+ </if>
+ </where>
+ </sql>
+
<select id="countRootComponents" resultType="int">
select count(p.id)
from projects p
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
index f7125fd1d34..0ca8782056f 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
@@ -51,6 +51,7 @@ public class ComponentDaoTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+ ComponentDbTester componentDb = new ComponentDbTester(db);
DbSession dbSession = db.getSession();
@@ -59,7 +60,7 @@ public class ComponentDaoTest {
@Test
public void get_by_uuid() {
db.prepareDbUnit(getClass(), "shared.xml");
-
+
ComponentDto result = underTest.selectByUuid(dbSession, "KLMN").get();
assertThat(result).isNotNull();
assertThat(result.uuid()).isEqualTo("KLMN");
@@ -612,8 +613,8 @@ public class ComponentDaoTest {
@Test
public void delete() throws Exception {
- ComponentDto project1= insertProject(newProjectDto().setKey("PROJECT_1"));
- insertProject(newProjectDto().setKey("PROJECT_2"));
+ ComponentDto project1 = componentDb.insertComponent(newProjectDto().setKey("PROJECT_1"));
+ componentDb.insertComponent(newProjectDto().setKey("PROJECT_2"));
underTest.delete(dbSession, project1.getId());
dbSession.commit();
@@ -624,23 +625,62 @@ public class ComponentDaoTest {
@Test
public void select_components_with_paging_query_and_qualifiers() {
- DbSession session = dbSession;
- underTest.insert(session, newProjectDto().setName("aaaa-name"));
- underTest.insert(session, newView());
- underTest.insert(session, newDeveloper("project-name"));
+ underTest.insert(dbSession, newProjectDto().setName("aaaa-name"));
+ underTest.insert(dbSession, newView());
+ underTest.insert(dbSession, newDeveloper("project-name"));
for (int i = 9; i >= 1; i--) {
- underTest.insert(session, newProjectDto().setName("project-" + i));
+ underTest.insert(dbSession, newProjectDto().setName("project-" + i));
}
- List<ComponentDto> result = underTest.selectComponents(session, singleton(Qualifiers.PROJECT), 1, 3, "project");
+ List<ComponentDto> result = underTest.selectComponents(dbSession, singleton(Qualifiers.PROJECT), 1, 3, "project");
assertThat(result).hasSize(3);
assertThat(result).extracting("name").containsExactly("project-2", "project-3", "project-4");
}
- private ComponentDto insertProject(ComponentDto project) {
- underTest.insert(dbSession, project);
- dbSession.commit();
- return project;
+ @Test
+ public void select_by_query_with_paging_query_and_qualifiers() {
+ componentDb.insertProjectAndSnapshot(dbSession, newProjectDto().setName("aaaa-name"));
+ componentDb.insertProjectAndSnapshot(dbSession, newView());
+ componentDb.insertProjectAndSnapshot(dbSession, newDeveloper("project-name"));
+ for (int i = 9; i >= 1; i--) {
+ componentDb.insertProjectAndSnapshot(dbSession, newProjectDto().setName("project-" + i));
+ }
+ db.commit();
+ componentDb.indexProjects();
+
+ ComponentQuery query = new ComponentQuery(db.database(), "oJect", Qualifiers.PROJECT);
+ List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 1, 3);
+
+ assertThat(result).hasSize(3);
+ assertThat(underTest.countByQuery(dbSession, query)).isEqualTo(9);
+ assertThat(result).extracting("name").containsExactly("project-2", "project-3", "project-4");
+ }
+
+ @Test
+ public void select_by_query_name_with_special_characters() {
+ componentDb.insertProjectAndSnapshot(dbSession, newProjectDto().setName("project-_%-name"));
+ db.commit();
+ componentDb.indexProjects();
+
+ ComponentQuery query = new ComponentQuery(db.database(), "-_%-", Qualifiers.PROJECT);
+ List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
+
+ assertThat(result).hasSize(1);
+ assertThat(result.get(0).name()).isEqualTo("project-_%-name");
+ }
+
+ @Test
+ public void select_by_query_key_with_special_characters() {
+ componentDb.insertProjectAndSnapshot(dbSession, newProjectDto()
+ .setKey("project-_%-key"));
+ db.commit();
+ componentDb.indexProjects();
+
+ ComponentQuery query = new ComponentQuery(db.database(), "project-_%-", Qualifiers.PROJECT);
+ List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
+
+ assertThat(result).hasSize(1);
+ assertThat(result.get(0).key()).isEqualTo("project-_%-key");
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java
new file mode 100644
index 00000000000..731a05a866a
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java
@@ -0,0 +1,54 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.db.component;
+
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+
+import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
+
+public class ComponentDbTester {
+ private final DbTester db;
+ private final DbClient dbClient;
+ private final DbSession dbSession;
+
+ public ComponentDbTester(DbTester db) {
+ this.db = db;
+ this.dbClient = db.getDbClient();
+ this.dbSession = db.getSession();
+ }
+
+ public void insertProjectAndSnapshot(DbSession dbSession, ComponentDto component) {
+ dbClient.componentDao().insert(dbSession, component);
+ dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(component));
+ }
+
+ public ComponentDto insertComponent(ComponentDto component) {
+ dbClient.componentDao().insert(dbSession, component);
+ return component;
+ }
+
+ public void indexProjects() {
+ dbClient.componentIndexDao().indexProjects();
+ db.commit();
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java
index e37007386a3..10c3eb0ab43 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/SnapshotTesting.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotTesting.java
@@ -18,12 +18,10 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-package org.sonar.server.component;
+package org.sonar.db.component;
import com.google.common.base.Preconditions;
import org.assertj.core.util.Strings;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.SnapshotDto;
public class SnapshotTesting {
@@ -39,12 +37,12 @@ public class SnapshotTesting {
.setPath(Strings.isNullOrEmpty(parentSnapshot.getPath()) ? Long.toString(parentSnapshot.getId()) + "." : parentSnapshot.getPath() + Long.toString(parentSnapshot.getId()) + ".");
}
- public static SnapshotDto createForProject(ComponentDto project) {
+ public static SnapshotDto newSnapshotForProject(ComponentDto project) {
return createBasicSnapshot(project, project.getId())
.setPath("");
}
- public static SnapshotDto createForView(ComponentDto view) {
+ public static SnapshotDto newSnapshotForView(ComponentDto view) {
return createBasicSnapshot(view, view.getId())
.setPath("");
}
diff --git a/sonar-db/src/test/java/org/sonar/db/dialect/DialectTest.java b/sonar-db/src/test/java/org/sonar/db/dialect/DialectTest.java
new file mode 100644
index 00000000000..c20b8ccd9f4
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/dialect/DialectTest.java
@@ -0,0 +1,53 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.db.dialect;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.dialect.WildcardPosition.AFTER;
+import static org.sonar.db.dialect.WildcardPosition.BEFORE;
+import static org.sonar.db.dialect.WildcardPosition.BEFORE_AND_AFTER;
+
+public class DialectTest {
+
+ Dialect underTest = new H2();
+
+ @Test
+ public void buildLikeValue_with_H2() {
+ String escapedValue = "like-\\_\\%-value";
+ String wildcard = "%";
+
+ assertThat(underTest.buildLikeValue("like-_%-value", BEFORE)).isEqualTo(wildcard + escapedValue);
+ assertThat(underTest.buildLikeValue("like-_%-value", AFTER)).isEqualTo(escapedValue + wildcard);
+ assertThat(underTest.buildLikeValue("like-_%-value", BEFORE_AND_AFTER)).isEqualTo(wildcard + escapedValue + wildcard);
+ }
+
+ @Test
+ public void buildLikeValue_with_Oracle() {
+ underTest = new Oracle();
+ String escapedValue = "like-\\_\\%-value";
+ String wildcard = "%";
+
+ assertThat(underTest.buildLikeValue("like-_%-value", BEFORE_AND_AFTER)).isEqualTo(wildcard + escapedValue + wildcard + " ESCAPE '\\'");
+
+ }
+}