]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5121 Refactor the way remediation function is defined on rules in order to...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 21 Mar 2014 11:23:12 +0000 (12:23 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 21 Mar 2014 11:23:20 +0000 (12:23 +0100)
19 files changed:
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java
plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunction.java
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunctions.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DefaultDebtRemediationFunction.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DefaultDebtRemediationFunctions.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DebtRemediationFunctionTest.java [deleted file]
sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DefaultDebtRemediationFunctionTest.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/DebtRemediationFunctionTest.java [deleted file]
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/DefaultDebtRemediationFunctionTest.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java
sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java
sonar-server/src/main/java/org/sonar/server/rule/RuleRegistration.java
sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java
sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java
sonar-server/src/test/java/org/sonar/server/rule/RuleRegistrationTest.java
sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/fail_to_import_linear_having_offset.xml [deleted file]

index f1fa1e60f55038296a8bff0fd09b80a7df8bbcad..8bea7cbab69a002b22577b42e6a8fc88eb919c1c 100644 (file)
@@ -51,6 +51,10 @@ public class XooRulesDefinition implements RulesDefinition {
         // default severity when the rule is activated on a Quality profile. Default value is MAJOR.
       .setSeverity(Severity.MINOR);
 
+    x1Rule
+      .setDebtCharacteristic("INTEGRATION_TESTABILITY")
+      .setDebtRemediationFunction(x1Rule.debtRemediationFunctions().linearWithOffset("1h", "30min"));
+
     x1Rule.createParam("acceptWhitespace")
       .setDefaultValue("false")
       .setType(RuleParamType.BOOLEAN)
index 2de402e20c7e5dc1811663eda74e31509599d31c..f6e8173e4447c6496b4a37df3747d9a633b9f1d1 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.xoo.rule;
 
 import org.junit.Test;
+import org.sonar.api.server.rule.DebtRemediationFunction;
 import org.sonar.api.server.rule.RulesDefinition;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -42,5 +43,10 @@ public class XooRulesDefinitionTest {
     assertThat(x1.key()).isEqualTo("x1");
     assertThat(x1.tags()).containsOnly("style", "security");
     assertThat(x1.htmlDescription()).isNotEmpty();
+
+    assertThat(x1.debtCharacteristic()).isEqualTo("INTEGRATION_TESTABILITY");
+    assertThat(x1.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+    assertThat(x1.debtRemediationFunction().factor()).isEqualTo("1h");
+    assertThat(x1.debtRemediationFunction().offset()).isEqualTo("30min");
   }
 }
index d355dbf5fee73294a549238a0309341361da1427..d1ae702432aef129764ed31d4eed1864a101dcf5 100644 (file)
 
 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;
-
 import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
 
 /**
  * @since 4.3
  */
-public class DebtRemediationFunction {
-
-  public static enum Type {
-    LINEAR, LINEAR_OFFSET, CONSTANT_ISSUE
-  }
+public interface DebtRemediationFunction {
 
-  public static class ValidationException extends RuntimeException {
+  static class ValidationException extends RuntimeException {
     public ValidationException(String message) {
       super(message);
     }
   }
 
-  private Type type;
-  private String factor;
-  private String offset;
-
-  private DebtRemediationFunction(Type type, @Nullable String factor, @Nullable String offset) {
-    this.type = type;
-    // TODO validate factor and offset format
-    this.factor = StringUtils.deleteWhitespace(factor);
-    this.offset = StringUtils.deleteWhitespace(offset);
-    validate();
-  }
-
-  public static DebtRemediationFunction create(Type type, @Nullable String factor, @Nullable String offset) {
-    return new DebtRemediationFunction(type, factor, offset);
-  }
-
-  public static DebtRemediationFunction createLinear(String factor) {
-    return new DebtRemediationFunction(Type.LINEAR, factor, null);
-  }
-
-  public static DebtRemediationFunction createLinearWithOffset(String factor, String offset) {
-    return new DebtRemediationFunction(Type.LINEAR_OFFSET, factor, offset);
-  }
-
-  public static DebtRemediationFunction createConstantPerIssue(String offset) {
-    return new DebtRemediationFunction(Type.CONSTANT_ISSUE, null, offset);
+  static enum Type {
+    LINEAR, LINEAR_OFFSET, CONSTANT_ISSUE
   }
 
-  public Type type() {
-    return type;
-  }
+  Type type();
 
   @CheckForNull
-  public String factor() {
-    return factor;
-  }
+  String factor();
 
   @CheckForNull
-  public String offset() {
-    return offset;
-  }
-
-  private void validate(){
-    switch (type) {
-      case LINEAR:
-        if (this.factor == null || this.offset != null) {
-          throw new ValidationException(String.format("%s is invalid, Linear remediation function should only define a factor", this));
-        }
-        break;
-      case LINEAR_OFFSET:
-        if (this.factor == null || this.offset == null) {
-          throw new ValidationException(String.format("%s is invalid,  Linear with offset remediation function should define both factor and offset", this));
-        }
-        break;
-      case CONSTANT_ISSUE:
-        if (this.factor != null || this.offset == null) {
-          throw new ValidationException(String.format("%s is invalid, Constant/issue remediation function should only define an offset", this));
-        }
-        break;
-      default:
-        throw new IllegalStateException(String.format("Remediation function of %s is unknown", this));
-    }
-  }
-
-  @Override
-  public boolean equals(Object o) {
-    if (this == o) {
-      return true;
-    }
-    if (o == null || getClass() != o.getClass()) {
-      return false;
-    }
-
-    DebtRemediationFunction that = (DebtRemediationFunction) o;
-    return new EqualsBuilder()
-      .append(type, that.type())
-      .append(factor, that.factor())
-      .append(offset, that.offset())
-      .isEquals();
-  }
+  String offset();
 
-  @Override
-  public int hashCode() {
-    return new HashCodeBuilder(15, 33)
-      .append(type)
-      .append(factor)
-      .append(offset)
-      .toHashCode();
-  }
-
-  @Override
-  public String toString() {
-    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
-  }
 }
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunctions.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DebtRemediationFunctions.java
new file mode 100644 (file)
index 0000000..ace4b88
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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.server.rule;
+
+/**
+ * Factory of {@link org.sonar.api.server.rule.DebtRemediationFunction}
+ *
+ * @since 4.3
+ */
+public interface DebtRemediationFunctions {
+
+  DebtRemediationFunction linear(String factor);
+
+  DebtRemediationFunction linearWithOffset(String factor, String offset);
+
+  DebtRemediationFunction constantPerIssue(String offset);
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DefaultDebtRemediationFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DefaultDebtRemediationFunction.java
new file mode 100644 (file)
index 0000000..a5bda3a
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * 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.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;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+class DefaultDebtRemediationFunction implements DebtRemediationFunction {
+
+  private Type type;
+  private String factor;
+  private String offset;
+
+  private DefaultDebtRemediationFunction(Type type, @Nullable String factor, @Nullable String offset) {
+    this.type = type;
+    // TODO validate factor and offset format
+    this.factor = StringUtils.deleteWhitespace(factor);
+    this.offset = StringUtils.deleteWhitespace(offset);
+    validate();
+  }
+
+  static DebtRemediationFunction create(Type type, @Nullable String factor, @Nullable String offset) {
+    return new DefaultDebtRemediationFunction(type, factor, offset);
+  }
+
+  static DebtRemediationFunction createLinear(String factor) {
+    return new DefaultDebtRemediationFunction(Type.LINEAR, factor, null);
+  }
+
+  static DebtRemediationFunction createLinearWithOffset(String factor, String offset) {
+    return new DefaultDebtRemediationFunction(Type.LINEAR_OFFSET, factor, offset);
+  }
+
+  static DebtRemediationFunction createConstantPerIssue(String offset) {
+    return new DefaultDebtRemediationFunction(Type.CONSTANT_ISSUE, null, offset);
+  }
+
+  public Type type() {
+    return type;
+  }
+
+  @CheckForNull
+  public String factor() {
+    return factor;
+  }
+
+  @CheckForNull
+  public String offset() {
+    return offset;
+  }
+
+  private void validate() {
+    switch (type) {
+      case LINEAR:
+        if (this.factor == null || this.offset != null) {
+          throw new ValidationException(String.format("%s is invalid, Linear remediation function should only define a factor", this));
+        }
+        break;
+      case LINEAR_OFFSET:
+        if (this.factor == null || this.offset == null) {
+          throw new ValidationException(String.format("%s is invalid,  Linear with offset remediation function should define both factor and offset", this));
+        }
+        break;
+      case CONSTANT_ISSUE:
+        if (this.factor != null || this.offset == null) {
+          throw new ValidationException(String.format("%s is invalid, Constant/issue remediation function should only define an offset", this));
+        }
+        break;
+      default:
+        throw new IllegalStateException(String.format("Remediation function of %s is unknown", this));
+    }
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    DebtRemediationFunction that = (DebtRemediationFunction) o;
+    return new EqualsBuilder()
+      .append(type, that.type())
+      .append(factor, that.factor())
+      .append(offset, that.offset())
+      .isEquals();
+  }
+
+  @Override
+  public int hashCode() {
+    return new HashCodeBuilder(15, 33)
+      .append(type)
+      .append(factor)
+      .append(offset)
+      .toHashCode();
+  }
+
+  @Override
+  public String toString() {
+    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DefaultDebtRemediationFunctions.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/DefaultDebtRemediationFunctions.java
new file mode 100644 (file)
index 0000000..3c529e7
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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.server.rule;
+
+import org.sonar.api.utils.MessageException;
+
+import javax.annotation.Nullable;
+
+class DefaultDebtRemediationFunctions implements DebtRemediationFunctions {
+
+  private final String repoKey, key;
+
+  DefaultDebtRemediationFunctions(String repoKey, String key) {
+    this.repoKey = repoKey;
+    this.key = key;
+  }
+
+  @Override
+  public DebtRemediationFunction linear(String factor) {
+    return create(DefaultDebtRemediationFunction.Type.LINEAR, factor, null);
+  }
+
+  @Override
+  public DebtRemediationFunction linearWithOffset(String factor, String offset) {
+    return create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, factor, offset);
+  }
+
+  @Override
+  public DebtRemediationFunction constantPerIssue(String offset) {
+    return create(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE, null, offset);
+  }
+
+  private DebtRemediationFunction create(DefaultDebtRemediationFunction.Type type, @Nullable String factor, @Nullable String offset) {
+    try {
+      return DefaultDebtRemediationFunction.create(type, factor, offset);
+    } catch (DefaultDebtRemediationFunction.ValidationException e) {
+      throw MessageException.of(String.format("The rule '%s:%s' is invalid : %s ", this.repoKey, this.key, e.getMessage()));
+    }
+  }
+
+}
index 68b4d17d9ff00950b117cacd97a9ab1ed9bb5025..45650ecf6c04e07f7b4919f3d005496181f4e712 100644 (file)
@@ -317,10 +317,12 @@ public interface RulesDefinition extends ServerExtension {
     private String effortToFixDescription;
     private final Set<String> tags = Sets.newTreeSet();
     private final Map<String, NewParam> paramsByKey = Maps.newHashMap();
+    private final DefaultDebtRemediationFunctions functions;
 
     private NewRule(String repoKey, String key) {
       this.repoKey = repoKey;
       this.key = key;
+      this.functions = new DefaultDebtRemediationFunctions(repoKey, key);
     }
 
     public String key() {
@@ -379,6 +381,10 @@ public interface RulesDefinition extends ServerExtension {
       return this;
     }
 
+    public DebtRemediationFunctions debtRemediationFunctions() {
+      return functions;
+    }
+
     public NewRule setDebtRemediationFunction(@Nullable DebtRemediationFunction debtRemediationFunction) {
       this.debtRemediationFunction = debtRemediationFunction;
       return this;
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
deleted file mode 100644 (file)
index 7f5ec00..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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.utils.Duration;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DebtRemediationFunctionTest {
-
-  @Test
-  public void create_linear() throws Exception {
-    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 {
-    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 {
-    DebtRemediationFunction function = DebtRemediationFunction.createConstantPerIssue(Duration.create(10));
-    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
-    assertThat(function.factor()).isNull();
-    assertThat(function.offset()).isEqualTo(Duration.create(10));
-  }
-
-  @Test
-  public void test_equals_and_hashcode() throws Exception {
-    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(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());
-    assertThat(function.hashCode()).isNotEqualTo(functionWithDifferentType.hashCode());
-  }
-
-  @Test
-  public void test_to_string() throws Exception {
-    assertThat(DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(5)).toString()).isNotNull();
-  }
-}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DefaultDebtRemediationFunctionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/DefaultDebtRemediationFunctionTest.java
new file mode 100644 (file)
index 0000000..d63bfea
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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.utils.Duration;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultDebtRemediationFunctionTest {
+
+  @Test
+  public void create_linear() throws Exception {
+    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 {
+    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 {
+    DebtRemediationFunction function = DebtRemediationFunction.createConstantPerIssue(Duration.create(10));
+    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
+    assertThat(function.factor()).isNull();
+    assertThat(function.offset()).isEqualTo(Duration.create(10));
+  }
+
+  @Test
+  public void test_equals_and_hashcode() throws Exception {
+    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(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());
+    assertThat(function.hashCode()).isNotEqualTo(functionWithDifferentType.hashCode());
+  }
+
+  @Test
+  public void test_to_string() throws Exception {
+    assertThat(DebtRemediationFunction.createLinearWithOffset(Duration.create(10), Duration.create(5)).toString()).isNotNull();
+  }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/DebtRemediationFunctionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/DebtRemediationFunctionTest.java
deleted file mode 100644 (file)
index 2c2b4cd..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * 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.server.rule;
-
-import org.junit.Test;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.Fail.fail;
-
-public class DebtRemediationFunctionTest {
-
-  @Test
-  public void create_linear() throws Exception {
-    DebtRemediationFunction function = DebtRemediationFunction.createLinear("10h");
-    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
-    assertThat(function.factor()).isEqualTo("10h");
-    assertThat(function.offset()).isNull();
-  }
-
-  @Test
-  public void create_linear_with_offset() throws Exception {
-    DebtRemediationFunction function = DebtRemediationFunction.createLinearWithOffset("10h", "5min");
-    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
-    assertThat(function.factor()).isEqualTo("10h");
-    assertThat(function.offset()).isEqualTo("5min");
-  }
-
-  @Test
-  public void create_constant_per_issue() throws Exception {
-    DebtRemediationFunction function = DebtRemediationFunction.createConstantPerIssue("10h");
-    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
-    assertThat(function.factor()).isNull();
-    assertThat(function.offset()).isEqualTo("10h");
-  }
-
-  @Test
-  public void sanitize_remediation_factor_and_offset() {
-    DebtRemediationFunction function = DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, "  1  h   ", "  10   mi n");
-
-    assertThat(function.factor()).isEqualTo("1h");
-    assertThat(function.offset()).isEqualTo("10min");
-  }
-
-  @Test
-  public void fail_to_create_linear_when_no_factor() throws Exception {
-    try {
-      DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR, null, "10h");
-      fail();
-    } catch(Exception e) {
-      assertThat(e).isInstanceOf(DebtRemediationFunction.ValidationException.class);
-    }
-  }
-
-  @Test
-  public void fail_to_create_linear_when_offset() throws Exception {
-    try {
-      DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR, "5min", "10h");
-      fail();
-    } catch(Exception e) {
-      assertThat(e).isInstanceOf(DebtRemediationFunction.ValidationException.class);
-    }
-  }
-
-  @Test
-  public void fail_to_create_constant_per_issue_when_no_offset() throws Exception {
-    try {
-      DebtRemediationFunction.create(DebtRemediationFunction.Type.CONSTANT_ISSUE, "10h", null);
-      fail();
-    } catch(Exception e) {
-      assertThat(e).isInstanceOf(DebtRemediationFunction.ValidationException.class);
-    }
-  }
-
-  @Test
-  public void fail_to_create_constant_per_issue_when_factor() throws Exception {
-    try {
-      DebtRemediationFunction.create(DebtRemediationFunction.Type.CONSTANT_ISSUE, "5min", "10h");
-      fail();
-    } catch(Exception e) {
-      assertThat(e).isInstanceOf(DebtRemediationFunction.ValidationException.class);
-    }
-  }
-
-  @Test
-  public void fail_to_create_linear_with_offset_when_no_factor() throws Exception {
-    try {
-      DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, null, "10h");
-      fail();
-    } catch(Exception e) {
-      assertThat(e).isInstanceOf(DebtRemediationFunction.ValidationException.class);
-    }
-  }
-
-  @Test
-  public void fail_to_create_linear_with_offset_when_no_offset() throws Exception {
-    try {
-      DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, "5min", null);
-      fail();
-    } catch(Exception e) {
-      assertThat(e).isInstanceOf(DebtRemediationFunction.ValidationException.class);
-    }
-  }
-
-  @Test
-  public void test_equals_and_hashcode() throws Exception {
-    DebtRemediationFunction function = DebtRemediationFunction.createLinearWithOffset("10h", "5min");
-    DebtRemediationFunction functionWithSameValue = DebtRemediationFunction.createLinearWithOffset("10h", "5min");
-    DebtRemediationFunction functionWithDifferentType = DebtRemediationFunction.createConstantPerIssue("5min");
-
-    assertThat(function).isEqualTo(function);
-    assertThat(function).isEqualTo(functionWithSameValue);
-    assertThat(function).isNotEqualTo(functionWithDifferentType);
-    assertThat(function).isNotEqualTo(DebtRemediationFunction.createLinearWithOffset("11h", "5min"));
-    assertThat(function).isNotEqualTo(DebtRemediationFunction.createLinearWithOffset("10h", "6min"));
-    assertThat(function).isNotEqualTo(DebtRemediationFunction.createLinear("10h"));
-    assertThat(function).isNotEqualTo(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();
-  }
-
-}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/DefaultDebtRemediationFunctionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/DefaultDebtRemediationFunctionTest.java
new file mode 100644 (file)
index 0000000..e504ee5
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * 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.server.rule;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+public class DefaultDebtRemediationFunctionTest {
+
+  @Test
+  public void create_linear() throws Exception {
+    DebtRemediationFunction function = DefaultDebtRemediationFunction.createLinear("10h");
+    assertThat(function.type()).isEqualTo(DefaultDebtRemediationFunction.Type.LINEAR);
+    assertThat(function.factor()).isEqualTo("10h");
+    assertThat(function.offset()).isNull();
+  }
+
+  @Test
+  public void create_linear_with_offset() throws Exception {
+    DebtRemediationFunction function = DefaultDebtRemediationFunction.createLinearWithOffset("10h", "5min");
+    assertThat(function.type()).isEqualTo(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET);
+    assertThat(function.factor()).isEqualTo("10h");
+    assertThat(function.offset()).isEqualTo("5min");
+  }
+
+  @Test
+  public void create_constant_per_issue() throws Exception {
+    DebtRemediationFunction function = DefaultDebtRemediationFunction.createConstantPerIssue("10h");
+    assertThat(function.type()).isEqualTo(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE);
+    assertThat(function.factor()).isNull();
+    assertThat(function.offset()).isEqualTo("10h");
+  }
+
+  @Test
+  public void sanitize_remediation_factor_and_offset() {
+    DebtRemediationFunction function = DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "  1  h   ", "  10   mi n");
+
+    assertThat(function.factor()).isEqualTo("1h");
+    assertThat(function.offset()).isEqualTo("10min");
+  }
+
+  @Test
+  public void fail_to_create_linear_when_no_factor() throws Exception {
+    try {
+      DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR, null, "10h");
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class);
+    }
+  }
+
+  @Test
+  public void fail_to_create_linear_when_offset() throws Exception {
+    try {
+      DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR, "5min", "10h");
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class);
+    }
+  }
+
+  @Test
+  public void fail_to_create_constant_per_issue_when_no_offset() throws Exception {
+    try {
+      DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE, "10h", null);
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class);
+    }
+  }
+
+  @Test
+  public void fail_to_create_constant_per_issue_when_factor() throws Exception {
+    try {
+      DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE, "5min", "10h");
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class);
+    }
+  }
+
+  @Test
+  public void fail_to_create_linear_with_offset_when_no_factor() throws Exception {
+    try {
+      DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, null, "10h");
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class);
+    }
+  }
+
+  @Test
+  public void fail_to_create_linear_with_offset_when_no_offset() throws Exception {
+    try {
+      DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "5min", null);
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class);
+    }
+  }
+
+  @Test
+  public void test_equals_and_hashcode() throws Exception {
+    DebtRemediationFunction function = DefaultDebtRemediationFunction.createLinearWithOffset("10h", "5min");
+    DebtRemediationFunction functionWithSameValue = DefaultDebtRemediationFunction.createLinearWithOffset("10h", "5min");
+    DebtRemediationFunction functionWithDifferentType = DefaultDebtRemediationFunction.createConstantPerIssue("5min");
+
+    assertThat(function).isEqualTo(function);
+    assertThat(function).isEqualTo(functionWithSameValue);
+    assertThat(function).isNotEqualTo(functionWithDifferentType);
+    assertThat(function).isNotEqualTo(DefaultDebtRemediationFunction.createLinearWithOffset("11h", "5min"));
+    assertThat(function).isNotEqualTo(DefaultDebtRemediationFunction.createLinearWithOffset("10h", "6min"));
+    assertThat(function).isNotEqualTo(DefaultDebtRemediationFunction.createLinear("10h"));
+    assertThat(function).isNotEqualTo(DefaultDebtRemediationFunction.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(DefaultDebtRemediationFunction.createLinearWithOffset("10h", "5min").toString()).isNotNull();
+  }
+
+}
index 2cc13c0abc894de95376df6cbbb90bd53038ccc8..665b1c87d2507251387bf88fd7a408e8efc85f74 100644 (file)
@@ -71,7 +71,7 @@ public class RulesDefinitionTest {
       .setInternalKey("/something")
       .setStatus(RuleStatus.BETA)
       .setDebtCharacteristic("COMPILER")
-      .setDebtRemediationFunction(DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"))
+      .setDebtRemediationFunction(DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"))
       .setEffortToFixDescription("squid.S115.effortToFix")
       .setTags("one", "two")
       .addTags("two", "three", "four");
@@ -92,7 +92,7 @@ public class RulesDefinitionTest {
     assertThat(npeRule.template()).isFalse();
     assertThat(npeRule.status()).isEqualTo(RuleStatus.BETA);
     assertThat(npeRule.debtCharacteristic()).isEqualTo("COMPILER");
-    assertThat(npeRule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"));
+    assertThat(npeRule.debtRemediationFunction()).isEqualTo(DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"));
     assertThat(npeRule.effortToFixDescription()).isEqualTo("squid.S115.effortToFix");
     assertThat(npeRule.toString()).isEqualTo("[repository=findbugs, key=NPE]");
     assertThat(npeRule.repository()).isSameAs(findbugs);
@@ -312,7 +312,7 @@ public class RulesDefinitionTest {
     RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
     newRepository.createRule("NPE").setName("NPE").setHtmlDescription("Detect <code>java.lang.NullPointerException</code>")
       .setDebtCharacteristic("")
-      .setDebtRemediationFunction(DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"));
+      .setDebtRemediationFunction(DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"));
     try {
       newRepository.done();
       fail();
index 5f76c882bb900f0998e1600a2dc22ff211526bbb..fb08a965599171f58a813dd0524092e650d7cf76 100644 (file)
@@ -35,7 +35,6 @@ import org.sonar.api.ServerExtension;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.rule.DebtRemediationFunction;
 import org.sonar.api.utils.Duration;
-import org.sonar.api.utils.MessageException;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
@@ -138,7 +137,7 @@ public class DebtRulesXMLImporter implements ServerExtension {
       }
     }
     if (StringUtils.isNotBlank(ruleRepositoryKey) && StringUtils.isNotBlank(ruleKey)) {
-      return processRule(RuleKey.of(ruleRepositoryKey, ruleKey), properties);
+      return createRule(RuleKey.of(ruleRepositoryKey, ruleKey), properties);
     }
     return null;
   }
@@ -169,15 +168,6 @@ public class DebtRulesXMLImporter implements ServerExtension {
     return new Property(key, value, textValue);
   }
 
-  @CheckForNull
-  private RuleDebt processRule(RuleKey ruleKey, Properties properties) {
-    try {
-      return createRule(ruleKey, properties);
-    } catch (DebtRemediationFunction.ValidationException e) {
-      throw MessageException.of(String.format("Rule '%s' is invalid : %s", ruleKey, e.getMessage()));
-    }
-  }
-
   @CheckForNull
   private RuleDebt createRule(RuleKey ruleKey, Properties properties) {
     Property function = properties.function();
@@ -197,13 +187,13 @@ public class DebtRulesXMLImporter implements ServerExtension {
   private RuleDebt createRuleDebt(RuleKey ruleKey, String function, @Nullable String factor, @Nullable String offset) {
     if ("linear_threshold".equals(function) && factor != null) {
       LOG.warn(String.format("Linear with threshold function is no longer used, remediation function of '%s' is replaced by linear.", ruleKey));
-      return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.createLinear(factor));
+      return new RuleDebt().setRuleKey(ruleKey).setType(DebtRemediationFunction.Type.LINEAR).setFactor(factor);
     } else if ("constant_resource".equals(function)) {
       LOG.warn(String.format("Constant/file function is no longer used, technical debt definitions on '%s' are ignored.", ruleKey));
     } else if (DebtRemediationFunction.Type.CONSTANT_ISSUE.name().equalsIgnoreCase(function) && factor != null && offset == null) {
-      return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.createConstantPerIssue(factor));
+      return new RuleDebt().setRuleKey(ruleKey).setType(DebtRemediationFunction.Type.CONSTANT_ISSUE).setOffset(factor);
     } else {
-      return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.create(DebtRemediationFunction.Type.valueOf(function.toUpperCase()), factor, offset));
+      return new RuleDebt().setRuleKey(ruleKey).setType(DebtRemediationFunction.Type.valueOf(function.toUpperCase())).setFactor(factor).setOffset(offset);
     }
     return null;
   }
@@ -279,7 +269,9 @@ public class DebtRulesXMLImporter implements ServerExtension {
   public static class RuleDebt {
     private RuleKey ruleKey;
     private String characteristicKey;
-    private DebtRemediationFunction function;
+    private DebtRemediationFunction.Type type;
+    private String factor;
+    private String offset;
 
     public RuleKey ruleKey() {
       return ruleKey;
@@ -299,12 +291,32 @@ public class DebtRulesXMLImporter implements ServerExtension {
       return this;
     }
 
-    public DebtRemediationFunction function() {
-      return function;
+    public DebtRemediationFunction.Type type() {
+      return type;
+    }
+
+    public RuleDebt setType(DebtRemediationFunction.Type type) {
+      this.type = type;
+      return this;
+    }
+
+    @CheckForNull
+    public String factor() {
+      return factor;
+    }
+
+    public RuleDebt setFactor(@Nullable String factor) {
+      this.factor = factor;
+      return this;
+    }
+
+    @CheckForNull
+    public String offset() {
+      return offset;
     }
 
-    public RuleDebt setFunction(DebtRemediationFunction function) {
-      this.function = function;
+    public RuleDebt setOffset(@Nullable String offset) {
+      this.offset = offset;
       return this;
     }
   }
index 4dedd5254ab047284f178897fcd7ae4b39db098b..84c0d5b7c60272f81fbb85af91cc57e858fb5dba 100644 (file)
@@ -105,7 +105,19 @@ public class DeprecatedRulesDefinition implements RulesDefinition {
     DebtRulesXMLImporter.RuleDebt ruleDebt = findRequirement(ruleDebts, repoKey, ruleKey);
     if (ruleDebt != null) {
       newRule.setDebtCharacteristic(ruleDebt.characteristicKey());
-      newRule.setDebtRemediationFunction(ruleDebt.function());
+      switch (ruleDebt.type()) {
+        case LINEAR :
+          newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linear(ruleDebt.factor()));
+          break;
+        case LINEAR_OFFSET:
+          newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linearWithOffset(ruleDebt.factor(), ruleDebt.offset()));
+          break;
+        case CONSTANT_ISSUE:
+          newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().constantPerIssue(ruleDebt.offset()));
+          break;
+        default :
+          throw new IllegalArgumentException(String.format("The type '%s' is unknown", ruleDebt.type()));
+      }
     }
   }
 
index ee747ed4ba9168c3af656aa4ebf23e2e3596994e..775cb4f03b9c88d2024f0c5df216cc7ab8fe6d9d 100644 (file)
@@ -459,11 +459,11 @@ public class RuleRegistration implements Startable {
    */
   private void removeActiveRulesOnStillExistingRepositories(List<RuleDto> removedRules, RulesDefinition.Context context) {
     List<String> repositoryKeys = newArrayList(Iterables.transform(context.repositories(), new Function<RulesDefinition.Repository, String>() {
-      @Override
-      public String apply(RulesDefinition.Repository input) {
-        return input.key();
+        @Override
+        public String apply(RulesDefinition.Repository input) {
+          return input.key();
+        }
       }
-    }
     ));
 
     for (RuleDto rule : removedRules) {
index 9fdaefb428f9796f9af80ffdd314d15db173fe0b..d2ddfde267bf7a9a4d8de726480eba57b117538b 100644 (file)
@@ -25,7 +25,6 @@ import com.google.common.io.Resources;
 import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.rule.DebtRemediationFunction;
-import org.sonar.api.utils.MessageException;
 
 import java.io.IOException;
 import java.util.List;
@@ -55,7 +54,9 @@ public class DebtRulesXMLImporterTest {
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
     assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinear("3h"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+    assertThat(ruleDebt.factor()).isEqualTo("3h");
+    assertThat(ruleDebt.offset()).isNull();
   }
 
   @Test
@@ -68,7 +69,9 @@ public class DebtRulesXMLImporterTest {
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
     assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinear("3h"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+    assertThat(ruleDebt.factor()).isEqualTo("3h");
+    assertThat(ruleDebt.offset()).isNull();
   }
 
   @Test
@@ -80,7 +83,9 @@ public class DebtRulesXMLImporterTest {
 
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinearWithOffset("3h", "1min"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+    assertThat(ruleDebt.factor()).isEqualTo("3h");
+    assertThat(ruleDebt.offset()).isEqualTo("1min");
   }
 
   @Test
@@ -92,7 +97,9 @@ public class DebtRulesXMLImporterTest {
 
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createConstantPerIssue("3d"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
+    assertThat(ruleDebt.factor()).isNull();
+    assertThat(ruleDebt.offset()).isEqualTo("3d");
   }
 
   @Test
@@ -104,7 +111,9 @@ public class DebtRulesXMLImporterTest {
 
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinearWithOffset("3d", "1d"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+    assertThat(ruleDebt.factor()).isEqualTo("3d");
+    assertThat(ruleDebt.offset()).isEqualTo("1d");
   }
 
   @Test
@@ -116,7 +125,9 @@ public class DebtRulesXMLImporterTest {
 
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinear("3min"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+    assertThat(ruleDebt.factor()).isEqualTo("3min");
+    assertThat(ruleDebt.offset()).isNull();
   }
 
   @Test
@@ -128,7 +139,9 @@ public class DebtRulesXMLImporterTest {
 
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinear("3h"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+    assertThat(ruleDebt.factor()).isEqualTo("3h");
+    assertThat(ruleDebt.offset()).isNull();
   }
 
   @Test
@@ -140,7 +153,9 @@ public class DebtRulesXMLImporterTest {
 
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createConstantPerIssue("3h"));
+    assertThat(ruleDebt.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
+    assertThat(ruleDebt.factor()).isNull();
+    assertThat(ruleDebt.offset()).isEqualTo("3h");
   }
 
   @Test
@@ -169,7 +184,9 @@ public class DebtRulesXMLImporterTest {
     DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0);
     assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
     assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
-    assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.createLinear("3h"));
+    assertThat(ruleDebt.type()).isEqualTo(org.sonar.api.server.rule.DebtRemediationFunction.Type.LINEAR);
+    assertThat(ruleDebt.factor()).isEqualTo("3h");
+    assertThat(ruleDebt.offset()).isNull();
   }
 
   @Test
@@ -179,17 +196,6 @@ public class DebtRulesXMLImporterTest {
     assertThat(results).isEmpty();
   }
 
-  @Test
-  public void fail_to_import_linear_having_offset() throws Exception {
-    String xml = getFileContent("fail_to_import_linear_having_offset.xml");
-    try {
-      importer.importXML(xml);
-      fail();
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(MessageException.class);
-    }
-  }
-
   @Test
   public void fail_on_bad_xml() {
     String xml = getFileContent("fail_on_bad_xml.xml");
@@ -197,7 +203,7 @@ public class DebtRulesXMLImporterTest {
     try {
       new DebtCharacteristicsXMLImporter().importXML(xml);
       fail();
-    } catch (Exception e){
+    } catch (Exception e) {
       assertThat(e).isInstanceOf(IllegalStateException.class);
     }
   }
index 512bd923d771ee4e8b48b5b8d23bf682b920235d..636b5f757deadf656c93ed3152d7f8dbc3364d77 100644 (file)
@@ -158,8 +158,10 @@ public class DeprecatedRulesDefinitionTest {
       new DebtRulesXMLImporter.RuleDebt()
         .setCharacteristicKey("MEMORY_EFFICIENCY")
         .setRuleKey(RuleKey.of("checkstyle", "ConstantName"))
-        .setFunction(DebtRemediationFunction.createLinearWithOffset("1d", "10min")
-        ));
+        .setType(DebtRemediationFunction.Type.LINEAR_OFFSET)
+        .setFactor("1d")
+        .setOffset("10min")
+    );
 
     Reader javaModelReader = mock(Reader.class);
     when(debtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
@@ -176,7 +178,9 @@ public class DeprecatedRulesDefinitionTest {
     assertThat(rule).isNotNull();
     assertThat(rule.key()).isEqualTo("ConstantName");
     assertThat(rule.debtCharacteristic()).isEqualTo("MEMORY_EFFICIENCY");
-    assertThat(rule.debtRemediationFunction()).isEqualTo(DebtRemediationFunction.createLinearWithOffset("1d", "10min"));
+    assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+    assertThat(rule.debtRemediationFunction().factor()).isEqualTo("1d");
+    assertThat(rule.debtRemediationFunction().offset()).isEqualTo("10min");
   }
 
 }
index c0a4a97bdad9a8f28bbe190cbf8e938ff7337c7e..b4b2f85ee6acf382bd7da8b364e5c57797c31ea5 100644 (file)
@@ -24,7 +24,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
-import org.sonar.api.server.rule.DebtRemediationFunction;
 import org.sonar.api.server.rule.RulesDefinition;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.MessageException;
@@ -268,7 +267,8 @@ public class RuleRegistrationTest extends AbstractDaoTestCase {
   public void insert_extended_repositories() {
     task = new RuleRegistration(new RuleDefinitionsLoader(mock(RuleRepositories.class), new RulesDefinition[]{
       new FindbugsRepository(), new FbContribRepository()}),
-      profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtModel.class));
+      profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtModel.class)
+    );
 
     setupData("empty");
     task.start();
@@ -285,11 +285,13 @@ public class RuleRegistrationTest extends AbstractDaoTestCase {
         .setName("One")
         .setHtmlDescription("Description of One")
         .setSeverity(Severity.BLOCKER)
-        .setDebtCharacteristic("MEMORY_EFFICIENCY")
-        .setDebtRemediationFunction(DebtRemediationFunction.createLinearWithOffset("5d", "10h"))
-        .setEffortToFixDescription("squid.S115.effortToFix")
         .setInternalKey("config1")
         .setTags("tag1", "tag3", "tag5");
+
+      rule1.setDebtCharacteristic("MEMORY_EFFICIENCY")
+        .setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("5d", "10h"))
+        .setEffortToFixDescription("squid.S115.effortToFix");
+
       rule1.createParam("param1").setDescription("parameter one").setDefaultValue("default value one");
       rule1.createParam("param2").setDescription("parameter two").setDefaultValue("default value two");
 
diff --git a/sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/fail_to_import_linear_having_offset.xml b/sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/fail_to_import_linear_having_offset.xml
deleted file mode 100644 (file)
index f95e35e..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<!--
-  ~ 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.
-  -->
-
-<sqale>
-  <chc>
-    <key>USABILITY</key>
-    <name>Usability</name>
-    <desc>Estimate usability</desc>
-  </chc>
-  <chc>
-    <key>EFFICIENCY</key>
-    <name>Efficiency</name>
-    <chc>
-      <key>MEMORY_EFFICIENCY</key>
-      <name>Memory use</name>
-      <chc>
-        <rule-repo>checkstyle</rule-repo>
-        <rule-key>Regexp</rule-key>
-        <prop>
-          <key>offset</key>
-          <val>3.0</val>
-          <txt>h</txt>
-        </prop>
-        <prop>
-          <key>remediationFunction</key>
-          <txt>linear</txt>
-        </prop>
-      </chc>
-    </chc>
-  </chc>
-
-</sqale>