diff options
author | Klaudio Sinani <klaudio.sinani@sonarsource.com> | 2021-11-17 22:54:06 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-11-19 20:03:27 +0000 |
commit | a3d88ea27c35921647d7602755828ca73e15e865 (patch) | |
tree | 5626c38afab1ea00ab9897da431476c17b478bbe /server/sonar-db-dao | |
parent | 92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff) | |
download | sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip |
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'server/sonar-db-dao')
68 files changed, 421 insertions, 681 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java index 8a1d5373da4..373b9439baa 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java @@ -23,7 +23,6 @@ import org.junit.Test; import org.sonar.core.platform.ComponentContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class DaoModuleTest { @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoUtilsTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoUtilsTest.java index 5ac9660c492..2ae748dc784 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoUtilsTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoUtilsTest.java @@ -38,6 +38,4 @@ public class DaoUtilsTest { assertThat(buildLikeValue("like-\\_%/-value", AFTER)).isEqualTo(escapedValue + wildcard); assertThat(buildLikeValue("like-\\_%/-value", BEFORE_AND_AFTER)).isEqualTo(wildcard + escapedValue + wildcard); } - - } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/IsAliveMapperTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/IsAliveMapperTest.java index 3879edaafa0..84085a5ed1a 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/IsAliveMapperTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/IsAliveMapperTest.java @@ -26,8 +26,6 @@ import org.junit.Test; import org.sonar.api.utils.System2; import static org.assertj.core.api.Assertions.assertThat; - - public class IsAliveMapperTest { @Rule diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/PaginationTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/PaginationTest.java index 00e39446bed..aaf90cbc676 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/PaginationTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/PaginationTest.java @@ -20,17 +20,12 @@ package org.sonar.db; import java.util.Random; -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.sonar.db.Pagination.forPage; - - public class PaginationTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void all_is_page_1_with_MAX_INTEGER_page_size() { @@ -47,38 +42,34 @@ public class PaginationTest { @Test public void forPage_fails_with_IAE_if_page_is_0() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("page index must be >= 1"); - - forPage(0); + assertThatThrownBy(() -> forPage(0)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("page index must be >= 1"); } @Test public void forPage_fails_with_IAE_if_page_is_less_than_0() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("page index must be >= 1"); - - forPage(-Math.abs(new Random().nextInt()) - 1); + assertThatThrownBy(() -> forPage(-Math.abs(new Random().nextInt()) - 1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("page index must be >= 1"); } @Test public void andSize_fails_with_IAE_if_size_is_0() { Pagination.Builder builder = forPage(1); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("page size must be >= 1"); - - builder.andSize(0); + assertThatThrownBy(() -> builder.andSize(0)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("page size must be >= 1"); } @Test public void andSize_fails_with_IAE_if_size_is_less_than_0() { Pagination.Builder builder = forPage(1); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("page size must be >= 1"); - - builder.andSize(-Math.abs(new Random().nextInt()) - 1); + assertThatThrownBy(() -> builder.andSize(-Math.abs(new Random().nextInt()) - 1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("page size must be >= 1"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java index e1ce9095f12..3a111a8e2b1 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/ProjectAlmSettingDaoWithPersisterTest.java @@ -36,9 +36,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto; import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto; diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java index 1e25770c33f..1e27e37d998 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDtoTest.java @@ -23,21 +23,16 @@ 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 org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @RunWith(DataProviderRunner.class) public class CeActivityDtoTest { private static final String STR_40_CHARS = "0123456789012345678901234567890123456789"; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private CeActivityDto underTest = new CeActivityDto(); @Test @@ -82,10 +77,9 @@ public class CeActivityDtoTest { public void setComponentUuid_throws_IAE_if_value_is_41_chars() { String str_41_chars = STR_40_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value is too long for column CE_ACTIVITY.COMPONENT_UUID: " + str_41_chars); - - underTest.setComponentUuid(str_41_chars); + assertThatThrownBy(() -> underTest.setComponentUuid(str_41_chars)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Value is too long for column CE_ACTIVITY.COMPONENT_UUID: " + str_41_chars); } @Test @@ -100,10 +94,9 @@ public class CeActivityDtoTest { public void setMainComponentUuid_throws_IAE_if_value_is_41_chars() { String str_41_chars = STR_40_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value is too long for column CE_ACTIVITY.MAIN_COMPONENT_UUID: " + str_41_chars); - - underTest.setMainComponentUuid(str_41_chars); + assertThatThrownBy(() -> underTest.setMainComponentUuid(str_41_chars)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Value is too long for column CE_ACTIVITY.MAIN_COMPONENT_UUID: " + str_41_chars); } @Test @@ -137,9 +130,8 @@ public class CeActivityDtoTest { underTest.setWarningCount(0); underTest.setWarningCount(1 + new Random().nextInt(10)); - expectedException.expect(IllegalArgumentException.class); - - underTest.setWarningCount(-1 - new Random().nextInt(10)); + assertThatThrownBy(() -> underTest.setWarningCount(-1 - new Random().nextInt(10))) + .isInstanceOf(IllegalArgumentException.class); } @DataProvider diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeQueueDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeQueueDtoTest.java index 678660e30a1..66ffd7de5b9 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeQueueDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeQueueDtoTest.java @@ -19,19 +19,15 @@ */ package org.sonar.db.ce; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class CeQueueDtoTest { private static final String STR_15_CHARS = "012345678901234"; private static final String STR_40_CHARS = "0123456789012345678901234567890123456789"; private static final String STR_255_CHARS = STR_40_CHARS + STR_40_CHARS + STR_40_CHARS + STR_40_CHARS + STR_40_CHARS + STR_40_CHARS + STR_15_CHARS; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private CeQueueDto underTest = new CeQueueDto(); @Test @@ -46,10 +42,9 @@ public class CeQueueDtoTest { public void setComponentUuid_throws_IAE_if_value_is_41_chars() { String str_41_chars = STR_40_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value is too long for column CE_QUEUE.COMPONENT_UUID: " + str_41_chars); - - underTest.setComponentUuid(str_41_chars); + assertThatThrownBy(() -> underTest.setComponentUuid(str_41_chars)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Value is too long for column CE_QUEUE.COMPONENT_UUID: " + str_41_chars); } @Test @@ -64,17 +59,15 @@ public class CeQueueDtoTest { public void setMainComponentUuid_throws_IAE_if_value_is_41_chars() { String str_41_chars = STR_40_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value is too long for column CE_QUEUE.MAIN_COMPONENT_UUID: " + str_41_chars); - - underTest.setMainComponentUuid(str_41_chars); + assertThatThrownBy(() -> underTest.setMainComponentUuid(str_41_chars)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Value is too long for column CE_QUEUE.MAIN_COMPONENT_UUID: " + str_41_chars); } @Test public void setTaskType_throws_NPE_if_argument_is_null() { - expectedException.expect(NullPointerException.class); - - underTest.setTaskType(null); + assertThatThrownBy(() -> underTest.setTaskType(null)) + .isInstanceOf(NullPointerException.class); } @Test @@ -88,10 +81,9 @@ public class CeQueueDtoTest { public void setTaskType_throws_IAE_if_value_is_41_chars() { String str_16_chars = STR_15_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value of task type is too long: " + str_16_chars); - - underTest.setTaskType(str_16_chars); + assertThatThrownBy(() -> underTest.setTaskType(str_16_chars)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Value of task type is too long: " + str_16_chars); } @Test @@ -106,9 +98,8 @@ public class CeQueueDtoTest { public void setSubmitterLogin_throws_IAE_if_value_is_41_chars() { String str_256_chars = STR_255_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value of submitter uuid is too long: " + str_256_chars); - - underTest.setSubmitterUuid(str_256_chars); + assertThatThrownBy(() -> underTest.setSubmitterUuid(str_256_chars)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Value of submitter uuid is too long: " + str_256_chars); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeScannerContextDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeScannerContextDaoTest.java index 7358d5751f3..c30fc96f0bd 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeScannerContextDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeScannerContextDaoTest.java @@ -22,7 +22,6 @@ package org.sonar.db.ce; import com.google.common.collect.ImmutableSet; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.core.util.CloseableIterator; import org.sonar.db.DbSession; @@ -31,6 +30,7 @@ import org.sonar.db.DbTester; import static java.lang.System.lineSeparator; import static java.util.Collections.singleton; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; public class CeScannerContextDaoTest { @@ -40,8 +40,6 @@ public class CeScannerContextDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private System2 system = mock(System2.class); private DbSession dbSession = dbTester.getSession(); @@ -65,10 +63,9 @@ public class CeScannerContextDaoTest { @Test public void insert_fails_with_IAE_if_data_is_empty() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Scanner context can not be empty"); - - underTest.insert(dbSession, SOME_UUID, CloseableIterator.emptyCloseableIterator()); + assertThatThrownBy(() -> underTest.insert(dbSession, SOME_UUID, CloseableIterator.emptyCloseableIterator())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Scanner context can not be empty"); } @Test @@ -76,10 +73,9 @@ public class CeScannerContextDaoTest { CloseableIterator<String> iterator = scannerContextInputStreamOf("aa"); iterator.next(); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Scanner context can not be empty"); - - underTest.insert(dbSession, SOME_UUID, iterator); + assertThatThrownBy(() -> underTest.insert(dbSession, SOME_UUID, iterator)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Scanner context can not be empty"); } @Test @@ -89,10 +85,9 @@ public class CeScannerContextDaoTest { assertThat(dbTester.countRowsOfTable(dbSession, TABLE_NAME)).isEqualTo(1); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Fail to insert scanner context for task " + SOME_UUID); - - underTest.insert(dbSession, SOME_UUID, scannerContextInputStreamOf("blo")); + assertThatThrownBy(() -> underTest.insert(dbSession, SOME_UUID, scannerContextInputStreamOf("blo"))) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Fail to insert scanner context for task " + SOME_UUID); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDaoTest.java index a1b146f662a..7f24139d9d8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDaoTest.java @@ -22,7 +22,6 @@ package org.sonar.db.ce; import com.google.common.collect.ImmutableSet; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; @@ -35,8 +34,6 @@ import static org.assertj.core.api.Assertions.tuple; public class CeTaskCharacteristicDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private CeTaskCharacteristicDao underTest = new CeTaskCharacteristicDao(); @@ -54,8 +51,6 @@ public class CeTaskCharacteristicDaoTest { tuple("task2", "uuid2", "key2", "value2")); assertThat(underTest.selectByTaskUuids(dbTester.getSession(), singletonList("unknown"))).isEmpty(); } - - @Test public void deleteByTaskUuids() { insert("key1", "value1", "uuid1", "task1"); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDtoTest.java index 7190b7838be..9f4ab2a3da9 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskCharacteristicDtoTest.java @@ -19,10 +19,10 @@ */ package org.sonar.db.ce; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + public class CeTaskCharacteristicDtoTest { @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskInputDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskInputDaoTest.java index df597c593c3..6242cd1bf92 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskInputDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskInputDaoTest.java @@ -24,12 +24,12 @@ import java.util.Optional; import org.apache.commons.io.IOUtils; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import static java.util.Collections.singleton; 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.when; @@ -42,10 +42,6 @@ public class CeTaskInputDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private System2 system = mock(System2.class); private CeTaskInputDao underTest = new CeTaskInputDao(system); @@ -67,8 +63,8 @@ public class CeTaskInputDaoTest { @Test public void fail_to_insert_invalid_row() { - expectedException.expectMessage("Fail to insert data of CE task null"); - underTest.insert(dbTester.getSession(), null, IOUtils.toInputStream(SOME_DATA)); + assertThatThrownBy(() -> underTest.insert(dbTester.getSession(), null, IOUtils.toInputStream(SOME_DATA))) + .hasMessage("Fail to insert data of CE task null"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDaoTest.java index 40e9de11bb6..2b325d49b12 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDaoTest.java @@ -24,7 +24,6 @@ import java.util.Optional; import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -37,8 +36,6 @@ import static org.assertj.core.api.Assertions.assertThat; public class CeTaskMessageDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final CeTaskMessageDao underTest = new CeTaskMessageDao(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDtoTest.java index 75b1228c86b..07053cba552 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeTaskMessageDtoTest.java @@ -20,33 +20,28 @@ package org.sonar.db.ce; import java.util.Random; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class CeTaskMessageDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private CeTaskMessageDto underTest = new CeTaskMessageDto(); @Test public void setMessage_fails_with_IAE_if_argument_is_null() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("message can't be null nor empty"); - - underTest.setMessage(null); + assertThatThrownBy(() -> underTest.setMessage(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("message can't be null nor empty"); } @Test public void setMessage_fails_with_IAE_if_argument_is_empty() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("message can't be null nor empty"); - - underTest.setMessage(""); + assertThatThrownBy(() -> underTest.setMessage("")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("message can't be null nor empty"); } @Test @@ -61,9 +56,9 @@ public class CeTaskMessageDtoTest { public void setMessage_fails_with_IAE_if_argument_has_size_bigger_then_4000() { int size = 4000 + 1 + new Random().nextInt(100); String str = repeat("a", size); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("message is too long: " + size); - underTest.setMessage(str); + assertThatThrownBy(() -> underTest.setMessage(str)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("message is too long: " + size); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/LogsIteratorInputStreamTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/LogsIteratorInputStreamTest.java index e97de96bf9e..85e7a8f1e5f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/LogsIteratorInputStreamTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/LogsIteratorInputStreamTest.java @@ -22,17 +22,14 @@ package org.sonar.db.ce; import java.io.IOException; import java.util.Arrays; import org.apache.commons.io.IOUtils; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.core.util.CloseableIterator; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class LogsIteratorInputStreamTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void read_from_ClosableIterator_with_several_lines() throws IOException { @@ -57,10 +54,9 @@ public class LogsIteratorInputStreamTest { @Test public void constructor_throws_IAE_when_ClosableIterator_is_empty() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("LogsIterator can't be empty or already read"); - - create(); + assertThatThrownBy(LogsIteratorInputStreamTest::create) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("LogsIterator can't be empty or already read"); } @Test @@ -70,10 +66,9 @@ public class LogsIteratorInputStreamTest { // read iterator to the end iterator.next(); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("LogsIterator can't be empty or already read"); - - new LogsIteratorInputStream(iterator, UTF_8); + assertThatThrownBy(() -> new LogsIteratorInputStream(iterator, UTF_8)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("LogsIterator can't be empty or already read"); } private static LogsIteratorInputStream create(String... lines) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/UpdateIfTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/UpdateIfTest.java index 9370364fc16..29d794367d7 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/UpdateIfTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/UpdateIfTest.java @@ -24,20 +24,15 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.Random; import org.apache.commons.lang.RandomStringUtils; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @RunWith(DataProviderRunner.class) public class UpdateIfTest { private static final String STR_40_CHARS = "0123456789012345678901234567890123456789"; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void newProperties_constructor_accepts_null_workerUuid() { UpdateIf.NewProperties newProperties = new UpdateIf.NewProperties(CeQueueDto.Status.PENDING, null, 123, 456); @@ -47,20 +42,18 @@ public class UpdateIfTest { @Test public void newProperties_constructor_fails_with_NPE_if_status_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("status can't be null"); - - new UpdateIf.NewProperties(null, "foo", 123, 456); + assertThatThrownBy(() -> new UpdateIf.NewProperties(null, "foo", 123, 456)) + .isInstanceOf(NullPointerException.class) + .hasMessage("status can't be null"); } @Test public void newProperties_constructor_fails_with_IAE_if_workerUuid_is_41_or_more() { String workerUuid = RandomStringUtils.randomAlphanumeric(41 + new Random().nextInt(5)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("worker uuid is too long: " + workerUuid); - - new UpdateIf.NewProperties(CeQueueDto.Status.PENDING, workerUuid, 123, 456); + assertThatThrownBy(() -> new UpdateIf.NewProperties(CeQueueDto.Status.PENDING, workerUuid, 123, 456)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("worker uuid is too long: " + workerUuid); } @Test @@ -83,9 +76,8 @@ public class UpdateIfTest { public void newProperties_constructor_IAE_if_workerUuid_is_41_chars() { String str_41_chars = STR_40_CHARS + "a"; - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("worker uuid is too long: " + str_41_chars); - - new UpdateIf.NewProperties(CeQueueDto.Status.PENDING, str_41_chars, 123, 345); + assertThatThrownBy(() -> new UpdateIf.NewProperties(CeQueueDto.Status.PENDING, str_41_chars, 123, 345)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("worker uuid is too long: " + str_41_chars); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java index 090ecdddb84..f4278817184 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java @@ -25,7 +25,6 @@ import java.util.Random; import java.util.Set; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.impl.utils.TestSystem2; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; @@ -34,6 +33,7 @@ import org.sonar.db.project.ProjectDto; 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.AssertionsForClassTypes.tuple; public class AnalysisPropertiesDaoTest { @@ -41,8 +41,6 @@ public class AnalysisPropertiesDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final System2 system2 = new TestSystem2().setNow(NOW); private final DbSession dbSession = dbTester.getSession(); @@ -56,10 +54,9 @@ public class AnalysisPropertiesDaoTest { .setKey(randomAlphanumeric(10)) .setValue(randomAlphanumeric(10)); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("uuid cannot be null"); - - underTest.insert(dbSession, analysisPropertyDto); + assertThatThrownBy(() -> underTest.insert(dbSession, analysisPropertyDto)) + .isInstanceOf(NullPointerException.class) + .hasMessage("uuid cannot be null"); } @Test @@ -69,10 +66,9 @@ public class AnalysisPropertiesDaoTest { .setUuid(randomAlphanumeric(10)) .setValue(randomAlphanumeric(10)); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("key cannot be null"); - - underTest.insert(dbSession, analysisPropertyDto); + assertThatThrownBy(() -> underTest.insert(dbSession, analysisPropertyDto)) + .isInstanceOf(NullPointerException.class) + .hasMessage("key cannot be null"); } @Test @@ -82,10 +78,9 @@ public class AnalysisPropertiesDaoTest { .setKey(randomAlphanumeric(10)) .setValue(randomAlphanumeric(10)); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("analysis uuid cannot be null"); - - underTest.insert(dbSession, analysisPropertyDto); + assertThatThrownBy(() -> underTest.insert(dbSession, analysisPropertyDto)) + .isInstanceOf(NullPointerException.class) + .hasMessage("analysis uuid cannot be null"); } @Test @@ -95,10 +90,9 @@ public class AnalysisPropertiesDaoTest { .setUuid(randomAlphanumeric(10)) .setKey(randomAlphanumeric(10)); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("value cannot be null"); - - underTest.insert(dbSession, analysisPropertyDto); + assertThatThrownBy(() -> underTest.insert(dbSession, analysisPropertyDto)) + .isInstanceOf(NullPointerException.class) + .hasMessage("value cannot be null"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertyDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertyDtoTest.java index 8c114996e23..aaa88f39dc5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertyDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertyDtoTest.java @@ -19,17 +19,14 @@ */ package org.sonar.db.component; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class AnalysisPropertyDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private AnalysisPropertyDto underTest; @@ -37,40 +34,36 @@ public class AnalysisPropertyDtoTest { public void null_key_should_throw_NPE() { underTest = new AnalysisPropertyDto(); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("key cannot be null"); - - underTest.setKey(null); + assertThatThrownBy(() -> underTest.setKey(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("key cannot be null"); } @Test public void null_value_should_throw_NPE() { underTest = new AnalysisPropertyDto(); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("value cannot be null"); - - underTest.setValue(null); + assertThatThrownBy(() -> underTest.setValue(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("value cannot be null"); } @Test public void null_uuid_should_throw_NPE() { underTest = new AnalysisPropertyDto(); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("uuid cannot be null"); - - underTest.setUuid(null); + assertThatThrownBy(() -> underTest.setUuid(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("uuid cannot be null"); } @Test public void null_analysis_uuid_should_throw_NPE() { underTest = new AnalysisPropertyDto(); - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("analysisUuid cannot be null"); - - underTest.setAnalysisUuid(null); + assertThatThrownBy(() -> underTest.setAnalysisUuid(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("analysisUuid cannot be null"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java index 9ad34ab0e70..cf859283393 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java @@ -26,7 +26,6 @@ import java.util.function.Predicate; import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; @@ -37,6 +36,7 @@ import org.sonar.db.audit.model.ComponentKeyNewValue; import org.sonar.db.component.ComponentKeyUpdaterDao.RekeyedResource; 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.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -53,8 +53,6 @@ import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; public class ComponentKeyUpdaterDaoTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); @Rule public DbTester db = DbTester.create(System2.INSTANCE); @@ -142,10 +140,9 @@ public class ComponentKeyUpdaterDaoTest { ComponentDto appBranch = db.components().insertProjectBranch(app); db.components().insertProjectBranch(app, b -> b.setKey("newName")); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage(String.format("Impossible to update key: a component with key \"%s\" already exists.", generateBranchKey(app.getDbKey(), "newName"))); - - underTest.updateApplicationBranchKey(dbSession, appBranch.uuid(), app.getDbKey(), "newName"); + assertThatThrownBy(() -> underTest.updateApplicationBranchKey(dbSession, appBranch.uuid(), app.getDbKey(), "newName")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(String.format("Impossible to update key: a component with key \"%s\" already exists.", generateBranchKey(app.getDbKey(), "newName"))); } @Test @@ -222,10 +219,9 @@ public class ComponentKeyUpdaterDaoTest { public void updateKey_throws_IAE_if_component_with_specified_key_does_not_exist() { populateSomeData(); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Impossible to update key: a component with key \"org.struts:struts-ui\" already exists."); - - underTest.updateKey(dbSession, "B", "org.struts:struts-ui"); + assertThatThrownBy(() -> underTest.updateKey(dbSession, "B", "org.struts:struts-ui")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Impossible to update key: a component with key \"org.struts:struts-ui\" already exists."); } @Test @@ -234,10 +230,10 @@ public class ComponentKeyUpdaterDaoTest { db.components().insertComponent(project); db.components().insertComponent(newFileDto(project, null).setDbKey("old-project-key:file")); String newLongProjectKey = Strings.repeat("a", 400); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Component key length (405) is longer than the maximum authorized (400). '" + newLongProjectKey + ":file' was provided."); - underTest.updateKey(dbSession, project.uuid(), newLongProjectKey); + assertThatThrownBy(() -> underTest.updateKey(dbSession, project.uuid(), newLongProjectKey)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Component key length (405) is longer than the maximum authorized (400). '" + newLongProjectKey + ":file' was provided."); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentQueryTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentQueryTest.java index 7645fae9aeb..3f2e3751a2f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentQueryTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentQueryTest.java @@ -21,19 +21,16 @@ package org.sonar.db.component; import java.util.Date; import java.util.function.Supplier; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static java.util.Collections.emptySet; import static java.util.Collections.singleton; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.api.resources.Qualifiers.PROJECT; public class ComponentQueryTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void build_query() { @@ -87,17 +84,15 @@ public class ComponentQueryTest { @Test public void fail_if_no_qualifier_provided() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("At least one qualifier must be provided"); - - ComponentQuery.builder().build(); + assertThatThrownBy(() -> ComponentQuery.builder().build()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("At least one qualifier must be provided"); } @Test public void fail_if_partial_match_on_key_without_a_query() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("A query must be provided if a partial match on key is specified."); - - ComponentQuery.builder().setQualifiers(PROJECT).setPartialMatchOnKey(false).build(); + assertThatThrownBy(() -> ComponentQuery.builder().setQualifiers(PROJECT).setPartialMatchOnKey(false).build()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("A query must be provided if a partial match on key is specified."); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTreeQueryTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTreeQueryTest.java index cf77524d231..0ce0cfe619e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTreeQueryTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTreeQueryTest.java @@ -19,12 +19,11 @@ */ package org.sonar.db.component; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; import static org.sonar.db.component.ComponentTreeQuery.Strategy.CHILDREN; import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES; @@ -33,8 +32,6 @@ public class ComponentTreeQueryTest { private static final String BASE_UUID = "ABCD"; - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void create_query() { @@ -77,17 +74,21 @@ public class ComponentTreeQueryTest { @Test public void fail_when_no_base_uuid() { - expectedException.expect(NullPointerException.class); - ComponentTreeQuery.builder() - .setStrategy(CHILDREN) - .build(); + assertThatThrownBy(() -> { + ComponentTreeQuery.builder() + .setStrategy(CHILDREN) + .build(); + }) + .isInstanceOf(NullPointerException.class); } @Test public void fail_when_no_strategy() { - expectedException.expect(NullPointerException.class); - ComponentTreeQuery.builder() - .setBaseUuid(BASE_UUID) - .build(); + assertThatThrownBy(() -> { + ComponentTreeQuery.builder() + .setBaseUuid(BASE_UUID) + .build(); + }) + .isInstanceOf(NullPointerException.class); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java index e5a623edea5..d76fa0e7bf3 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java @@ -19,17 +19,14 @@ */ package org.sonar.db.component; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static com.google.common.base.Strings.repeat; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.test.TestUtils.hasOnlyPrivateConstructors; public class ComponentValidatorTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void check_name() { @@ -40,10 +37,9 @@ public class ComponentValidatorTest { @Test public void fail_when_name_longer_than_500_characters() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Component name length"); - - ComponentValidator.checkComponentName(repeat("a", 500 + 1)); + assertThatThrownBy(() -> ComponentValidator.checkComponentName(repeat("a", 500 + 1))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Component name length"); } @Test @@ -55,11 +51,12 @@ public class ComponentValidatorTest { @Test public void fail_when_key_longer_than_400_characters() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Component key length"); - String key = repeat("a", 400 + 1); - - ComponentValidator.checkComponentKey(key); + assertThatThrownBy(() -> { + String key = repeat("a", 400 + 1); + ComponentValidator.checkComponentKey(key); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Component key length"); } @Test @@ -71,10 +68,9 @@ public class ComponentValidatorTest { @Test public void fail_when_qualifier_is_longer_than_10_characters() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Component qualifier length"); - - ComponentValidator.checkComponentQualifier(repeat("a", 10 + 1)); + assertThatThrownBy(() -> ComponentValidator.checkComponentQualifier(repeat("a", 10 + 1))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Component qualifier length"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ProjectLinkDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ProjectLinkDaoTest.java index c12c927edd4..4a7c96e0c25 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ProjectLinkDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ProjectLinkDaoTest.java @@ -22,8 +22,8 @@ package org.sonar.db.component; import java.util.Collections; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.utils.System2; import org.sonar.api.impl.utils.TestSystem2; +import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import static java.util.Arrays.asList; diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java index ed402198582..7164082cff9 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java @@ -33,7 +33,6 @@ import java.util.stream.IntStream; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; @@ -66,8 +65,6 @@ import static org.sonar.db.component.SnapshotTesting.newAnalysis; public class SnapshotDaoTest { @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(System2.INSTANCE); private final DbClient dbClient = db.getDbClient(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java index 931f10b88c3..b43f88f1bfa 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java @@ -19,18 +19,15 @@ */ package org.sonar.db.component; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.api.utils.DateUtils.parseDate; public class SnapshotDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void test_getter_and_setter() { @@ -61,12 +58,11 @@ public class SnapshotDtoTest { snapshotDto.setProjectVersion("1.0"); snapshotDto.setProjectVersion(repeat("a", 100)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("projectVersion" + - " length (101) is longer than the maximum authorized (100). " + - "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); - - snapshotDto.setProjectVersion(repeat("a", 101)); + assertThatThrownBy(() -> snapshotDto.setProjectVersion(repeat("a", 101))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("projectVersion" + + " length (101) is longer than the maximum authorized (100). " + + "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); } @Test @@ -76,11 +72,10 @@ public class SnapshotDtoTest { snapshotDto.setBuildString("1.0"); snapshotDto.setBuildString(repeat("a", 100)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("buildString" + - " length (101) is longer than the maximum authorized (100). " + - "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); - - snapshotDto.setBuildString(repeat("a", 101)); + assertThatThrownBy(() -> snapshotDto.setBuildString(repeat("a", 101))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("buildString" + + " length (101) is longer than the maximum authorized (100). " + + "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java index 546c3661b2b..4f01e75a540 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/CreateDb.java @@ -24,8 +24,8 @@ import com.sonar.orchestrator.db.DatabaseClient; import com.sonar.orchestrator.db.DatabaseFactory; import com.sonar.orchestrator.db.DefaultDatabase; import java.util.function.Consumer; -import org.sonar.api.config.internal.Settings; import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.config.internal.Settings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.db.SQDatabase; diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventComponentChangeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventComponentChangeDaoTest.java index 2e6416aaee5..7392677ece5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventComponentChangeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventComponentChangeDaoTest.java @@ -25,7 +25,6 @@ import java.util.Random; import java.util.stream.IntStream; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -42,8 +41,6 @@ public class EventComponentChangeDaoTest { private static Random random = new Random(); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); private DbSession dbSession = dbTester.getSession(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventDaoTest.java index 309f44b9355..d31c7998f96 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventDaoTest.java @@ -25,7 +25,6 @@ import java.util.Random; import java.util.stream.IntStream; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -43,8 +42,6 @@ import static org.sonar.db.event.EventTesting.newEvent; public class EventDaoTest { @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); private final DbClient dbClient = dbTester.getDbClient(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventValidatorTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventValidatorTest.java index f626bfbdead..9b76be9becf 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventValidatorTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/event/EventValidatorTest.java @@ -19,15 +19,12 @@ */ package org.sonar.db.event; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static com.google.common.base.Strings.repeat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class EventValidatorTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void valid_cases() { @@ -41,25 +38,22 @@ public class EventValidatorTest { @Test public void fail_if_name_longer_than_400() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Event name length (401) is longer than the maximum authorized (400)."); - - EventValidator.checkEventName(repeat("a", 400 + 1)); + assertThatThrownBy(() -> EventValidator.checkEventName(repeat("a", 400 + 1))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Event name length (401) is longer than the maximum authorized (400)."); } @Test public void fail_if_category_longer_than_50() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Event category length (51) is longer than the maximum authorized (50). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); - - EventValidator.checkEventCategory(repeat("a", 50 + 1)); + assertThatThrownBy(() -> EventValidator.checkEventCategory(repeat("a", 50 + 1))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Event category length (51) is longer than the maximum authorized (50). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); } @Test public void fail_if_description_longer_than_4000() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Event description length (4001) is longer than the maximum authorized (4000)."); - - EventValidator.checkEventDescription(repeat("a", 4000 + 1)); + assertThatThrownBy(() -> EventValidator.checkEventDescription(repeat("a", 4000 + 1))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Event description length (4001) is longer than the maximum authorized (4000)."); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java index 0ee427cdeca..d3ba10a6349 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java @@ -27,7 +27,6 @@ import java.util.Set; import java.util.stream.Stream; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.issue.Issue; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; @@ -46,8 +45,8 @@ import static java.util.Arrays.asList; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.math.RandomUtils.nextInt; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertFalse; -import static org.junit.rules.ExpectedException.none; import static org.sonar.db.component.ComponentTesting.newDirectory; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newModuleDto; @@ -67,8 +66,6 @@ public class IssueDaoTest { .toArray(RuleType[]::new); @Rule - public ExpectedException expectedException = none(); - @Rule public DbTester db = DbTester.create(System2.INSTANCE); private IssueDao underTest = db.getDbClient().issueDao(); @@ -113,12 +110,12 @@ public class IssueDaoTest { @Test public void selectByKeyOrFail_fails_if_key_not_found() { - expectedException.expect(RowNotFoundException.class); - expectedException.expectMessage("Issue with key 'DOES_NOT_EXIST' does not exist"); - - prepareTables(); - - underTest.selectOrFailByKey(db.getSession(), "DOES_NOT_EXIST"); + assertThatThrownBy(() -> { + prepareTables(); + underTest.selectOrFailByKey(db.getSession(), "DOES_NOT_EXIST"); + }) + .isInstanceOf(RowNotFoundException.class) + .hasMessage("Issue with key 'DOES_NOT_EXIST' does not exist"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java index 2007e1b56c6..644f33d74f7 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java @@ -26,9 +26,7 @@ import java.util.Date; import java.util.List; import java.util.Set; import org.apache.commons.lang.time.DateUtils; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.issue.Issue; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; @@ -37,22 +35,22 @@ import org.sonar.core.issue.DefaultIssue; import org.sonar.db.rule.RuleDefinitionDto; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class IssueDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void set_data_check_maximal_length() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Value is too long for issue attributes:"); - - StringBuilder s = new StringBuilder(4500); - for (int i = 0; i < 4500; i++) { - s.append('a'); - } - new IssueDto().setIssueAttributes(s.toString()); + assertThatThrownBy(() -> { + StringBuilder s = new StringBuilder(4500); + for (int i = 0; i < 4500; i++) { + s.append('a'); + } + new IssueDto().setIssueAttributes(s.toString()); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Value is too long for issue attributes:"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/mapping/ProjectMappingsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/mapping/ProjectMappingsDaoTest.java index a2f577bf0b3..c5ce8c525a4 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/mapping/ProjectMappingsDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/mapping/ProjectMappingsDaoTest.java @@ -24,15 +24,16 @@ import java.util.Objects; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.core.util.SequenceUuidFactory; import org.sonar.db.DbSession; import org.sonar.db.DbTester; 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.when; @@ -49,8 +50,6 @@ public class ProjectMappingsDaoTest { private System2 system2 = mock(System2.class); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(system2); private DbSession dbSession = dbTester.getSession(); @@ -59,37 +58,27 @@ public class ProjectMappingsDaoTest { @Test public void put_throws_IAE_if_key_type_is_null() { - expectKeyTypeNullOrEmptyIAE(); - - underTest.put(dbSession, null, A_KEY, PROJECT_UUID); + expectKeyTypeNullOrEmptyIAE(() -> underTest.put(dbSession, null, A_KEY, PROJECT_UUID)); } @Test public void put_throws_IAE_if_key_is_null() { - expectKeyNullOrEmptyIAE(); - - underTest.put(dbSession, A_KEY_TYPE, null, PROJECT_UUID); + expectKeyNullOrEmptyIAE(() -> underTest.put(dbSession, A_KEY_TYPE, null, PROJECT_UUID)); } @Test public void put_throws_IAE_if_key_is_empty() { - expectKeyNullOrEmptyIAE(); - - underTest.put(dbSession, A_KEY_TYPE, EMPTY_STRING, PROJECT_UUID); + expectKeyNullOrEmptyIAE(() -> underTest.put(dbSession, A_KEY_TYPE, EMPTY_STRING, PROJECT_UUID)); } @Test public void save_throws_IAE_if_project_uuid_is_null() { - expectValueNullOrEmptyIAE(); - - underTest.put(dbSession, A_KEY_TYPE, A_KEY, null); + expectValueNullOrEmptyIAE(() -> underTest.put(dbSession, A_KEY_TYPE, A_KEY, null)); } @Test public void put_throws_IAE_if_project_uuid_is_empty() { - expectValueNullOrEmptyIAE(); - - underTest.put(dbSession, A_KEY_TYPE, A_KEY, EMPTY_STRING); + expectValueNullOrEmptyIAE(() -> underTest.put(dbSession, A_KEY_TYPE, A_KEY, EMPTY_STRING)); } @Test @@ -133,23 +122,17 @@ public class ProjectMappingsDaoTest { @Test public void get_throws_IAE_when_key_type_is_null() { - expectKeyTypeNullOrEmptyIAE(); - - underTest.get(dbSession, null, A_KEY); + expectKeyTypeNullOrEmptyIAE(() -> underTest.get(dbSession, null, A_KEY)); } @Test public void get_throws_IAE_when_key_is_null() { - expectKeyNullOrEmptyIAE(); - - underTest.get(dbSession, A_KEY_TYPE, null); + expectKeyNullOrEmptyIAE(() -> underTest.get(dbSession, A_KEY_TYPE, null)); } @Test public void get_throws_IAE_when_key_is_empty() { - expectKeyNullOrEmptyIAE(); - - underTest.get(dbSession, A_KEY_TYPE, EMPTY_STRING); + expectKeyNullOrEmptyIAE(() -> underTest.get(dbSession, A_KEY_TYPE, EMPTY_STRING)); } @Test @@ -164,19 +147,22 @@ public class ProjectMappingsDaoTest { assertThat(underTest.get(dbSession, A_KEY_TYPE, A_KEY).get().getProjectUuid()).isEqualTo(PROJECT_UUID); } - private void expectKeyTypeNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("key type can't be null nor empty"); + private void expectKeyTypeNullOrEmptyIAE(ThrowingCallable callback) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("key type can't be null nor empty"); } - private void expectKeyNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("key can't be null nor empty"); + private void expectKeyNullOrEmptyIAE(ThrowingCallable callback) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("key can't be null nor empty"); } - private void expectValueNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("projectUuid can't be null nor empty"); + private void expectValueNullOrEmptyIAE(ThrowingCallable callback) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("projectUuid can't be null nor empty"); } private ProjectMappingAssert assertThatProjectMapping(String keyType, String key) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java index 36e3e018191..e95ef5dcb67 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java @@ -24,7 +24,6 @@ import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; @@ -48,8 +47,6 @@ public class MeasureDaoTest { private MetricDto complexity; private MetricDto ncloc; - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Rule public DbTester db = DbTester.create(System2.INSTANCE); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureTreeQueryTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureTreeQueryTest.java index d10658b480e..f0cd3d60169 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureTreeQueryTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureTreeQueryTest.java @@ -20,20 +20,17 @@ package org.sonar.db.measure; import java.util.Collections; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.db.component.ComponentTesting; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.measure.MeasureTreeQuery.Strategy.CHILDREN; import static org.sonar.db.measure.MeasureTreeQuery.Strategy.LEAVES; public class MeasureTreeQueryTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void create_query() { @@ -111,9 +108,11 @@ public class MeasureTreeQueryTest { @Test public void fail_when_no_strategy() { - expectedException.expect(NullPointerException.class); - MeasureTreeQuery.builder() - .build(); + assertThatThrownBy(() -> { + MeasureTreeQuery.builder() + .build(); + }) + .isInstanceOf(NullPointerException.class); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/PastMeasureDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/PastMeasureDtoTest.java index 295e9ea1c35..11d76693804 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/PastMeasureDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/PastMeasureDtoTest.java @@ -22,6 +22,7 @@ package org.sonar.db.measure; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class PastMeasureDtoTest { @@ -48,8 +49,9 @@ public class PastMeasureDtoTest { assertThat(measureWithoutValue.hasValue()).isFalse(); } - @Test(expected = NullPointerException.class) + @Test public void get_value_throw_a_NPE_if_value_is_null() { - new PastMeasureDto().getValue(); + assertThatThrownBy(() -> new PastMeasureDto().getValue()) + .isInstanceOf(NullPointerException.class); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java index e0e99b46386..21421d35b0e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java @@ -24,7 +24,6 @@ import java.util.Map; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -49,8 +48,6 @@ import static org.sonar.db.component.SnapshotTesting.newAnalysis; public class ProjectMeasuresIndexerIteratorTest { @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); private final DbClient dbClient = dbTester.getDbClient(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDaoTest.java index 71d680a4d72..6297f2bfe84 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDaoTest.java @@ -28,7 +28,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.core.util.Uuids; import org.sonar.db.DbSession; @@ -37,6 +36,7 @@ import org.sonar.db.RowNotFoundException; import static com.google.common.collect.Sets.newHashSet; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.within; import static org.sonar.db.metric.MetricTesting.newMetricDto; @@ -44,8 +44,6 @@ public class MetricDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private DbSession dbSession = db.getSession(); private MetricDao underTest = new MetricDao(); @@ -66,9 +64,8 @@ public class MetricDaoTest { @Test public void select_or_fail_by_key() { - expectedException.expect(RowNotFoundException.class); - - underTest.selectOrFailByKey(dbSession, "unknown"); + assertThatThrownBy(() -> underTest.selectOrFailByKey(dbSession, "unknown")) + .isInstanceOf(RowNotFoundException.class); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDtoTest.java index 4bee8934a12..0748e068b4e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/metric/MetricDtoTest.java @@ -19,16 +19,13 @@ */ package org.sonar.db.metric; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static com.google.common.base.Strings.repeat; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class MetricDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private MetricDto underTest = new MetricDto(); @@ -70,39 +67,35 @@ public class MetricDtoTest { public void fail_if_key_longer_than_64_characters() { String a65 = repeat("a", 65); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Metric key length (65) is longer than the maximum authorized (64). '" + a65 + "' was provided."); - - underTest.setKey(a65); + assertThatThrownBy(() -> underTest.setKey(a65)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Metric key length (65) is longer than the maximum authorized (64). '" + a65 + "' was provided."); } @Test public void fail_if_name_longer_than_64_characters() { String a65 = repeat("a", 65); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Metric name length (65) is longer than the maximum authorized (64). '" + a65 + "' was provided."); - - underTest.setShortName(a65); + assertThatThrownBy(() -> underTest.setShortName(a65)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Metric name length (65) is longer than the maximum authorized (64). '" + a65 + "' was provided."); } @Test public void fail_if_description_longer_than_255_characters() { String a256 = repeat("a", 256); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Metric description length (256) is longer than the maximum authorized (255). '" + a256 + "' was provided."); - - underTest.setDescription(a256); + assertThatThrownBy(() -> underTest.setDescription(a256)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Metric description length (256) is longer than the maximum authorized (255). '" + a256 + "' was provided."); } @Test public void fail_if_domain_longer_than_64_characters() { String a65 = repeat("a", 65); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Metric domain length (65) is longer than the maximum authorized (64). '" + a65 + "' was provided."); - - underTest.setDomain(a65); + assertThatThrownBy(() -> underTest.setDomain(a65)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Metric domain length (65) is longer than the maximum authorized (64). '" + a65 + "' was provided."); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDtoTest.java index 783178b558f..7f09acaf2f7 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDtoTest.java @@ -19,16 +19,12 @@ */ package org.sonar.db.newcodeperiod; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import static org.assertj.core.api.Assertions.assertThat; public class NewCodePeriodDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void getters_and_setters() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/PermissionQueryTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/PermissionQueryTest.java index 7271744f73a..4672f385847 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/PermissionQueryTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/PermissionQueryTest.java @@ -19,18 +19,15 @@ */ package org.sonar.db.permission; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.db.component.ComponentDto; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.component.ComponentTesting.newPublicProjectDto; public class PermissionQueryTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void create_query() { @@ -68,11 +65,12 @@ public class PermissionQueryTest { @Test public void fail_when_search_query_length_is_less_than_3_characters() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Search query should contains at least 3 characters"); - - PermissionQuery.builder() - .setSearchQuery("so") - .build(); + assertThatThrownBy(() -> { + PermissionQuery.builder() + .setSearchQuery("so") + .build(); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Search query should contains at least 3 characters"); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDtoTest.java index cb3087e72e2..e5cec96aff9 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDtoTest.java @@ -20,22 +20,18 @@ package org.sonar.db.permission.template; import com.google.common.base.Strings; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class PermissionTemplateCharacteristicDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); PermissionTemplateCharacteristicDto underTest = new PermissionTemplateCharacteristicDto(); @Test public void check_permission_field_length() { - expectedException.expect(IllegalArgumentException.class); - expectedException - .expectMessage("Permission key length (65) is longer than the maximum authorized (64). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); - - underTest.setPermission(Strings.repeat("a", 65)); + assertThatThrownBy(() -> underTest.setPermission(Strings.repeat("a", 65))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Permission key length (65) is longer than the maximum authorized (64). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java index 3cfe40e6d75..396e4edb085 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java @@ -25,7 +25,6 @@ import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.util.UuidFactory; @@ -55,8 +54,6 @@ public class PermissionTemplateDaoTest { private static final Date NOW = new Date(500_000_000_000L); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(); private System2 system2 = mock(System2.class); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java index 4b9f4f904bf..5562e83d46f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java @@ -22,7 +22,6 @@ package org.sonar.db.plugin; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.plugin.PluginDto.Type; @@ -34,8 +33,6 @@ import static org.sonar.db.plugin.PluginDto.Type.EXTERNAL; public class PluginDaoTest { @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(System2.INSTANCE); private final PluginDao underTest = db.getDbClient().pluginDao(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoWithPersisterTest.java index 0d8132a118a..d3eaec31597 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoWithPersisterTest.java @@ -21,7 +21,6 @@ package org.sonar.db.plugin; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; @@ -39,8 +38,6 @@ public class PluginDaoWithPersisterTest { private final AuditPersister auditPersister = mock(AuditPersister.class); @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Rule public final DbTester db = DbTester.create(System2.INSTANCE, auditPersister); private final ArgumentCaptor<PluginNewValue> newValueCaptor = ArgumentCaptor.forClass(PluginNewValue.class); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertiesDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertiesDaoTest.java index a9672e352ee..8c262d2957e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertiesDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertiesDaoTest.java @@ -22,7 +22,6 @@ package org.sonar.db.property; import java.util.Optional; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactory; import org.sonar.core.util.UuidFactoryFast; @@ -43,8 +42,6 @@ public class InternalComponentPropertiesDaoTest { private System2 system2 = mock(System2.class); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(system2); private DbSession dbSession = dbTester.getSession(); private UuidFactory uuidFactory = UuidFactoryFast.getInstance(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertyDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertyDtoTest.java index 95aedbd7bd5..1089188d382 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertyDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalComponentPropertyDtoTest.java @@ -22,18 +22,15 @@ package org.sonar.db.property; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import org.apache.commons.lang.StringUtils; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @RunWith(DataProviderRunner.class) public class InternalComponentPropertyDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void setter_and_getter() { @@ -56,30 +53,27 @@ public class InternalComponentPropertyDtoTest { @Test @DataProvider({"null", ""}) public void setKey_throws_IAE_if_key_is_null_or_empty(String key) { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("key can't be null nor empty"); - - new InternalComponentPropertyDto().setKey(key); + assertThatThrownBy(() -> new InternalComponentPropertyDto().setKey(key)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("key can't be null nor empty"); } @Test public void setKey_throws_IAE_if_key_is_too_long() { String veryLongKey = StringUtils.repeat("a", 513); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(String.format("key length (513) is longer than the maximum authorized (512). '%s' was provided", veryLongKey)); - - new InternalComponentPropertyDto().setKey(veryLongKey); + assertThatThrownBy(() -> new InternalComponentPropertyDto().setKey(veryLongKey)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(String.format("key length (513) is longer than the maximum authorized (512). '%s' was provided", veryLongKey)); } @Test public void setValue_throws_IAE_if_value_is_too_long() { String veryLongValue = StringUtils.repeat("a", 4001); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage(String.format("value length (4001) is longer than the maximum authorized (4000). '%s' was provided", veryLongValue)); - - new InternalComponentPropertyDto().setValue(veryLongValue); + assertThatThrownBy(() -> new InternalComponentPropertyDto().setValue(veryLongValue)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage(String.format("value length (4001) is longer than the maximum authorized (4000). '%s' was provided", veryLongValue)); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalPropertiesDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalPropertiesDaoTest.java index 020d2e7ff97..cfd4277c609 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalPropertiesDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/InternalPropertiesDaoTest.java @@ -34,10 +34,10 @@ import java.util.stream.Stream; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.sonar.api.utils.System2; @@ -50,6 +50,7 @@ import static java.lang.Boolean.FALSE; import static java.lang.Boolean.TRUE; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.entry; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; @@ -82,8 +83,6 @@ public class InternalPropertiesDaoTest { private final String DEFAULT_PROJECT_TEMPLATE = "defaultTemplate.prj"; @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(system2); private final DbSession dbSession = dbTester.getSession(); @@ -98,30 +97,22 @@ public class InternalPropertiesDaoTest { @Test public void save_throws_IAE_if_key_is_null() { - expectKeyNullOrEmptyIAE(); - - underTest.save(dbSession, null, VALUE_SMALL); + expectKeyNullOrEmptyIAE(() -> underTest.save(dbSession, null, VALUE_SMALL)); } @Test public void save_throws_IAE_if_key_is_empty() { - expectKeyNullOrEmptyIAE(); - - underTest.save(dbSession, EMPTY_STRING, VALUE_SMALL); + expectKeyNullOrEmptyIAE(() -> underTest.save(dbSession, EMPTY_STRING, VALUE_SMALL)); } @Test public void save_throws_IAE_if_value_is_null() { - expectValueNullOrEmptyIAE(); - - underTest.save(dbSession, A_KEY, null); + expectValueNullOrEmptyIAE(() -> underTest.save(dbSession, A_KEY, null)); } @Test public void save_throws_IAE_if_value_is_empty() { - expectValueNullOrEmptyIAE(); - - underTest.save(dbSession, A_KEY, EMPTY_STRING); + expectValueNullOrEmptyIAE(() -> underTest.save(dbSession, A_KEY, EMPTY_STRING)); } @Test @@ -140,7 +131,7 @@ public class InternalPropertiesDaoTest { dbSession.commit(); assertThat(dbTester.countRowsOfTable("internal_properties")).isOne(); clearInvocations(auditPersister); - + underTest.delete(dbSession, A_KEY); dbSession.commit(); @@ -266,16 +257,12 @@ public class InternalPropertiesDaoTest { @Test public void saveAsEmpty_throws_IAE_if_key_is_null() { - expectKeyNullOrEmptyIAE(); - - underTest.saveAsEmpty(dbSession, null); + expectKeyNullOrEmptyIAE(() -> underTest.saveAsEmpty(dbSession, null)); } @Test public void saveAsEmpty_throws_IAE_if_key_is_empty() { - expectKeyNullOrEmptyIAE(); - - underTest.saveAsEmpty(dbSession, EMPTY_STRING); + expectKeyNullOrEmptyIAE(() -> underTest.saveAsEmpty(dbSession, EMPTY_STRING)); } @Test @@ -337,16 +324,12 @@ public class InternalPropertiesDaoTest { @Test public void selectByKey_throws_IAE_when_key_is_null() { - expectKeyNullOrEmptyIAE(); - - underTest.selectByKey(dbSession, null); + expectKeyNullOrEmptyIAE(() -> underTest.selectByKey(dbSession, null)); } @Test public void selectByKey_throws_IAE_when_key_is_empty() { - expectKeyNullOrEmptyIAE(); - - underTest.selectByKey(dbSession, EMPTY_STRING); + expectKeyNullOrEmptyIAE(() -> underTest.selectByKey(dbSession, EMPTY_STRING)); } @Test @@ -395,9 +378,7 @@ public class InternalPropertiesDaoTest { .flatMap(s -> s) .collect(Collectors.toSet()); - expectKeyNullOrEmptyIAE(); - - underTest.selectByKeys(dbSession, keysIncludingANull); + expectKeyNullOrEmptyIAE(() -> underTest.selectByKeys(dbSession, keysIncludingANull)); } @Test @@ -410,9 +391,7 @@ public class InternalPropertiesDaoTest { .flatMap(s -> s) .collect(Collectors.toSet()); - expectKeyNullOrEmptyIAE(); - - underTest.selectByKeys(dbSession, keysIncludingAnEmptyString); + expectKeyNullOrEmptyIAE(() -> underTest.selectByKeys(dbSession, keysIncludingAnEmptyString)); } @Test @@ -563,20 +542,18 @@ public class InternalPropertiesDaoTest { @Test public void tryLock_throws_IAE_if_lock_name_is_empty() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("lock name can't be empty"); - - underTest.tryLock(dbSession, "", 60); + assertThatThrownBy(() -> underTest.tryLock(dbSession, "", 60)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("lock name can't be empty"); } @Test public void tryLock_throws_IAE_if_lock_name_length_is_16_or_more() { String tooLongName = randomAlphabetic(16 + new Random().nextInt(30)); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("lock name is too long"); - - underTest.tryLock(dbSession, tooLongName, 60); + assertThatThrownBy(() -> underTest.tryLock(dbSession, tooLongName, 60)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("lock name is too long"); } private void assertAuditValue(String key, @Nullable String value) { @@ -591,14 +568,16 @@ public class InternalPropertiesDaoTest { return "lock." + lockName; } - private void expectKeyNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("key can't be null nor empty"); + private void expectKeyNullOrEmptyIAE(ThrowingCallable callback) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("key can't be null nor empty"); } - private void expectValueNullOrEmptyIAE() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("value can't be null nor empty"); + private void expectValueNullOrEmptyIAE(ThrowingCallable callback) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("value can't be null nor empty"); } private InternalPropertyAssert assertThatInternalProperty(String key) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java index 58637968dcc..d11d76153c2 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java @@ -34,7 +34,6 @@ import javax.annotation.Nullable; 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.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.resources.Qualifiers; @@ -54,6 +53,7 @@ import static com.google.common.collect.Sets.newHashSet; import static java.util.Collections.singletonList; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.groups.Tuple.tuple; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -77,8 +77,6 @@ public class PropertiesDaoTest { private final AuditPersister auditPersister = mock(AuditPersister.class); @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(system2, auditPersister); private final DbClient dbClient = db.getDbClient(); @@ -1269,14 +1267,14 @@ public class PropertiesDaoTest { @Test public void should_not_rename_with_empty_key() { - thrown.expect(IllegalArgumentException.class); - underTest.renamePropertyKey("foo", ""); + assertThatThrownBy(() -> underTest.renamePropertyKey("foo", "")) + .isInstanceOf(IllegalArgumentException.class); } @Test public void should_not_rename_an_empty_key() { - thrown.expect(IllegalArgumentException.class); - underTest.renamePropertyKey(null, "foo"); + assertThatThrownBy(() -> underTest.renamePropertyKey(null, "foo")) + .isInstanceOf(IllegalArgumentException.class); } private PropertyDto findByKey(List<PropertyDto> properties, String key) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java index 1ba0912b70b..653ef24f2d8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoWithPersisterTest.java @@ -24,7 +24,6 @@ import java.util.List; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; @@ -67,8 +66,6 @@ public class PropertiesDaoWithPersisterTest { private final ArgumentCaptor<PropertyNewValue> newValueCaptor = ArgumentCaptor.forClass(PropertyNewValue.class); @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(system2, auditPersister); private final DbClient dbClient = db.getDbClient(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index ac34726bd91..f2566237fe2 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -39,7 +39,6 @@ import org.apache.commons.lang.math.RandomUtils; import org.apache.commons.lang.time.DateUtils; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.issue.Issue; import org.sonar.api.utils.System2; import org.sonar.core.util.CloseableIterator; @@ -117,8 +116,6 @@ public class PurgeDaoTest { @Rule public DbTester db = DbTester.create(system2); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final DbClient dbClient = db.getDbClient(); private final DbSession dbSession = db.getSession(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java index 70c27f914ea..bf0900bbc6e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoWithAuditTest.java @@ -34,7 +34,6 @@ import org.sonar.db.component.ComponentDto; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java index 4228ca0cb4d..4905705ce2c 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java @@ -19,15 +19,12 @@ */ package org.sonar.db.purge; -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 PurgeableAnalysisDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void testEquals() { @@ -49,8 +46,9 @@ public class PurgeableAnalysisDtoTest { // no uuid => NPE dto = new PurgeableAnalysisDto(); - expectedException.expect(NullPointerException.class); - dto.hashCode(); + assertThatThrownBy(dto::hashCode) + .isInstanceOf(NullPointerException.class); + } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java index 36e09eb8f5b..5126294af22 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java @@ -27,7 +27,6 @@ import java.util.function.Consumer; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.impl.utils.TestSystem2; import org.sonar.api.rule.Severity; import org.sonar.api.rules.RuleType; @@ -43,6 +42,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.api.rule.RuleStatus.BETA; @@ -56,8 +56,6 @@ import static org.sonar.db.qualityprofile.ActiveRuleDto.createFor; public class ActiveRuleDaoTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); private static final long NOW = 10_000_000L; @@ -279,26 +277,23 @@ public class ActiveRuleDaoTest { @Test public void fail_to_insert_when_profile_id_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Quality profile is not persisted (missing id)"); - - underTest.insert(dbSession, createFor(profile1, rule1).setProfileUuid(null)); + assertThatThrownBy(() -> underTest.insert(dbSession, createFor(profile1, rule1).setProfileUuid(null))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Quality profile is not persisted (missing id)"); } @Test public void fail_to_insert_when_rule_uuid_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Rule is not persisted"); - - underTest.insert(dbSession, createFor(profile1, rule1).setRuleUuid(null)); + assertThatThrownBy(() -> underTest.insert(dbSession, createFor(profile1, rule1).setRuleUuid(null))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Rule is not persisted"); } @Test public void fail_to_insert_when_uuid_is_not_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("ActiveRule is already persisted"); - - underTest.insert(dbSession, createFor(profile1, rule1).setUuid("uuid")); + assertThatThrownBy(() -> underTest.insert(dbSession, createFor(profile1, rule1).setUuid("uuid"))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("ActiveRule is already persisted"); } @Test @@ -333,26 +328,23 @@ public class ActiveRuleDaoTest { @Test public void fail_to_update_when_profile_id_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Quality profile is not persisted (missing id)"); - - underTest.update(dbSession, createFor(profile1, rule1).setUuid("uuid").setProfileUuid(null)); + assertThatThrownBy(() -> underTest.update(dbSession, createFor(profile1, rule1).setUuid("uuid").setProfileUuid(null))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Quality profile is not persisted (missing id)"); } @Test public void fail_to_update_when_rule_id_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Rule is not persisted"); - - underTest.update(dbSession, createFor(profile1, rule1).setUuid("uuid").setRuleUuid(null)); + assertThatThrownBy(() -> underTest.update(dbSession, createFor(profile1, rule1).setUuid("uuid").setRuleUuid(null))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Rule is not persisted"); } @Test public void fail_to_update_when_id_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("ActiveRule is not persisted"); - - underTest.update(dbSession, createFor(profile1, rule1).setUuid(null)); + assertThatThrownBy(() -> underTest.update(dbSession, createFor(profile1, rule1).setUuid(null))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("ActiveRule is not persisted"); } @Test @@ -470,32 +462,35 @@ public class ActiveRuleDaoTest { @Test public void insertParam_fails_when_active_rule_id_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("ActiveRule is not persisted"); - - underTest.insertParam(dbSession, - createFor(profile1, rule1).setUuid(null), - ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1")); + assertThatThrownBy(() -> { + underTest.insertParam(dbSession, + createFor(profile1, rule1).setUuid(null), + ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1")); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("ActiveRule is not persisted"); } @Test public void insertParam_fails_when_active_rule_param_id_is_null() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("ActiveRuleParam is already persisted"); - - underTest.insertParam(dbSession, - createFor(profile1, rule1).setUuid("uuid"), - ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setUuid("uuid-1")); + assertThatThrownBy(() -> { + underTest.insertParam(dbSession, + createFor(profile1, rule1).setUuid("uuid"), + ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setUuid("uuid-1")); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("ActiveRuleParam is already persisted"); } @Test public void insertParam_fails_when_uuid_is_not_null() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("Rule param is not persisted"); - - underTest.insertParam(dbSession, - createFor(profile1, rule1).setUuid("uuid"), - ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterUuid(null)); + assertThatThrownBy(() -> { + underTest.insertParam(dbSession, + createFor(profile1, rule1).setUuid("uuid"), + ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterUuid(null)); + }) + .isInstanceOf(NullPointerException.class) + .hasMessage("Rule param is not persisted"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java index 1870a09e685..cd49fca93ca 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileChangeDaoTest.java @@ -24,7 +24,6 @@ import java.util.Map; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.utils.System2; import org.sonar.core.util.SequenceUuidFactory; @@ -33,6 +32,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static java.util.Arrays.asList; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class QProfileChangeDaoTest { @@ -40,8 +40,6 @@ public class QProfileChangeDaoTest { private System2 system2 = new AlwaysIncreasingSystem2(); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(system2); private DbSession dbSession = db.getSession(); @@ -77,10 +75,9 @@ public class QProfileChangeDaoTest { @Test public void insert_throws_ISE_if_date_is_already_set() { - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Date of QProfileChangeDto must be set by DAO only. Got 123."); - - underTest.insert(dbSession, new QProfileChangeDto().setCreatedAt(123L)); + assertThatThrownBy(() -> underTest.insert(dbSession, new QProfileChangeDto().setCreatedAt(123L))) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Date of QProfileChangeDto must be set by DAO only. Got 123."); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java index 8989a72e6ac..05df576f38b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java @@ -23,7 +23,6 @@ import java.sql.SQLException; import java.util.List; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.sonar.api.impl.utils.TestSystem2; import org.sonar.api.utils.System2; @@ -38,6 +37,7 @@ import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -46,7 +46,6 @@ import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.AN import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.IN; import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.OUT; import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.builder; -import static org.sonar.test.ExceptionCauseMatcher.hasType; public class QProfileEditUsersDaoTest { @@ -57,8 +56,6 @@ public class QProfileEditUsersDaoTest { private final ArgumentCaptor<UserEditorNewValue> newValueCaptor = ArgumentCaptor.forClass(UserEditorNewValue.class); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(system2, auditPersister); private final QProfileEditUsersDao underTest = db.getDbClient().qProfileEditUsersDao(); @@ -259,13 +256,14 @@ public class QProfileEditUsersDaoTest { .setQProfileUuid(qualityProfileUuid), qualityProfileName, userLogin); - expectedException.expectCause(hasType(SQLException.class)); - - underTest.insert(db.getSession(), new QProfileEditUsersDto() - .setUuid("UUID-2") - .setUserUuid(userUuid) - .setQProfileUuid(qualityProfileUuid), - qualityProfileName, userLogin); + assertThatThrownBy(() -> { + underTest.insert(db.getSession(), new QProfileEditUsersDto() + .setUuid("UUID-2") + .setUserUuid(userUuid) + .setQProfileUuid(qualityProfileUuid), + qualityProfileName, userLogin); + }) + .hasCauseInstanceOf(SQLException.class); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java index f8bb1002e1d..0aa60519e8b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -32,7 +32,6 @@ import org.apache.ibatis.exceptions.PersistenceException; import org.apache.ibatis.session.ResultHandler; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; @@ -51,6 +50,7 @@ import static java.util.Collections.emptyList; 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.Assertions.tuple; import static org.sonar.api.rule.RuleStatus.REMOVED; import static org.sonar.db.rule.RuleTesting.newRuleMetadata; @@ -59,8 +59,6 @@ public class RuleDaoTest { private static final String UNKNOWN_RULE_UUID = "unknown-uuid"; @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public DbTester db = DbTester.create(System2.INSTANCE); private final RuleDao underTest = db.getDbClient().ruleDao(); @@ -179,18 +177,16 @@ public class RuleDaoTest { @Test public void selectOrFailByKey_fails_if_rule_not_found() { - thrown.expect(RowNotFoundException.class); - thrown.expectMessage("Rule with key 'NOT:FOUND' does not exist"); - - underTest.selectOrFailByKey(db.getSession(), RuleKey.of("NOT", "FOUND")); + assertThatThrownBy(() -> underTest.selectOrFailByKey(db.getSession(), RuleKey.of("NOT", "FOUND"))) + .isInstanceOf(RowNotFoundException.class) + .hasMessage("Rule with key 'NOT:FOUND' does not exist"); } @Test public void selectOrFailDefinitionByKey_fails_if_rule_not_found() { - thrown.expect(RowNotFoundException.class); - thrown.expectMessage("Rule with key 'NOT:FOUND' does not exist"); - - underTest.selectOrFailDefinitionByKey(db.getSession(), RuleKey.of("NOT", "FOUND")); + assertThatThrownBy(() -> underTest.selectOrFailDefinitionByKey(db.getSession(), RuleKey.of("NOT", "FOUND"))) + .isInstanceOf(RowNotFoundException.class) + .hasMessage("Rule with key 'NOT:FOUND' does not exist"); } @Test @@ -738,8 +734,8 @@ public class RuleDaoTest { underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, param); - thrown.expect(PersistenceException.class); - underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, param); + assertThatThrownBy(() -> underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, param)) + .isInstanceOf(PersistenceException.class); } @Test @@ -1035,10 +1031,11 @@ public class RuleDaoTest { db.rules().insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey) .setOldRuleKey(ruleKey)); - thrown.expect(PersistenceException.class); - - db.rules().insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey) - .setOldRuleKey(ruleKey)); + assertThatThrownBy(() -> { + db.rules().insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey) + .setOldRuleKey(ruleKey)); + }) + .isInstanceOf(PersistenceException.class); } private static class Accumulator<T> implements Consumer<T> { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java index c431285f3de..ca4645a8087 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java @@ -22,41 +22,37 @@ package org.sonar.db.rule; import com.google.common.collect.ImmutableSet; import java.util.Collections; import java.util.Set; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class RuleDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void fail_if_key_is_too_long() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Rule key is too long: "); - - new RuleDto().setRuleKey(repeat("x", 250)); + assertThatThrownBy(() -> new RuleDto().setRuleKey(repeat("x", 250))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Rule key is too long: "); } @Test public void fail_if_name_is_too_long() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Rule name is too long: "); - - new RuleDto().setName(repeat("x", 300)); + assertThatThrownBy(() -> new RuleDto().setName(repeat("x", 300))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Rule name is too long: "); } @Test public void fail_if_tags_are_too_long() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Rule tags are too long: "); - - Set<String> tags = ImmutableSet.of(repeat("a", 2000), repeat("b", 1000), repeat("c", 2000)); - new RuleDto().setTags(tags); + assertThatThrownBy(() -> { + Set<String> tags = ImmutableSet.of(repeat("a", 2000), repeat("b", 1000), repeat("c", 2000)); + new RuleDto().setTags(tags); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Rule tags are too long: "); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java index 7ef08756877..8ecb550b730 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleRepositoryDaoTest.java @@ -26,7 +26,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; @@ -35,14 +34,13 @@ import org.sonar.db.DbTester; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class RuleRepositoryDaoTest { private System2 system2 = new AlwaysIncreasingSystem2(); @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public DbTester dbTester = DbTester.create(system2); private RuleRepositoryDao underTest = new RuleRepositoryDao(system2); @@ -116,11 +114,12 @@ public class RuleRepositoryDaoTest { @Test public void deleteIfKeyNotIn_fails_if_more_than_1000_keys() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("too many rule repositories: 1100"); - - Collection<String> keys = IntStream.range(0, 1_100).mapToObj(index -> "repo" + index).collect(Collectors.toSet()); - underTest.deleteIfKeyNotIn(dbTester.getSession(), keys); + assertThatThrownBy(() -> { + Collection<String> keys = IntStream.range(0, 1_100).mapToObj(index -> "repo" + index).collect(Collectors.toSet()); + underTest.deleteIfKeyNotIn(dbTester.getSession(), keys); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("too many rule repositories: 1100"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java index 86de1101116..c29c53771cb 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/schemamigration/SchemaMigrationDaoTest.java @@ -23,18 +23,16 @@ import java.sql.Statement; import org.junit.After; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class SchemaMigrationDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private DbSession dbSession = dbTester.getSession(); private SchemaMigrationDao underTest = dbTester.getDbClient().schemaMigrationDao(); @@ -49,18 +47,16 @@ public class SchemaMigrationDaoTest { @Test public void insert_fails_with_NPE_if_argument_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("version can't be null"); - - underTest.insert(dbSession, null); + assertThatThrownBy(() -> underTest.insert(dbSession, null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("version can't be null"); } @Test public void insert_fails_with_IAE_if_argument_is_empty() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("version can't be empty"); - - underTest.insert(dbSession, ""); + assertThatThrownBy(() -> underTest.insert(dbSession, "")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("version can't be empty"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java index 53efd6f8a48..ae89e31d7bc 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java @@ -25,12 +25,11 @@ import java.util.List; import java.util.Random; import java.util.stream.Collectors; import java.util.stream.IntStream; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.db.protobuf.DbFileSources; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class FileSourceDtoTest { private static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac magna libero. " + @@ -39,8 +38,6 @@ public class FileSourceDtoTest { "Curabitur sit amet dignissim magna, at efficitur dolor. Ut non felis aliquam justo euismod gravida. Morbi eleifend vitae ante eu pulvinar. " + "Aliquam rhoncus magna quis lorem posuere semper."; - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void getSourceData_throws_ISE_with_id_fileUuid_and_projectUuid_in_message_when_data_cant_be_read() { @@ -53,10 +50,9 @@ public class FileSourceDtoTest { .setFileUuid(fileUuid) .setProjectUuid(projectUuid); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Fail to decompress and deserialize source data [uuid=" + uuid + ",fileUuid=" + fileUuid + ",projectUuid=" + projectUuid + "]"); - - underTest.getSourceData(); + assertThatThrownBy(underTest::getSourceData) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Fail to decompress and deserialize source data [uuid=" + uuid + ",fileUuid=" + fileUuid + ",projectUuid=" + projectUuid + "]"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/source/LineHashVersionTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/source/LineHashVersionTest.java index e36b08bc0d6..3069e6057b5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/source/LineHashVersionTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/source/LineHashVersionTest.java @@ -19,15 +19,12 @@ */ package org.sonar.db.source; -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 LineHashVersionTest { - @Rule - public ExpectedException exception = ExpectedException.none(); @Test public void should_create_from_int() { @@ -37,15 +34,15 @@ public class LineHashVersionTest { @Test public void should_throw_exception_if_version_is_too_high() { - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Unknown line hash version: 2"); - LineHashVersion.valueOf(2); + assertThatThrownBy(() -> LineHashVersion.valueOf(2)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown line hash version: 2"); } @Test public void should_throw_exception_if_version_is_too_low() { - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Unknown line hash version: -1"); - LineHashVersion.valueOf(-1); + assertThatThrownBy(() -> LineHashVersion.valueOf(-1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown line hash version: -1"); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/GroupMembershipQueryTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/GroupMembershipQueryTest.java index 15156c004c7..70f0b2e6451 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/GroupMembershipQueryTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/GroupMembershipQueryTest.java @@ -19,16 +19,13 @@ */ package org.sonar.db.user; -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 GroupMembershipQueryTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void create_query() { @@ -47,11 +44,12 @@ public class GroupMembershipQueryTest { @Test public void fail_on_invalid_membership() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Membership is not valid (got unknwown). Availables values are [ANY, IN, OUT]"); - - GroupMembershipQuery.builder() - .membership("unknwown") - .build(); + assertThatThrownBy(() -> { + GroupMembershipQuery.builder() + .membership("unknwown") + .build(); + }) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Membership is not valid (got unknwown). Availables values are [ANY, IN, OUT]"); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java index 1280d8843e0..493b526e98d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java @@ -20,10 +20,10 @@ package org.sonar.db.user; import java.util.List; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.util.Uuids; @@ -32,14 +32,13 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.permission.GlobalPermission.ADMINISTER; public class RoleDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private DbSession dbSession = db.getSession(); private RoleDao underTest = db.getDbClient().roleDao(); @@ -59,21 +58,18 @@ public class RoleDaoTest { @Test public void selectComponentUuidsByPermissionAndUserId_throws_IAR_if_permission_USER_is_specified() { - expectUnsupportedUserAndCodeViewerPermission(); - - underTest.selectComponentUuidsByPermissionAndUserUuid(dbSession, UserRole.USER, Uuids.createFast()); + expectUnsupportedUserAndCodeViewerPermission(() -> underTest.selectComponentUuidsByPermissionAndUserUuid(dbSession, UserRole.USER, Uuids.createFast())); } @Test public void selectComponentUuidsByPermissionAndUserId_throws_IAR_if_permission_CODEVIEWER_is_specified() { - expectUnsupportedUserAndCodeViewerPermission(); - - underTest.selectComponentUuidsByPermissionAndUserUuid(dbSession, UserRole.CODEVIEWER, Uuids.createFast()); + expectUnsupportedUserAndCodeViewerPermission(() -> underTest.selectComponentUuidsByPermissionAndUserUuid(dbSession, UserRole.CODEVIEWER, Uuids.createFast())); } - private void expectUnsupportedUserAndCodeViewerPermission() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Permissions [user, codeviewer] are not supported by selectComponentUuidsByPermissionAndUserUuid"); + private void expectUnsupportedUserAndCodeViewerPermission(ThrowingCallable callback) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Permissions [user, codeviewer] are not supported by selectComponentUuidsByPermissionAndUserUuid"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDtoTest.java index dc10e100482..5df972a123a 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDtoTest.java @@ -21,16 +21,12 @@ package org.sonar.db.user; import java.util.Arrays; import java.util.Collections; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; public class UserDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void encode_scm_accounts() { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java index fdcda4f0ad3..b59790d0d04 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoWithPersisterTest.java @@ -33,7 +33,6 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; public class UserGroupDaoWithPersisterTest { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java index 43b4f48a66c..bc4ec3e7d14 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoTest.java @@ -22,7 +22,6 @@ package org.sonar.db.user; import java.util.Map; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -34,8 +33,6 @@ import static org.sonar.db.user.UserTokenTesting.newUserToken; public class UserTokenDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private DbSession dbSession = db.getSession(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java index 16eed5e1f01..92e6d71afa8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDaoWithPersisterTest.java @@ -21,7 +21,6 @@ package org.sonar.db.user; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; @@ -46,8 +45,6 @@ public class UserTokenDaoWithPersisterTest { @Rule public final DbTester db = DbTester.create(System2.INSTANCE, auditPersister); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final DbSession dbSession = db.getSession(); private final DbClient dbClient = db.getDbClient(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java index 84366cf7f90..7e2f1e44c51 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java @@ -19,21 +19,17 @@ */ package org.sonar.db.user; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class UserTokenDtoTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void fail_if_token_hash_is_longer_than_255_characters() { - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Token hash length (256) is longer than the maximum authorized (255)"); - - new UserTokenDto().setTokenHash(randomAlphabetic(256)); + assertThatThrownBy(() -> new UserTokenDto().setTokenHash(randomAlphabetic(256))) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Token hash length (256) is longer than the maximum authorized (255)"); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java index c1eee3e00f0..fb2b70c8a7b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.Optional; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -42,8 +41,6 @@ public class WebhookDeliveryDaoTest { @Rule public final DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final DbClient dbClient = dbTester.getDbClient(); private final DbSession dbSession = dbTester.getSession(); |