private final String projectSearch;
// for internal use in MyBatis
- final String projectSearchSql;
+ private final String projectSearchSql;
// max results per page
private final int pageSize;
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() {
return projectSearch;
}
+ public String projectSearchSql() {
+ return projectSearchSql;
+ }
+
public int pageSize() {
return pageSize;
}
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);
}
}
}
public ProjectQgateAssociationQuery build() {
- Preconditions.checkNotNull(gateId, "Gate ID cant be null.");
+ Preconditions.checkNotNull(gateId, "Gate ID cannot be null.");
initMembership();
initPageIndex();
initPageSize();
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();
+ }
}
@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();
}
@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);
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");
}
}