aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api-impl/src/test/java
diff options
context:
space:
mode:
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>2021-11-17 22:54:06 +0100
committersonartech <sonartech@sonarsource.com>2021-11-19 20:03:27 +0000
commita3d88ea27c35921647d7602755828ca73e15e865 (patch)
tree5626c38afab1ea00ab9897da431476c17b478bbe /sonar-plugin-api-impl/src/test/java
parent92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff)
downloadsonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz
sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'sonar-plugin-api-impl/src/test/java')
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultFileSystemTest.java12
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/FileMetadataTest.java3
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java18
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java33
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java11
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java31
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/InMemorySensorStorageTest.java20
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java17
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultExternalIssueTest.java22
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java53
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java16
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/rule/internal/DefaultAdHocRuleTest.java37
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java4
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/AesECBCipherTest.java24
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MapSettingsTest.java103
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MultivaluePropertyTest.java18
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/AlwaysIncreasingSystem2Test.java40
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/DefaultTempFolderTest.java37
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java34
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java23
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/SonarRuntimeImplTest.java6
21 files changed, 219 insertions, 343 deletions
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultFileSystemTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultFileSystemTest.java
index 9a9cab80f64..6f7c70c1c92 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultFileSystemTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/DefaultFileSystemTest.java
@@ -24,21 +24,18 @@ import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class DefaultFileSystemTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
private DefaultFileSystem fs;
private File basedir;
@@ -122,13 +119,12 @@ public class DefaultFileSystemTest {
@Test
public void input_file_fails_if_too_many_results() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("expected one element");
-
fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build());
- fs.inputFile(fs.predicates().all());
+ assertThatThrownBy(() -> fs.inputFile(fs.predicates().all()))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("expected one element");
}
@Test
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/FileMetadataTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/FileMetadataTest.java
index 806f463fcd9..8fd67f6ed6b 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/FileMetadataTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/fs/internal/fs/FileMetadataTest.java
@@ -28,7 +28,6 @@ import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.FileMetadata;
@@ -46,8 +45,6 @@ import static org.mockito.Mockito.verify;
public class FileMetadataTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
index 71f25a03de3..66c0520b4e8 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/RulesBuilderTest.java
@@ -20,7 +20,6 @@
package org.sonar.api.batch.rule.internal;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.Rules;
import org.sonar.api.rule.RuleKey;
@@ -28,10 +27,9 @@ import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class RulesBuilderTest {
- @org.junit.Rule
- public ExpectedException thrown = ExpectedException.none();
@Test
public void no_rules() {
@@ -91,10 +89,9 @@ public class RulesBuilderTest {
RulesBuilder builder = new RulesBuilder();
builder.add(RuleKey.of("squid", "S0001"));
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Rule 'squid:S0001' already exists");
-
- builder.add(RuleKey.of("squid", "S0001"));
+ assertThatThrownBy(() -> builder.add(RuleKey.of("squid", "S0001")))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Rule 'squid:S0001' already exists");
}
@Test
@@ -104,9 +101,8 @@ public class RulesBuilderTest {
newRule.addParam("min");
newRule.addParam("max");
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Parameter 'min' already exists on rule 'squid:S0001'");
-
- newRule.addParam("min");
+ assertThatThrownBy(() -> newRule.addParam("min"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Parameter 'min' already exists on rule 'squid:S0001'");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java
index efd93e5a8f1..eaa2e4d5c00 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java
@@ -19,14 +19,12 @@
*/
package org.sonar.api.batch.sensor.code.internal;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
-import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.api.batch.sensor.internal.SensorStorage;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -37,9 +35,6 @@ public class DefaultSignificantCodeTest {
.setContents("this is\na file\n with some code")
.build();
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
@Test
public void should_save_ranges() {
underTest.onFile(inputFile)
@@ -50,9 +45,9 @@ public class DefaultSignificantCodeTest {
@Test
public void fail_if_save_without_file() {
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Call onFile() first");
- underTest.save();
+ assertThatThrownBy(() -> underTest.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Call onFile() first");
}
@Test
@@ -60,24 +55,24 @@ public class DefaultSignificantCodeTest {
underTest.onFile(inputFile);
underTest.addRange(inputFile.selectLine(1));
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Significant code was already reported for line '1'.");
- underTest.addRange(inputFile.selectLine(1));
+ assertThatThrownBy(() -> underTest.addRange(inputFile.selectLine(1)))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Significant code was already reported for line '1'.");
}
@Test
public void fail_if_range_includes_many_lines() {
underTest.onFile(inputFile);
- exception.expect(IllegalArgumentException.class);
- exception.expectMessage("Ranges of significant code must be located in a single line");
- underTest.addRange(inputFile.newRange(1, 1, 2, 1));
+ assertThatThrownBy(() -> underTest.addRange(inputFile.newRange(1, 1, 2, 1)))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Ranges of significant code must be located in a single line");
}
@Test
public void fail_if_add_range_before_setting_file() {
- exception.expect(IllegalStateException.class);
- exception.expectMessage("addRange() should be called after on()");
- underTest.addRange(inputFile.selectLine(1));
+ assertThatThrownBy(() -> underTest.addRange(inputFile.selectLine(1)))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("addRange() should be called after on()");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java
index 39f7518c493..934dcb35f5c 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java
@@ -21,9 +21,7 @@ package org.sonar.api.batch.sensor.error.internal;
import org.assertj.core.api.Assertions;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextPointer;
@@ -32,6 +30,7 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.error.NewAnalysisError;
import org.sonar.api.batch.sensor.internal.SensorStorage;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.fail;
public class DefaultAnalysisErrorTest {
@@ -39,9 +38,6 @@ public class DefaultAnalysisErrorTest {
private SensorStorage storage;
private TextPointer textPointer;
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
@Before
public void setUp() {
inputFile = new TestInputFileBuilder("module1", "src/File.java").build();
@@ -72,9 +68,10 @@ public class DefaultAnalysisErrorTest {
@Test
public void test_no_storage() {
- exception.expect(NullPointerException.class);
DefaultAnalysisError analysisError = new DefaultAnalysisError();
- analysisError.onFile(inputFile).save();
+
+ assertThatThrownBy(() -> analysisError.onFile(inputFile).save())
+ .isInstanceOf(NullPointerException.class);
}
@Test
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java
index 7a3c09c3a5b..8331b08f340 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/highlighting/internal/DefaultHighlightingTest.java
@@ -22,9 +22,7 @@ package org.sonar.api.batch.sensor.highlighting.internal;
import java.util.Collection;
import org.assertj.core.api.Assertions;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
@@ -33,6 +31,7 @@ import org.sonar.api.batch.fs.internal.DefaultTextRange;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.batch.sensor.highlighting.TypeOfText.COMMENT;
import static org.sonar.api.batch.sensor.highlighting.TypeOfText.KEYWORD;
@@ -40,16 +39,13 @@ public class DefaultHighlightingTest {
private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java")
.setLines(2)
- .setOriginalLineStartOffsets(new int[] {0, 50})
- .setOriginalLineEndOffsets(new int[] {49, 100})
+ .setOriginalLineStartOffsets(new int[]{0, 50})
+ .setOriginalLineEndOffsets(new int[]{49, 100})
.setLastValidOffset(101)
.build();
private Collection<SyntaxHighlightingRule> highlightingRules;
- @Rule
- public ExpectedException throwable = ExpectedException.none();
-
@Before
public void setUpSampleRules() {
@@ -99,27 +95,24 @@ public class DefaultHighlightingTest {
@Test
public void should_prevent_start_equal_end() {
- throwable.expect(IllegalArgumentException.class);
- throwable
- .expectMessage("Unable to highlight file");
-
- new DefaultHighlighting(Mockito.mock(SensorStorage.class))
+ assertThatThrownBy(() -> new DefaultHighlighting(Mockito.mock(SensorStorage.class))
.onFile(INPUT_FILE)
.highlight(1, 10, 1, 10, KEYWORD)
- .save();
+ .save())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Unable to highlight file");
}
@Test
public void should_prevent_boudaries_overlapping() {
- throwable.expect(IllegalStateException.class);
- throwable
- .expectMessage("Cannot register highlighting rule for characters at Range[from [line=1, lineOffset=8] to [line=1, lineOffset=15]] as it overlaps at least one existing rule");
-
- new DefaultHighlighting(Mockito.mock(SensorStorage.class))
+ assertThatThrownBy(() -> new DefaultHighlighting(Mockito.mock(SensorStorage.class))
.onFile(INPUT_FILE)
.highlight(1, 0, 1, 10, KEYWORD)
.highlight(1, 8, 1, 15, KEYWORD)
- .save();
+ .save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Cannot register highlighting rule for characters at Range[from [line=1, lineOffset=8] to [line=1, lineOffset=15]] " +
+ "as it overlaps at least one existing rule");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/InMemorySensorStorageTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/InMemorySensorStorageTest.java
index 4f17fe410fb..219ec47cdda 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/InMemorySensorStorageTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/InMemorySensorStorageTest.java
@@ -19,18 +19,14 @@
*/
package org.sonar.api.batch.sensor.internal;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.MapEntry.entry;
public class InMemorySensorStorageTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
InMemorySensorStorage underTest = new InMemorySensorStorage();
@Test
@@ -43,17 +39,15 @@ public class InMemorySensorStorageTest {
@Test
public void storeProperty_throws_IAE_if_key_is_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Key of context property must not be null");
-
- underTest.storeProperty(null, "bar");
+ assertThatThrownBy(() -> underTest.storeProperty(null, "bar"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Key of context property must not be null");
}
@Test
public void storeProperty_throws_IAE_if_value_is_null() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Value of context property must not be null");
-
- underTest.storeProperty("foo", null);
+ assertThatThrownBy(() -> underTest.storeProperty("foo", null))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Value of context property must not be null");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
index 7f9aa09466d..73420dfe4b6 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
@@ -24,7 +24,6 @@ import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
@@ -49,6 +48,7 @@ import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.data.MapEntry.entry;
@@ -57,9 +57,6 @@ public class SensorContextTesterTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
private SensorContextTester tester;
private File baseDir;
@@ -261,21 +258,21 @@ public class SensorContextTesterTest {
assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
- exception.expect(IllegalStateException.class);
- tester.newCoverage()
+ assertThatThrownBy(() -> tester.newCoverage()
.onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
- .lineHits(0, 3);
+ .lineHits(0, 3))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
public void testCoverageAtLineOutOfRange() {
assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
- exception.expect(IllegalStateException.class);
- tester.newCoverage()
+ assertThatThrownBy(() -> tester.newCoverage()
.onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
- .lineHits(4, 3);
+ .lineHits(4, 3))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultExternalIssueTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultExternalIssueTest.java
index 9659c371e22..84f57635817 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultExternalIssueTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultExternalIssueTest.java
@@ -23,7 +23,6 @@ import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
@@ -35,6 +34,7 @@ import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -53,8 +53,6 @@ public class DefaultExternalIssueTest {
.setWorkDir(temp.newFolder()));
}
- @Rule
- public ExpectedException exception = ExpectedException.none();
private DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
.initMetadata("Foo\nBar\n")
@@ -127,9 +125,9 @@ public class DefaultExternalIssueTest {
.remediationEffortMinutes(10l)
.severity(Severity.BLOCKER);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Type is mandatory");
- issue.save();
+ assertThatThrownBy(() -> issue.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Type is mandatory");
}
@Test
@@ -144,9 +142,9 @@ public class DefaultExternalIssueTest {
.type(RuleType.BUG)
.severity(Severity.BLOCKER);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("External issues must have a message");
- issue.save();
+ assertThatThrownBy(() -> issue.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("External issues must have a message");
}
@Test
@@ -161,9 +159,9 @@ public class DefaultExternalIssueTest {
.remediationEffortMinutes(10l)
.type(RuleType.BUG);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Severity is mandatory");
- issue.save();
+ assertThatThrownBy(() -> issue.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Severity is mandatory");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java
index 1577d37fd25..1fc3ace49f5 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueLocationTest.java
@@ -20,24 +20,15 @@
package org.sonar.api.batch.sensor.issue.internal;
import org.apache.commons.lang.StringUtils;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-import org.hamcrest.TypeSafeMatcher;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
-import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.rules.ExpectedException.none;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class DefaultIssueLocationTest {
- @Rule
- public ExpectedException thrown = none();
-
private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
.initMetadata("Foo\nBar\n")
.build();
@@ -53,12 +44,12 @@ public class DefaultIssueLocationTest {
@Test
public void not_allowed_to_call_on_twice() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("on() already called");
- new DefaultIssueLocation()
+ assertThatThrownBy(() -> new DefaultIssueLocation()
.on(inputFile)
.on(inputFile)
- .message("Wrong way!");
+ .message("Wrong way!"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("on() already called");
}
@Test
@@ -74,35 +65,19 @@ public class DefaultIssueLocationTest {
@Test
public void prevent_null_character_in_message_text() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Character \\u0000 is not supported in issue message");
-
- new DefaultIssueLocation()
- .message("pipo " + '\u0000' + " bimbo");
+ assertThatThrownBy(() -> new DefaultIssueLocation()
+ .message("pipo " + '\u0000' + " bimbo"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Character \\u0000 is not supported in issue message");
}
@Test
public void prevent_null_character_in_message_text_when_builder_has_been_initialized() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage(customMatcher("Character \\u0000 is not supported in issue message", ", on component: src/Foo.php"));
-
- new DefaultIssueLocation()
+ assertThatThrownBy(() -> new DefaultIssueLocation()
.on(inputFile)
- .message("pipo " + '\u0000' + " bimbo");
+ .message("pipo " + '\u0000' + " bimbo"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageStartingWith("Character \\u0000 is not supported in issue message")
+ .hasMessageEndingWith(", on component: src/Foo.php");
}
-
- private Matcher<String> customMatcher(String startWith, String endWith) {
- return new TypeSafeMatcher<String>() {
- @Override
- public void describeTo(Description description) {
- description.appendText("Invalid message");
- }
-
- @Override
- protected boolean matchesSafely(final String item) {
- return item.startsWith(startWith) && item.endsWith(endWith);
- }
- };
- }
-
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java
index ca39e079dc1..2305aff4589 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/measure/internal/DefaultMeasureTest.java
@@ -23,7 +23,6 @@ import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
@@ -33,10 +32,9 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.measures.CoreMetrics;
-public class DefaultMeasureTest {
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
- @Rule
- public ExpectedException thrown = ExpectedException.none();
+public class DefaultMeasureTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@@ -77,14 +75,14 @@ public class DefaultMeasureTest {
}
@Test
- public void not_allowed_to_call_on_twice() throws IOException {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("on() already called");
- new DefaultMeasure<Integer>()
+ public void not_allowed_to_call_on_twice() {
+ assertThatThrownBy(() -> new DefaultMeasure<Integer>()
.on(new DefaultInputProject(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())))
.on(new TestInputFileBuilder("foo", "src/Foo.php").build())
.withValue(3)
- .save();
+ .save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("on() already called");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/rule/internal/DefaultAdHocRuleTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/rule/internal/DefaultAdHocRuleTest.java
index f3936b03aa7..6df497fe94b 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/rule/internal/DefaultAdHocRuleTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/rule/internal/DefaultAdHocRuleTest.java
@@ -19,25 +19,20 @@
*/
package org.sonar.api.batch.sensor.rule.internal;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.batch.sensor.rule.NewAdHocRule;
-import org.sonar.api.batch.sensor.rule.internal.DefaultAdHocRule;
import org.sonar.api.rules.RuleType;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class DefaultAdHocRuleTest {
- @Rule
- public ExpectedException exception = ExpectedException.none();
-
@Test
public void store() {
SensorStorage storage = mock(SensorStorage.class);
@@ -86,9 +81,9 @@ public class DefaultAdHocRuleTest {
.severity(Severity.BLOCKER)
.type(RuleType.CODE_SMELL);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Engine id is mandatory");
- rule.save();
+ assertThatThrownBy(() -> rule.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Engine id is mandatory");
}
@Test
@@ -102,9 +97,9 @@ public class DefaultAdHocRuleTest {
.severity(Severity.BLOCKER)
.type(RuleType.CODE_SMELL);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Rule id is mandatory");
- rule.save();
+ assertThatThrownBy(() -> rule.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Rule id is mandatory");
}
@Test
@@ -118,9 +113,9 @@ public class DefaultAdHocRuleTest {
.severity(Severity.BLOCKER)
.type(RuleType.CODE_SMELL);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Name is mandatory");
- rule.save();
+ assertThatThrownBy(() -> rule.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Name is mandatory");
}
@@ -134,9 +129,9 @@ public class DefaultAdHocRuleTest {
.description("desc")
.type(RuleType.CODE_SMELL);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Severity is mandatory");
- rule.save();
+ assertThatThrownBy(() -> rule.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Severity is mandatory");
}
@Test
@@ -149,9 +144,9 @@ public class DefaultAdHocRuleTest {
.description("desc")
.severity(Severity.BLOCKER);
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Type is mandatory");
- rule.save();
+ assertThatThrownBy(() -> rule.save())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Type is mandatory");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java
index 6db2e987d54..9ed337dc62a 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java
@@ -22,9 +22,7 @@ package org.sonar.api.batch.sensor.symbol.internal;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
@@ -44,8 +42,6 @@ public class DefaultSymbolTableTest {
private Map<TextRange, Set<TextRange>> referencesPerSymbol;
- @Rule
- public ExpectedException throwable = ExpectedException.none();
@Before
public void setUpSampleSymbols() {
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/AesECBCipherTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/AesECBCipherTest.java
index 28de7d573b0..15839a736d6 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/AesECBCipherTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/AesECBCipherTest.java
@@ -26,18 +26,14 @@ import java.security.Key;
import javax.crypto.BadPaddingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.fail;
public class AesECBCipherTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Test
public void generateRandomSecretKey() {
AesECBCipher cipher = new AesECBCipher(null);
@@ -60,13 +56,12 @@ public class AesECBCipherTest {
@Test
public void encrypt_bad_key() throws Exception {
- thrown.expect(RuntimeException.class);
- thrown.expectMessage("Invalid AES key");
-
URL resource = getClass().getResource("/org/sonar/api/config/internal/AesCipherTest/bad_secret_key.txt");
AesECBCipher cipher = new AesECBCipher(new File(resource.toURI()).getCanonicalPath());
- cipher.encrypt("this is a secret");
+ assertThatThrownBy(() -> cipher.encrypt("this is a secret"))
+ .isInstanceOf(RuntimeException.class)
+ .hasMessageContaining("Invalid AES key");
}
@Test
@@ -147,18 +142,17 @@ public class AesECBCipherTest {
@Test
public void loadSecretKeyFromFile_file_does_not_exist() throws Exception {
- thrown.expect(IllegalStateException.class);
-
AesECBCipher cipher = new AesECBCipher(null);
- cipher.loadSecretFileFromFile("/file/does/not/exist");
+
+ assertThatThrownBy(() -> cipher.loadSecretFileFromFile("/file/does/not/exist"))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
public void loadSecretKeyFromFile_no_property() throws Exception {
- thrown.expect(IllegalStateException.class);
-
AesECBCipher cipher = new AesECBCipher(null);
- cipher.loadSecretFileFromFile(null);
+ assertThatThrownBy(() -> cipher.loadSecretFileFromFile(null))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MapSettingsTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MapSettingsTest.java
index ea029039586..41c28b0529f 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MapSettingsTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MapSettingsTest.java
@@ -31,9 +31,7 @@ import java.util.stream.IntStream;
import org.assertj.core.api.Assertions;
import org.assertj.core.data.Offset;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.sonar.api.Properties;
import org.sonar.api.Property;
@@ -46,11 +44,11 @@ import org.sonar.api.utils.System2;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.ThrowableAssert.ThrowingCallable;
@RunWith(DataProviderRunner.class)
public class MapSettingsTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
private PropertyDefinitions definitions;
@@ -71,9 +69,6 @@ public class MapSettingsTest {
private static class Init {
}
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
@Before
public void init_definitions() {
definitions = new PropertyDefinitions(System2.INSTANCE);
@@ -84,19 +79,16 @@ public class MapSettingsTest {
public void set_throws_NPE_if_key_is_null() {
MapSettings underTest = new MapSettings();
- expectKeyNullNPE();
-
- underTest.set(null, randomAlphanumeric(3));
+ expectKeyNullNPE(() -> underTest.set(null, randomAlphanumeric(3)));
}
@Test
public void set_throws_NPE_if_value_is_null() {
MapSettings underTest = new MapSettings();
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("value can't be null");
-
- underTest.set(randomAlphanumeric(3), null);
+ assertThatThrownBy(() -> underTest.set(randomAlphanumeric(3), null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("value can't be null");
}
@Test
@@ -121,9 +113,7 @@ public class MapSettingsTest {
public void all_setProperty_methods_throws_NPE_if_key_is_null(BiConsumer<Settings, String> setPropertyCaller) {
Settings settings = new MapSettings();
- expectKeyNullNPE();
-
- setPropertyCaller.accept(settings, null);
+ expectKeyNullNPE(() -> setPropertyCaller.accept(settings, null));
}
@Test
@@ -132,14 +122,13 @@ public class MapSettingsTest {
Settings underTest = new MapSettings(new PropertyDefinitions(System2.INSTANCE, singletonList(PropertyDefinition.builder(key).multiValues(true).build())));
- expectKeyNullNPE();
-
- underTest.setProperty(null, new String[] {"1", "2"});
+ expectKeyNullNPE(() -> underTest.setProperty(null, new String[]{"1", "2"}));
}
- private void expectKeyNullNPE() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("key can't be null");
+ private void expectKeyNullNPE(ThrowingCallable shouldRaiseException) {
+ assertThatThrownBy(shouldRaiseException)
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("key can't be null");
}
@Test
@@ -167,7 +156,7 @@ public class MapSettingsTest {
String blankBefore = blank(random);
String blankAfter = blank(random);
- underTest.setProperty(blankBefore + key + blankAfter, new String[] {"1", "2"});
+ underTest.setProperty(blankBefore + key + blankAfter, new String[]{"1", "2"});
assertThat(underTest.hasKey(key)).isTrue();
}
@@ -189,7 +178,7 @@ public class MapSettingsTest {
(settings, key) -> settings.setProperty(key, new Date()),
(settings, key) -> settings.setProperty(key, new Date(), true));
- return callers.stream().map(t -> new Object[] {t}).toArray(Object[][]::new);
+ return callers.stream().map(t -> new Object[]{t}).toArray(Object[][]::new);
}
@Test
@@ -225,11 +214,11 @@ public class MapSettingsTest {
@Test
public void getInt_value_must_be_valid() {
- thrown.expect(NumberFormatException.class);
-
Settings settings = new MapSettings();
settings.setProperty("foo", "not a number");
- settings.getInt("foo");
+
+ assertThatThrownBy(() -> settings.getInt("foo"))
+ .isInstanceOf(NumberFormatException.class);
}
@Test
@@ -311,9 +300,9 @@ public class MapSettingsTest {
Settings settings = new MapSettings();
settings.setProperty("foo", "bar");
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("The property 'foo' is not a float value");
- settings.getFloat("foo");
+ assertThatThrownBy(() -> settings.getFloat("foo"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("The property 'foo' is not a float value");
}
@Test
@@ -321,9 +310,9 @@ public class MapSettingsTest {
Settings settings = new MapSettings();
settings.setProperty("foo", "bar");
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("The property 'foo' is not a double value");
- settings.getDouble("foo");
+ assertThatThrownBy(() -> settings.getDouble("foo"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("The property 'foo' is not a double value");
}
@Test
@@ -344,53 +333,53 @@ public class MapSettingsTest {
public void getStringArray() {
Settings settings = new MapSettings(definitions);
String[] array = settings.getStringArray("array");
- assertThat(array).isEqualTo(new String[] {"one", "two", "three"});
+ assertThat(array).isEqualTo(new String[]{"one", "two", "three"});
}
@Test
public void setStringArray() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("multi_values", new String[] {"A", "B"});
+ settings.setProperty("multi_values", new String[]{"A", "B"});
String[] array = settings.getStringArray("multi_values");
- assertThat(array).isEqualTo(new String[] {"A", "B"});
+ assertThat(array).isEqualTo(new String[]{"A", "B"});
}
@Test
public void setStringArrayTrimValues() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("multi_values", new String[] {" A ", " B "});
+ settings.setProperty("multi_values", new String[]{" A ", " B "});
String[] array = settings.getStringArray("multi_values");
- assertThat(array).isEqualTo(new String[] {"A", "B"});
+ assertThat(array).isEqualTo(new String[]{"A", "B"});
}
@Test
public void setStringArrayEscapeCommas() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("multi_values", new String[] {"A,B", "C,D"});
+ settings.setProperty("multi_values", new String[]{"A,B", "C,D"});
String[] array = settings.getStringArray("multi_values");
- assertThat(array).isEqualTo(new String[] {"A,B", "C,D"});
+ assertThat(array).isEqualTo(new String[]{"A,B", "C,D"});
}
@Test
public void setStringArrayWithEmptyValues() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("multi_values", new String[] {"A,B", "", "C,D"});
+ settings.setProperty("multi_values", new String[]{"A,B", "", "C,D"});
String[] array = settings.getStringArray("multi_values");
- assertThat(array).isEqualTo(new String[] {"A,B", "", "C,D"});
+ assertThat(array).isEqualTo(new String[]{"A,B", "", "C,D"});
}
@Test
public void setStringArrayWithNullValues() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("multi_values", new String[] {"A,B", null, "C,D"});
+ settings.setProperty("multi_values", new String[]{"A,B", null, "C,D"});
String[] array = settings.getStringArray("multi_values");
- assertThat(array).isEqualTo(new String[] {"A,B", "", "C,D"});
+ assertThat(array).isEqualTo(new String[]{"A,B", "", "C,D"});
}
@Test(expected = IllegalStateException.class)
public void shouldFailToSetArrayValueOnSingleValueProperty() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("array", new String[] {"A", "B", "C"});
+ settings.setProperty("array", new String[]{"A", "B", "C"});
}
@Test
@@ -405,7 +394,7 @@ public class MapSettingsTest {
Settings settings = new MapSettings();
settings.setProperty("foo", " one, two, three ");
String[] array = settings.getStringArray("foo");
- assertThat(array).isEqualTo(new String[] {"one", "two", "three"});
+ assertThat(array).isEqualTo(new String[]{"one", "two", "three"});
}
@Test
@@ -413,7 +402,7 @@ public class MapSettingsTest {
Settings settings = new MapSettings();
settings.setProperty("foo", " one, , two");
String[] array = settings.getStringArray("foo");
- assertThat(array).isEqualTo(new String[] {"one", "", "two"});
+ assertThat(array).isEqualTo(new String[]{"one", "", "two"});
}
@Test
@@ -478,34 +467,34 @@ public class MapSettingsTest {
public void getStringLines_single_line() {
Settings settings = new MapSettings();
settings.setProperty("foo", "the line");
- assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"the line"});
+ assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"the line"});
}
@Test
public void getStringLines_linux() {
Settings settings = new MapSettings();
settings.setProperty("foo", "one\ntwo");
- assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+ assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
settings.setProperty("foo", "one\ntwo\n");
- assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+ assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
}
@Test
public void getStringLines_windows() {
Settings settings = new MapSettings();
settings.setProperty("foo", "one\r\ntwo");
- assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+ assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
settings.setProperty("foo", "one\r\ntwo\r\n");
- assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"});
+ assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two"});
}
@Test
public void getStringLines_mix() {
Settings settings = new MapSettings();
settings.setProperty("foo", "one\r\ntwo\nthree");
- assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two", "three"});
+ assertThat(settings.getStringLines("foo")).isEqualTo(new String[]{"one", "two", "three"});
}
@Test
@@ -559,8 +548,8 @@ public class MapSettingsTest {
@Test
public void should_support_deprecated_props_with_multi_values() {
Settings settings = new MapSettings(definitions);
- settings.setProperty("new_multi_values", new String[] {" A ", " B "});
- assertThat(settings.getStringArray("new_multi_values")).isEqualTo(new String[] {"A", "B"});
- assertThat(settings.getStringArray("old_multi_values")).isEqualTo(new String[] {"A", "B"});
+ settings.setProperty("new_multi_values", new String[]{" A ", " B "});
+ assertThat(settings.getStringArray("new_multi_values")).isEqualTo(new String[]{"A", "B"});
+ assertThat(settings.getStringArray("old_multi_values")).isEqualTo(new String[]{"A", "B"});
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MultivaluePropertyTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MultivaluePropertyTest.java
index 9ac4bedc574..d7503e7765c 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MultivaluePropertyTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/config/internal/MultivaluePropertyTest.java
@@ -23,14 +23,13 @@ import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.Random;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static java.util.function.UnaryOperator.identity;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
import static org.sonar.api.config.internal.MultivalueProperty.trimFieldsAndRemoveEmptyFields;
@@ -38,9 +37,6 @@ import static org.sonar.api.config.internal.MultivalueProperty.trimFieldsAndRemo
public class MultivaluePropertyTest {
private static final String[] EMPTY_STRING_ARRAY = {};
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
@UseDataProvider("testParseAsCsv")
public void parseAsCsv_for_coverage(String value, String[] expected) {
@@ -52,10 +48,9 @@ public class MultivaluePropertyTest {
@Test
public void parseAsCsv_fails_with_ISE_if_value_can_not_be_parsed() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Property: 'multi' doesn't contain a valid CSV value: '\"a ,b'");
-
- parseAsCsv("multi", "\"a ,b");
+ assertThatThrownBy(() -> parseAsCsv("multi", "\"a ,b"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Property: 'multi' doesn't contain a valid CSV value: '\"a ,b'");
}
@DataProvider
@@ -81,9 +76,8 @@ public class MultivaluePropertyTest {
@Test
public void trimFieldsAndRemoveEmptyFields_throws_NPE_if_arg_is_null() {
- expectedException.expect(NullPointerException.class);
-
- trimFieldsAndRemoveEmptyFields(null);
+ assertThatThrownBy(() -> trimFieldsAndRemoveEmptyFields(null))
+ .isInstanceOf(NullPointerException.class);
}
@Test
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/AlwaysIncreasingSystem2Test.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/AlwaysIncreasingSystem2Test.java
index 00785791115..a96ff9579c0 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/AlwaysIncreasingSystem2Test.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/AlwaysIncreasingSystem2Test.java
@@ -20,15 +20,12 @@
package org.sonar.api.impl.utils;
import javax.annotation.Nullable;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class AlwaysIncreasingSystem2Test {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void default_constructor_makes_now_start_with_random_number_and_increase_returned_value_by_100_with_each_call() {
@@ -52,10 +49,9 @@ public class AlwaysIncreasingSystem2Test {
@Test
public void constructor_with_initial_value_and_increment_throws_IAE_if_initial_value_is_less_than_0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Initial value must be >= 0");
-
- new AlwaysIncreasingSystem2(-1, 100);
+ assertThatThrownBy(() -> new AlwaysIncreasingSystem2(-1, 100))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Initial value must be >= 0");
}
@Test
@@ -67,34 +63,30 @@ public class AlwaysIncreasingSystem2Test {
@Test
public void constructor_with_initial_value_and_increment_throws_IAE_if_increment_is_0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("increment must be > 0");
-
- new AlwaysIncreasingSystem2(10, 0);
+ assertThatThrownBy(() -> new AlwaysIncreasingSystem2(10, 0))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("increment must be > 0");
}
@Test
public void constructor_with_initial_value_and_increment_throws_IAE_if_increment_is_less_than_0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("increment must be > 0");
-
- new AlwaysIncreasingSystem2(10, -66);
+ assertThatThrownBy(() -> new AlwaysIncreasingSystem2(10, -66))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("increment must be > 0");
}
@Test
public void constructor_with_increment_throws_IAE_if_increment_is_0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("increment must be > 0");
-
- new AlwaysIncreasingSystem2(0);
+ assertThatThrownBy(() -> new AlwaysIncreasingSystem2(0))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("increment must be > 0");
}
@Test
public void constructor_with_increment_throws_IAE_if_increment_is_less_than_0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("increment must be > 0");
-
- new AlwaysIncreasingSystem2(-20);
+ assertThatThrownBy(() -> new AlwaysIncreasingSystem2(-20))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("increment must be > 0");
}
private void verifyValuesReturnedByNow(AlwaysIncreasingSystem2 underTest, @Nullable Long initialValue, int increment) {
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/DefaultTempFolderTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/DefaultTempFolderTest.java
index 3e5d1d6e24b..f1e4d188b9b 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/DefaultTempFolderTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/DefaultTempFolderTest.java
@@ -21,26 +21,33 @@ package org.sonar.api.impl.utils;
import java.io.File;
import org.apache.commons.io.FileUtils;
+import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class DefaultTempFolderTest {
@Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public LogTester logTester = new LogTester();
+ private static StringBuilder tooLong = new StringBuilder("tooooolong");
+
+ @BeforeClass
+ public static void setUp() {
+ for (int i = 0; i < 50; i++) {
+ tooLong.append("tooooolong");
+ }
+ }
+
@Test
public void createTempFolderAndFile() throws Exception {
File rootTempFolder = temp.newFolder();
@@ -70,30 +77,20 @@ public class DefaultTempFolderTest {
public void newDir_throws_ISE_if_name_is_not_valid() throws Exception {
File rootTempFolder = temp.newFolder();
DefaultTempFolder underTest = new DefaultTempFolder(rootTempFolder);
- String tooLong = "tooooolong";
- for (int i = 0; i < 50; i++) {
- tooLong += "tooooolong";
- }
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Failed to create temp directory");
- underTest.newDir(tooLong);
+ assertThatThrownBy(() -> underTest.newDir(tooLong.toString()))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Failed to create temp directory");
}
@Test
public void newFile_throws_ISE_if_name_is_not_valid() throws Exception {
File rootTempFolder = temp.newFolder();
DefaultTempFolder underTest = new DefaultTempFolder(rootTempFolder);
- String tooLong = "tooooolong";
- for (int i = 0; i < 50; i++) {
- tooLong += "tooooolong";
- }
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Failed to create temp file");
- underTest.newFile(tooLong, ".txt");
+ assertThatThrownBy(() -> underTest.newFile(tooLong.toString(), ".txt"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Failed to create temp file");
}
@Test
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java
index e209baea5d4..d37eea223c1 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java
@@ -21,20 +21,16 @@ package org.sonar.api.impl.utils;
import java.util.TimeZone;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class TestSystem2Test {
private TestSystem2 underTest;
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Before
public void setup() {
underTest = new TestSystem2();
@@ -64,39 +60,35 @@ public class TestSystem2Test {
public void throw_ISE_if_now_equal_zero() {
underTest.setNow(0);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Method setNow() was not called by test");
-
- underTest.now();
+ assertThatThrownBy(() -> underTest.now())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Method setNow() was not called by test");
}
@Test
public void throw_ISE_if_now_lesser_than_zero() {
underTest.setNow(-1);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Method setNow() was not called by test");
-
- underTest.now();
+ assertThatThrownBy(() -> underTest.now())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Method setNow() was not called by test");
}
@Test
public void throw_ISE_if_now_equal_zero_and_try_to_tick() {
underTest.setNow(0);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Method setNow() was not called by test");
-
- underTest.tick();
+ assertThatThrownBy(() -> underTest.tick())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Method setNow() was not called by test");
}
@Test
public void throw_ISE_if_now_lesser_than_zero_and_try_to_tick() {
underTest.setNow(-1);
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Method setNow() was not called by test");
-
- underTest.tick();
+ assertThatThrownBy(() -> underTest.tick())
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Method setNow() was not called by test");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java
index 8e2490acb2d..c72ca3873a9 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java
@@ -21,14 +21,13 @@ package org.sonar.api.internal;
import java.io.File;
import java.net.MalformedURLException;
-import org.sonar.api.SonarEdition;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
+import org.sonar.api.SonarEdition;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.Version;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -36,9 +35,6 @@ import static org.mockito.Mockito.when;
public class MetadataLoaderTest {
private System2 system = mock(System2.class);
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void load_version_from_file_in_classpath() {
Version version = MetadataLoader.loadVersion(System2.INSTANCE);
@@ -61,19 +57,18 @@ public class MetadataLoaderTest {
@Test
public void throw_ISE_if_edition_is_invalid() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Invalid edition found in '/sonar-edition.txt': 'TRASH'");
-
- MetadataLoader.parseEdition("trash");
+ assertThatThrownBy(() -> MetadataLoader.parseEdition("trash"))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Invalid edition found in '/sonar-edition.txt': 'TRASH'");
}
@Test
public void throw_ISE_if_fail_to_load_version() throws Exception {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can not load /sonar-api-version.txt from classpath");
-
when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL());
- MetadataLoader.loadVersion(system);
+
+ assertThatThrownBy(() -> MetadataLoader.loadVersion(system))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Can not load /sonar-api-version.txt from classpath");
}
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/SonarRuntimeImplTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/SonarRuntimeImplTest.java
index f927e84028e..01226a2e478 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/SonarRuntimeImplTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/SonarRuntimeImplTest.java
@@ -19,11 +19,9 @@
*/
package org.sonar.api.internal;
-import org.sonar.api.SonarEdition;
import org.assertj.core.api.Assertions;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
+import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
@@ -35,8 +33,6 @@ public class SonarRuntimeImplTest {
private static final Version A_VERSION = Version.parse("6.0");
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Test
public void sonarQube_environment() {