diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-30 15:17:55 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-30 15:25:18 +0200 |
commit | ed7933c1bb27808106fe52e50e018207ffea5fed (patch) | |
tree | 37180022812adbbe5a2c088dab15e1076e475d56 | |
parent | c477a34b4ac84dbac9bba08f13dc1718da1b2231 (diff) | |
download | sonarqube-ed7933c1bb27808106fe52e50e018207ffea5fed.tar.gz sonarqube-ed7933c1bb27808106fe52e50e018207ffea5fed.zip |
Fix quality flaws
10 files changed, 84 insertions, 61 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java index 9d8bae793f5..2dbe12f40ce 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsAction.java @@ -215,8 +215,7 @@ public class ProjectReferentialsAction implements RequestHandler { rule.name(), activeRule.severity(), rule.internalKey(), - qProfile.language() - ); + qProfile.language()); for (Map.Entry<String, String> entry : activeRule.params().entrySet()) { inputActiveRule.addParam(entry.getKey(), entry.getValue()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java index 0e9f286f265..ccc2afb7032 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java @@ -27,6 +27,8 @@ import org.sonar.core.component.ComponentDto; import org.sonar.core.persistence.DbSession; import org.sonar.server.component.persistence.ComponentDao; +import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -90,42 +92,45 @@ public class DuplicationsJsonWriter implements ServerComponent { ComponentDto file = componentDao.getNullableByKey(session, componentKey); if (file != null) { json.name(ref).beginObject(); - json.prop("key", file.key()); - json.prop("name", file.longName()); - - Long projectId = file.projectId(); - ComponentDto project = projectsById.get(file.projectId()); - if (project == null && projectId != null) { - project = componentDao.getById(projectId, session); - if (project != null) { - projectsById.put(projectId, project); - } - } - - Long subProjectId = file.subProjectId(); - ComponentDto subProject = subProjectsById.get(subProjectId); - if (subProject == null && subProjectId != null) { - subProject = componentDao.getById(subProjectId, session); - if (subProject != null) { - subProjectsById.put(subProject.getId(), subProject); - } - } - - if (project != null) { - json.prop("project", project.key()); - json.prop("projectName", project.longName()); - - // Do not return sub project if sub project and project are the same - boolean displaySubProject = subProject != null && !subProject.getId().equals(project.getId()); - if (displaySubProject) { - json.prop("subProject", subProject.key()); - json.prop("subProjectName", subProject.longName()); - } - } + + addFile(json, file); + ComponentDto project = getProject(file.projectId(), projectsById, session); + ComponentDto subProject = getProject(file.subProjectId(), subProjectsById, session); + addProject(json, project, subProject); json.endObject(); } } } + private void addFile(JsonWriter json, ComponentDto file) { + json.prop("key", file.key()); + json.prop("name", file.longName()); + } + + private void addProject(JsonWriter json, @Nullable ComponentDto project, @Nullable ComponentDto subProject) { + if (project != null) { + json.prop("project", project.key()); + json.prop("projectName", project.longName()); + + // Do not return sub project if sub project and project are the same + boolean displaySubProject = subProject != null && !subProject.getId().equals(project.getId()); + if (displaySubProject) { + json.prop("subProject", subProject.key()); + json.prop("subProjectName", subProject.longName()); + } + } + } + + private ComponentDto getProject(@Nullable Long projectId, Map<Long, ComponentDto> projectsById, DbSession session) { + ComponentDto project = projectsById.get(projectId); + if (project == null && projectId != null) { + project = componentDao.getById(projectId, session); + if (project != null) { + projectsById.put(project.getId(), project); + } + } + return project; + } + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java b/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java index f6f0fb7fade..920b84c60dc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java @@ -36,7 +36,7 @@ public class RulesAggregation { } public RulesAggregation add(RuleDto ruleDto) { - rules.add(new Rule().setRuleKey(ruleDto.getKey()).setName(ruleDto.getName())); + rules.add(new Rule(ruleDto.getKey(), ruleDto.getName())); return this; } @@ -53,24 +53,19 @@ public class RulesAggregation { private RuleKey ruleKey; private String name; - public RuleKey ruleKey() { - return ruleKey; + public Rule(RuleKey ruleKey, String name) { + this.ruleKey = ruleKey; + this.name = name; } - public Rule setRuleKey(RuleKey ruleKey) { - this.ruleKey = ruleKey; - return this; + public RuleKey ruleKey() { + return ruleKey; } public String name() { return name; } - public Rule setName(String name) { - this.name = name; - return this; - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java index da24181f9fb..429b192a336 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java @@ -53,12 +53,12 @@ public class QProfileProjectOperations implements ServerComponent { void addProject(int profileId, long projectId, UserSession userSession, DbSession session) { checkPermission(userSession); - ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session); - QualityProfileDto qualityProfile = findNotNull(profileId, session); + ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session); + QualityProfileDto qualityProfile = findNotNull(profileId, session); - db.propertiesDao().setProperty(new PropertyDto().setKey( - QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage()).setValue(qualityProfile.getName()).setResourceId(project.getId()), session); - session.commit(); + db.propertiesDao().setProperty(new PropertyDto().setKey( + QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage()).setValue(qualityProfile.getName()).setResourceId(project.getId()), session); + session.commit(); } public void removeProject(int profileId, long projectId, UserSession userSession) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java index 3b9533951bb..a57469683ba 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java @@ -36,6 +36,7 @@ import org.sonar.server.text.MacroInterpreter; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.util.Collection; import java.util.Map; @@ -165,12 +166,18 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> { public void write(Rule rule, JsonWriter json, @Nullable SearchOptions options) { RuleMappingContext context = new RuleMappingContext(); - if (needDebtCharacteristicNames(options) && rule.debtCharacteristicKey() != null) { - // load debt characteristics if requested - context.add(debtModel.characteristicByKey(rule.debtCharacteristicKey())); + if (needDebtCharacteristicNames(options)) { + String debtCharacteristicKey = rule.debtCharacteristicKey(); + if (debtCharacteristicKey != null) { + // load debt characteristics if requested + context.add(debtModel.characteristicByKey(debtCharacteristicKey)); + } } - if (needDebtSubCharacteristicNames(options) && rule.debtSubCharacteristicKey() != null) { - context.add(debtModel.characteristicByKey(rule.debtSubCharacteristicKey())); + if (needDebtSubCharacteristicNames(options)) { + String debtSubCharacteristicKey = rule.debtSubCharacteristicKey(); + if (debtSubCharacteristicKey != null) { + context.add(debtModel.characteristicByKey(debtSubCharacteristicKey)); + } } doWrite((RuleDoc) rule, context, json, options); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java index 8f34fe3015c..2d90ff52a2b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java @@ -31,7 +31,6 @@ import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.rule.Rule; import org.sonar.server.rule.RuleService; -import org.sonar.server.rule.index.RuleDoc; import org.sonar.server.rule.index.RuleNormalizer; import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.search.FacetValue; @@ -259,7 +258,7 @@ public class SearchAction implements RequestHandler { private void writeRules(Result<Rule> result, JsonWriter json, SearchOptions options) { json.name("rules").beginArray(); for (Rule rule : result.getHits()) { - mapping.write((RuleDoc) rule, json, options); + mapping.write(rule, json, options); } json.endArray(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java index 31cb3fbc9e9..7ae8476846f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java @@ -43,7 +43,7 @@ public class RulesAggregationTest { rulesAggregation.add(ruleDto); rulesAggregation.add(ruleDto); - RulesAggregation.Rule rule = new RulesAggregation.Rule().setRuleKey(ruleKey).setName("Rule name"); + RulesAggregation.Rule rule = new RulesAggregation.Rule(ruleKey, "Rule name"); assertThat(rulesAggregation.rules()).hasSize(1); assertThat(rulesAggregation.rules().iterator().next().name()).isEqualTo("Rule name"); @@ -61,4 +61,19 @@ public class RulesAggregationTest { assertThat(rulesAggregation.rules()).hasSize(2); } + + @Test + public void test_equals_and_hash_code() throws Exception { + RulesAggregation.Rule rule = new RulesAggregation.Rule(RuleKey.of("xoo", "S001"), "S001"); + RulesAggregation.Rule ruleSameRuleKey = new RulesAggregation.Rule(RuleKey.of("xoo", "S001"), "S001"); + RulesAggregation.Rule ruleWithDifferentRuleKey = new RulesAggregation.Rule(RuleKey.of("xoo", "S002"), "S002"); + + assertThat(rule).isEqualTo(rule); + assertThat(rule).isEqualTo(ruleSameRuleKey); + assertThat(rule).isNotEqualTo(ruleWithDifferentRuleKey); + + assertThat(rule.hashCode()).isEqualTo(rule.hashCode()); + assertThat(rule.hashCode()).isEqualTo(ruleSameRuleKey.hashCode()); + assertThat(rule.hashCode()).isNotEqualTo(ruleWithDifferentRuleKey.hashCode()); + } } diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java index 6c1992ac1e2..4fda09f8910 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java @@ -20,6 +20,7 @@ package org.sonar.batch.protocol.input; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -29,7 +30,7 @@ public class ActiveRule { private final String name, severity, internalKey, language; private final Map<String, String> params = new HashMap<String, String>(); - public ActiveRule(String repositoryKey, String ruleKey, String name, String severity, String internalKey, String language) { + public ActiveRule(String repositoryKey, String ruleKey, String name, String severity, @Nullable String internalKey, String language) { this.repositoryKey = repositoryKey; this.ruleKey = ruleKey; this.name = name; @@ -72,6 +73,7 @@ public class ActiveRule { return params; } + @CheckForNull public String internalKey() { return internalKey; } diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java index f09f3b34e81..775acb52523 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java @@ -49,7 +49,7 @@ public class Metric { public Metric(int id, String key, String valueType, - String description, + @Nullable String description, int direction, String name, boolean qualitative, @@ -82,6 +82,7 @@ public class Metric { return valueType; } + @CheckForNull public String description() { return description; } diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 4357258a1b4..e7a13b39f26 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -96,7 +96,7 @@ public class PropertiesDao implements BatchComponent, ServerComponent, DaoCompon } public List<PropertyDto> selectProjectProperties(String resourceKey, SqlSession session) { - return session.getMapper(PropertiesMapper.class).selectProjectProperties(resourceKey); + return session.getMapper(PropertiesMapper.class).selectProjectProperties(resourceKey); } public List<PropertyDto> selectProjectProperties(String resourceKey) { |