From 0e1aeeb98b0458804682531ea27fe2afdf02ebfc Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 4 Apr 2014 14:51:03 +0200 Subject: [PATCH] Fix quality flaws --- .../org/sonar/server/debt/DebtModelBackup.java | 16 ++++++++++++---- .../java/org/sonar/server/rule/RuleRegistry.java | 5 +++-- ...pyRequirementsFromCharacteristicsToRules.java | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java index abe57f2dc80..f4a972e5f3c 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java @@ -25,6 +25,8 @@ import com.google.common.collect.Iterables; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ObjectUtils; import org.apache.ibatis.session.SqlSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.api.rule.RuleKey; import org.sonar.api.server.debt.DebtCharacteristic; @@ -58,6 +60,8 @@ import static org.sonar.server.debt.DebtModelXMLExporter.RuleDebt; public class DebtModelBackup implements ServerComponent { + private static final Logger LOG = LoggerFactory.getLogger(DebtModelBackup.class); + private final MyBatis mybatis; private final CharacteristicDao dao; private final RuleDao ruleDao; @@ -213,6 +217,7 @@ public class DebtModelBackup implements ServerComponent { session.commit(); } catch (IllegalArgumentException e) { + LOG.debug("Error when restoring the model", e); validationMessages.addErrorText(e.getMessage()); } finally { MyBatis.closeQuietly(session); @@ -365,13 +370,16 @@ public class DebtModelBackup implements ServerComponent { debtModel.characteristicById(effectiveSubCharacteristicId) : null; if (subCharacteristic != null) { ruleDebt.setSubCharacteristicKey(subCharacteristic.key()); - if (rule.getRemediationFunction() != null) { - ruleDebt.setFunction(rule.getRemediationFunction()); + + String overriddenFunction = rule.getRemediationFunction(); + String defaultFunction = rule.getDefaultRemediationFunction(); + if (overriddenFunction != null) { + ruleDebt.setFunction(overriddenFunction); ruleDebt.setCoefficient(rule.getRemediationCoefficient()); ruleDebt.setOffset(rule.getRemediationOffset()); return ruleDebt; - } else if (rule.getDefaultRemediationFunction() != null) { - ruleDebt.setFunction(rule.getDefaultRemediationFunction()); + } else if (defaultFunction != null) { + ruleDebt.setFunction(defaultFunction); ruleDebt.setCoefficient(rule.getDefaultRemediationCoefficient()); ruleDebt.setOffset(rule.getDefaultRemediationOffset()); return ruleDebt; diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java index 6bc01497bd5..5e32a3cd0bc 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java @@ -177,8 +177,9 @@ public class RuleRegistry { Integer effectiveSubCharacteristicId = rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId(); CharacteristicDto subCharacteristic = effectiveSubCharacteristicId != null ? characteristicDao.selectById(effectiveSubCharacteristicId, session) : null; // The parent id of the sub-characteristic should never be null - CharacteristicDto characteristic = (subCharacteristic != null && subCharacteristic.getParentId() != null) ? - characteristicDao.selectById(subCharacteristic.getParentId(), session) : null; + Integer parentId = subCharacteristic!= null ? subCharacteristic.getParentId() : null; + CharacteristicDto characteristic = (subCharacteristic != null && parentId != null) ? + characteristicDao.selectById(parentId, session) : null; searchIndex.putSynchronous(INDEX_RULES, TYPE_RULE, Long.toString(ruleId), ruleDocument(rule, characteristic, subCharacteristic, ruleDao.selectParametersByRuleIds(newArrayList(ruleId), session), diff --git a/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java b/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java index 88aab6ced84..4f6ea91372a 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java @@ -192,8 +192,8 @@ public class CopyRequirementsFromCharacteristicsToRules { private static RequirementDto enabledRequirement(Collection requirementsForRule) { return Iterables.find(requirementsForRule, new Predicate() { @Override - public boolean apply(RequirementDto input) { - return input.isEnabled(); + public boolean apply(@Nullable RequirementDto input) { + return input != null && input.isEnabled(); } }, null); } -- 2.39.5