private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) {
RuleKey ruleKey = issue.ruleKey();
Rule rule = ruleFinder.findByKey(ruleKey);
+ validateRule(issue, rule);
+ ActiveRule activeRule = qProfile.getActiveRule(ruleKey.repository(), ruleKey.rule());
+ if (activeRule == null || activeRule.getRule() == null) {
+ // rule does not exist or is not enabled -> ignore the issue
+ return false;
+ }
+ updateIssue(issue, rule, activeRule);
+ if (filters.accept(issue, violation)) {
+ cache.put(issue);
+ return true;
+ }
+ return false;
+ }
+
+ private void validateRule(DefaultIssue issue, Rule rule){
+ RuleKey ruleKey = issue.ruleKey();
if (rule == null) {
throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey));
}
if (Strings.isNullOrEmpty(rule.getName()) && Strings.isNullOrEmpty(issue.message())) {
throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey));
}
+ }
- ActiveRule activeRule = qProfile.getActiveRule(ruleKey.repository(), ruleKey.rule());
- if (activeRule == null || activeRule.getRule() == null) {
- // rule does not exist or is not enabled -> ignore the issue
- return false;
- }
-
+ private void updateIssue(DefaultIssue issue, Rule rule, ActiveRule activeRule ){
if (Strings.isNullOrEmpty(issue.message())) {
issue.setMessage(rule.getName());
}
issue.setSeverity(activeRule.getSeverity().name());
}
issue.setTechnicalDebt(technicalDebtCalculator.calculTechnicalDebt(issue));
-
- if (filters.accept(issue, violation)) {
- cache.put(issue);
- return true;
- }
- return false;
}
}
import static org.fest.assertions.Assertions.assertThat;
public class IssueChangeDtoTest {
+
@Test
public void create_from_comment() throws Exception {
DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "emmerik", "the comment");
assertThat(dto.getIssueKey()).isEqualTo("ABCDE");
assertThat(dto.getUserLogin()).isEqualTo("emmerik");
}
+
+ @Test
+ public void create_from_diff_without_date() throws Exception {
+ FieldDiffs diffs = new FieldDiffs();
+ diffs.setDiff("severity", "INFO", "BLOCKER");
+ diffs.setUserLogin("emmerik");
+ diffs.setCreationDate(null);
+
+ IssueChangeDto dto = IssueChangeDto.of("ABCDE", diffs);
+
+ assertThat(dto.getChangeData()).isEqualTo("severity=INFO|BLOCKER");
+ assertThat(dto.getChangeType()).isEqualTo("diff");
+ assertThat(dto.getCreatedAt()).isNotNull();
+ assertThat(dto.getUpdatedAt()).isNotNull();
+ assertThat(dto.getIssueChangeCreationDate()).isNull();
+ assertThat(dto.getIssueKey()).isEqualTo("ABCDE");
+ assertThat(dto.getUserLogin()).isEqualTo("emmerik");
+ }
+
+ @Test
+ public void to_string() throws Exception {
+ DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "emmerik", "the comment");
+ IssueChangeDto dto = IssueChangeDto.of(comment);
+ assertThat(dto.toString()).contains("ABCDE");
+ }
}
assertThat(result.getMeasureId()).isEqualTo(1);
assertThat(result.getSnapshotId()).isEqualTo(1);
assertThat(result.getText()).isNotNull();
+ assertThat(result.getData()).isNotNull();
// FIXME failing because data is returned in wrong format
// assertThat(result.getText()).isEqualTo("test");
}
+
+ @Test
+ public void find_by_component_key_and_metric_key_without_text() throws Exception {
+ setupData("find_by_component_key_and_metric_key_without_text");
+
+ MeasureDataDto result = dao.findByComponentKeyAndMetricKey("org.sonar.core.measure.db.MeasureData", "authors_by_line");
+ assertThat(result.getId()).isEqualTo(1);
+ assertThat(result.getMeasureId()).isEqualTo(1);
+ assertThat(result.getSnapshotId()).isEqualTo(1);
+ assertThat(result.getText()).isNull();
+ assertThat(result.getData()).isNull();
+ }
}
--- /dev/null
+<dataset>
+
+ <snapshots id="1" project_id="1" islast="[true]" />
+ <project_measures id="1" snapshot_id="1" metric_id="1"/>
+ <metrics id="1" name="authors_by_line"/>
+ <projects id="1" kee="org.sonar.core.measure.db.MeasureData" enabled="[true]"/>
+
+ <measure_data id="1" measure_id="1" snapshot_id="1" data="[null]"/>
+
+</dataset>
public ActiveRule setParameter(String key, String value) {
RuleParam ruleParameter = rule.getParam(key);
if (ruleParameter != null) {
- activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, key, value));
+ activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, value));
}
return this;
}
* @deprecated visibility should be decreased to protected or package
*/
@Deprecated
- public ActiveRuleParam(ActiveRule activeRule, RuleParam ruleParam, String paramKey, String value) {
+ public ActiveRuleParam(ActiveRule activeRule, RuleParam ruleParam, String value) {
this.activeRule = activeRule;
this.ruleParam = ruleParam;
this.value = value;
- this.paramKey = paramKey;
+ this.paramKey = ruleParam.getKey();
}
public ActiveRule getActiveRule() {
@Override
public Object clone() {
- return new ActiveRuleParam(getActiveRule(), getRuleParam(), getParamKey(), getValue());
+ return new ActiveRuleParam(getActiveRule(), getRuleParam(), getValue());
}
}
while (reader.hasMoreChildren()) {
reader.moveDown();
Map<String, String> valuesParam = readNode(reader);
- ActiveRuleParam activeRuleParam = new ActiveRuleParam(null, new RuleParam(null, valuesParam.get(KEY), null, null), valuesParam.get(KEY),
- valuesParam.get(VALUE));
+ ActiveRuleParam activeRuleParam = new ActiveRuleParam(null, new RuleParam(null, valuesParam.get(KEY), null, null), valuesParam.get(VALUE));
params.add(activeRuleParam);
reader.moveUp();
}
public class Rule {
+ public static final String MANUAL_REPOSITORY_KEY = "manual";
+
private int id;
private RuleKey ruleKey;
private String language;
public class RuleDocumentParser {
- public static Rule parse(Map<String, Object> ruleSource) {
- Rule.Builder ruleBuilder = new Rule.Builder();
+ private void RuleDocumentParser() {
+ // Utility class
+ }
- ruleBuilder.setId((Integer) ruleSource.get(RuleDocument.FIELD_ID));
- ruleBuilder.setKey((String) ruleSource.get(RuleDocument.FIELD_KEY));
- ruleBuilder.setLanguage((String) ruleSource.get(RuleDocument.FIELD_LANGUAGE));
- ruleBuilder.setRepositoryKey((String) ruleSource.get(RuleDocument.FIELD_REPOSITORY_KEY));
- ruleBuilder.setSeverity((String) ruleSource.get(RuleDocument.FIELD_SEVERITY));
- ruleBuilder.setName((String) ruleSource.get(RuleDocument.FIELD_NAME));
- ruleBuilder.setDescription((String) ruleSource.get(RuleDocument.FIELD_DESCRIPTION));
- ruleBuilder.setStatus((String) ruleSource.get(RuleDocument.FIELD_STATUS));
- ruleBuilder.setCardinality((String) ruleSource.get("cardinality"));
- ruleBuilder.setTemplateId((Integer) ruleSource.get(RuleDocument.FIELD_TEMPLATE_ID));
- ruleBuilder.setCreatedAt(parseOptionalDate(RuleDocument.FIELD_CREATED_AT, ruleSource));
- ruleBuilder.setUpdatedAt(parseOptionalDate(RuleDocument.FIELD_UPDATED_AT, ruleSource));
+ public static Rule parse(Map<String, Object> ruleSource) {
+ Rule.Builder ruleBuilder = new Rule.Builder()
+ .setId((Integer) ruleSource.get(RuleDocument.FIELD_ID))
+ .setKey((String) ruleSource.get(RuleDocument.FIELD_KEY))
+ .setLanguage((String) ruleSource.get(RuleDocument.FIELD_LANGUAGE))
+ .setRepositoryKey((String) ruleSource.get(RuleDocument.FIELD_REPOSITORY_KEY))
+ .setSeverity((String) ruleSource.get(RuleDocument.FIELD_SEVERITY))
+ .setName((String) ruleSource.get(RuleDocument.FIELD_NAME))
+ .setDescription((String) ruleSource.get(RuleDocument.FIELD_DESCRIPTION))
+ .setStatus((String) ruleSource.get(RuleDocument.FIELD_STATUS))
+ .setCardinality((String) ruleSource.get("cardinality"))
+ .setTemplateId((Integer) ruleSource.get(RuleDocument.FIELD_TEMPLATE_ID))
+ .setCreatedAt(parseOptionalDate(RuleDocument.FIELD_CREATED_AT, ruleSource))
+ .setUpdatedAt(parseOptionalDate(RuleDocument.FIELD_UPDATED_AT, ruleSource));
if (ruleSource.containsKey(RuleDocument.FIELD_NOTE)) {
Map<String, Object> ruleNoteDocument = (Map<String, Object>) ruleSource.get(RuleDocument.FIELD_NOTE);
List<RuleParam> params = Lists.newArrayList();
if (ruleSource.containsKey(RuleDocument.FIELD_PARAMS)) {
Map<String, Map<String, Object>> ruleParams = Maps.newHashMap();
- for (Map<String, Object> ruleParam: (List<Map<String, Object>>) ruleSource.get(RuleDocument.FIELD_PARAMS)) {
+ for (Map<String, Object> ruleParam : (List<Map<String, Object>>) ruleSource.get(RuleDocument.FIELD_PARAMS)) {
ruleParams.put((String) ruleParam.get(RuleDocument.FIELD_PARAM_KEY), ruleParam);
}
- for(Map.Entry<String, Map<String, Object>> ruleParam: ruleParams.entrySet()) {
+ for (Map.Entry<String, Map<String, Object>> ruleParam : ruleParams.entrySet()) {
RuleParamType type = RuleParamType.parse((String) ruleParam.getValue().get(RuleDocument.FIELD_PARAM_TYPE));
params.add(new RuleParam(
(String) ruleParam.getValue().get(RuleDocument.FIELD_PARAM_KEY),
List<String> systemTags = newArrayList();
if (ruleSource.containsKey(RuleDocument.FIELD_SYSTEM_TAGS)) {
- for (String tag: (List<String>) ruleSource.get(RuleDocument.FIELD_SYSTEM_TAGS)) {
+ for (String tag : (List<String>) ruleSource.get(RuleDocument.FIELD_SYSTEM_TAGS)) {
systemTags.add(tag);
}
}
List<String> adminTags = newArrayList();
if (ruleSource.containsKey(RuleDocument.FIELD_ADMIN_TAGS)) {
- for (String tag: (List<String>) ruleSource.get(RuleDocument.FIELD_ADMIN_TAGS)) {
+ for (String tag : (List<String>) ruleSource.get(RuleDocument.FIELD_ADMIN_TAGS)) {
adminTags.add(tag);
}
}
@CheckForNull
private Rule findRule(RuleKey ruleKey) {
- if (ruleKey.repository().equals("manual")) {
+ if (ruleKey.repository().equals(Rule.MANUAL_REPOSITORY_KEY)) {
org.sonar.api.rules.Rule rule = ruleFinder.findByKey(ruleKey);
if (rule != null) {
return new Rule.Builder()
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.qualityprofile;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class QProfileTest {
+
+ @Test
+ public void test_getters_and_setters() {
+ QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java").setParent("Parent").setUsed(true).setVersion(1);
+
+ assertThat(profile.id()).isEqualTo(1);
+ assertThat(profile.name()).isEqualTo("Default");
+ assertThat(profile.language()).isEqualTo("java");
+ assertThat(profile.parent()).isEqualTo("Parent");
+ assertThat(profile.used()).isTrue();
+ assertThat(profile.version()).isEqualTo(1);
+ }
+
+ @Test
+ public void to_string() throws Exception {
+ assertThat(new QProfile().setId(1).setName("Default").setLanguage("java").setParent("Parent").setUsed(true).setVersion(1).toString())
+ .contains("[id=1,name=Default,language=java,parent=Parent,version=1,used=true]");
+ }
+
+ @Test
+ public void is_inherited() throws Exception {
+ assertThat(new QProfile().setId(1).setName("Default").setLanguage("java").setParent("Parent").isInherited()).isTrue();
+ assertThat(new QProfile().setId(1).setName("Default").setLanguage("java").setParent(null).isInherited()).isFalse();
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.rule;
+
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.check.Cardinality;
+
+import java.util.Date;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RuleTest {
+
+ @Test
+ public void test_getters_and_setters() throws Exception {
+ Rule rule = new Rule.Builder()
+ .setId(1)
+ .setKey("AvoidCycle")
+ .setRepositoryKey("squid")
+ .setName("Avoid Cycle")
+ .setDescription("Avoid cycle between packages")
+ .setLanguage("java")
+ .setSeverity(Severity.BLOCKER)
+ .setStatus("BETA")
+ .setCardinality(Cardinality.SINGLE.name())
+ .setTemplateId(2)
+ .setRuleNote(new RuleNote("Some note", "john", new Date(), new Date()))
+ .setAdminTags(newArrayList("AdminTag"))
+ .setSystemTags(newArrayList("SysTag"))
+ .setParams(newArrayList(new RuleParam("key", "desc", "default", RuleParamType.STRING)))
+ .setCreatedAt(new Date())
+ .setUpdatedAt(new Date())
+ .build();
+
+ assertThat(rule.id()).isEqualTo(1);
+ assertThat(rule.ruleKey()).isEqualTo(RuleKey.of("squid", "AvoidCycle"));
+ assertThat(rule.name()).isEqualTo("Avoid Cycle");
+ assertThat(rule.description()).isEqualTo("Avoid cycle between packages");
+ assertThat(rule.language()).isEqualTo("java");
+ assertThat(rule.severity()).isEqualTo("BLOCKER");
+ assertThat(rule.status()).isEqualTo("BETA");
+ assertThat(rule.cardinality()).isEqualTo("SINGLE");
+ assertThat(rule.templateId()).isEqualTo(2);
+ assertThat(rule.ruleNote()).isNotNull();
+ assertThat(rule.adminTags()).hasSize(1);
+ assertThat(rule.systemTags()).hasSize(1);
+ assertThat(rule.params()).hasSize(1);
+ assertThat(rule.createdAt()).isNotNull();
+ assertThat(rule.updatedAt()).isNotNull();
+ }
+
+ @Test
+ public void is_template() throws Exception {
+ assertThat(new Rule.Builder()
+ .setId(1)
+ .setKey("AvoidCycle")
+ .setRepositoryKey("squid")
+ .setName("Avoid Cycle")
+ .setDescription("Avoid cycle between packages")
+ .setLanguage("java")
+ .setSeverity(Severity.BLOCKER)
+ .setStatus("BETA")
+ .setCardinality(Cardinality.MULTIPLE.name())
+ .setCreatedAt(new Date())
+ .build().isTemplate()).isTrue();
+
+ assertThat(new Rule.Builder()
+ .setId(1)
+ .setKey("AvoidCycle")
+ .setRepositoryKey("squid")
+ .setName("Avoid Cycle")
+ .setDescription("Avoid cycle between packages")
+ .setLanguage("java")
+ .setSeverity(Severity.BLOCKER)
+ .setStatus("BETA")
+ .setCardinality(Cardinality.SINGLE.name())
+ .setCreatedAt(new Date())
+ .build().isTemplate()).isFalse();
+ }
+
+ @Test
+ public void is_copy_of_template() throws Exception {
+ assertThat(new Rule.Builder()
+ .setId(1)
+ .setKey("AvoidCycle")
+ .setRepositoryKey("squid")
+ .setName("Avoid Cycle")
+ .setDescription("Avoid cycle between packages")
+ .setLanguage("java")
+ .setSeverity(Severity.BLOCKER)
+ .setStatus("BETA")
+ .setCardinality(Cardinality.MULTIPLE.name())
+ .setTemplateId(null)
+ .setCreatedAt(new Date())
+ .build().isEditable()).isFalse();
+
+ assertThat(new Rule.Builder()
+ .setId(1)
+ .setKey("AvoidCycle")
+ .setRepositoryKey("squid")
+ .setName("Avoid Cycle")
+ .setDescription("Avoid cycle between packages")
+ .setLanguage("java")
+ .setSeverity(Severity.BLOCKER)
+ .setStatus("BETA")
+ .setCardinality(Cardinality.SINGLE.name())
+ .setTemplateId(2)
+ .setCreatedAt(new Date())
+ .build().isEditable()).isTrue();
+ }
+}
+
+
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class RulesTest {
rules = new Rules(ruleDao, ruleOperations, ruleRegistry);
}
-
@Test
public void create_rule_note() throws Exception {
RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle");
verify(fakeTypeValidation).validate("10", newArrayList("a"));
}
+ @Test
+ public void validate__multiple_values() throws Exception {
+ TypeValidation fakeTypeValidation = mock(TypeValidation.class);
+ when(fakeTypeValidation.key()).thenReturn("Fake");
+
+ TypeValidations typeValidations = new TypeValidations(newArrayList(fakeTypeValidation));
+ typeValidations.validate(newArrayList("10", "11", "12"), "Fake", newArrayList("11"));
+
+ verify(fakeTypeValidation).validate("10", newArrayList("11"));
+ }
+
@Test
public void fail_on_unknown_type() throws Exception {
TypeValidation fakeTypeValidation = mock(TypeValidation.class);