]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR_3621 add missing support of key regexp
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 9 Oct 2012 12:17:53 +0000 (14:17 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 9 Oct 2012 12:17:53 +0000 (14:17 +0200)
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java
sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java
sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java
sonar-server/src/main/webapp/WEB-INF/app/models/filters.rb

index 0fbf9f2f6d6886f4cd234b54058a6aa03ad0462b..9b9c124aa13dfd6a473bab116477f9b90f0175bc 100644 (file)
@@ -34,6 +34,7 @@ public class MeasureFilter {
   private List<String> resourceScopes = Lists.newArrayList();
   private List<String> resourceQualifiers = Lists.newArrayList();
   private List<String> resourceLanguages = Lists.newArrayList();
+  private String resourceKeyRegexp;
   private String resourceName;
   private Date fromDate = null, toDate = null;
   private boolean userFavourites = false;
@@ -105,6 +106,15 @@ public class MeasureFilter {
     return this;
   }
 
+  public String getResourceKeyRegexp() {
+    return resourceKeyRegexp;
+  }
+
+  public MeasureFilter setResourceKeyRegexp(String s) {
+    this.resourceKeyRegexp = s;
+    return this;
+  }
+
   public MeasureFilter addCondition(MeasureFilterCondition condition) {
     this.measureConditions.add(condition);
     return this;
index 7cf092bed0088bc62f088349d59f9a33941a6dbf..c1871be38deed8b5f0d32aca8ed0cf5c907ff627 100644 (file)
@@ -58,6 +58,7 @@ public class MeasureFilterDecoder implements ServerComponent {
     filter.setResourceQualifiers((List<String>) map.get("qualifiers"));
     filter.setResourceLanguages((List<String>) map.get("languages"));
     filter.setResourceName((String) map.get("name"));
+    filter.setResourceKeyRegexp((String) map.get("keyRegexp"));
 
     if (map.containsKey("fromDate")) {
       filter.setFromDate(toDate(map, "fromDate"));
index b529f5a384aabc41147f9bade3e7b6280eac302f..f930d07118edbbc345e0d379385faf89512097da 100644 (file)
@@ -52,6 +52,7 @@ public class MeasureFilterExecutor implements ServerComponent {
       if (isValid(filter, context)) {
         MeasureFilterSql sql = new MeasureFilterSql(database, filter, context);
         context.setSql(sql.sql());
+        System.out.println(sql.sql());
         Connection connection = session.getConnection();
         rows = sql.execute(connection);
       } else {
index 3952a86396fa3d130ece23d793f241854ca35d55..40f35126b392bd6c64652636b8db629e6a7efc05 100644 (file)
@@ -173,6 +173,14 @@ class MeasureFilterSql {
       }
       sql.append(") ");
     }
+    if (StringUtils.isNotBlank(filter.getResourceKeyRegexp())) {
+      sql.append(" AND UPPER(p.kee) LIKE '");
+      // limitation : special characters _ and % are not escaped
+      String regexp = StringEscapeUtils.escapeSql(filter.getResourceKeyRegexp());
+      regexp = StringUtils.replaceChars(regexp, '*', '%');
+      regexp = StringUtils.replaceChars(regexp, '?', '_');
+      sql.append(StringUtils.upperCase(regexp)).append("'");
+    }
     SnapshotDto baseSnapshot = context.getBaseSnapshot();
     if (baseSnapshot != null) {
       if (filter.isOnBaseResourceChildren()) {
index 750cd6667d011f43e93488da4dab5d9be2217d66..3dedb2d09a114a60f7813cf3e9e945a4d5f2ec24 100644 (file)
@@ -51,7 +51,7 @@ public class MeasureFilterDecoderTest {
   @Test
   public void should_decode() throws ParseException {
     String json = "{\"base\": \"org.struts\", \"onBaseChildren\": true, \"scopes\": [\"PRJ\"], " +
-      "\"qualifiers\": [\"TRK\",\"CLA\"], " +
+      "\"qualifiers\": [\"TRK\",\"CLA\"], \"keyRegexp\": \"*foo*\"" +
       "\"languages\": [\"java\", \"php\"], \"name\": \"Struts\", \"fromDate\": \"2012-12-25\", " +
       "\"toDate\": \"2013-01-31\", " +
       "\"favourites\": true, " +
@@ -66,6 +66,7 @@ public class MeasureFilterDecoderTest {
     assertThat(filter.getResourceQualifiers()).containsExactly("TRK", "CLA");
     assertThat(filter.getResourceLanguages()).containsExactly("java", "php");
     assertThat(filter.getResourceName()).isEqualTo("Struts");
+    assertThat(filter.getResourceKeyRegexp()).isEqualTo("*foo*");
     assertThat(filter.getFromDate().getYear()).isEqualTo(2012 - 1900);
     assertThat(filter.getToDate().getYear()).isEqualTo(2013 - 1900);
     assertThat(filter.isOnFavourites()).isTrue();
@@ -107,6 +108,7 @@ public class MeasureFilterDecoderTest {
     assertThat(filter.getResourceQualifiers()).isEmpty();
     assertThat(filter.getResourceLanguages()).isEmpty();
     assertThat(filter.getResourceName()).isNull();
+    assertThat(filter.getResourceKeyRegexp()).isNull();
     assertThat(filter.getFromDate()).isNull();
     assertThat(filter.getToDate()).isNull();
     assertThat(filter.isOnFavourites()).isFalse();
index e2eb14507bf190a60106307a5c634b38247a3ebe..97412dcb14e523d275695a9cb3b28e12a9914fd4 100644 (file)
@@ -310,6 +310,23 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
     verifyPhpProject(rows.get(0));
   }
 
+  @Test
+  public void filter_by_resource_key_star_regexp() throws SQLException {
+    MeasureFilter filter = new MeasureFilter().setResourceQualifiers("TRK").setResourceKeyRegexp("java*");
+    List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+
+    assertThat(rows).hasSize(1);
+    verifyJavaProject(rows.get(0));
+  }
+
+  @Test
+  public void filter_by_resource_key_exclamation_mark() throws SQLException {
+    MeasureFilter filter = new MeasureFilter().setResourceQualifiers("TRK").setResourceKeyRegexp("JaV?_proje*");
+    List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+    assertThat(rows).hasSize(1);
+    verifyJavaProject(rows.get(0));
+  }
+
   @Test
   public void filter_by_base_resource() throws SQLException {
     MeasureFilter filter = new MeasureFilter().setResourceQualifiers("CLA").setBaseResourceKey("java_project");
index 67198337ad83dca1f544bd337df84bdbd205bf73..5428d14dad14c722749d86d8f97c791b8a3b83a2 100644 (file)
@@ -44,7 +44,7 @@ class Filters
 
     key_criterion=filter.criterion('key')
     if key_criterion
-      #java_filter.setKeyRegexp(key_criterion.text_value)
+      filter_json[:keyRegexp]=name_criterion.text_value
     end
 
     name_criterion=filter.criterion('name')