diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-10-10 16:00:46 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-10-10 16:21:24 +0200 |
commit | b3b21c66379d99688ce5e65a811fdd79bf0a9c6d (patch) | |
tree | 47ecf0137958dfca3ea323e5331b852066352f42 | |
parent | 611c7b7227895160f699204964a186e71cd02f4f (diff) | |
download | sonarqube-b3b21c66379d99688ce5e65a811fdd79bf0a9c6d.tar.gz sonarqube-b3b21c66379d99688ce5e65a811fdd79bf0a9c6d.zip |
SONAR-3621 add some unit tests
4 files changed, 76 insertions, 21 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java index c7dcc8eceb9..ebf5cec2809 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java @@ -88,9 +88,15 @@ public class MeasureFilterExecutor implements ServerComponent { } static boolean isValid(MeasureFilter filter, MeasureFilterContext context) { - boolean valid = - !(filter.isOnBaseResourceChildren() && context.getBaseSnapshot() == null) && - !(filter.isOnFavourites() && context.getUserId() == null); + boolean valid = !(filter.isOnBaseResourceChildren() && context.getBaseSnapshot() == null); + valid &= !(filter.isOnFavourites() && context.getUserId() == null); + valid &= validateMeasureConditions(filter); + valid &= validateSort(filter); + return valid; + } + + private static boolean validateMeasureConditions(MeasureFilter filter) { + boolean valid = true; for (MeasureFilterCondition condition : filter.getMeasureConditions()) { if (condition.period() != null && condition.period() < 1) { valid = false; @@ -99,6 +105,11 @@ public class MeasureFilterExecutor implements ServerComponent { valid = false; } } + return valid; + } + + private static boolean validateSort(MeasureFilter filter) { + boolean valid = true; if (filter.sort().getPeriod() != null && filter.sort().getPeriod() < 1) { valid = false; } diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java index d0e2d53f731..3eae1162992 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java @@ -101,6 +101,8 @@ class MeasureFilterSort { column = "pm.text_value"; } break; + default: + throw new IllegalArgumentException("Unsupported sorting: " + field); } return column; } diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java index d554b828cac..68015b1e07e 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java @@ -156,29 +156,46 @@ class MeasureFilterSql { sql.append(" AND p.language IN "); appendInStatement(filter.getResourceLanguages(), sql); } + appendDateConditions(); + appendFavouritesCondition(); + appendResourceNameCondition(); + appendResourceKeyCondition(); + appendResourceBaseCondition(); + } + + private void appendDateConditions() { if (filter.getFromDate() != null) { sql.append(" AND s.created_at >= ? "); - dateParameters.add(new java.sql.Date(filter.getFromDate().getTime())); + dateParameters.add(new Date(filter.getFromDate().getTime())); } if (filter.getToDate() != null) { sql.append(" AND s.created_at <= ? "); - dateParameters.add(new java.sql.Date(filter.getToDate().getTime())); + dateParameters.add(new Date(filter.getToDate().getTime())); } + } + + private void appendFavouritesCondition() { if (filter.isOnFavourites()) { sql.append(" AND props.prop_key='favourite' AND props.resource_id IS NOT NULL AND props.user_id="); sql.append(context.getUserId()); sql.append(" "); } - if (StringUtils.isNotBlank(filter.getResourceName())) { - sql.append(" AND s.project_id IN (SELECT rindex.resource_id FROM resource_index rindex WHERE rindex.kee like '"); - sql.append(StringEscapeUtils.escapeSql(StringUtils.lowerCase(filter.getResourceName()))); - sql.append("%'"); - if (!filter.getResourceQualifiers().isEmpty()) { - sql.append(" AND rindex.qualifier IN "); - appendInStatement(filter.getResourceQualifiers(), sql); + } + + private void appendResourceBaseCondition() { + SnapshotDto baseSnapshot = context.getBaseSnapshot(); + if (baseSnapshot != null) { + if (filter.isOnBaseResourceChildren()) { + sql.append(" AND s.parent_snapshot_id=").append(baseSnapshot.getId()); + } else { + Long rootSnapshotId = (baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId()); + sql.append(" AND s.root_snapshot_id=").append(rootSnapshotId); + sql.append(" AND s.path LIKE '").append(StringUtils.defaultString(baseSnapshot.getPath())).append(baseSnapshot.getId()).append(".%'"); } - sql.append(") "); } + } + + private void appendResourceKeyCondition() { if (StringUtils.isNotBlank(filter.getResourceKeyRegexp())) { sql.append(" AND UPPER(p.kee) LIKE '"); // limitation : special characters _ and % are not escaped @@ -187,15 +204,18 @@ class MeasureFilterSql { regexp = StringUtils.replaceChars(regexp, '?', '_'); sql.append(StringUtils.upperCase(regexp)).append("'"); } - SnapshotDto baseSnapshot = context.getBaseSnapshot(); - if (baseSnapshot != null) { - if (filter.isOnBaseResourceChildren()) { - sql.append(" AND s.parent_snapshot_id=").append(baseSnapshot.getId()); - } else { - Long rootSnapshotId = (baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId()); - sql.append(" AND s.root_snapshot_id=").append(rootSnapshotId); - sql.append(" AND s.path LIKE '").append(StringUtils.defaultString(baseSnapshot.getPath())).append(baseSnapshot.getId()).append(".%'"); + } + + private void appendResourceNameCondition() { + if (StringUtils.isNotBlank(filter.getResourceName())) { + sql.append(" AND s.project_id IN (SELECT rindex.resource_id FROM resource_index rindex WHERE rindex.kee like '"); + sql.append(StringEscapeUtils.escapeSql(StringUtils.lowerCase(filter.getResourceName()))); + sql.append("%'"); + if (!filter.getResourceQualifiers().isEmpty()) { + sql.append(" AND rindex.qualifier IN "); + appendInStatement(filter.getResourceQualifiers(), sql); } + sql.append(") "); } } diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java index 97412dcb14e..5d3c01d107c 100644 --- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java @@ -126,6 +126,28 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase { } @Test + public void sort_by_ascending_resource_key() throws SQLException { + MeasureFilter filter = new MeasureFilter().setResourceQualifiers("CLA").setSortAsc(true).setSortOn(MeasureFilterSort.Field.KEY); + List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext()); + + // Big -> Tiny + assertThat(rows).hasSize(2); + verifyJavaBigFile(rows.get(0)); + verifyJavaTinyFile(rows.get(1)); + } + + @Test + public void sort_by_ascending_resource_version() throws SQLException { + MeasureFilter filter = new MeasureFilter().setResourceQualifiers("TRK").setSortAsc(true).setSortOn(MeasureFilterSort.Field.VERSION); + List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext()); + + // Java Project 1.0 then Php Project 3.0 + assertThat(rows).hasSize(2); + verifyJavaProject(rows.get(0)); + verifyPhpProject(rows.get(1)); + } + + @Test public void sort_by_descending_resource_name() throws SQLException { MeasureFilter filter = new MeasureFilter().setResourceQualifiers("CLA").setSortAsc(false); List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext()); |