From: Julien Lancelot Date: Mon, 24 Mar 2014 12:50:21 +0000 (+0100) Subject: SONAR-5056 Remove useless static creation methods X-Git-Tag: 4.3~302 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=df9e488fe8c877931155916bb76932caeaeef897;p=sonarqube.git SONAR-5056 Remove useless static creation methods --- 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 index a5bda3a8f25..dedf5a3e60c 100644 --- 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 @@ -35,7 +35,7 @@ class DefaultDebtRemediationFunction implements DebtRemediationFunction { private String factor; private String offset; - private DefaultDebtRemediationFunction(Type type, @Nullable String factor, @Nullable String offset) { + public DefaultDebtRemediationFunction(Type type, @Nullable String factor, @Nullable String offset) { this.type = type; // TODO validate factor and offset format this.factor = StringUtils.deleteWhitespace(factor); @@ -43,31 +43,18 @@ class DefaultDebtRemediationFunction implements DebtRemediationFunction { 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); - } - + @Override public Type type() { return type; } + @Override @CheckForNull public String factor() { return factor; } + @Override @CheckForNull public String offset() { return offset; 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 index 3c529e73b57..befa7907095 100644 --- 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 @@ -50,7 +50,7 @@ class DefaultDebtRemediationFunctions implements DebtRemediationFunctions { private DebtRemediationFunction create(DefaultDebtRemediationFunction.Type type, @Nullable String factor, @Nullable String offset) { try { - return DefaultDebtRemediationFunction.create(type, factor, offset); + return new DefaultDebtRemediationFunction(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())); } 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 index e504ee5c557..5f1b5ae57ec 100644 --- 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 @@ -29,7 +29,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void create_linear() throws Exception { - DebtRemediationFunction function = DefaultDebtRemediationFunction.createLinear("10h"); + DebtRemediationFunction function = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "10h", null); assertThat(function.type()).isEqualTo(DefaultDebtRemediationFunction.Type.LINEAR); assertThat(function.factor()).isEqualTo("10h"); assertThat(function.offset()).isNull(); @@ -37,7 +37,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void create_linear_with_offset() throws Exception { - DebtRemediationFunction function = DefaultDebtRemediationFunction.createLinearWithOffset("10h", "5min"); + DebtRemediationFunction function = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "10h", "5min"); assertThat(function.type()).isEqualTo(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET); assertThat(function.factor()).isEqualTo("10h"); assertThat(function.offset()).isEqualTo("5min"); @@ -45,7 +45,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void create_constant_per_issue() throws Exception { - DebtRemediationFunction function = DefaultDebtRemediationFunction.createConstantPerIssue("10h"); + DebtRemediationFunction function = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "10h"); assertThat(function.type()).isEqualTo(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE); assertThat(function.factor()).isNull(); assertThat(function.offset()).isEqualTo("10h"); @@ -53,7 +53,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void sanitize_remediation_factor_and_offset() { - DebtRemediationFunction function = DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, " 1 h ", " 10 mi n"); + DebtRemediationFunction function = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, " 1 h ", " 10 mi n"); assertThat(function.factor()).isEqualTo("1h"); assertThat(function.offset()).isEqualTo("10min"); @@ -62,7 +62,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void fail_to_create_linear_when_no_factor() throws Exception { try { - DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR, null, "10h"); + new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, null, "10h"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class); @@ -72,7 +72,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void fail_to_create_linear_when_offset() throws Exception { try { - DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR, "5min", "10h"); + new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "5min", "10h"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class); @@ -82,7 +82,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void fail_to_create_constant_per_issue_when_no_offset() throws Exception { try { - DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE, "10h", null); + new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, "10h", null); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class); @@ -92,7 +92,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void fail_to_create_constant_per_issue_when_factor() throws Exception { try { - DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.CONSTANT_ISSUE, "5min", "10h"); + new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, "5min", "10h"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class); @@ -102,7 +102,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void fail_to_create_linear_with_offset_when_no_factor() throws Exception { try { - DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, null, "10h"); + new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, null, "10h"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class); @@ -112,7 +112,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void fail_to_create_linear_with_offset_when_no_offset() throws Exception { try { - DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "5min", null); + new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "5min", null); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(DefaultDebtRemediationFunction.ValidationException.class); @@ -121,17 +121,18 @@ public class DefaultDebtRemediationFunctionTest { @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"); + DebtRemediationFunction function = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "10h", "5min"); + DebtRemediationFunction functionWithSameValue = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "10h", "5min"); + DebtRemediationFunction functionWithDifferentType = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "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).isNotEqualTo(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "11h", "5min")); + assertThat(function).isNotEqualTo(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "10h", "6min")); + assertThat(function).isNotEqualTo(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "10h", null)); + assertThat(function).isNotEqualTo(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "6min")); assertThat(function.hashCode()).isEqualTo(function.hashCode()); assertThat(function.hashCode()).isEqualTo(functionWithSameValue.hashCode()); @@ -140,7 +141,7 @@ public class DefaultDebtRemediationFunctionTest { @Test public void test_to_string() throws Exception { - assertThat(DefaultDebtRemediationFunction.createLinearWithOffset("10h", "5min").toString()).isNotNull(); + assertThat(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET, "10h", "5min").toString()).isNotNull(); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java index 665b1c87d25..5a9f2511c12 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java @@ -64,17 +64,18 @@ public class RulesDefinitionTest { @Test public void define_rules() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); - newFindbugs.createRule("NPE") + RulesDefinition.NewRule newFindbug = newFindbugs.createRule("NPE") .setName("Detect NPE") .setHtmlDescription("Detect java.lang.NullPointerException") .setSeverity(Severity.BLOCKER) .setInternalKey("/something") .setStatus(RuleStatus.BETA) .setDebtCharacteristic("COMPILER") - .setDebtRemediationFunction(DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min")) .setEffortToFixDescription("squid.S115.effortToFix") .setTags("one", "two") .addTags("two", "three", "four"); + newFindbug.setDebtRemediationFunction(newFindbug.debtRemediationFunctions().linearWithOffset("1h", "10min")); + newFindbugs.createRule("ABC").setName("ABC").setHtmlDescription("ABC"); newFindbugs.done(); @@ -92,7 +93,9 @@ public class RulesDefinitionTest { assertThat(npeRule.template()).isFalse(); assertThat(npeRule.status()).isEqualTo(RuleStatus.BETA); assertThat(npeRule.debtCharacteristic()).isEqualTo("COMPILER"); - assertThat(npeRule.debtRemediationFunction()).isEqualTo(DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min")); + assertThat(npeRule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET); + assertThat(npeRule.debtRemediationFunction().factor()).isEqualTo("1h"); + assertThat(npeRule.debtRemediationFunction().offset()).isEqualTo("10min"); assertThat(npeRule.effortToFixDescription()).isEqualTo("squid.S115.effortToFix"); assertThat(npeRule.toString()).isEqualTo("[repository=findbugs, key=NPE]"); assertThat(npeRule.repository()).isSameAs(findbugs); @@ -310,9 +313,9 @@ public class RulesDefinitionTest { @Test public void fail_if_define_function_without_characteristic() { RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java"); - newRepository.createRule("NPE").setName("NPE").setHtmlDescription("Detect java.lang.NullPointerException") - .setDebtCharacteristic("") - .setDebtRemediationFunction(DefaultDebtRemediationFunction.create(DefaultDebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min")); + RulesDefinition.NewRule newRule = newRepository.createRule("NPE").setName("NPE").setHtmlDescription("Detect java.lang.NullPointerException") + .setDebtCharacteristic(""); + newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linearWithOffset("1h", "10min")); try { newRepository.done(); fail();