if (debt != null) {
Rule rule = rules.find(ruleKey);
if (rule != null) {
- String characteristicKey = rule.characteristic();
+ String characteristicKey = rule.debtCharacteristic();
if (characteristicKey != null) {
Characteristic characteristic = model.characteristicByKey(characteristicKey);
if (characteristic != null) {
public void before() throws Exception {
when(perspectives.as(Issuable.class, resource)).thenReturn(issuable);
RulesBuilder rulesBuilder = new RulesBuilder();
- rulesBuilder.add(ruleKey1).setName("rule1").setCharacteristic("MEMORY_EFFICIENCY");
- rulesBuilder.add(ruleKey2).setName("rule2").setCharacteristic("MODULARITY");
+ rulesBuilder.add(ruleKey1).setName("rule1").setDebtCharacteristic("MEMORY_EFFICIENCY");
+ rulesBuilder.add(ruleKey2).setName("rule2").setDebtCharacteristic("MODULARITY");
rules = rulesBuilder.build();
when(ruleFinder.findByKey(ruleKey1)).thenReturn(org.sonar.api.rules.Rule.create(ruleKey1.repository(), ruleKey1.rule()));
import com.google.common.base.Objects;
import com.google.common.base.Strings;
-import org.sonar.api.batch.rule.ActiveRule;
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.Rule;
-import org.sonar.api.batch.rule.Rules;
+import org.sonar.api.batch.rule.*;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.resources.Project;
-import org.sonar.api.rule.RemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.Duration;
if (issue.severity() == null) {
issue.setSeverity(activeRule.severity());
}
- if (rule.characteristic() != null) {
+ if (rule.debtCharacteristic() != null) {
issue.setDebt(calculateDebt(rule, issue.effortToFix()));
}
}
private Duration calculateDebt(Rule rule, @Nullable Double effortToFix) {
- if (RemediationFunction.CONSTANT_ISSUE.equals(rule.function()) && effortToFix != null) {
+ DebtRemediationFunction function = rule.debtRemediationFunction();
+ if (DebtRemediationFunction.Type.CONSTANT_ISSUE.equals(function.type()) && effortToFix != null) {
throw new IllegalArgumentException("Rule '" + rule.key() + "' can not use 'Constant/issue' remediation function " +
"because this rule does not have a fixed remediation cost.");
}
Duration result = Duration.create(0);
- Duration factor = rule.factor();
- Duration offset = rule.offset();
+ Duration factor = function.factor();
+ Duration offset = function.offset();
if (factor != null) {
int effortToFixValue = Objects.firstNonNull(effortToFix, 1).intValue();
result = factor.multiply(effortToFixValue);
}
-
if (offset != null) {
result = result.add(offset);
}
import org.picocontainer.injectors.ProviderAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.rule.DebtRemediationFunction;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.batch.rule.internal.NewRule;
import org.sonar.api.batch.rule.internal.RulesBuilder;
-import org.sonar.api.rule.RemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.technicaldebt.batch.Characteristic;
// TODO should we set metadata ?
if (ruleDto.hasCharacteristic()) {
- newRule.setCharacteristic(characteristic(ruleDto, ruleKey, debtModel).key());
- setFunction(ruleDto, newRule, ruleKey, durations);
+ newRule.setDebtCharacteristic(effectiveCharacteristic(ruleDto, ruleKey, debtModel).key());
+ newRule.setDebtRemediationFunction(effectiveFunction(ruleDto, ruleKey, newRule, durations));
}
for (RuleParamDto ruleParamDto : paramDtosByRuleId.get(ruleDto.getId())) {
return rulesBuilder.build();
}
- private void setFunction(RuleDto ruleDto, NewRule newRule, RuleKey ruleKey, Durations durations) {
- String function = ruleDto.getRemediationFunction();
- String factor = ruleDto.getRemediationFactor();
- String offset = ruleDto.getRemediationOffset();
-
- String defaultFunction = ruleDto.getDefaultRemediationFunction();
- String defaultFactor = ruleDto.getDefaultRemediationFactor();
- String defaultOffset = ruleDto.getDefaultRemediationOffset();
-
- if (function != null) {
- newRule.setFunction(function(function, ruleKey));
- newRule.setFactor(factor != null ? durations.decode(factor) : null);
- newRule.setOffset(offset != null ? durations.decode(offset) : null);
- } else {
- newRule.setFunction(function(defaultFunction, ruleKey));
- newRule.setFactor(defaultFactor != null ? durations.decode(defaultFactor) : null);
- newRule.setOffset(defaultOffset != null ? durations.decode(defaultOffset) : null);
- }
- }
-
- private Characteristic characteristic(RuleDto ruleDto, RuleKey ruleKey, TechnicalDebtModel debtModel) {
+ private Characteristic effectiveCharacteristic(RuleDto ruleDto, RuleKey ruleKey, TechnicalDebtModel debtModel) {
Integer characteristicId = ruleDto.getCharacteristicId();
Integer defaultCharacteristicId = ruleDto.getDefaultCharacteristicId();
Integer effectiveCharacteristicId = characteristicId != null ? characteristicId : defaultCharacteristicId;
return characteristic;
}
- private RemediationFunction function(@Nullable String function, RuleKey ruleKey) {
- if (function == null) {
+ private DebtRemediationFunction effectiveFunction(RuleDto ruleDto, RuleKey ruleKey, NewRule newRule, Durations durations) {
+ String function = ruleDto.getRemediationFunction();
+ String defaultFunction = ruleDto.getDefaultRemediationFunction();
+ if (function != null) {
+ return createDebtRemediationFunction(function, ruleDto.getRemediationFactor(), ruleDto.getRemediationOffset(), durations);
+ } else if (defaultFunction != null) {
+ return createDebtRemediationFunction(ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationFactor(), ruleDto.getDefaultRemediationOffset(), durations);
+ } else {
throw new IllegalStateException(String.format("Remediation function should not be null on rule '%s'", ruleKey));
}
- return RemediationFunction.valueOf(function);
}
+
+ private DebtRemediationFunction createDebtRemediationFunction(String function, @Nullable String factor, @Nullable String offset, Durations durations) {
+ return DebtRemediationFunction.create(DebtRemediationFunction.Type.valueOf(function),
+ factor != null ? durations.decode(factor) : null,
+ offset != null ? durations.decode(offset) : null);
+ }
+
}
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.batch.rule.DebtRemediationFunction;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.api.rule.RemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RulePriority;
public void set_debt_with_linear_function() throws Exception {
ruleBuilder.add(SQUID_RULE_KEY)
.setName(SQUID_RULE_NAME)
- .setCharacteristic("COMPILER_RELATED_PORTABILITY")
- .setFunction(RemediationFunction.LINEAR)
- .setFactor(Duration.create(10L));
+ .setDebtCharacteristic("COMPILER_RELATED_PORTABILITY")
+ .setDebtRemediationFunction(DebtRemediationFunction.createLinear(Duration.create(10L)));
activeRulesBuilder.activate(SQUID_RULE_KEY).setSeverity(Severity.INFO);
initModuleIssues();
public void set_debt_with_linear_with_offset_function() throws Exception {
ruleBuilder.add(SQUID_RULE_KEY)
.setName(SQUID_RULE_NAME)
- .setCharacteristic("COMPILER_RELATED_PORTABILITY")
- .setFunction(RemediationFunction.LINEAR_OFFSET)
- .setFactor(Duration.create(10L))
- .setOffset(Duration.create(25L));
+ .setDebtCharacteristic("COMPILER_RELATED_PORTABILITY")
+ .setDebtRemediationFunction(DebtRemediationFunction.createLinearWithOffset(Duration.create(10L), Duration.create(25L)));
activeRulesBuilder.activate(SQUID_RULE_KEY).setSeverity(Severity.INFO);
initModuleIssues();
public void set_debt_with_constant_issue_function() throws Exception {
ruleBuilder.add(SQUID_RULE_KEY)
.setName(SQUID_RULE_NAME)
- .setCharacteristic("COMPILER_RELATED_PORTABILITY")
- .setFunction(RemediationFunction.CONSTANT_ISSUE)
- .setOffset(Duration.create(10L));
+ .setDebtCharacteristic("COMPILER_RELATED_PORTABILITY")
+ .setDebtRemediationFunction(DebtRemediationFunction.createConstantPerIssue(Duration.create(10L)));
activeRulesBuilder.activate(SQUID_RULE_KEY).setSeverity(Severity.INFO);
initModuleIssues();
public void fail_to_set_debt_with_constant_issue_function_when_effort_to_fix_is_set() throws Exception {
ruleBuilder.add(SQUID_RULE_KEY)
.setName(SQUID_RULE_NAME)
- .setCharacteristic("COMPILER_RELATED_PORTABILITY")
- .setFunction(RemediationFunction.CONSTANT_ISSUE)
- .setOffset(Duration.create(25L));
+ .setDebtCharacteristic("COMPILER_RELATED_PORTABILITY")
+ .setDebtRemediationFunction(DebtRemediationFunction.createConstantPerIssue(Duration.create(25L)));
activeRulesBuilder.activate(SQUID_RULE_KEY).setSeverity(Severity.INFO);
initModuleIssues();
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.batch.rule.DebtRemediationFunction;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.RuleParam;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.config.Settings;
-import org.sonar.api.rule.RemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
Rules rules = provider.provide(ruleDao, debtModel, durations);
Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
- assertThat(rule.characteristic()).isEqualTo("EFFICIENCY");
- assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR_OFFSET);
- assertThat(rule.factor()).isEqualTo(Duration.decode("5d", 8));
- assertThat(rule.offset()).isEqualTo(Duration.decode("10h", 8));
+ assertThat(rule.debtCharacteristic()).isEqualTo("EFFICIENCY");
+ assertThat(rule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.createLinearWithOffset(Duration.decode("5d", 8), Duration.decode("10h", 8)));
}
@Test
Rules rules = provider.provide(ruleDao, debtModel, durations);
Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
- assertThat(rule.characteristic()).isEqualTo("PORTABILITY");
- assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR);
- assertThat(rule.factor()).isEqualTo(Duration.decode("2h", 8));
- assertThat(rule.offset()).isNull();
+ assertThat(rule.debtCharacteristic()).isEqualTo("PORTABILITY");
+ assertThat(rule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.createLinear(Duration.decode("2h", 8)));
}
@Test
// As both default columns and user columns on debt are set, user debt columns should be used
Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
- assertThat(rule.characteristic()).isEqualTo("PORTABILITY");
- assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR);
- assertThat(rule.factor()).isEqualTo(Duration.decode("2h", 8));
- assertThat(rule.offset()).isNull();
+ assertThat(rule.debtCharacteristic()).isEqualTo("PORTABILITY");
+ assertThat(rule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.createLinear(Duration.decode("2h", 8)));
}
@Test
// As both default columns and user columns on debt are set, user debt columns should be used
Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
- assertThat(rule.characteristic()).isEqualTo("PORTABILITY");
- assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR);
- assertThat(rule.factor()).isEqualTo(Duration.decode("2h", 8));
- assertThat(rule.offset()).isNull();
+ assertThat(rule.debtCharacteristic()).isEqualTo("PORTABILITY");
+ assertThat(rule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.createLinear(Duration.decode("2h", 8)));
}
@Test
// As both default columns and user columns on debt are set, user debt columns should be used
Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
- assertThat(rule.characteristic()).isEqualTo("PORTABILITY");
- assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR);
- assertThat(rule.factor()).isEqualTo(Duration.decode("2h", 8));
- assertThat(rule.offset()).isNull();
+ assertThat(rule.debtCharacteristic()).isEqualTo("PORTABILITY");
+ assertThat(rule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.createLinear(Duration.decode("2h", 8)));
}
@Test
Rules rules = provider.provide(ruleDao, debtModel, durations);
Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
- assertThat(rule.characteristic()).isNull();
- assertThat(rule.function()).isNull();
- assertThat(rule.factor()).isNull();
- assertThat(rule.offset()).isNull();
+ assertThat(rule.debtCharacteristic()).isNull();
+ assertThat(rule.debtRemediationFunction()).isNull();
}
@Test
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.api.batch.rule;
+
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+import org.sonar.api.utils.Duration;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+/**
+ * @since 4.3
+ */
+public class DebtRemediationFunction {
+
+ public static enum Type {
+ LINEAR, LINEAR_OFFSET, CONSTANT_ISSUE
+ }
+
+ private Type type;
+ private Duration factor;
+ private Duration offset;
+
+ private DebtRemediationFunction(Type type, @Nullable Duration factor, @Nullable Duration offset) {
+ this.type = type;
+ this.factor = factor;
+ this.offset = offset;
+ }
+
+ public static DebtRemediationFunction create(Type type, @Nullable Duration factor, @Nullable Duration offset) {
+ return new DebtRemediationFunction(type, factor, offset);
+ }
+
+ public static DebtRemediationFunction createLinear(Duration factor) {
+ return new DebtRemediationFunction(Type.LINEAR, factor, null);
+ }
+
+ public static DebtRemediationFunction createLinearWithOffset(Duration factor, Duration offset) {
+ return new DebtRemediationFunction(Type.LINEAR_OFFSET, factor, offset);
+ }
+
+ public static DebtRemediationFunction createConstantPerIssue(Duration offset) {
+ return new DebtRemediationFunction(Type.CONSTANT_ISSUE, null, offset);
+ }
+
+ public Type type() {
+ return type;
+ }
+
+ @CheckForNull
+ public Duration factor() {
+ return factor;
+ }
+
+ @CheckForNull
+ public Duration offset() {
+ return offset;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ 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;
+ }
+
+ @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;
+ }
+
+ @Override
+ public String toString() {
+ return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
+ }
+}
package org.sonar.api.batch.rule;
import com.google.common.annotations.Beta;
-import org.sonar.api.rule.RemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.utils.Duration;
import javax.annotation.CheckForNull;
* @since 4.3
*/
@CheckForNull
- String characteristic();
+ String debtCharacteristic();
/**
- * Remediation function : one of LINEAR, LINEAR_OFFSET or CONSTANT_ISSUE.
+ * Remediation function : one of LINEAR (with a factor), LINEAR_OFFSET (with a factor and an offset) or CONSTANT_ISSUE (with an offset)
*
* @since 4.3
*/
@CheckForNull
- RemediationFunction function();
-
- /**
- * Remediation factor duration (used for LINEAR function).
- *
- * @since 4.3
- */
- @CheckForNull
- Duration factor();
-
- /**
- * Remediation offset duration (used for LINEAR_OFFSET or CONSTANT_ISSUE function).
- *
- * @since 4.3
- */
- @CheckForNull
- Duration offset();
+ DebtRemediationFunction debtRemediationFunction();
}
package org.sonar.api.batch.rule.internal;
import com.google.common.collect.ImmutableMap;
+import org.sonar.api.batch.rule.DebtRemediationFunction;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.RuleParam;
-import org.sonar.api.rule.RemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.utils.Duration;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
private final RuleKey key;
private final Integer id;
- private final String name, severity, description, metadata, characteristic;
+ private final String name, severity, description, metadata, debtCharacteristic;
private final RuleStatus status;
- private final RemediationFunction function;
- private final Duration factor, offset;
+ private final DebtRemediationFunction debtRemediationFunction;
private final Map<String, RuleParam> params;
this.description = newRule.description;
this.metadata = newRule.metadata;
this.status = newRule.status;
- this.characteristic = newRule.characteristic;
- this.function = newRule.function;
- this.factor = newRule.factor;
- this.offset = newRule.offset;
+ this.debtCharacteristic = newRule.debtCharacteristic;
+ this.debtRemediationFunction = newRule.debtRemediationFunction;
ImmutableMap.Builder<String, RuleParam> builder = ImmutableMap.builder();
for (NewRuleParam newRuleParam : newRule.params.values()) {
}
@Override
- public String characteristic() {
- return characteristic;
+ public String debtCharacteristic() {
+ return debtCharacteristic;
}
@Override
- public RemediationFunction function() {
- return function;
- }
-
- @Override
- public Duration factor() {
- return factor;
- }
-
- @Override
- public Duration offset() {
- return offset;
+ public DebtRemediationFunction debtRemediationFunction() {
+ return debtRemediationFunction;
}
@Override
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rule.RemediationFunction;
+import org.sonar.api.batch.rule.DebtRemediationFunction;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
-import org.sonar.api.utils.Duration;
import javax.annotation.Nullable;
final RuleKey key;
Integer id;
- String name, description, severity = DEFAULT_SEVERITY, metadata, characteristic;
- RemediationFunction function;
- Duration factor, offset;
+ String name, description, severity = DEFAULT_SEVERITY, metadata, debtCharacteristic;
+ DebtRemediationFunction debtRemediationFunction;
RuleStatus status = RuleStatus.defaultStatus();
Map<String, NewRuleParam> params = new HashMap<String, NewRuleParam>();
}
public NewRule setStatus(@Nullable RuleStatus s) {
- this.status = (RuleStatus)ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
+ this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
return this;
}
return this;
}
- public NewRule setCharacteristic(@Nullable String c) {
- this.characteristic = c;
+ public NewRule setDebtCharacteristic(@Nullable String c) {
+ this.debtCharacteristic = c;
return this;
}
- public NewRule setFunction(@Nullable RemediationFunction f) {
- this.function = f;
- return this;
- }
-
- public NewRule setFactor(@Nullable Duration f) {
- this.factor = f;
- return this;
- }
-
- public NewRule setOffset(@Nullable Duration o) {
- this.offset = o;
+ public NewRule setDebtRemediationFunction(@Nullable DebtRemediationFunction f) {
+ this.debtRemediationFunction = f;
return this;
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 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.api.rule;
-
-/**
- * List of availables remediation function
- *
- * @since 4.3
- */
-public enum RemediationFunction {
- LINEAR, LINEAR_OFFSET, CONSTANT_ISSUE
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.api.batch.rule;
+
+import org.junit.Test;
+import org.sonar.api.server.rule.DebtRemediationFunction;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+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");
+ 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");
+ }
+
+ @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);
+ assertThat(function.factor()).isNull();
+ assertThat(function.offset()).isEqualTo("10h");
+ }
+
+ @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");
+
+ 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.hashCode()).isEqualTo(function.hashCode());
+ assertThat(function.hashCode()).isEqualTo(functionWithSameValue.hashCode());
+ assertThat(function.hashCode()).isNotEqualTo(functionWithDifferentType.hashCode());
+ }
+
+ @Test
+ public void test_to_string() throws Exception {
+ assertThat(DebtRemediationFunction.createLinearWithOffset("10h", "5min").toString()).isNotNull();
+ }
+}
package org.sonar.api.batch.rule.internal;
import org.junit.Test;
+import org.sonar.api.batch.rule.DebtRemediationFunction;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
+import org.sonar.api.utils.Duration;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
newSquid1.setMetadata("foo=bar");
newSquid1.setSeverity(Severity.CRITICAL);
newSquid1.setStatus(RuleStatus.BETA);
+ newSquid1.setDebtCharacteristic("COMPILER");
+ newSquid1.setDebtRemediationFunction(DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, Duration.create(10), Duration.create(60)));
newSquid1.addParam("min");
newSquid1.addParam("max").setDescription("Maximum");
// most simple rule
assertThat(squid1.metadata()).isEqualTo("foo=bar");
assertThat(squid1.status()).isEqualTo(RuleStatus.BETA);
assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
+ assertThat(squid1.debtCharacteristic()).isEqualTo("COMPILER");
+ assertThat(squid1.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+ assertThat(squid1.debtRemediationFunction().factor()).isEqualTo(Duration.create(10));
+ assertThat(squid1.debtRemediationFunction().offset()).isEqualTo(Duration.create(60));
+ assertThat(squid1.debtCharacteristic()).isEqualTo("COMPILER");
assertThat(squid1.params()).hasSize(2);
assertThat(squid1.param("min").key()).isEqualTo("min");
assertThat(squid1.param("min").description()).isNull();
assertThat(squid2.metadata()).isNull();
assertThat(squid2.status()).isEqualTo(RuleStatus.defaultStatus());
assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
+ assertThat(squid2.debtCharacteristic()).isNull();
+ assertThat(squid2.debtRemediationFunction()).isNull();
assertThat(squid2.params()).isEmpty();
}