From 770be1eac18e8a7f99f0596938f38054e2abfb40 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 2 Oct 2015 09:14:02 +0200 Subject: [PATCH] SONAR-6781 Quality Gate Search doesn't work with _ --- .../ProjectQgateAssociationQuery.java | 25 ++++++---- .../ProjectQgateAssociationQueryTest.java | 46 +++++++++++-------- .../QualityGateConditionDaoTest.java | 18 ++++---- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java b/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java index 7a96fe57b05..bc18f52f35d 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java +++ b/sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java @@ -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(); diff --git a/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java b/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java index e8b12e11569..585280a1f98 100644 --- a/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java +++ b/sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java @@ -20,38 +20,46 @@ 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(); + } } diff --git a/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java index 1fe6314d951..2b5643d4963 100644 --- a/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java @@ -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"); } } -- 2.39.5