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-webserver-api/src/test | |
parent | 92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff) | |
download | sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip |
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'server/sonar-webserver-api/src/test')
19 files changed, 163 insertions, 262 deletions
diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/app/ProcessCommandWrapperImplTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/app/ProcessCommandWrapperImplTest.java index c9a74ee0272..9b582f7ad58 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/app/ProcessCommandWrapperImplTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/app/ProcessCommandWrapperImplTest.java @@ -24,12 +24,12 @@ import java.io.IOException; import java.util.Random; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.config.internal.MapSettings; import org.sonar.process.sharedmemoryfile.DefaultProcessCommands; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX; import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH; @@ -38,8 +38,6 @@ public class ProcessCommandWrapperImplTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private MapSettings settings = new MapSettings(); @@ -47,10 +45,9 @@ public class ProcessCommandWrapperImplTest { public void requestSQRestart_throws_IAE_if_process_index_property_not_set() { ProcessCommandWrapperImpl processCommandWrapper = new ProcessCommandWrapperImpl(settings.asConfig()); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Property process.index is not set"); - - processCommandWrapper.requestSQRestart(); + assertThatThrownBy(processCommandWrapper::requestSQRestart) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property process.index is not set"); } @Test @@ -58,10 +55,9 @@ public class ProcessCommandWrapperImplTest { settings.setProperty(PROPERTY_PROCESS_INDEX, 1); ProcessCommandWrapperImpl processCommandWrapper = new ProcessCommandWrapperImpl(settings.asConfig()); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Property process.sharedDir is not set"); - - processCommandWrapper.requestSQRestart(); + assertThatThrownBy(processCommandWrapper::requestSQRestart) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property process.sharedDir is not set"); } @Test @@ -83,10 +79,9 @@ public class ProcessCommandWrapperImplTest { settings.setProperty(PROPERTY_PROCESS_INDEX, 1); ProcessCommandWrapperImpl processCommandWrapper = new ProcessCommandWrapperImpl(settings.asConfig()); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Property process.sharedDir is not set"); - - processCommandWrapper.requestHardStop(); + assertThatThrownBy(processCommandWrapper::requestHardStop) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property process.sharedDir is not set"); } @Test @@ -108,20 +103,18 @@ public class ProcessCommandWrapperImplTest { settings.setProperty(PROPERTY_PROCESS_INDEX, 1); ProcessCommandWrapperImpl processCommandWrapper = new ProcessCommandWrapperImpl(settings.asConfig()); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Property process.sharedDir is not set"); - - processCommandWrapper.notifyOperational(); + assertThatThrownBy(processCommandWrapper::notifyOperational) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property process.sharedDir is not set"); } @Test public void notifyOperational_throws_IAE_if_process_index_property_not_set() { ProcessCommandWrapperImpl processCommandWrapper = new ProcessCommandWrapperImpl(settings.asConfig()); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Property process.index is not set"); - - processCommandWrapper.notifyOperational(); + assertThatThrownBy(processCommandWrapper::notifyOperational) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property process.index is not set"); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/BadRequestExceptionTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/BadRequestExceptionTest.java index b228271865a..ddc026e7b5a 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/BadRequestExceptionTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/BadRequestExceptionTest.java @@ -20,18 +20,13 @@ package org.sonar.server.exceptions; import java.util.Collections; -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; public class BadRequestExceptionTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void text_error() { BadRequestException exception = BadRequestException.create("error"); @@ -61,25 +56,22 @@ public class BadRequestExceptionTest { @Test public void fail_when_creating_exception_with_empty_list() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("At least one error message is required"); - - BadRequestException.create(Collections.emptyList()); + assertThatThrownBy(() -> BadRequestException.create(Collections.emptyList())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("At least one error message is required"); } @Test public void fail_when_creating_exception_with_one_empty_element() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Message cannot be empty"); - - BadRequestException.create(asList("error", "")); + assertThatThrownBy(() -> BadRequestException.create(asList("error", ""))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Message cannot be empty"); } @Test public void fail_when_creating_exception_with_one_null_element() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Message cannot be empty"); - - BadRequestException.create(asList("error", null)); + assertThatThrownBy(() -> BadRequestException.create(asList("error", null))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Message cannot be empty"); } } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/MessageTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/MessageTest.java index 52deb2c6192..bf3e75dbc1e 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/MessageTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/MessageTest.java @@ -19,17 +19,12 @@ */ package org.sonar.server.exceptions; -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 MessageTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void create_message() { Message message = Message.of("key1 %s", "param1"); @@ -44,16 +39,14 @@ public class MessageTest { @Test public void fail_when_message_is_null() { - expectedException.expect(IllegalArgumentException.class); - - Message.of(null); + assertThatThrownBy(() -> Message.of(null)) + .isInstanceOf(IllegalArgumentException.class); } @Test public void fail_when_message_is_empty() { - expectedException.expect(IllegalArgumentException.class); - - Message.of(""); + assertThatThrownBy(() -> Message.of("")) + .isInstanceOf(IllegalArgumentException.class); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java index c21aa93a676..d4d57ad60d0 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java @@ -26,7 +26,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentMatcher; import org.mockito.invocation.InvocationOnMock; @@ -45,6 +44,7 @@ import static org.apache.commons.io.FileUtils.copyFileToDirectory; import static org.apache.commons.io.FileUtils.touch; import static org.apache.commons.io.FilenameUtils.separatorsToUnix; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -61,8 +61,6 @@ public class PluginDownloaderTest { @Rule public TemporaryFolder testFolder = new TemporaryFolder(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private File downloadDir; private UpdateCenterMatrixFactory updateCenterMatrixFactory; private UpdateCenter updateCenter; @@ -178,9 +176,8 @@ public class PluginDownloaderTest { @Test public void fail_if_no_compatible_plugin_found() { - expectedException.expect(BadRequestException.class); - - pluginDownloader.download("foo", create("1.0")); + assertThatThrownBy(() -> pluginDownloader.download("foo", create("1.0"))) + .isInstanceOf(BadRequestException.class); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginJarLoaderTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginJarLoaderTest.java index 80686c8c23a..0c18988aa70 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginJarLoaderTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginJarLoaderTest.java @@ -34,7 +34,6 @@ import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.SonarRuntime; import org.sonar.api.utils.MessageException; @@ -45,13 +44,12 @@ import org.sonar.updatecenter.common.PluginManifest; import static java.util.jar.Attributes.Name.MANIFEST_VERSION; 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; public class PluginJarLoaderTest { @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule public TemporaryFolder temp = new TemporaryFolder(); @Rule public LogTester logs = new LogTester(); @@ -212,10 +210,11 @@ public class PluginJarLoaderTest { createJar(fs.getInstalledBundledPluginsDir(), "plugin1", "main", null); String dir = getDirName(fs.getInstalledExternalPluginsDir()); - expectedException.expectMessage("Found a plugin 'plugin1' in the directory '" + dir + "' with the same key [plugin1] as a built-in feature 'plugin1'. " - + "Please remove '" + new File(dir, jar.getName()) + "'"); - expectedException.expect(MessageException.class); - underTest.loadPlugins(); + + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessageContaining("Found a plugin 'plugin1' in the directory '" + dir + "' with the same key [plugin1] as a built-in feature 'plugin1'. " + + "Please remove '" + new File(dir, jar.getName()) + "'"); } @Test @@ -223,10 +222,11 @@ public class PluginJarLoaderTest { File downloaded = createJar(fs.getDownloadedPluginsDir(), "plugin1", "main", null); createJar(fs.getInstalledBundledPluginsDir(), "plugin1", "main", null); String dir = getDirName(fs.getDownloadedPluginsDir()); - expectedException.expectMessage("Fail to update plugin: plugin1. Built-in feature with same key already exists: plugin1. " - + "Move or delete plugin from " + dir + " directory"); - expectedException.expect(MessageException.class); - underTest.loadPlugins(); + + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessage("Fail to update plugin: plugin1. Built-in feature with same key already exists: plugin1. " + + "Move or delete plugin from " + dir + " directory"); } @Test @@ -235,11 +235,12 @@ public class PluginJarLoaderTest { File jar2 = createJar(fs.getInstalledExternalPluginsDir(), "plugin1", "main", null); String dir = getDirName(fs.getInstalledExternalPluginsDir()); - expectedException.expectMessage("Found two versions of the plugin 'plugin1' [plugin1] in the directory '" + dir + "'. Please remove "); - expectedException.expectMessage(jar2.getName()); - expectedException.expectMessage(jar1.getName()); - expectedException.expect(MessageException.class); - underTest.loadPlugins(); + + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessageContaining("Found two versions of the plugin 'plugin1' [plugin1] in the directory '" + dir + "'. Please remove ") + .hasMessageContaining(jar2.getName()) + .hasMessageContaining(jar1.getName()); } @Test @@ -247,20 +248,21 @@ public class PluginJarLoaderTest { File jar1 = createJar(fs.getInstalledBundledPluginsDir(), "plugin1", "main", null); File jar2 = createJar(fs.getInstalledBundledPluginsDir(), "plugin1", "main", null); String dir = getDirName(fs.getInstalledBundledPluginsDir()); - expectedException.expectMessage("Found two versions of the plugin plugin1 [plugin1] in the directory " + dir + ". Please remove one of "); - expectedException.expectMessage(jar1.getName()); - expectedException.expectMessage(jar2.getName()); - expectedException.expect(MessageException.class); - underTest.loadPlugins(); + + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessageContaining("Found two versions of the plugin plugin1 [plugin1] in the directory " + dir + ". Please remove one of ") + .hasMessageContaining(jar2.getName()) + .hasMessageContaining(jar1.getName()); } @Test public void fail_when_sqale_plugin_is_installed() throws Exception { copyTestPluginTo("fake-sqale-plugin", fs.getInstalledExternalPluginsDir()); - expectedException.expect(MessageException.class); - expectedException.expectMessage("The following plugin is no longer compatible with this version of SonarQube: 'sqale'"); - underTest.loadPlugins(); + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessage("The following plugin is no longer compatible with this version of SonarQube: 'sqale'"); } @Test @@ -269,27 +271,27 @@ public class PluginJarLoaderTest { createJar(fs.getInstalledExternalPluginsDir(), "scmgit", "main", null); createJar(fs.getInstalledExternalPluginsDir(), "scmsvn", "main", null); - expectedException.expect(MessageException.class); - expectedException.expectMessage("The following plugins are no longer compatible with this version of SonarQube: 'scmgit', 'scmsvn', 'sqale'"); - underTest.loadPlugins(); + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessage("The following plugins are no longer compatible with this version of SonarQube: 'scmgit', 'scmsvn', 'sqale'"); } @Test public void fail_when_report_is_installed() throws Exception { copyTestPluginTo("fake-report-plugin", fs.getInstalledExternalPluginsDir()); - expectedException.expect(MessageException.class); - expectedException.expectMessage("The following plugin is no longer compatible with this version of SonarQube: 'report'"); - underTest.loadPlugins(); + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessage("The following plugin is no longer compatible with this version of SonarQube: 'report'"); } @Test public void fail_when_views_is_installed() throws Exception { copyTestPluginTo("fake-views-plugin", fs.getInstalledExternalPluginsDir()); - expectedException.expect(MessageException.class); - expectedException.expectMessage("The following plugin is no longer compatible with this version of SonarQube: 'views'"); - underTest.loadPlugins(); + assertThatThrownBy(() -> underTest.loadPlugins()) + .isInstanceOf(MessageException.class) + .hasMessage("The following plugin is no longer compatible with this version of SonarQube: 'views'"); } @Test @@ -297,8 +299,8 @@ public class PluginJarLoaderTest { when(runtime.getApiVersion()).thenReturn(org.sonar.api.utils.Version.parse("1.0")); copyTestPluginTo("test-base-plugin", fs.getInstalledExternalPluginsDir()); - expectedException.expectMessage("Plugin Base Plugin [testbase] requires at least SonarQube 4.5.4"); - underTest.loadPlugins(); + assertThatThrownBy(() -> underTest.loadPlugins()) + .hasMessage("Plugin Base Plugin [testbase] requires at least SonarQube 4.5.4"); } private static File copyTestPluginTo(String testPluginName, File toDir) throws IOException { diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginUninstallerTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginUninstallerTest.java index 16e98ecdde8..7569e129509 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginUninstallerTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/PluginUninstallerTest.java @@ -28,7 +28,6 @@ import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.Plugin; import org.sonar.api.utils.log.LogTester; @@ -47,8 +46,6 @@ public class PluginUninstallerTest { @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @Rule - public ExpectedException exception = ExpectedException.none(); - @Rule public LogTester logs = new LogTester(); private File uninstallDir; diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java index 2eebec65185..461c5d9f255 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/ServerPluginRepositoryTest.java @@ -22,8 +22,6 @@ package org.sonar.server.plugins; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.stream.Collectors; - import org.junit.Assert; import org.junit.Test; import org.sonar.api.Plugin; diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/TestProjectUtils.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/TestProjectUtils.java index a9ace6379b0..8e1a7de9122 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/TestProjectUtils.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/TestProjectUtils.java @@ -19,10 +19,9 @@ */ package org.sonar.server.plugins; -import org.apache.commons.io.FileUtils; - import java.io.File; import java.util.Collection; +import org.apache.commons.io.FileUtils; public class TestProjectUtils { diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/UpdateCenterServlet.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/UpdateCenterServlet.java index 9b831ac5a6d..5480fe0eff1 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/UpdateCenterServlet.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/UpdateCenterServlet.java @@ -19,12 +19,12 @@ */ package org.sonar.server.plugins; +import java.io.IOException; +import java.util.Properties; import javax.servlet.GenericServlet; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; -import java.util.Properties; public class UpdateCenterServlet extends GenericServlet { diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/edition/EditionBundledPluginsTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/edition/EditionBundledPluginsTest.java index cc889cb76e7..1bb46ed6e30 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/edition/EditionBundledPluginsTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/plugins/edition/EditionBundledPluginsTest.java @@ -20,26 +20,22 @@ package org.sonar.server.plugins.edition; import java.util.Random; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.core.platform.PluginInfo; import org.sonar.updatecenter.common.Plugin; 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 EditionBundledPluginsTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final Random random = new Random(); @Test public void isEditionBundled_on_Plugin_fails_with_NPE_if_arg_is_null() { - expectedException.expect(NullPointerException.class); - - EditionBundledPlugins.isEditionBundled((Plugin) null); + assertThatThrownBy(() -> EditionBundledPlugins.isEditionBundled((Plugin) null)) + .isInstanceOf(NullPointerException.class); } @Test @@ -79,9 +75,8 @@ public class EditionBundledPluginsTest { @Test public void isEditionBundled_on_PluginInfo_fails_with_NPE_if_arg_is_null() { - expectedException.expect(NullPointerException.class); - - EditionBundledPlugins.isEditionBundled((PluginInfo) null); + assertThatThrownBy(() -> EditionBundledPlugins.isEditionBundled((PluginInfo) null)) + .isInstanceOf(NullPointerException.class); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectLifeCycleListenersImplTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectLifeCycleListenersImplTest.java index d2bf33214c6..74007de52ff 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectLifeCycleListenersImplTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/project/ProjectLifeCycleListenersImplTest.java @@ -26,9 +26,7 @@ import java.util.Collections; import java.util.Random; import java.util.Set; import java.util.stream.IntStream; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.InOrder; import org.mockito.Mockito; @@ -41,11 +39,10 @@ import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoInteractions; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @RunWith(DataProviderRunner.class) public class ProjectLifeCycleListenersImplTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private final ProjectLifeCycleListener listener1 = mock(ProjectLifeCycleListener.class); private final ProjectLifeCycleListener listener2 = mock(ProjectLifeCycleListener.class); @@ -56,18 +53,16 @@ public class ProjectLifeCycleListenersImplTest { @Test public void onProjectsDeleted_throws_NPE_if_set_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("projects can't be null"); - - underTestWithListeners.onProjectsDeleted(null); + assertThatThrownBy(() -> underTestWithListeners.onProjectsDeleted(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("projects can't be null"); } @Test public void onProjectsDeleted_throws_NPE_if_set_is_null_even_if_no_listeners() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("projects can't be null"); - - underTestNoListeners.onProjectsDeleted(null); + assertThatThrownBy(() -> underTestNoListeners.onProjectsDeleted(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("projects can't be null"); } @Test @@ -131,18 +126,16 @@ public class ProjectLifeCycleListenersImplTest { @Test public void onProjectBranchesDeleted_throws_NPE_if_set_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("projects can't be null"); - - underTestWithListeners.onProjectBranchesDeleted(null); + assertThatThrownBy(() -> underTestWithListeners.onProjectBranchesDeleted(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("projects can't be null"); } @Test public void onProjectBranchesDeleted_throws_NPE_if_set_is_null_even_if_no_listeners() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("projects can't be null"); - - underTestNoListeners.onProjectBranchesDeleted(null); + assertThatThrownBy(() -> underTestNoListeners.onProjectBranchesDeleted(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("projects can't be null"); } @Test @@ -215,18 +208,16 @@ public class ProjectLifeCycleListenersImplTest { @Test public void onProjectsRekeyed_throws_NPE_if_set_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("rekeyedProjects can't be null"); - - underTestWithListeners.onProjectsRekeyed(null); + assertThatThrownBy(() -> underTestWithListeners.onProjectsRekeyed(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("rekeyedProjects can't be null"); } @Test public void onProjectsRekeyed_throws_NPE_if_set_is_null_even_if_no_listeners() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("rekeyedProjects can't be null"); - - underTestNoListeners.onProjectsRekeyed(null); + assertThatThrownBy(() -> underTestNoListeners.onProjectsRekeyed(null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("rekeyedProjects can't be null"); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/project/RekeyedProjectTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/project/RekeyedProjectTest.java index 46578707c4a..e743cfc5ec6 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/project/RekeyedProjectTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/project/RekeyedProjectTest.java @@ -19,33 +19,28 @@ */ package org.sonar.server.project; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static java.util.Collections.emptyList; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; public class RekeyedProjectTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void constructor_throws_NPE_if_project_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("project can't be null"); - - new RekeyedProject(null, randomAlphanumeric(3)); + assertThatThrownBy(() -> new RekeyedProject(null, randomAlphanumeric(3))) + .isInstanceOf(NullPointerException.class) + .hasMessage("project can't be null"); } @Test public void constructor_throws_NPE_if_previousKey_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("previousKey can't be null"); - - new RekeyedProject(newRandomProject(), null); + assertThatThrownBy(() -> new RekeyedProject(newRandomProject(), null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("previousKey can't be null"); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventTest.java index 754f7f0e5e9..289352be624 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/qualitygate/changeevent/QGChangeEventTest.java @@ -22,9 +22,7 @@ package org.sonar.server.qualitygate.changeevent; import java.util.Optional; import java.util.Random; import java.util.function.Supplier; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mockito; import org.sonar.api.config.Configuration; import org.sonar.api.measures.Metric; @@ -35,10 +33,9 @@ import org.sonar.db.project.ProjectDto; import org.sonar.server.qualitygate.EvaluatedQualityGate; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class QGChangeEventTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private ProjectDto project = new ProjectDto() .setKey("foo") @@ -57,34 +54,30 @@ public class QGChangeEventTest { @Test public void constructor_fails_with_NPE_if_project_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("project can't be null"); - - new QGChangeEvent(null, branch, analysis, configuration, previousStatus, supplier); + assertThatThrownBy(() -> new QGChangeEvent(null, branch, analysis, configuration, previousStatus, supplier)) + .isInstanceOf(NullPointerException.class) + .hasMessage("project can't be null"); } @Test public void constructor_fails_with_NPE_if_branch_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("branch can't be null"); - - new QGChangeEvent(project, null, analysis, configuration, previousStatus, supplier); + assertThatThrownBy(() -> new QGChangeEvent(project, null, analysis, configuration, previousStatus, supplier)) + .isInstanceOf(NullPointerException.class) + .hasMessage("branch can't be null"); } @Test public void constructor_fails_with_NPE_if_analysis_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("analysis can't be null"); - - new QGChangeEvent(project, branch, null, configuration, previousStatus, supplier); + assertThatThrownBy(() -> new QGChangeEvent(project, branch, null, configuration, previousStatus, supplier)) + .isInstanceOf(NullPointerException.class) + .hasMessage("analysis can't be null"); } @Test public void constructor_fails_with_NPE_if_configuration_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("projectConfiguration can't be null"); - - new QGChangeEvent(project, branch, analysis, null, previousStatus, supplier); + assertThatThrownBy(() -> new QGChangeEvent(project, branch, analysis, null, previousStatus, supplier)) + .isInstanceOf(NullPointerException.class) + .hasMessage("projectConfiguration can't be null"); } @Test @@ -94,10 +87,9 @@ public class QGChangeEventTest { @Test public void constructor_fails_with_NPE_if_supplier_is_null() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("qualityGateSupplier can't be null"); - - new QGChangeEvent(project, branch, analysis, configuration, previousStatus, null); + assertThatThrownBy(() -> new QGChangeEvent(project, branch, analysis, configuration, previousStatus, null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("qualityGateSupplier can't be null"); } @Test diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java index 5a679dd8ed0..e2d7a340ece 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java @@ -19,18 +19,13 @@ */ package org.sonar.server.util; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.server.exceptions.BadRequestException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class BooleanTypeValidationTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private BooleanTypeValidation underTest = new BooleanTypeValidation(); @Test @@ -48,10 +43,9 @@ public class BooleanTypeValidationTest { @Test public void fail_on_invalid_boolean() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value 'abc' must be one of \"true\" or \"false\"."); - - underTest.validate("abc", null); + assertThatThrownBy(() -> underTest.validate("abc", null)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value 'abc' must be one of \"true\" or \"false\"."); } } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java index e82a86d1479..cc3b8c54d45 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java @@ -19,18 +19,13 @@ */ package org.sonar.server.util; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.server.exceptions.BadRequestException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class FloatTypeValidationTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private FloatTypeValidation validation = new FloatTypeValidation(); @Test @@ -47,10 +42,9 @@ public class FloatTypeValidationTest { @Test public void fail_on_invalid_float() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value 'abc' must be an floating point number."); - - validation.validate("abc", null); + assertThatThrownBy(() -> validation.validate("abc", null)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value 'abc' must be an floating point number."); } } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/GlobalLockManagerImplTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/GlobalLockManagerImplTest.java index 5312e2c0ba0..03b6e779c45 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/GlobalLockManagerImplTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/GlobalLockManagerImplTest.java @@ -24,10 +24,9 @@ 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.assertj.core.api.ThrowableAssert.ThrowingCallable; 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.db.DbClient; import org.sonar.db.DbSession; @@ -35,6 +34,7 @@ import org.sonar.db.property.InternalPropertiesDao; 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.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -45,10 +45,6 @@ import static org.sonar.server.util.GlobalLockManager.DEFAULT_LOCK_DURATION_SECO public class GlobalLockManagerImplTest { private static final int LOCK_NAME_MAX_LENGTH = 15; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private final DbClient dbClient = mock(DbClient.class); private final InternalPropertiesDao internalPropertiesDao = mock(InternalPropertiesDao.class); private final DbSession dbSession = mock(DbSession.class); @@ -64,18 +60,14 @@ public class GlobalLockManagerImplTest { public void tryLock_fails_with_IAE_if_name_is_empty() { String badLockName = ""; - expectBadLockNameIAE(badLockName); - - underTest.tryLock(badLockName); + expectBadLockNameIAE(() -> underTest.tryLock(badLockName), badLockName); } @Test public void tryLock_fails_with_IAE_if_name_length_is_16_or_more() { String badLockName = RandomStringUtils.random(LOCK_NAME_MAX_LENGTH + 1 + new Random().nextInt(96)); - expectBadLockNameIAE(badLockName); - - underTest.tryLock(badLockName); + expectBadLockNameIAE(() -> underTest.tryLock(badLockName), badLockName); } @Test @@ -105,9 +97,7 @@ public class GlobalLockManagerImplTest { public void tryLock_with_duration_fails_with_IAE_if_name_is_empty(int randomValidDuration) { String badLockName = ""; - expectBadLockNameIAE(badLockName); - - underTest.tryLock(badLockName, randomValidDuration); + expectBadLockNameIAE(() -> underTest.tryLock(badLockName, randomValidDuration), badLockName); } @Test @@ -123,17 +113,13 @@ public class GlobalLockManagerImplTest { public void tryLock_with_duration_fails_with_IAE_if_name_length_is_16_or_more(int randomValidDuration) { String badLockName = RandomStringUtils.random(LOCK_NAME_MAX_LENGTH + 1 + new Random().nextInt(65)); - expectBadLockNameIAE(badLockName); - - underTest.tryLock(badLockName, randomValidDuration); + expectBadLockNameIAE(() -> underTest.tryLock(badLockName, randomValidDuration), badLockName); } @Test @UseDataProvider("randomValidLockName") public void tryLock_with_duration_fails_with_IAE_if_duration_is_0(String randomValidLockName) { - expectBadDuration(0); - - underTest.tryLock(randomValidLockName, 0); + expectBadDuration(() -> underTest.tryLock(randomValidLockName, 0),0); } @Test @@ -141,9 +127,7 @@ public class GlobalLockManagerImplTest { public void tryLock_with_duration_fails_with_IAE_if_duration_is_less_than_0(String randomValidLockName) { int negativeDuration = -1 - new Random().nextInt(100); - expectBadDuration(negativeDuration); - - underTest.tryLock(randomValidLockName, negativeDuration); + expectBadDuration(() -> underTest.tryLock(randomValidLockName, negativeDuration), negativeDuration); } @Test @@ -176,14 +160,16 @@ public class GlobalLockManagerImplTest { }; } - private void expectBadLockNameIAE(String badLockName) { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("name's length must be > 0 and <= " + LOCK_NAME_MAX_LENGTH + ": '" + badLockName + "'"); + private void expectBadLockNameIAE(ThrowingCallable callback, String badLockName) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("name's length must be > 0 and <= " + LOCK_NAME_MAX_LENGTH + ": '" + badLockName + "'"); } - private void expectBadDuration(int badDuration) { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("duration must be > 0: " + badDuration); + private void expectBadDuration(ThrowingCallable callback, int badDuration) { + assertThatThrownBy(callback) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("duration must be > 0: " + badDuration); } } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java index 773f33f4c55..1044ea2288c 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java @@ -19,18 +19,13 @@ */ package org.sonar.server.util; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.server.exceptions.BadRequestException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class IntegerTypeValidationTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private IntegerTypeValidation validation = new IntegerTypeValidation(); @Test @@ -46,18 +41,17 @@ public class IntegerTypeValidationTest { @Test public void fail_on_string() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value 'abc' must be an integer."); - - validation.validate("abc", null); + assertThatThrownBy(() -> validation.validate("abc", null)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value 'abc' must be an integer."); } @Test public void fail_on_float() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value '10.1' must be an integer."); - validation.validate("10.1", null); + assertThatThrownBy(() -> validation.validate("10.1", null)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value '10.1' must be an integer."); } } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/LongTypeValidationTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/LongTypeValidationTest.java index d06a7918f7f..8c142509a78 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/LongTypeValidationTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/LongTypeValidationTest.java @@ -19,19 +19,16 @@ */ package org.sonar.server.util; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.PropertyType; import org.sonar.server.exceptions.BadRequestException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class LongTypeValidationTest { LongTypeValidation underTest = new LongTypeValidation(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); @Test public void key_is_long_type_name() { @@ -46,17 +43,15 @@ public class LongTypeValidationTest { @Test public void fail_when_float() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value '3.14' must be a long."); - - underTest.validate("3.14", null); + assertThatThrownBy(() -> underTest.validate("3.14", null)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value '3.14' must be a long."); } @Test public void fail_when_string() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value 'original string' must be a long."); - - underTest.validate("original string", null); + assertThatThrownBy(() -> underTest.validate("original string", null)) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value 'original string' must be a long."); } } diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java index 91f1fbd998f..cf98f9d4dca 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java @@ -19,19 +19,14 @@ */ package org.sonar.server.util; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.server.exceptions.BadRequestException; import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class StringListTypeValidationTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private StringListTypeValidation validation = new StringListTypeValidation(); @Test @@ -47,10 +42,9 @@ public class StringListTypeValidationTest { @Test public void fail_on_invalid_option() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Value 'abc' must be one of : a, b, c."); - - validation.validate("abc", newArrayList("a", "b", "c")); + assertThatThrownBy(() -> validation.validate("abc", newArrayList("a", "b", "c"))) + .isInstanceOf(BadRequestException.class) + .hasMessage("Value 'abc' must be one of : a, b, c."); } } |