aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-07-17 17:03:59 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-07-17 17:37:25 +0200
commit123eaf25e79b8f15dbc90dd1678e861d49b3d41e (patch)
treec04fa9f19e08e528ae42d337789dde44e12a8971 /server/sonar-server
parentc522de4c43833715bf6cfa46f6099cb4b45bdf7c (diff)
downloadsonarqube-123eaf25e79b8f15dbc90dd1678e861d49b3d41e.tar.gz
sonarqube-123eaf25e79b8f15dbc90dd1678e861d49b3d41e.zip
Fix some quality flaws
Diffstat (limited to 'server/sonar-server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/db/migrations/Upsert.java3
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java19
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java9
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/db/migrations/v45/AddMissingRuleParameterDefaultValuesMigration.java92
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java10
5 files changed, 75 insertions, 58 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/Upsert.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/Upsert.java
index 58835275705..302d0c99b03 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/Upsert.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/Upsert.java
@@ -21,6 +21,9 @@ package org.sonar.server.db.migrations;
import java.sql.SQLException;
+/**
+ * INSERT, UPDATE or DELETE
+ */
public interface Upsert extends SqlStatement<Upsert> {
Upsert addBatch() throws SQLException;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java
index a8621136fe3..284b1893cdf 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v42/PackageKeysMigration.java
@@ -26,6 +26,9 @@ import org.sonar.server.db.migrations.MassUpdate;
import org.sonar.server.db.migrations.Select;
import org.sonar.server.db.migrations.SqlStatement;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
import java.sql.SQLException;
/**
@@ -56,12 +59,16 @@ public class PackageKeysMigration extends BaseDataChange {
});
}
- String convertKey(String packageKey) {
- String prefix = StringUtils.substringBeforeLast(packageKey, ":") + ":";
- String key = StringUtils.substringAfterLast(packageKey, ":");
- if ("[default]".equals(key)) {
- return prefix + "[root]";
+ @CheckForNull
+ String convertKey(@Nullable String packageKey) {
+ if (packageKey != null) {
+ String prefix = StringUtils.substringBeforeLast(packageKey, ":") + ":";
+ String key = StringUtils.substringAfterLast(packageKey, ":");
+ if ("[default]".equals(key)) {
+ return prefix + "[root]";
+ }
+ return prefix + StringUtils.replace(key, ".", "/");
}
- return prefix + StringUtils.replace(key, ".", "/");
+ return null;
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java
index 5d51e5ae81a..e0a4397e268 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/IssueChangelogMigration.java
@@ -30,6 +30,9 @@ import org.sonar.server.db.migrations.MassUpdate;
import org.sonar.server.db.migrations.Select;
import org.sonar.server.db.migrations.SqlStatement;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
import java.sql.SQLException;
import java.util.Date;
import java.util.regex.Matcher;
@@ -80,7 +83,11 @@ public class IssueChangelogMigration extends BaseDataChange {
}
@VisibleForTesting
- String convertChangelog(String data) {
+ @CheckForNull
+ String convertChangelog(@Nullable String data) {
+ if (data == null) {
+ return null;
+ }
Matcher matcher = pattern.matcher(data);
StringBuffer sb = new StringBuffer();
if (matcher.find()) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v45/AddMissingRuleParameterDefaultValuesMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v45/AddMissingRuleParameterDefaultValuesMigration.java
index f76bffa8402..f8ec7ab6129 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v45/AddMissingRuleParameterDefaultValuesMigration.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v45/AddMissingRuleParameterDefaultValuesMigration.java
@@ -25,6 +25,8 @@ import org.sonar.server.db.migrations.BaseDataChange;
import org.sonar.server.db.migrations.Select;
import org.sonar.server.db.migrations.Upsert;
+import javax.annotation.Nullable;
+
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
@@ -42,63 +44,59 @@ public class AddMissingRuleParameterDefaultValuesMigration extends BaseDataChang
}
@Override
- public void execute(Context context) {
- try {
- // get all the parameters with default value
- List<RuleParam> ruleParameters = context.prepareSelect("select id,rule_id,name,default_value from rules_parameters where default_value is not null")
- .list(new Select.RowReader<RuleParam>() {
+ public void execute(Context context) throws SQLException {
+ // get all the parameters with default value
+ List<RuleParam> ruleParameters = context.prepareSelect("select id,rule_id,name,default_value from rules_parameters where default_value is not null")
+ .list(new Select.RowReader<RuleParam>() {
+ @Override
+ public RuleParam read(Select.Row row) throws SQLException {
+ return new RuleParam(row.getLong(1), row.getLong(2), row.getString(3), row.getString(4));
+ }
+ });
+
+ for (RuleParam ruleParameter : ruleParameters) {
+ List<ActiveRule> activeRules = context.prepareSelect("select ar.id, ar.profile_id from active_rules ar " +
+ "left outer join active_rule_parameters arp on arp.active_rule_id=ar.id and arp.rules_parameter_id=? " +
+ "where ar.rule_id=? and arp.id is null")
+ .setLong(1, ruleParameter.id)
+ .setLong(2, ruleParameter.ruleId)
+ .list(new Select.RowReader<ActiveRule>() {
@Override
- public RuleParam read(Select.Row row) throws SQLException {
- return new RuleParam(row.getLong(1), row.getLong(2), row.getString(3), row.getString(4));
+ public ActiveRule read(Select.Row row) throws SQLException {
+ return new ActiveRule(row.getLong(1), row.getLong(2));
}
});
- for (RuleParam ruleParameter : ruleParameters) {
- List<ActiveRule> activeRules = context.prepareSelect("select ar.id, ar.profile_id from active_rules ar " +
- "left outer join active_rule_parameters arp on arp.active_rule_id=ar.id and arp.rules_parameter_id=? " +
- "where ar.rule_id=? and arp.id is null")
- .setLong(1, ruleParameter.id)
- .setLong(2, ruleParameter.ruleId)
- .list(new Select.RowReader<ActiveRule>() {
- @Override
- public ActiveRule read(Select.Row row) throws SQLException {
- return new ActiveRule(row.getLong(1), row.getLong(2));
- }
- });
-
- Upsert upsert = context.prepareUpsert("insert into active_rule_parameters(active_rule_id, rules_parameter_id, value, rules_parameter_key) values (?, ?, ?, ?)");
- for (ActiveRule activeRule : activeRules) {
- upsert
- .setLong(1, activeRule.id)
- .setLong(2, ruleParameter.id)
- .setString(3, ruleParameter.defaultValue)
- .setString(4, ruleParameter.name)
- .addBatch();
- }
- upsert.execute().commit().close();
-
- // update date for ES indexation
- upsert = context.prepareUpsert("update active_rules set updated_at=? where id=?");
- Date now = new Date(system.now());
- for (ActiveRule activeRule : activeRules) {
- upsert
- .setDate(1, now)
- .setLong(2, activeRule.id)
- .addBatch();
- }
- upsert.execute().commit().close();
+ Upsert upsert = context.prepareUpsert("insert into active_rule_parameters(active_rule_id, rules_parameter_id, value, rules_parameter_key) values (?, ?, ?, ?)");
+ for (ActiveRule activeRule : activeRules) {
+ upsert
+ .setLong(1, activeRule.id)
+ .setLong(2, ruleParameter.id)
+ .setString(3, ruleParameter.defaultValue)
+ .setString(4, ruleParameter.name)
+ .addBatch();
+ }
+ upsert.execute().commit().close();
+ // update date for ES indexation
+ upsert = context.prepareUpsert("update active_rules set updated_at=? where id=?");
+ Date now = new Date(system.now());
+ for (ActiveRule activeRule : activeRules) {
+ upsert
+ .setDate(1, now)
+ .setLong(2, activeRule.id)
+ .addBatch();
}
- } catch (SQLException e) {
- e.printStackTrace();
+ upsert.execute().commit().close();
+
}
}
private static class RuleParam {
- final long id, ruleId;
+ final Long id, ruleId;
final String defaultValue, name;
- RuleParam(long id, long ruleId, String name, String defaultValue) {
+ RuleParam(@Nullable Long id, @Nullable Long ruleId, @Nullable String name, @Nullable String defaultValue) {
this.id = id;
this.ruleId = ruleId;
this.name = name;
@@ -107,9 +105,9 @@ public class AddMissingRuleParameterDefaultValuesMigration extends BaseDataChang
}
private static class ActiveRule {
- final long id, profileId;
+ final Long id, profileId;
- ActiveRule(long id, long profileId) {
+ ActiveRule(@Nullable Long id, @Nullable Long profileId) {
this.id = id;
this.profileId = profileId;
}
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 2b54eeea59b..b118a340140 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
@@ -144,12 +144,14 @@ 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) {
+ String characteristicKey;
+ if (needDebtCharacteristicNames(options) && (characteristicKey = rule.debtCharacteristicKey()) != null) {
// load debt characteristics if requested
- context.add(debtModel.characteristicByKey(rule.debtCharacteristicKey()));
+ context.add(debtModel.characteristicByKey(characteristicKey));
}
- if (needDebtSubCharacteristicNames(options) && rule.debtSubCharacteristicKey() != null) {
- context.add(debtModel.characteristicByKey(rule.debtSubCharacteristicKey()));
+ String subCharacteristicKey;
+ if (needDebtSubCharacteristicNames(options) && (subCharacteristicKey = rule.debtSubCharacteristicKey()) != null) {
+ context.add(debtModel.characteristicByKey(subCharacteristicKey));
}
doWrite((RuleDoc) rule, context, json, options);
}