aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2015-05-13 11:55:37 +0200
committerDavid Gageot <david@gageot.net>2015-05-13 15:13:54 +0200
commitd8797fd0f659ae1159b26b7cb4131833daedd3f7 (patch)
treefb15432207d6d03f2184a97f88c4b0f93d85535d /sonar-plugin-api
parent7e997a0ed5c7552a70f60ac55869fab51370f4ac (diff)
downloadsonarqube-d8797fd0f659ae1159b26b7cb4131833daedd3f7.tar.gz
sonarqube-d8797fd0f659ae1159b26b7cb4131833daedd3f7.zip
Tests should use ExpectedException
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java17
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java27
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java45
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java14
4 files changed, 51 insertions, 52 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java
index 29cfc369d0a..726395963ab 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java
@@ -19,16 +19,20 @@
*/
package org.sonar.api.batch.rule.internal;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
public class ActiveRulesBuilderTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
public void no_rules() {
ActiveRulesBuilder builder = new ActiveRulesBuilder();
@@ -81,11 +85,10 @@ public class ActiveRulesBuilderTest {
public void fail_to_add_twice_the_same_rule() {
ActiveRulesBuilder builder = new ActiveRulesBuilder();
builder.create(RuleKey.of("squid", "S0001")).activate();
- try {
- builder.create(RuleKey.of("squid", "S0001")).activate();
- fail();
- } catch (IllegalStateException e) {
- assertThat(e).hasMessage("Rule 'squid:S0001' is already activated");
- }
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Rule 'squid:S0001' is already activated");
+
+ builder.create(RuleKey.of("squid", "S0001")).activate();
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
index 95f0cea83cd..a64550d7b09 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
@@ -20,6 +20,7 @@
package org.sonar.api.batch.rule.internal;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.batch.debt.DebtRemediationFunction;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.Rules;
@@ -29,9 +30,11 @@ import org.sonar.api.rule.Severity;
import org.sonar.api.utils.Duration;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
public class RulesBuilderTest {
+ @org.junit.Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
public void no_rules() {
RulesBuilder builder = new RulesBuilder();
@@ -98,12 +101,11 @@ public class RulesBuilderTest {
public void fail_to_add_twice_the_same_rule() {
RulesBuilder builder = new RulesBuilder();
builder.add(RuleKey.of("squid", "S0001"));
- try {
- builder.add(RuleKey.of("squid", "S0001"));
- fail();
- } catch (IllegalStateException e) {
- assertThat(e).hasMessage("Rule 'squid:S0001' already exists");
- }
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Rule 'squid:S0001' already exists");
+
+ builder.add(RuleKey.of("squid", "S0001"));
}
@Test
@@ -112,11 +114,10 @@ public class RulesBuilderTest {
NewRule newRule = builder.add(RuleKey.of("squid", "S0001"));
newRule.addParam("min");
newRule.addParam("max");
- try {
- newRule.addParam("min");
- fail();
- } catch (IllegalStateException e) {
- assertThat(e).hasMessage("Parameter 'min' already exists on rule 'squid:S0001'");
- }
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Parameter 'min' already exists on rule 'squid:S0001'");
+
+ newRule.addParam("min");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
index 52ed7d9f2d9..cc1ba978c6b 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
@@ -19,7 +19,9 @@
*/
package org.sonar.api.config;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.api.PropertyField;
@@ -28,9 +30,10 @@ import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.AnnotationUtils;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
public class PropertyDefinitionTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
@Test
public void should_override_toString() {
@@ -268,42 +271,34 @@ public class PropertyDefinitionTest {
@Test
public void should_not_authorise_empty_key() {
- try {
- PropertyDefinition.builder(null).build();
- fail();
- } catch (Exception e) {
- assertThat(e).hasMessage("Key must be set").isInstanceOf(IllegalArgumentException.class);
- }
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Key must be set");
+
+ PropertyDefinition.builder(null).build();
}
@Test
public void should_not_authorize_defining_on_qualifiers_and_hidden() {
- try {
- PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).hidden().build();
- fail();
- } catch (Exception e) {
- assertThat(e).hasMessage("Cannot be hidden and defining qualifiers on which to display").isInstanceOf(IllegalArgumentException.class);
- }
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Cannot be hidden and defining qualifiers on which to display");
+
+ PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).hidden().build();
}
@Test
public void should_not_authorize_defining_ony_on_qualifiers_and_hidden() {
- try {
- PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.FILE).hidden().build();
- fail();
- } catch (Exception e) {
- assertThat(e).hasMessage("Cannot be hidden and defining qualifiers on which to display").isInstanceOf(IllegalArgumentException.class);
- }
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Cannot be hidden and defining qualifiers on which to display");
+
+ PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.FILE).hidden().build();
}
@Test
public void should_not_authorize_defining_on_qualifiers_and_only_on_qualifiers() {
- try {
- PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).onlyOnQualifiers(Qualifiers.PROJECT).build();
- fail();
- } catch (Exception e) {
- assertThat(e).hasMessage("Cannot define both onQualifiers and onlyOnQualifiers").isInstanceOf(IllegalArgumentException.class);
- }
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Cannot define both onQualifiers and onlyOnQualifiers");
+
+ PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).onlyOnQualifiers(Qualifiers.PROJECT).build();
}
@Properties(@Property(key = "hello", name = "Hello", defaultValue = "world", description = "desc",
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java
index e1fb1f2bb41..1b065e082e4 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java
@@ -20,16 +20,19 @@
package org.sonar.api.issue.action;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.issue.condition.Condition;
import org.sonar.api.issue.internal.DefaultIssue;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ActionTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
Condition condition1 = mock(Condition.class);
Condition condition2 = mock(Condition.class);
@@ -49,12 +52,9 @@ public class ActionTest {
@Test
public void key_should_be_set() {
- try {
- new Action("");
- fail();
- } catch (Exception e) {
- assertThat(e).hasMessage("Action key must be set");
- }
+ thrown.expectMessage("Action key must be set");
+
+ new Action("");
}
@Test