]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7946 Fail with IllegalStateException on errors
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 31 Jan 2017 10:04:39 +0000 (11:04 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 1 Feb 2017 13:13:02 +0000 (14:13 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java

index 0f21dd713061530a76966337d91deaa2d5b5b084..55a7e8effaf6d42d52b6443a33093a3c45688ff7 100644 (file)
@@ -339,7 +339,7 @@ public class RulesDefinitionXmlLoader {
       fillRemediationFunction(rule, debtRemediationFunction, debtRemediationFunctionGapMultiplier, debtRemediationFunctionBaseEffort);
       fillParams(rule, params);
     } catch (Exception e) {
-      throw new IllegalArgumentException(format("Fail to load the rule with key [%s:%s]", repo.key(), key), e);
+      throw new IllegalStateException(format("Fail to load the rule with key [%s:%s]", repo.key(), key), e);
     }
   }
 
index 2780f878f941127c5431c3d4ddaeba95663d7e47..9276b3e706c5c482d372caa9081558a3f2cfeda4 100644 (file)
@@ -32,7 +32,7 @@ import org.sonar.api.rules.RuleType;
 import org.sonar.api.server.debt.DebtRemediationFunction;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
+import static org.sonar.test.ExceptionCauseMatcher.hasType;
 
 public class RulesDefinitionXmlLoaderTest {
 
@@ -199,21 +199,20 @@ public class RulesDefinitionXmlLoaderTest {
 
   @Test
   public void fail_if_invalid_remediation_function() {
-    try {
-      load("" +
-        "<rules>" +
-        "  <rule>" +
-        "    <key>1</key>" +
-        "    <name>One</name>" +
-        "    <description>Desc</description>" +
-        "    <remediationFunction>UNKNOWN</remediationFunction>" +
-        "  </rule>" +
-        "</rules>");
-      fail();
-    } catch (IllegalArgumentException e) {
-      assertThat(e).hasMessageContaining("Fail to load the rule with key [squid:1]");
-      assertThat(e.getCause()).hasMessageContaining("No enum constant org.sonar.api.server.debt.DebtRemediationFunction.Type.UNKNOWN");
-    }
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to load the rule with key [squid:1]");
+    expectedException.expectCause(hasType(IllegalArgumentException.class)
+      .andMessage("No enum constant org.sonar.api.server.debt.DebtRemediationFunction.Type.UNKNOWN"));
+
+    load("" +
+      "<rules>" +
+      "  <rule>" +
+      "    <key>1</key>" +
+      "    <name>One</name>" +
+      "    <description>Desc</description>" +
+      "    <remediationFunction>UNKNOWN</remediationFunction>" +
+      "  </rule>" +
+      "</rules>");
   }
 
   @Test
@@ -253,23 +252,22 @@ public class RulesDefinitionXmlLoaderTest {
 
   @Test
   public void fail_if_unsupported_description_format() {
-    try {
-      String xml = "" +
-        "<rules>" +
-        "  <rule>" +
-        "    <key>1</key>" +
-        "    <name>One</name>" +
-        "    <description>Desc</description>" +
-        "    <descriptionFormat>UNKNOWN</descriptionFormat>" +
-        "  </rule>" +
-        "</rules>";
-      RulesDefinition.Rule rule = load(xml).rule("1");
-      assertThat(rule.markdownDescription()).isEqualTo("Desc");
-      assertThat(rule.htmlDescription()).isNull();
-    } catch (IllegalArgumentException e) {
-      assertThat(e).hasMessageContaining("Fail to load the rule with key [squid:1]");
-      assertThat(e.getCause()).hasMessageContaining("No enum constant org.sonar.api.server.rule.RulesDefinitionXmlLoader.DescriptionFormat.UNKNOWN");
-    }
+    String xml = "" +
+      "<rules>" +
+      "  <rule>" +
+      "    <key>1</key>" +
+      "    <name>One</name>" +
+      "    <description>Desc</description>" +
+      "    <descriptionFormat>UNKNOWN</descriptionFormat>" +
+      "  </rule>" +
+      "</rules>";
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to load the rule with key [squid:1]");
+    expectedException.expectCause(hasType(IllegalArgumentException.class)
+      .andMessage("No enum constant org.sonar.api.server.rule.RulesDefinitionXmlLoader.DescriptionFormat.UNKNOWN"));
+
+    load(xml).rule("1");
   }
 
   @Test