aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java10
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/technicaldebt/TechnicalDebtModelRepository.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/DebtRemediationFunction.java29
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rule.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunction.java29
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DebtRemediationFunctionTest.java38
7 files changed, 57 insertions, 63 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
index 751aa64eedd..de553d4435f 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
@@ -107,15 +107,15 @@ public class ModuleIssues {
if (issue.severity() == null) {
issue.setSeverity(activeRule.severity());
}
- if (rule.debtCharacteristic() != null) {
- issue.setDebt(calculateDebt(rule, issue.effortToFix()));
+ DebtRemediationFunction function = rule.debtRemediationFunction();
+ if (rule.debtCharacteristic() != null && function != null) {
+ issue.setDebt(calculateDebt(function, issue.effortToFix(), rule.key()));
}
}
- private Duration calculateDebt(Rule rule, @Nullable Double effortToFix) {
- DebtRemediationFunction function = rule.debtRemediationFunction();
+ private Duration calculateDebt(DebtRemediationFunction function, @Nullable Double effortToFix, RuleKey ruleKey) {
if (DebtRemediationFunction.Type.CONSTANT_ISSUE.equals(function.type()) && effortToFix != null) {
- throw new IllegalArgumentException("Rule '" + rule.key() + "' can not use 'Constant/issue' remediation function " +
+ throw new IllegalArgumentException("Rule '" + ruleKey + "' can not use 'Constant/issue' remediation function " +
"because this rule does not have a fixed remediation cost.");
}
Duration result = Duration.create(0);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
index 2d0f1294e8d..75afe603f4f 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
@@ -109,7 +109,7 @@ public class RulesProvider extends ProviderAdapter {
if (function != null) {
return createDebtRemediationFunction(function, ruleDto.getRemediationFactor(), ruleDto.getRemediationOffset(), durations);
} else if (defaultFunction != null) {
- return createDebtRemediationFunction(ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationFactor(), ruleDto.getDefaultRemediationOffset(), durations);
+ return createDebtRemediationFunction(defaultFunction, ruleDto.getDefaultRemediationFactor(), ruleDto.getDefaultRemediationOffset(), durations);
} else {
throw new IllegalStateException(String.format("Remediation function should not be null on rule '%s'", ruleKey));
}
diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/TechnicalDebtModelRepository.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/TechnicalDebtModelRepository.java
index dacd7765d2a..f9cc46262a0 100644
--- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/TechnicalDebtModelRepository.java
+++ b/sonar-core/src/main/java/org/sonar/core/technicaldebt/TechnicalDebtModelRepository.java
@@ -23,6 +23,7 @@ package org.sonar.core.technicaldebt;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import org.picocontainer.Startable;
+import org.sonar.api.Plugin;
import org.sonar.api.ServerExtension;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.api.platform.PluginRepository;
@@ -87,9 +88,12 @@ public class TechnicalDebtModelRepository implements ServerExtension, Startable
contributingPluginKeyToClassLoader.put(DEFAULT_MODEL, getClass().getClassLoader());
for (PluginMetadata metadata : pluginRepository.getMetadata()) {
String pluginKey = metadata.getKey();
- ClassLoader classLoader = pluginRepository.getPlugin(pluginKey).getClass().getClassLoader();
- if (classLoader.getResource(getXMLFilePath(pluginKey)) != null) {
- contributingPluginKeyToClassLoader.put(pluginKey, classLoader);
+ Plugin plugin = pluginRepository.getPlugin(pluginKey);
+ if (plugin != null) {
+ ClassLoader classLoader = plugin.getClass().getClassLoader();
+ if (classLoader.getResource(getXMLFilePath(pluginKey)) != null) {
+ contributingPluginKeyToClassLoader.put(pluginKey, classLoader);
+ }
}
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/DebtRemediationFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/DebtRemediationFunction.java
index 60f3faccefe..459f96655de 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/DebtRemediationFunction.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/DebtRemediationFunction.java
@@ -20,6 +20,8 @@
package org.sonar.api.batch.rule;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.utils.Duration;
@@ -84,28 +86,21 @@ public class DebtRemediationFunction {
if (o == null || getClass() != o.getClass()) {
return false;
}
-
DebtRemediationFunction that = (DebtRemediationFunction) o;
-
- if (type != that.type) {
- return false;
- }
- if (factor != null ? !factor.equals(that.factor) : that.factor != null) {
- return false;
- }
- if (offset != null ? !offset.equals(that.offset) : that.offset != null) {
- return false;
- }
-
- return true;
+ return new EqualsBuilder()
+ .append(type, that.type())
+ .append(factor, that.factor())
+ .append(offset, that.offset())
+ .isEquals();
}
@Override
public int hashCode() {
- int result = type.hashCode();
- result = 31 * result + (factor != null ? factor.hashCode() : 0);
- result = 31 * result + (offset != null ? offset.hashCode() : 0);
- return result;
+ return new HashCodeBuilder(15, 31)
+ .append(type)
+ .append(factor)
+ .append(offset)
+ .toHashCode();
}
@Override
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rule.java
index 27a75566c09..b1aed121df0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rule.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rule.java
@@ -61,7 +61,7 @@ public interface Rule {
String debtCharacteristic();
/**
- * Remediation function : one of LINEAR (with a factor), LINEAR_OFFSET (with a factor and an offset) or CONSTANT_ISSUE (with an offset)
+ * Remediation function : can by Linear (with a factor), Linear with offset (with a factor and an offset) or Constant per issue (with an offset)
*
* @since 4.3
*/
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunction.java
index 74b95aea2f1..476fe769849 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunction.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunction.java
@@ -21,6 +21,8 @@
package org.sonar.api.server.rule;
import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@@ -37,7 +39,6 @@ public class DebtRemediationFunction {
}
public static class ValidationException extends RuntimeException {
-
public ValidationException(String message) {
super(message);
}
@@ -117,26 +118,20 @@ public class DebtRemediationFunction {
}
DebtRemediationFunction that = (DebtRemediationFunction) o;
-
- if (type != that.type) {
- return false;
- }
- if (factor != null ? !factor.equals(that.factor) : that.factor != null) {
- return false;
- }
- if (offset != null ? !offset.equals(that.offset) : that.offset != null) {
- return false;
- }
-
- return true;
+ return new EqualsBuilder()
+ .append(type, that.type())
+ .append(factor, that.factor())
+ .append(offset, that.offset())
+ .isEquals();
}
@Override
public int hashCode() {
- int result = type.hashCode();
- result = 31 * result + (factor != null ? factor.hashCode() : 0);
- result = 31 * result + (offset != null ? offset.hashCode() : 0);
- return result;
+ return new HashCodeBuilder(15, 33)
+ .append(type)
+ .append(factor)
+ .append(offset)
+ .toHashCode();
}
@Override
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DebtRemediationFunctionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DebtRemediationFunctionTest.java
index a377c4abb73..7f5ec006ce9 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DebtRemediationFunctionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DebtRemediationFunctionTest.java
@@ -21,7 +21,7 @@
package org.sonar.api.batch.rule;
import org.junit.Test;
-import org.sonar.api.server.rule.DebtRemediationFunction;
+import org.sonar.api.utils.Duration;
import static org.fest.assertions.Assertions.assertThat;
@@ -29,41 +29,41 @@ public class DebtRemediationFunctionTest {
@Test
public void create_linear() throws Exception {
- org.sonar.api.server.rule.DebtRemediationFunction function = org.sonar.api.server.rule.DebtRemediationFunction.createLinear("10h");
- assertThat(function.type()).isEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.Type.LINEAR);
- assertThat(function.factor()).isEqualTo("10h");
+ DebtRemediationFunction function = DebtRemediationFunction.createLinear(Duration.create(10));
+ assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(function.factor()).isEqualTo(Duration.create(10));
assertThat(function.offset()).isNull();
}
@Test
public void create_linear_with_offset() throws Exception {
- org.sonar.api.server.rule.DebtRemediationFunction function = org.sonar.api.server.rule.DebtRemediationFunction.createLinearWithOffset("10h", "5min");
- assertThat(function.type()).isEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.Type.LINEAR_OFFSET);
- assertThat(function.factor()).isEqualTo("10h");
- assertThat(function.offset()).isEqualTo("5min");
+ DebtRemediationFunction function = DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(5));
+ assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+ assertThat(function.factor()).isEqualTo(Duration.create(10));
+ assertThat(function.offset()).isEqualTo(Duration.create(5));
}
@Test
public void create_constant_per_issue() throws Exception {
- org.sonar.api.server.rule.DebtRemediationFunction function = org.sonar.api.server.rule.DebtRemediationFunction.createConstantPerIssue("10h");
- assertThat(function.type()).isEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.Type.CONSTANT_ISSUE);
+ DebtRemediationFunction function = DebtRemediationFunction.createConstantPerIssue(Duration.create(10));
+ assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
assertThat(function.factor()).isNull();
- assertThat(function.offset()).isEqualTo("10h");
+ assertThat(function.offset()).isEqualTo(Duration.create(10));
}
@Test
public void test_equals_and_hashcode() throws Exception {
- org.sonar.api.server.rule.DebtRemediationFunction function = org.sonar.api.server.rule.DebtRemediationFunction.createLinearWithOffset("10h", "5min");
- org.sonar.api.server.rule.DebtRemediationFunction functionWithSameValue = org.sonar.api.server.rule.DebtRemediationFunction.createLinearWithOffset("10h", "5min");
- org.sonar.api.server.rule.DebtRemediationFunction functionWithDifferentType = org.sonar.api.server.rule.DebtRemediationFunction.createConstantPerIssue("5min");
+ DebtRemediationFunction function = DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(5));
+ DebtRemediationFunction functionWithSameValue = DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(5));
+ DebtRemediationFunction functionWithDifferentType = DebtRemediationFunction.createConstantPerIssue(Duration.create(5));
assertThat(function).isEqualTo(function);
assertThat(function).isEqualTo(functionWithSameValue);
assertThat(function).isNotEqualTo(functionWithDifferentType);
- assertThat(function).isNotEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.createLinearWithOffset("11h", "5min"));
- assertThat(function).isNotEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.createLinearWithOffset("10h", "6min"));
- assertThat(function).isNotEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.createLinear("10h"));
- assertThat(function).isNotEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.createConstantPerIssue("6min"));
+ assertThat(function).isNotEqualTo(DebtRemediationFunction.createLinearWithOffset(Duration.create(11), Duration.create(5)));
+ assertThat(function).isNotEqualTo(DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(6)));
+ assertThat(function).isNotEqualTo(DebtRemediationFunction.createLinear(Duration.create(10)));
+ assertThat(function).isNotEqualTo(DebtRemediationFunction.createConstantPerIssue(Duration.create(6)));
assertThat(function.hashCode()).isEqualTo(function.hashCode());
assertThat(function.hashCode()).isEqualTo(functionWithSameValue.hashCode());
@@ -72,6 +72,6 @@ public class DebtRemediationFunctionTest {
@Test
public void test_to_string() throws Exception {
- assertThat(DebtRemediationFunction.createLinearWithOffset("10h", "5min").toString()).isNotNull();
+ assertThat(DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(5)).toString()).isNotNull();
}
}