]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6781 Quality Gate Search doesn't work with _
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 2 Oct 2015 07:14:02 +0000 (09:14 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 2 Oct 2015 07:14:02 +0000 (09:14 +0200)
sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java
sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java
sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java

index 7a96fe57b0582c7099c1f0691ab487546bfcc401..bc18f52f35dd4608501d9b65929e9463f8926b32 100644 (file)
@@ -42,7 +42,7 @@ public class ProjectQgateAssociationQuery {
   private final String projectSearch;
 
   // for internal use in MyBatis
-  final String projectSearchSql;
+  private final String projectSearchSql;
 
   // max results per page
   private final int pageSize;
@@ -60,14 +60,15 @@ public class ProjectQgateAssociationQuery {
     this.pageIndex = builder.pageIndex;
   }
 
-  private String projectSearchToSql(@Nullable String s) {
-    String sql = null;
-    if (s != null) {
-      sql = StringUtils.replace(StringUtils.lowerCase(s), "%", "/%");
-      sql = StringUtils.replace(sql, "_", "/_");
-      sql = sql + "%";
+  private String projectSearchToSql(@Nullable String value) {
+    if (value == null) {
+      return null;
     }
-    return sql;
+
+    return value
+      .replaceAll("%", "\\\\%")
+      .replaceAll("_", "\\\\_")
+      .toLowerCase() + "%";
   }
 
   public String gateId() {
@@ -87,6 +88,10 @@ public class ProjectQgateAssociationQuery {
     return projectSearch;
   }
 
+  public String projectSearchSql() {
+    return projectSearchSql;
+  }
+
   public int pageSize() {
     return pageSize;
   }
@@ -140,7 +145,7 @@ public class ProjectQgateAssociationQuery {
         membership = ProjectQgateAssociationQuery.ANY;
       } else {
         Preconditions.checkArgument(AVAILABLE_MEMBERSHIP.contains(membership),
-          "Membership is not valid (got " + membership + "). Availables values are " + AVAILABLE_MEMBERSHIP);
+          "Membership is not valid (got " + membership + "). Available values are " + AVAILABLE_MEMBERSHIP);
       }
     }
 
@@ -158,7 +163,7 @@ public class ProjectQgateAssociationQuery {
     }
 
     public ProjectQgateAssociationQuery build() {
-      Preconditions.checkNotNull(gateId, "Gate ID cant be null.");
+      Preconditions.checkNotNull(gateId, "Gate ID cannot be null.");
       initMembership();
       initPageIndex();
       initPageSize();
index e8b12e11569407bf348b00020a279ab2c53d470b..585280a1f9863ffcf768bea170acdc0678217dbd 100644 (file)
 
 package org.sonar.db.qualitygate;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 
 public class ProjectQgateAssociationQueryTest {
 
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void handle_underscore_and_percent() {
+    ProjectQgateAssociationQuery underTest = ProjectQgateAssociationQuery.builder()
+      .projectSearch("project-_%-search")
+      .gateId("1").build();
+
+    assertThat(underTest.projectSearchSql()).isEqualTo("project-\\_\\%-search%");
+  }
+
   @Test
   public void fail_on_null_login() {
-    ProjectQgateAssociationQuery.Builder builder = ProjectQgateAssociationQuery.builder();
-    builder.gateId(null);
-
-    try {
-      builder.build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(NullPointerException.class).hasMessage("Gate ID cant be null.");
-    }
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage("Gate ID cannot be null");
+
+    ProjectQgateAssociationQuery.Builder builder = ProjectQgateAssociationQuery.builder()
+      .gateId(null);
+
+    builder.build();
   }
 
   @Test
   public void fail_on_invalid_membership() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Membership is not valid (got unknown). Available values are [all, selected, deselected]");
+
     ProjectQgateAssociationQuery.Builder builder = ProjectQgateAssociationQuery.builder();
     builder.gateId("nelson");
-    builder.membership("unknwown");
-
-    try {
-      builder.build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Membership is not valid (got unknwown). Availables values are [all, selected, deselected]");
-    }
-  }
+    builder.membership("unknown");
 
+    builder.build();
+  }
 }
index 1fe6314d951f1eb475e1db6a0057b4dca7944fdc..2b5643d496309aead90dd9ac8f3723351fab6bcf 100644 (file)
@@ -38,14 +38,14 @@ public class QualityGateConditionDaoTest {
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
-  QualityGateConditionDao dao = dbTester.getDbClient().gateConditionDao();
+  QualityGateConditionDao underTest = dbTester.getDbClient().gateConditionDao();
 
   @Test
   public void testInsert() throws Exception {
     dbTester.prepareDbUnit(getClass(), "insert.xml");
     QualityGateConditionDto newCondition = new QualityGateConditionDto()
       .setQualityGateId(1L).setMetricId(2L).setOperator("GT").setWarningThreshold("10").setErrorThreshold("20").setPeriod(3);
-    dao.insert(newCondition);
+    underTest.insert(newCondition);
     dbTester.assertDbUnitTable(getClass(), "insert-result.xml", "quality_gate_conditions", "metric_id", "operator", "error_value", "warning_value", "period");
     assertThat(newCondition.getId()).isNotNull();
   }
@@ -53,14 +53,14 @@ public class QualityGateConditionDaoTest {
   @Test
   public void testSelectForQualityGate() throws Exception {
     dbTester.prepareDbUnit(getClass(), "selectForQualityGate.xml");
-    assertThat(dao.selectForQualityGate(1L)).hasSize(3);
-    assertThat(dao.selectForQualityGate(2L)).hasSize(2);
+    assertThat(underTest.selectForQualityGate(1L)).hasSize(3);
+    assertThat(underTest.selectForQualityGate(2L)).hasSize(2);
   }
 
   @Test
   public void testSelectById() throws Exception {
     dbTester.prepareDbUnit(getClass(), "selectForQualityGate.xml");
-    QualityGateConditionDto selectById = dao.selectById(1L);
+    QualityGateConditionDto selectById = underTest.selectById(1L);
     assertThat(selectById).isNotNull();
     assertThat(selectById.getId()).isNotNull().isNotEqualTo(0L);
     assertThat(selectById.getMetricId()).isEqualTo(2L);
@@ -69,27 +69,27 @@ public class QualityGateConditionDaoTest {
     assertThat(selectById.getQualityGateId()).isEqualTo(1L);
     assertThat(selectById.getWarningThreshold()).isEqualTo("10");
     assertThat(selectById.getErrorThreshold()).isEqualTo("20");
-    assertThat(dao.selectById(42L)).isNull();
+    assertThat(underTest.selectById(42L)).isNull();
   }
 
   @Test
   public void testDelete() throws Exception {
     dbTester.prepareDbUnit(getClass(), "selectForQualityGate.xml");
-    dao.delete(new QualityGateConditionDto().setId(1L));
+    underTest.delete(new QualityGateConditionDto().setId(1L));
     dbTester.assertDbUnitTable(getClass(), "delete-result.xml", "quality_gate_conditions", COLUMNS_WITHOUT_TIMESTAMPS);
   }
 
   @Test
   public void testUpdate() throws Exception {
     dbTester.prepareDbUnit(getClass(), "selectForQualityGate.xml");
-    dao.update(new QualityGateConditionDto().setId(1L).setMetricId(7L).setOperator(">").setPeriod(1).setWarningThreshold("50").setErrorThreshold("80"));
+    underTest.update(new QualityGateConditionDto().setId(1L).setMetricId(7L).setOperator(">").setPeriod(1).setWarningThreshold("50").setErrorThreshold("80"));
     dbTester.assertDbUnitTable(getClass(), "update-result.xml", "quality_gate_conditions", COLUMNS_WITHOUT_TIMESTAMPS);
   }
 
   @Test
   public void shouldCleanConditions() {
     dbTester.prepareDbUnit(getClass(), "shouldCleanConditions.xml");
-    dao.deleteConditionsWithInvalidMetrics();
+    underTest.deleteConditionsWithInvalidMetrics();
     dbTester.assertDbUnit(getClass(), "shouldCleanConditions-result.xml", new String[]{"created_at", "updated_at"}, "quality_gate_conditions");
   }
 }