import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.fs.InputFile; | import org.sonar.api.batch.fs.InputFile; | ||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
@Before | @Before |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.fs.InputFile; | import org.sonar.api.batch.fs.InputFile; | ||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
@Before | @Before |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.fs.InputFile; | import org.sonar.api.batch.fs.InputFile; | ||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
@Before | @Before |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.fs.InputFile; | import org.sonar.api.batch.fs.InputFile; | ||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
@Before | @Before |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.fs.InputFile; | import org.sonar.api.batch.fs.InputFile; | ||||
import org.sonar.api.batch.measure.MetricFinder; | |||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
import org.sonar.api.batch.measure.MetricFinder; | |||||
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; | import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; | ||||
import org.sonar.api.batch.sensor.internal.SensorContextTester; | import org.sonar.api.batch.sensor.internal.SensorContextTester; | ||||
import org.sonar.api.measures.CoreMetrics; | import org.sonar.api.measures.CoreMetrics; | ||||
import org.sonar.api.measures.Metric; | import org.sonar.api.measures.Metric; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
private MetricFinder metricFinder; | private MetricFinder metricFinder; | ||||
InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build(); | InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build(); | ||||
context.fileSystem().add(inputFile); | context.fileSystem().add(inputFile); | ||||
thrown.expect(IllegalStateException.class); | |||||
sensor.execute(context); | |||||
assertThatThrownBy(() -> sensor.execute(context)) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
} | } |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.fs.InputFile; | import org.sonar.api.batch.fs.InputFile; | ||||
import org.sonar.api.batch.fs.TextRange; | import org.sonar.api.batch.fs.TextRange; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
private InputFile inputFile; | private InputFile inputFile; |
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.sensor.highlighting.TypeOfText; | |||||
import org.sonar.api.batch.fs.internal.DefaultInputFile; | import org.sonar.api.batch.fs.internal.DefaultInputFile; | ||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
import org.sonar.api.batch.sensor.highlighting.TypeOfText; | |||||
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; | import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; | ||||
import org.sonar.api.batch.sensor.internal.SensorContextTester; | import org.sonar.api.batch.sensor.internal.SensorContextTester; | ||||
*/ | */ | ||||
package org.sonar.xoo.rule; | package org.sonar.xoo.rule; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.rule.RuleKey; | import org.sonar.api.rule.RuleKey; | ||||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; | import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; | ||||
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile; | import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile; | ||||
public class XooBuiltInQualityProfilesDefinitionTest { | public class XooBuiltInQualityProfilesDefinitionTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private XooBuiltInQualityProfilesDefinition underTest = new XooBuiltInQualityProfilesDefinition(); | private XooBuiltInQualityProfilesDefinition underTest = new XooBuiltInQualityProfilesDefinition(); | ||||
@Test | @Test |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import org.sonar.api.batch.scm.BlameCommand.BlameInput; | |||||
import org.sonar.api.batch.scm.BlameCommand.BlameOutput; | |||||
import org.sonar.api.batch.scm.BlameLine; | |||||
import org.sonar.api.batch.fs.internal.DefaultFileSystem; | import org.sonar.api.batch.fs.internal.DefaultFileSystem; | ||||
import org.sonar.api.batch.fs.internal.DefaultInputFile; | import org.sonar.api.batch.fs.internal.DefaultInputFile; | ||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | import org.sonar.api.batch.fs.internal.TestInputFileBuilder; | ||||
import org.sonar.api.batch.scm.BlameCommand.BlameInput; | |||||
import org.sonar.api.batch.scm.BlameCommand.BlameOutput; | |||||
import org.sonar.api.batch.scm.BlameLine; | |||||
import org.sonar.api.utils.DateUtils; | import org.sonar.api.utils.DateUtils; | ||||
import org.sonar.xoo.Xoo; | import org.sonar.xoo.Xoo; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private DefaultFileSystem fs; | private DefaultFileSystem fs; | ||||
private File baseDir; | private File baseDir; | ||||
private BlameInput input; | private BlameInput input; |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.TemporaryFolder; | import org.junit.rules.TemporaryFolder; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
@Rule | @Rule | ||||
public TemporaryFolder temp = new TemporaryFolder(); | public TemporaryFolder temp = new TemporaryFolder(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private File baseDir; | private File baseDir; | ||||
@Before | @Before |
*/ | */ | ||||
package org.sonar.alm.client.bitbucketserver; | package org.sonar.alm.client.bitbucketserver; | ||||
import java.util.Optional; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.*; | |||||
import java.util.Optional; | |||||
import static org.assertj.core.api.Assertions.assertThat; | |||||
public class BranchesListTest { | public class BranchesListTest { | ||||
import com.tngtech.java.junit.dataprovider.DataProvider; | import com.tngtech.java.junit.dataprovider.DataProvider; | ||||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.Random; | import java.util.Random; | ||||
import java.util.stream.Stream; | import java.util.stream.Stream; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.apache.commons.lang.ArrayUtils; | import org.apache.commons.lang.ArrayUtils; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class GithubAppConfigurationTest { | public class GithubAppConfigurationTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
@UseDataProvider("incompleteConfigurationParametersSonarQube") | @UseDataProvider("incompleteConfigurationParametersSonarQube") | ||||
public void getId_throws_ISE_if_config_is_incomplete(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) { | public void getId_throws_ISE_if_config_is_incomplete(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) { | ||||
GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint); | GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint); | ||||
expectConfigurationIncompleteISE(); | |||||
underTest.getId(); | |||||
assertThatThrownBy(underTest::getId) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessageContaining("Configuration is not complete"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getPrivateKeyFile_throws_ISE_if_config_is_incomplete(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) { | public void getPrivateKeyFile_throws_ISE_if_config_is_incomplete(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) { | ||||
GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint); | GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint); | ||||
expectConfigurationIncompleteISE(); | |||||
underTest.getPrivateKey(); | |||||
assertThatThrownBy(underTest::getPrivateKey) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessageContaining("Configuration is not complete"); | |||||
} | } | ||||
@DataProvider | @DataProvider | ||||
assertThat(underTest.hashCode()).isNotEqualTo(new GithubAppConfiguration(applicationId, privateKey, apiEndpoint)); | assertThat(underTest.hashCode()).isNotEqualTo(new GithubAppConfiguration(applicationId, privateKey, apiEndpoint)); | ||||
} | } | ||||
private void expectConfigurationIncompleteISE() { | |||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Configuration is not complete"); | |||||
} | |||||
private GithubAppConfiguration newValidConfiguration(long applicationId) { | private GithubAppConfiguration newValidConfiguration(long applicationId) { | ||||
return new GithubAppConfiguration(applicationId, randomAlphabetic(6), randomAlphabetic(6)); | return new GithubAppConfiguration(applicationId, randomAlphabetic(6), randomAlphabetic(6)); | ||||
} | } |
package org.sonar.alm.client.github.security; | package org.sonar.alm.client.github.security; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.sonar.alm.client.github.security.AppToken; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
package org.sonar.alm.client.github.security; | package org.sonar.alm.client.github.security; | ||||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.security.spec.InvalidKeySpecException; | import java.security.spec.InvalidKeySpecException; | ||||
import java.time.Clock; | import java.time.Clock; | ||||
import java.time.Instant; | import java.time.Instant; | ||||
import java.time.ZoneId; | import java.time.ZoneId; | ||||
import java.util.Random; | import java.util.Random; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.alm.client.github.config.GithubAppConfiguration; | import org.sonar.alm.client.github.config.GithubAppConfiguration; |
*/ | */ | ||||
package org.sonar.auth.bitbucket; | package org.sonar.auth.bitbucket; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.api.server.authentication.OAuth2IdentityProvider; | import org.sonar.api.server.authentication.OAuth2IdentityProvider; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.verify; | import static org.mockito.Mockito.verify; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
public class BitbucketIdentityProviderTest { | public class BitbucketIdentityProviderTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private final MapSettings settings = new MapSettings(); | private final MapSettings settings = new MapSettings(); | ||||
private final BitbucketSettings bitbucketSettings = new BitbucketSettings(settings.asConfig()); | private final BitbucketSettings bitbucketSettings = new BitbucketSettings(settings.asConfig()); | ||||
enableBitbucketAuthentication(false); | enableBitbucketAuthentication(false); | ||||
OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class); | OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class); | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Bitbucket authentication is disabled"); | |||||
underTest.init(context); | |||||
assertThatThrownBy(() -> underTest.init(context)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Bitbucket authentication is disabled"); | |||||
} | } | ||||
private void enableBitbucketAuthentication(boolean enabled) { | private void enableBitbucketAuthentication(boolean enabled) { |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static java.lang.String.format; | import static java.lang.String.format; | ||||
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; | import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.sonar.auth.OAuthRestClient.executePaginatedRequest; | import static org.sonar.auth.OAuthRestClient.executePaginatedRequest; | ||||
import static org.sonar.auth.OAuthRestClient.executeRequest; | import static org.sonar.auth.OAuthRestClient.executeRequest; | ||||
public class OAuthRestClientTest { | public class OAuthRestClientTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public MockWebServer mockWebServer = new MockWebServer(); | public MockWebServer mockWebServer = new MockWebServer(); | ||||
public void fail_to_execute_request() throws IOException { | public void fail_to_execute_request() throws IOException { | ||||
mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!")); | mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!")); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage(format("Fail to execute request '%s/test'. HTTP code: 404, response: Error!", serverUrl)); | |||||
executeRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken); | |||||
assertThatThrownBy(() -> executeRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage(format("Fail to execute request '%s/test'. HTTP code: 404, response: Error!", serverUrl)); | |||||
} | } | ||||
@Test | @Test | ||||
.setBody("A")); | .setBody("A")); | ||||
mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!")); | mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("Error!")); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage(format("Fail to execute request '%s/test?per_page=100&page=2'. HTTP code: 404, response: Error!", serverUrl)); | |||||
executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList); | |||||
assertThatThrownBy(() -> executePaginatedRequest(serverUrl + "/test", oAuth20Service, auth2AccessToken, Arrays::asList)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage(format("Fail to execute request '%s/test?per_page=100&page=2'. HTTP code: 404, response: Error!", serverUrl)); | |||||
} | } | ||||
private class TestAPI extends DefaultApi20 { | private class TestAPI extends DefaultApi20 { |
*/ | */ | ||||
package org.sonar.auth.github; | package org.sonar.auth.github; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.api.server.authentication.OAuth2IdentityProvider; | import org.sonar.api.server.authentication.OAuth2IdentityProvider; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.verify; | import static org.mockito.Mockito.verify; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
public class GitHubIdentityProviderTest { | public class GitHubIdentityProviderTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private MapSettings settings = new MapSettings(); | private MapSettings settings = new MapSettings(); | ||||
private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig()); | private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig()); | ||||
setSettings(false); | setSettings(false); | ||||
OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class); | OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class); | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("GitHub authentication is disabled"); | |||||
underTest.init(context); | |||||
assertThatThrownBy(() -> underTest.init(context)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("GitHub authentication is disabled"); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.auth.github; | package org.sonar.auth.github; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.config.PropertyDefinitions; | import org.sonar.api.config.PropertyDefinitions; | ||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.api.server.authentication.UserIdentity; | import org.sonar.api.server.authentication.UserIdentity; | ||||
public class UserIdentityFactoryImplTest { | public class UserIdentityFactoryImplTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, GitHubSettings.definitions())); | private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, GitHubSettings.definitions())); | ||||
private UserIdentityFactoryImpl underTest = new UserIdentityFactoryImpl(); | private UserIdentityFactoryImpl underTest = new UserIdentityFactoryImpl(); |
package org.sonar.auth.gitlab; | package org.sonar.auth.gitlab; | ||||
import org.assertj.core.api.Assertions; | import org.assertj.core.api.Assertions; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.server.authentication.Display; | import org.sonar.api.server.authentication.Display; | ||||
import org.sonar.api.server.authentication.OAuth2IdentityProvider; | import org.sonar.api.server.authentication.OAuth2IdentityProvider; | ||||
package org.sonar.auth.gitlab; | package org.sonar.auth.gitlab; | ||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.config.PropertyDefinitions; | import org.sonar.api.config.PropertyDefinitions; | ||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
public class GitLabSettingsTest { | public class GitLabSettingsTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private MapSettings settings; | private MapSettings settings; | ||||
private GitLabSettings config; | private GitLabSettings config; |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
public class CallbackHandlerImplTest { | public class CallbackHandlerImplTest { | ||||
assertThat(passwordCallback.getPassword()).isEqualTo("secret".toCharArray()); | assertThat(passwordCallback.getPassword()).isEqualTo("secret".toCharArray()); | ||||
} | } | ||||
@Test(expected = UnsupportedCallbackException.class) | |||||
public void unsupportedCallback() throws Exception { | |||||
new CallbackHandlerImpl("tester", "secret").handle(new Callback[] {mock(Callback.class)}); | |||||
@Test | |||||
public void unsupportedCallback() { | |||||
assertThatThrownBy(() -> { | |||||
new CallbackHandlerImpl("tester", "secret").handle(new Callback[] {mock(Callback.class)}); | |||||
}) | |||||
.isInstanceOf(UnsupportedCallbackException.class); | |||||
} | } | ||||
} | } |
import javax.naming.NamingException; | import javax.naming.NamingException; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.mockito.Mockito.doThrow; | import static org.mockito.Mockito.doThrow; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
ContextHelper.closeQuietly(context); | ContextHelper.closeQuietly(context); | ||||
} | } | ||||
@Test(expected = NamingException.class) | |||||
@Test | |||||
public void shouldNotSwallow() throws Exception { | public void shouldNotSwallow() throws Exception { | ||||
Context context = mock(Context.class); | Context context = mock(Context.class); | ||||
doThrow(new NamingException()).when(context).close(); | doThrow(new NamingException()).when(context).close(); | ||||
ContextHelper.close(context, false); | |||||
assertThatThrownBy(() -> ContextHelper.close(context, false)) | |||||
.isInstanceOf(NamingException.class); | |||||
} | } | ||||
@Test | @Test |
import javax.naming.directory.SearchControls; | import javax.naming.directory.SearchControls; | ||||
import org.junit.BeforeClass; | import org.junit.BeforeClass; | ||||
import org.junit.ClassRule; | import org.junit.ClassRule; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.auth.ldap.server.LdapServer; | import org.sonar.auth.ldap.server.LdapServer; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class LdapSearchTest { | public class LdapSearchTest { | ||||
@ClassRule | @ClassRule | ||||
public static LdapServer server = new LdapServer("/users.example.org.ldif"); | public static LdapServer server = new LdapServer("/users.example.org.ldif"); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private static Map<String, LdapContextFactory> contextFactories; | private static Map<String, LdapContextFactory> contextFactories; | ||||
@BeforeClass | @BeforeClass | ||||
assertThat(search.getReturningAttributes()).isEqualTo(new String[] {"objectClass"}); | assertThat(search.getReturningAttributes()).isEqualTo(new String[] {"objectClass"}); | ||||
assertThat(search.toString()).isEqualTo("LdapSearch{baseDn=dc=example,dc=org, scope=subtree, request=(objectClass={0}), parameters=[inetOrgPerson], attributes=[objectClass]}"); | assertThat(search.toString()).isEqualTo("LdapSearch{baseDn=dc=example,dc=org, scope=subtree, request=(objectClass={0}), parameters=[inetOrgPerson], attributes=[objectClass]}"); | ||||
assertThat(enumerationToArrayList(search.find()).size()).isEqualTo(3); | assertThat(enumerationToArrayList(search.find()).size()).isEqualTo(3); | ||||
thrown.expect(NamingException.class); | |||||
thrown.expectMessage("Non unique result for " + search.toString()); | |||||
search.findUnique(); | |||||
assertThatThrownBy(() -> search.findUnique()) | |||||
.isInstanceOf(NamingException.class) | |||||
.hasMessage("Non unique result for " + search.toString()); | |||||
} | } | ||||
@Test | @Test |
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.auth.ldap.LdapAutodiscovery.LdapSrvRecord; | import static org.sonar.auth.ldap.LdapAutodiscovery.LdapSrvRecord; | ||||
public class LdapSettingsManagerTest { | public class LdapSettingsManagerTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void shouldFailWhenNoLdapUrl() { | public void shouldFailWhenNoLdapUrl() { | ||||
MapSettings settings = generateMultipleLdapSettingsWithUserAndGroupMapping(); | MapSettings settings = generateMultipleLdapSettingsWithUserAndGroupMapping(); | ||||
settings.removeProperty("ldap.example.url"); | settings.removeProperty("ldap.example.url"); | ||||
LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()); | LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()); | ||||
thrown.expect(LdapException.class); | |||||
thrown.expectMessage("The property 'ldap.example.url' property is empty while it is mandatory."); | |||||
settingsManager.getContextFactories(); | |||||
assertThatThrownBy(settingsManager::getContextFactories) | |||||
.isInstanceOf(LdapException.class) | |||||
.hasMessage("The property 'ldap.example.url' property is empty while it is mandatory."); | |||||
} | } | ||||
@Test | @Test | ||||
settings.setProperty("ldap.url", "ldap://foo"); | settings.setProperty("ldap.url", "ldap://foo"); | ||||
LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()); | LdapSettingsManager settingsManager = new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()); | ||||
thrown.expect(LdapException.class); | |||||
thrown | |||||
.expectMessage( | |||||
"When defining multiple LDAP servers with the property 'ldap.servers', all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ..."); | |||||
settingsManager.getContextFactories(); | |||||
assertThatThrownBy(settingsManager::getContextFactories) | |||||
.isInstanceOf(LdapException.class) | |||||
.hasMessage("When defining multiple LDAP servers with the property 'ldap.servers', all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ..."); | |||||
} | } | ||||
@Test | @Test | ||||
LdapSettingsManager settingsManager = new LdapSettingsManager( | LdapSettingsManager settingsManager = new LdapSettingsManager( | ||||
generateAutodiscoverSettings().asConfig(), ldapAutodiscovery); | generateAutodiscoverSettings().asConfig(), ldapAutodiscovery); | ||||
thrown.expect(LdapException.class); | |||||
thrown.expectMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server."); | |||||
settingsManager.getContextFactories(); | |||||
assertThatThrownBy(settingsManager::getContextFactories) | |||||
.isInstanceOf(LdapException.class) | |||||
.hasMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server."); | |||||
} | } | ||||
/** | /** | ||||
LdapSettingsManager settingsManager = new LdapSettingsManager( | LdapSettingsManager settingsManager = new LdapSettingsManager( | ||||
new MapSettings().asConfig(), new LdapAutodiscovery()); | new MapSettings().asConfig(), new LdapAutodiscovery()); | ||||
thrown.expect(LdapException.class); | |||||
thrown.expectMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery."); | |||||
settingsManager.getContextFactories(); | |||||
assertThatThrownBy(settingsManager::getContextFactories) | |||||
.isInstanceOf(LdapException.class) | |||||
.hasMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery."); | |||||
} | } | ||||
private MapSettings generateMultipleLdapSettingsWithUserAndGroupMapping() { | private MapSettings generateMultipleLdapSettingsWithUserAndGroupMapping() { |
import com.tngtech.java.junit.dataprovider.DataProvider; | import com.tngtech.java.junit.dataprovider.DataProvider; | ||||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.config.PropertyDefinitions; | import org.sonar.api.config.PropertyDefinitions; | ||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class SamlSettingsTest { | public class SamlSettingsTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, SamlSettings.definitions())); | private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, SamlSettings.definitions())); | ||||
@Test | @Test | ||||
public void fail_to_get_provider_id_when_null() { | public void fail_to_get_provider_id_when_null() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Provider ID is missing"); | |||||
underTest.getProviderId(); | |||||
assertThatThrownBy(() -> underTest.getProviderId()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Provider ID is missing"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_to_get_login_url_when_null() { | public void fail_to_get_login_url_when_null() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Login URL is missing"); | |||||
underTest.getLoginUrl(); | |||||
assertThatThrownBy(() -> underTest.getLoginUrl()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Login URL is missing"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_to_get_certificate_when_null() { | public void fail_to_get_certificate_when_null() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Certificate is missing"); | |||||
underTest.getCertificate(); | |||||
assertThatThrownBy(() -> underTest.getCertificate()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Certificate is missing"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_to_get_user_login_attribute_when_null() { | public void fail_to_get_user_login_attribute_when_null() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("User login attribute is missing"); | |||||
underTest.getUserLogin(); | |||||
assertThatThrownBy(() -> underTest.getUserLogin()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("User login attribute is missing"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_to_get_user_name_attribute_when_null() { | public void fail_to_get_user_name_attribute_when_null() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("User name attribute is missing"); | |||||
underTest.getUserName(); | |||||
assertThatThrownBy(() -> underTest.getUserName()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("User name attribute is missing"); | |||||
} | } | ||||
private void initAllSettings() { | private void initAllSettings() { |
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.impl.utils.TestSystem2; | import org.sonar.api.impl.utils.TestSystem2; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import org.sonar.ce.queue.CeTaskSubmit.Component; | import org.sonar.ce.queue.CeTaskSubmit.Component; | ||||
import static java.util.Collections.emptyMap; | import static java.util.Collections.emptyMap; | ||||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.assertj.core.api.Assertions.catchThrowable; | import static org.assertj.core.api.Assertions.catchThrowable; | ||||
import static org.assertj.core.api.Assertions.tuple; | import static org.assertj.core.api.Assertions.tuple; | ||||
import static org.hamcrest.Matchers.startsWith; | |||||
import static org.sonar.ce.queue.CeQueue.SubmitOption.UNIQUE_QUEUE_PER_MAIN_COMPONENT; | import static org.sonar.ce.queue.CeQueue.SubmitOption.UNIQUE_QUEUE_PER_MAIN_COMPONENT; | ||||
public class CeQueueImplTest { | public class CeQueueImplTest { | ||||
private System2 system2 = new TestSystem2().setNow(NOW); | private System2 system2 = new TestSystem2().setNow(NOW); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public DbTester db = DbTester.create(system2); | public DbTester db = DbTester.create(system2); | ||||
submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(11))); | submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(11))); | ||||
CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false, false).get(); | CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false, false).get(); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage(startsWith("Task is in progress and can't be canceled")); | |||||
underTest.cancel(db.getSession(), ceQueueDto); | |||||
assertThatThrownBy(() -> underTest.cancel(db.getSession(), ceQueueDto)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessageStartingWith("Task is in progress and can't be canceled"); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.analysis; | package org.sonar.ce.task.projectanalysis.analysis; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class AnalysisImplTest { | public class AnalysisImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private static final long ID = 10; | private static final long ID = 10; | ||||
private static final String UUID = "uuid "; | private static final String UUID = "uuid "; | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_building_snapshot_without_uuid() { | public void fail_with_NPE_when_building_snapshot_without_uuid() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("uuid cannot be null"); | |||||
new Analysis.Builder() | |||||
.setCreatedAt(CREATED_AT) | |||||
.build(); | |||||
assertThatThrownBy(() -> new Analysis.Builder().setCreatedAt(CREATED_AT).build()) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("uuid cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_building_snapshot_without_created_at() { | public void fail_with_NPE_when_building_snapshot_without_created_at() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("createdAt cannot be null"); | |||||
new Analysis.Builder() | |||||
.setUuid(UUID) | |||||
.build(); | |||||
assertThatThrownBy(() -> new Analysis.Builder().setUuid(UUID).build()) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("createdAt cannot be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Optional; | import java.util.Optional; | ||||
import java.util.stream.Stream; | import java.util.stream.Stream; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl; | import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl; | ||||
import org.sonar.core.platform.PlatformEditionProvider; | import org.sonar.core.platform.PlatformEditionProvider; | ||||
import org.sonar.server.project.Project; | import org.sonar.server.project.Project; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.core.platform.EditionProvider.Edition; | import static org.sonar.core.platform.EditionProvider.Edition; | ||||
.build(); | .build(); | ||||
private static final long SOME_DATE = 10000000L; | private static final long SOME_DATE = 10000000L; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); | private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); | ||||
private final AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | private final AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
@Test | @Test | ||||
public void setUuid_throws_NPE_is_parameter_is_null() { | public void setUuid_throws_NPE_is_parameter_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("Analysis uuid can't be null"); | |||||
underTest.setUuid(null); | |||||
assertThatThrownBy(() -> underTest.setUuid(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Analysis uuid can't be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void setUuid_throws_ISE_if_called_twice() { | public void setUuid_throws_ISE_if_called_twice() { | ||||
underTest.setUuid("org1"); | underTest.setUuid("org1"); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Analysis uuid has already been set"); | |||||
underTest.setUuid("org1"); | |||||
assertThatThrownBy(() -> underTest.setUuid("org1")) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Analysis uuid has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() { | public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Analysis date has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getAnalysisDate(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getAnalysisDate()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Analysis date has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setAnalysisDate(SOME_DATE); | underTest.setAnalysisDate(SOME_DATE); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Analysis date has already been set"); | |||||
underTest.setAnalysisDate(SOME_DATE); | |||||
assertThatThrownBy(() -> underTest.setAnalysisDate(SOME_DATE)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Analysis date has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getForkDate_throws_ISE_when_holder_is_not_initialized() { | public void getForkDate_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Fork date has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getForkDate(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getForkDate()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Fork date has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setForkDate(SOME_DATE); | underTest.setForkDate(SOME_DATE); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Fork date has already been set"); | |||||
underTest.setForkDate(SOME_DATE); | |||||
assertThatThrownBy(() -> underTest.setForkDate(SOME_DATE)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Fork date has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void isFirstAnalysis_throws_ISE_when_base_project_snapshot_is_not_set() { | public void isFirstAnalysis_throws_ISE_when_base_project_snapshot_is_not_set() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Base project snapshot has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).isFirstAnalysis(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).isFirstAnalysis()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Base project snapshot has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
public void baseProjectSnapshot_throws_ISE_when_base_project_snapshot_is_not_set() { | public void baseProjectSnapshot_throws_ISE_when_base_project_snapshot_is_not_set() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Base project snapshot has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getBaseAnalysis(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getBaseAnalysis()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Base project snapshot has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setBaseAnalysis(baseProjectAnalysis); | underTest.setBaseAnalysis(baseProjectAnalysis); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Base project snapshot has already been set"); | |||||
underTest.setBaseAnalysis(baseProjectAnalysis); | |||||
assertThatThrownBy(() -> underTest.setBaseAnalysis(baseProjectAnalysis)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Base project snapshot has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initialized() { | public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Cross project duplication flag has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).isCrossProjectDuplicationEnabled(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).isCrossProjectDuplicationEnabled()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Cross project duplication flag has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setCrossProjectDuplicationEnabled(true); | underTest.setCrossProjectDuplicationEnabled(true); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Cross project duplication flag has already been set"); | |||||
underTest.setCrossProjectDuplicationEnabled(false); | |||||
assertThatThrownBy(() -> underTest.setCrossProjectDuplicationEnabled(false)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Cross project duplication flag has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getBranch_throws_ISE_when_holder_is_not_initialized() { | public void getBranch_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Branch has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getBranch(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getBranch()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Branch has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setBranch(new DefaultBranchImpl()); | underTest.setBranch(new DefaultBranchImpl()); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Branch has already been set"); | |||||
underTest.setBranch(new DefaultBranchImpl()); | |||||
assertThatThrownBy(() -> underTest.setBranch(new DefaultBranchImpl())) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Branch has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
when(branch.isMain()).thenReturn(false); | when(branch.isMain()).thenReturn(false); | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Branches and Pull Requests are not supported in Community Edition"); | |||||
underTest.setBranch(branch); | |||||
assertThatThrownBy(() -> underTest.setBranch(branch)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Branches and Pull Requests are not supported in Community Edition"); | |||||
} | } | ||||
@DataProvider | @DataProvider | ||||
@Test | @Test | ||||
public void getPullRequestId_throws_ISE_when_holder_is_not_initialized() { | public void getPullRequestId_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Pull request key has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getPullRequestKey(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getPullRequestKey()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Pull request key has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setPullRequestKey("pr-123"); | underTest.setPullRequestKey("pr-123"); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Pull request key has already been set"); | |||||
underTest.setPullRequestKey("pr-234"); | |||||
assertThatThrownBy(() -> underTest.setPullRequestKey("pr-234")) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Pull request key has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getProject_throws_ISE_when_holder_is_not_initialized() { | public void getProject_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Project has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getProject(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getProject()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Project has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setProject(Project.from(newPrivateProjectDto())); | underTest.setProject(Project.from(newPrivateProjectDto())); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Project has already been set"); | |||||
underTest.setProject(Project.from(newPrivateProjectDto())); | |||||
assertThatThrownBy(() -> underTest.setProject(Project.from(newPrivateProjectDto()))) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Project has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() { | public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Root component ref has not been set"); | |||||
new AnalysisMetadataHolderImpl(editionProvider).getRootComponentRef(); | |||||
assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getRootComponentRef()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Root component ref has not been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setRootComponentRef(10); | underTest.setRootComponentRef(10); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Root component ref has already been set"); | |||||
underTest.setRootComponentRef(9); | |||||
assertThatThrownBy(() -> underTest.setRootComponentRef(9)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Root component ref has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider); | ||||
underTest.setScmRevision("bd56dab"); | underTest.setScmRevision("bd56dab"); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("ScmRevision has already been set"); | |||||
underTest.setScmRevision("bd56dab"); | |||||
assertThatThrownBy(() -> underTest.setScmRevision("bd56dab")) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("ScmRevision has already been set"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void isBranch_throws_ISE_for_not_initialized_branch() { | public void isBranch_throws_ISE_for_not_initialized_branch() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Branch has not been set"); | |||||
underTest.isBranch(); | |||||
assertThatThrownBy(underTest::isBranch) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Branch has not been set"); | |||||
} | } | ||||
} | } |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.api.measurecomputer; | package org.sonar.ce.task.projectanalysis.api.measurecomputer; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.ce.measure.Component; | import org.sonar.api.ce.measure.Component; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class ComponentImplTest { | public class ComponentImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void create_project() { | public void create_project() { | ||||
ComponentImpl component = new ComponentImpl("Project", Component.Type.PROJECT, null); | ComponentImpl component = new ComponentImpl("Project", Component.Type.PROJECT, null); | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_calling_get_file_attributes_on_not_file() { | public void fail_with_ISE_when_calling_get_file_attributes_on_not_file() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Only component of type FILE have a FileAttributes object"); | |||||
ComponentImpl component = new ComponentImpl("Project", Component.Type.PROJECT, null); | |||||
component.getFileAttributes(); | |||||
assertThatThrownBy(() -> { | |||||
ComponentImpl component = new ComponentImpl("Project", Component.Type.PROJECT, null); | |||||
component.getFileAttributes(); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Only component of type FILE have a FileAttributes object"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_trying_to_create_a_file_without_file_attributes() { | public void fail_with_IAE_when_trying_to_create_a_file_without_file_attributes() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("omponent of type FILE must have a FileAttributes object"); | |||||
new ComponentImpl("File", Component.Type.FILE, null); | |||||
assertThatThrownBy(() -> new ComponentImpl("File", Component.Type.FILE, null)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessageContaining("omponent of type FILE must have a FileAttributes object"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_trying_to_create_not_a_file_with_file_attributes() { | public void fail_with_IAE_when_trying_to_create_not_a_file_with_file_attributes() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only component of type FILE have a FileAttributes object"); | |||||
new ComponentImpl("Project", Component.Type.PROJECT, new ComponentImpl.FileAttributesImpl(null, true)); | |||||
assertThatThrownBy(() -> new ComponentImpl("Project", Component.Type.PROJECT, new ComponentImpl.FileAttributesImpl(null, true))) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only component of type FILE have a FileAttributes object"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_creating_component_without_key() { | public void fail_with_NPE_when_creating_component_without_key() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Key cannot be null"); | |||||
new ComponentImpl(null, Component.Type.PROJECT, null); | |||||
assertThatThrownBy(() -> new ComponentImpl(null, Component.Type.PROJECT, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Key cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_creating_component_without_type() { | public void fail_with_NPE_when_creating_component_without_type() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Type cannot be null"); | |||||
new ComponentImpl("Project", null, null); | |||||
assertThatThrownBy(() -> new ComponentImpl("Project", null, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Type cannot be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Optional; | import java.util.Optional; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.ce.measure.Component; | import org.sonar.api.ce.measure.Component; | ||||
import org.sonar.api.ce.measure.MeasureComputer; | import org.sonar.api.ce.measure.MeasureComputer; | ||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.core.issue.DefaultIssue; | import org.sonar.core.issue.DefaultIssue; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY; | import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY; | ||||
public class MeasureComputerContextImplTest { | public class MeasureComputerContextImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private static final String INT_METRIC_KEY = "int_metric_key"; | private static final String INT_METRIC_KEY = "int_metric_key"; | ||||
private static final String DOUBLE_METRIC_KEY = "double_metric_key"; | private static final String DOUBLE_METRIC_KEY = "double_metric_key"; | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_get_measure_is_called_on_metric_not_in_input_list() { | public void fail_with_IAE_when_get_measure_is_called_on_metric_not_in_input_list() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only metrics in [another metric] can be used to load measures"); | |||||
MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt"); | |||||
underTest.getMeasure(NCLOC_KEY); | |||||
assertThatThrownBy(() -> { | |||||
MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt"); | |||||
underTest.getMeasure(NCLOC_KEY); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only metrics in [another metric] can be used to load measures"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_get_children_measures_is_called_on_metric_not_in_input_list() { | public void fail_with_IAE_when_get_children_measures_is_called_on_metric_not_in_input_list() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only metrics in [another metric] can be used to load measures"); | |||||
MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt"); | |||||
underTest.getChildrenMeasures(NCLOC_KEY); | |||||
assertThatThrownBy(() -> { | |||||
MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt"); | |||||
underTest.getChildrenMeasures(NCLOC_KEY); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only metrics in [another metric] can be used to load measures"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_add_measure_is_called_on_metric_not_in_output_list() { | public void fail_with_IAE_when_add_measure_is_called_on_metric_not_in_output_list() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only metrics in [int_metric_key] can be used to add measures. Metric 'double_metric_key' is not allowed."); | |||||
MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY); | |||||
underTest.addMeasure(DOUBLE_METRIC_KEY, 10); | |||||
assertThatThrownBy(() -> { | |||||
MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY); | |||||
underTest.addMeasure(DOUBLE_METRIC_KEY, 10); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only metrics in [int_metric_key] can be used to add measures. Metric 'double_metric_key' is not allowed."); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_unsupported_operation_when_adding_measure_that_already_exists() { | public void fail_with_unsupported_operation_when_adding_measure_that_already_exists() { | ||||
thrown.expect(UnsupportedOperationException.class); | |||||
thrown.expectMessage("A measure on metric 'int_metric_key' already exists on component 'fileKey'"); | |||||
measureRepository.addRawMeasure(FILE_1_REF, INT_METRIC_KEY, newMeasureBuilder().create(20)); | |||||
MeasureComputerContextImpl underTest = newContext(FILE_1_REF, NCLOC_KEY, INT_METRIC_KEY); | |||||
underTest.addMeasure(INT_METRIC_KEY, 10); | |||||
assertThatThrownBy(() -> { | |||||
measureRepository.addRawMeasure(FILE_1_REF, INT_METRIC_KEY, newMeasureBuilder().create(20)); | |||||
MeasureComputerContextImpl underTest = newContext(FILE_1_REF, NCLOC_KEY, INT_METRIC_KEY); | |||||
underTest.addMeasure(INT_METRIC_KEY, 10); | |||||
}) | |||||
.isInstanceOf(UnsupportedOperationException.class) | |||||
.hasMessage("A measure on metric 'int_metric_key' already exists on component 'fileKey'"); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.api.measurecomputer; | package org.sonar.ce.task.projectanalysis.api.measurecomputer; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.ce.measure.MeasureComputer; | import org.sonar.api.ce.measure.MeasureComputer; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class MeasureComputerDefinitionImplTest { | public class MeasureComputerDefinitionImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void build_measure_computer_definition() { | public void build_measure_computer_definition() { | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_null_input_metrics() { | public void fail_with_NPE_when_null_input_metrics() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Input metrics cannot be null"); | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics((String[]) null) | |||||
.setOutputMetrics("comment_density_1", "comment_density_2"); | |||||
assertThatThrownBy(() -> { | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics((String[]) null) | |||||
.setOutputMetrics("comment_density_1", "comment_density_2"); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Input metrics cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_one_input_metric_is_null() { | public void fail_with_NPE_when_one_input_metric_is_null() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Null metric is not allowed"); | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", null) | |||||
.setOutputMetrics("comment_density_1", "comment_density_2"); | |||||
assertThatThrownBy(() -> { | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", null) | |||||
.setOutputMetrics("comment_density_1", "comment_density_2"); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Null metric is not allowed"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_no_output_metrics() { | public void fail_with_NPE_when_no_output_metrics() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Output metrics cannot be null"); | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.build(); | |||||
assertThatThrownBy(() -> { | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.build(); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Output metrics cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_null_output_metrics() { | public void fail_with_NPE_when_null_output_metrics() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Output metrics cannot be null"); | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.setOutputMetrics((String[]) null); | |||||
assertThatThrownBy(() -> { | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.setOutputMetrics((String[]) null); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Output metrics cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_one_output_metric_is_null() { | public void fail_with_NPE_when_one_output_metric_is_null() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Null metric is not allowed"); | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.setOutputMetrics("comment_density_1", null); | |||||
assertThatThrownBy(() -> { | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.setOutputMetrics("comment_density_1", null); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Null metric is not allowed"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_IAE_with_empty_output_metrics() { | public void fail_with_IAE_with_empty_output_metrics() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("At least one output metric must be defined"); | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.setOutputMetrics(); | |||||
assertThatThrownBy(() -> { | |||||
new MeasureComputerDefinitionImpl.BuilderImpl() | |||||
.setInputMetrics("ncloc", "comment") | |||||
.setOutputMetrics(); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("At least one output metric must be defined"); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.api.measurecomputer; | package org.sonar.ce.task.projectanalysis.api.measurecomputer; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.measure.Measure; | import org.sonar.ce.task.projectanalysis.measure.Measure; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class MeasureImplTest { | public class MeasureImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void get_int_value() { | public void get_int_value() { | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_not_int_value() { | public void fail_with_ISE_when_not_int_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Value can not be converted to int because current value type is a DOUBLE"); | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1)); | |||||
measure.getIntValue(); | |||||
assertThatThrownBy(() -> { | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1)); | |||||
measure.getIntValue(); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Value can not be converted to int because current value type is a DOUBLE"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_not_double_value() { | public void fail_with_ISE_when_not_double_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Value can not be converted to double because current value type is a INT"); | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1)); | |||||
measure.getDoubleValue(); | |||||
assertThatThrownBy(() -> { | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1)); | |||||
measure.getDoubleValue(); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Value can not be converted to double because current value type is a INT"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_not_long_value() { | public void fail_with_ISE_when_not_long_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Value can not be converted to long because current value type is a STRING"); | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create("value")); | |||||
measure.getLongValue(); | |||||
assertThatThrownBy(() -> { | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create("value")); | |||||
measure.getLongValue(); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Value can not be converted to long because current value type is a STRING"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_not_string_value() { | public void fail_with_ISE_when_not_string_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Value can not be converted to string because current value type is a LONG"); | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1L)); | |||||
measure.getStringValue(); | |||||
assertThatThrownBy(() -> { | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1L)); | |||||
measure.getStringValue(); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Value can not be converted to string because current value type is a LONG"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_not_boolean_value() { | public void fail_with_ISE_when_not_boolean_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Value can not be converted to boolean because current value type is a DOUBLE"); | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1)); | |||||
measure.getBooleanValue(); | |||||
assertThatThrownBy(() -> { | |||||
MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1)); | |||||
measure.getBooleanValue(); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Value can not be converted to boolean because current value type is a DOUBLE"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_creating_measure_with_no_value() { | public void fail_with_ISE_when_creating_measure_with_no_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]"); | |||||
new MeasureImpl(Measure.newMeasureBuilder().createNoValue()); | |||||
assertThatThrownBy(() -> new MeasureImpl(Measure.newMeasureBuilder().createNoValue())) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_creating_measure_with_not_allowed_value() { | public void fail_with_ISE_when_creating_measure_with_not_allowed_value() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]"); | |||||
new MeasureImpl(Measure.newMeasureBuilder().create(Measure.Level.ERROR)); | |||||
assertThatThrownBy(() -> new MeasureImpl(Measure.newMeasureBuilder().create(Measure.Level.ERROR))) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_creating_measure_with_null_measure() { | public void fail_with_NPE_when_creating_measure_with_null_measure() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Measure couldn't be null"); | |||||
new MeasureImpl(null); | |||||
assertThatThrownBy(() -> new MeasureImpl(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Measure couldn't be null"); | |||||
} | } | ||||
} | } |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.api.posttask; | package org.sonar.ce.task.projectanalysis.api.posttask; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.ce.posttask.CeTask; | import org.sonar.api.ce.posttask.CeTask; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class CeTaskImplTest { | public class CeTaskImplTest { | ||||
private static final String SOME_ID = "some id"; | private static final String SOME_ID = "some id"; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_id_is_null() { | public void constructor_throws_NPE_if_id_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("id can not be null"); | |||||
new CeTaskImpl(null, CeTask.Status.SUCCESS); | |||||
assertThatThrownBy(() -> new CeTaskImpl(null, CeTask.Status.SUCCESS)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessageContaining("id can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_status_is_null() { | public void constructor_throws_NPE_if_status_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("status can not be null"); | |||||
new CeTaskImpl(SOME_ID, null); | |||||
assertThatThrownBy(() -> new CeTaskImpl(SOME_ID, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("status can not be null"); | |||||
} | } | ||||
@Test | @Test |
import com.tngtech.java.junit.dataprovider.DataProvider; | import com.tngtech.java.junit.dataprovider.DataProvider; | ||||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.ce.posttask.QualityGate; | import org.sonar.api.ce.posttask.QualityGate; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class ConditionImplTest { | public class ConditionImplTest { | ||||
private static final String ERROR_THRESHOLD = "error threshold"; | private static final String ERROR_THRESHOLD = "error threshold"; | ||||
private static final String VALUE = "value"; | private static final String VALUE = "value"; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private ConditionImpl.Builder builder = ConditionImpl.newBuilder() | private ConditionImpl.Builder builder = ConditionImpl.newBuilder() | ||||
.setStatus(QualityGate.EvaluationStatus.OK) | .setStatus(QualityGate.EvaluationStatus.OK) | ||||
public void build_throws_NPE_if_status_is_null() { | public void build_throws_NPE_if_status_is_null() { | ||||
builder.setStatus(null); | builder.setStatus(null); | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("status can not be null"); | |||||
builder.build(); | |||||
assertThatThrownBy(() -> builder.build()) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("status can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void build_throws_NPE_if_metricKey_is_null() { | public void build_throws_NPE_if_metricKey_is_null() { | ||||
builder.setMetricKey(null); | builder.setMetricKey(null); | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("metricKey can not be null"); | |||||
builder.build(); | |||||
assertThatThrownBy(() -> builder.build()) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("metricKey can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void build_throws_NPE_if_operator_is_null() { | public void build_throws_NPE_if_operator_is_null() { | ||||
builder.setOperator(null); | builder.setOperator(null); | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("operator can not be null"); | |||||
builder.build(); | |||||
assertThatThrownBy(() -> builder.build()) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("operator can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void build_throws_NPE_if_error_threshold_is_null() { | public void build_throws_NPE_if_error_threshold_is_null() { | ||||
builder.setErrorThreshold(null); | builder.setErrorThreshold(null); | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("errorThreshold can not be null"); | |||||
builder.build(); | |||||
assertThatThrownBy(() -> builder.build()) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("errorThreshold can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE).setValue(null); | builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE).setValue(null); | ||||
ConditionImpl condition = builder.build(); | ConditionImpl condition = builder.build(); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("There is no value when status is NO_VALUE"); | |||||
condition.getValue(); | |||||
assertThatThrownBy(condition::getValue) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("There is no value when status is NO_VALUE"); | |||||
} | } | ||||
@DataProvider | @DataProvider | ||||
builder.setStatus(status) | builder.setStatus(status) | ||||
.setValue(null); | .setValue(null); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("value can not be null when status is not NO_VALUE"); | |||||
builder.build(); | |||||
assertThatThrownBy(() -> builder.build()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("value can not be null when status is not NO_VALUE"); | |||||
} | } | ||||
@Test | @Test | ||||
public void build_throws_IAE_if_value_is_not_null_but_status_is_NO_VALUE() { | public void build_throws_IAE_if_value_is_not_null_but_status_is_NO_VALUE() { | ||||
builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE); | builder.setStatus(QualityGate.EvaluationStatus.NO_VALUE); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("value must be null when status is NO_VALUE"); | |||||
builder.build(); | |||||
assertThatThrownBy(() -> builder.build()) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("value must be null when status is NO_VALUE"); | |||||
} | } | ||||
@Test | @Test |
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.Map; | import java.util.Map; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.ce.posttask.QualityGate; | import org.sonar.api.ce.posttask.QualityGate; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import static com.google.common.collect.ImmutableMap.of; | import static com.google.common.collect.ImmutableMap.of; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
private static final String SOME_VALUE = "some value"; | private static final String SOME_VALUE = "some value"; | ||||
private static final ConditionStatus SOME_CONDITION_STATUS = ConditionStatus.create(ConditionStatus.EvaluationStatus.OK, SOME_VALUE); | private static final ConditionStatus SOME_CONDITION_STATUS = ConditionStatus.create(ConditionStatus.EvaluationStatus.OK, SOME_VALUE); | ||||
private static final Condition SOME_CONDITION = new Condition(newMetric(METRIC_KEY), Condition.Operator.LESS_THAN.getDbValue(), ERROR_THRESHOLD); | private static final Condition SOME_CONDITION = new Condition(newMetric(METRIC_KEY), Condition.Operator.LESS_THAN.getDbValue(), ERROR_THRESHOLD); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void apply_throws_NPE_if_Condition_argument_is_null() { | public void apply_throws_NPE_if_Condition_argument_is_null() { | ||||
ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS); | ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS); | ||||
expectedException.expect(NullPointerException.class); | |||||
underTest.apply(null); | |||||
assertThatThrownBy(() -> underTest.apply(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void apply_throws_ISE_if_there_is_no_ConditionStatus_for_Condition_argument() { | public void apply_throws_ISE_if_there_is_no_ConditionStatus_for_Condition_argument() { | ||||
ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS); | ConditionToCondition underTest = new ConditionToCondition(NO_STATUS_PER_CONDITIONS); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Missing ConditionStatus for condition on metric key " + METRIC_KEY); | |||||
underTest.apply(SOME_CONDITION); | |||||
assertThatThrownBy(() -> underTest.apply(SOME_CONDITION)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Missing ConditionStatus for condition on metric key " + METRIC_KEY); | |||||
} | } | ||||
@Test | @Test | ||||
QualityGate.Condition res = underTest.apply(otherCondition); | QualityGate.Condition res = underTest.apply(otherCondition); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("There is no value when status is NO_VALUE"); | |||||
res.getValue(); | |||||
assertThatThrownBy(res::getValue) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("There is no value when status is NO_VALUE"); | |||||
} | } | ||||
@DataProvider | @DataProvider |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.api.posttask; | package org.sonar.ce.task.projectanalysis.api.posttask; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class ProjectImplTest { | public class ProjectImplTest { | ||||
private static final String SOME_UUID = "some uuid"; | private static final String SOME_UUID = "some uuid"; | ||||
private static final String SOME_KEY = "some key"; | private static final String SOME_KEY = "some key"; | ||||
private static final String SOME_NAME = "some name"; | private static final String SOME_NAME = "some name"; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_uuid_is_null() { | public void constructor_throws_NPE_if_uuid_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("uuid can not be null"); | |||||
new ProjectImpl(null, SOME_KEY, SOME_NAME); | |||||
assertThatThrownBy(() -> new ProjectImpl(null, SOME_KEY, SOME_NAME)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("uuid can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_key_is_null() { | public void constructor_throws_NPE_if_key_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("key can not be null"); | |||||
new ProjectImpl(SOME_UUID, null, SOME_NAME); | |||||
assertThatThrownBy(() -> new ProjectImpl(SOME_UUID, null, SOME_NAME)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("key can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_name_is_null() { | public void constructor_throws_NPE_if_name_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("name can not be null"); | |||||
new ProjectImpl(SOME_UUID, SOME_KEY, null); | |||||
assertThatThrownBy(() -> new ProjectImpl(SOME_UUID, SOME_KEY, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("name can not be null"); | |||||
} | } | ||||
@Test | @Test |
import com.google.common.collect.ImmutableList; | import com.google.common.collect.ImmutableList; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.ce.posttask.QualityGate; | import org.sonar.api.ce.posttask.QualityGate; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
private static final String SOME_NAME = "some name"; | private static final String SOME_NAME = "some name"; | ||||
private static final QualityGate.Status SOME_STATUS = QualityGate.Status.OK; | private static final QualityGate.Status SOME_STATUS = QualityGate.Status.OK; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private QualityGate.Condition condition = mock(QualityGate.Condition.class); | private QualityGate.Condition condition = mock(QualityGate.Condition.class); | ||||
private QualityGateImpl underTest = new QualityGateImpl(SOME_ID, SOME_NAME, SOME_STATUS, ImmutableList.of(condition)); | private QualityGateImpl underTest = new QualityGateImpl(SOME_ID, SOME_NAME, SOME_STATUS, ImmutableList.of(condition)); | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_id_argument_is_null() { | public void constructor_throws_NPE_if_id_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("id can not be null"); | |||||
new QualityGateImpl(null, SOME_NAME, SOME_STATUS, Collections.emptyList()); | |||||
assertThatThrownBy(() -> new QualityGateImpl(null, SOME_NAME, SOME_STATUS, Collections.emptyList())) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("id can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_name_argument_is_null() { | public void constructor_throws_NPE_if_name_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("name can not be null"); | |||||
new QualityGateImpl(SOME_ID, null, SOME_STATUS, Collections.emptyList()); | |||||
assertThatThrownBy(() -> new QualityGateImpl(SOME_ID, null, SOME_STATUS, Collections.emptyList())) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("name can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_status_argument_is_null() { | public void constructor_throws_NPE_if_status_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("status can not be null"); | |||||
new QualityGateImpl(SOME_ID, SOME_NAME, null, Collections.emptyList()); | |||||
assertThatThrownBy(() -> new QualityGateImpl(SOME_ID, SOME_NAME, null, Collections.emptyList())) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("status can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_conditions_argument_is_null() { | public void constructor_throws_NPE_if_conditions_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("conditions can not be null"); | |||||
new QualityGateImpl(SOME_ID, SOME_NAME, SOME_STATUS, null); | |||||
assertThatThrownBy(() -> new QualityGateImpl(SOME_ID, SOME_NAME, SOME_STATUS, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("conditions can not be null"); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class BatchReportDirectoryHolderImplTest { | public class BatchReportDirectoryHolderImplTest { | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
public void getDirectory_throws_ISE_if_holder_is_empty() { | public void getDirectory_throws_ISE_if_holder_is_empty() { | ||||
new BatchReportDirectoryHolderImpl().getDirectory(); | |||||
assertThatThrownBy(() -> new BatchReportDirectoryHolderImpl().getDirectory()) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test |
import static com.google.common.collect.ImmutableList.of; | import static com.google.common.collect.ImmutableList.of; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class BatchReportReaderImplTest { | public class BatchReportReaderImplTest { | ||||
private static final int COMPONENT_REF = 1; | private static final int COMPONENT_REF = 1; | ||||
writer = new ScannerReportWriter(holder.getDirectory()); | writer = new ScannerReportWriter(holder.getDirectory()); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
public void readMetadata_throws_ISE_if_no_metadata() { | public void readMetadata_throws_ISE_if_no_metadata() { | ||||
underTest.readMetadata(); | |||||
assertThatThrownBy(() -> underTest.readMetadata()) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(underTest.readChangesets(COMPONENT_REF)).isNotSameAs(underTest.readChangesets(COMPONENT_REF)); | assertThat(underTest.readChangesets(COMPONENT_REF)).isNotSameAs(underTest.readChangesets(COMPONENT_REF)); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
public void readComponent_throws_ISE_if_file_does_not_exist() { | public void readComponent_throws_ISE_if_file_does_not_exist() { | ||||
underTest.readComponent(COMPONENT_REF); | |||||
assertThatThrownBy(() -> underTest.readComponent(COMPONENT_REF)) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.utils.MessageException; | import org.sonar.api.utils.MessageException; | ||||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | ||||
import org.sonar.ce.task.projectanalysis.analysis.Branch; | import org.sonar.ce.task.projectanalysis.analysis.Branch; | ||||
import org.sonar.scanner.protocol.output.ScannerReport; | import org.sonar.scanner.protocol.output.ScannerReport; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.times; | import static org.mockito.Mockito.times; | ||||
import static org.mockito.Mockito.verify; | import static org.mockito.Mockito.verify; | ||||
public class BranchLoaderTest { | public class BranchLoaderTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule(); | public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule(); | ||||
.setBranchName("bar") | .setBranchName("bar") | ||||
.build(); | .build(); | ||||
expectedException.expect(MessageException.class); | |||||
expectedException.expectMessage("Current edition does not support branch feature"); | |||||
new BranchLoader(metadataHolder).load(metadata); | |||||
assertThatThrownBy(() -> new BranchLoader(metadataHolder).load(metadata)) | |||||
.isInstanceOf(MessageException.class) | |||||
.hasMessage("Current edition does not support branch feature"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component.Status; | import org.sonar.ce.task.projectanalysis.component.Component.Status; | ||||
import static com.google.common.base.Strings.repeat; | import static com.google.common.base.Strings.repeat; | ||||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.assertj.core.api.Assertions.fail; | import static org.assertj.core.api.Assertions.fail; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | ||||
static final String KEY = "KEY"; | static final String KEY = "KEY"; | ||||
static final String UUID = "UUID"; | static final String UUID = "UUID"; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void verify_key_uuid_and_name() { | public void verify_key_uuid_and_name() { | ||||
@Test | @Test | ||||
public void builder_throws_NPE_if_component_arg_is_Null() { | public void builder_throws_NPE_if_component_arg_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(null); | |||||
assertThatThrownBy(() -> builder(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void builder_throws_NPE_if_status_arg_is_Null() { | public void builder_throws_NPE_if_status_arg_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(FILE).setStatus(null); | |||||
assertThatThrownBy(() -> builder(FILE).setStatus(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void builder_throws_NPE_if_status_is_Null() { | public void builder_throws_NPE_if_status_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(Component.Type.DIRECTORY) | |||||
.setName("DIR") | |||||
.setDbKey(KEY) | |||||
.setUuid(UUID) | |||||
.setReportAttributes(ReportAttributes.newBuilder(1).build()) | |||||
.build(); | |||||
assertThatThrownBy(() -> { | |||||
builder(Component.Type.DIRECTORY) | |||||
.setName("DIR") | |||||
.setDbKey(KEY) | |||||
.setUuid(UUID) | |||||
.setReportAttributes(ReportAttributes.newBuilder(1).build()) | |||||
.build(); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void set_key_throws_NPE_if_component_arg_is_Null() { | public void set_key_throws_NPE_if_component_arg_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(FILE).setUuid(null); | |||||
assertThatThrownBy(() -> builder(FILE).setUuid(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void set_uuid_throws_NPE_if_component_arg_is_Null() { | public void set_uuid_throws_NPE_if_component_arg_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(FILE).setDbKey(null); | |||||
assertThatThrownBy(() -> builder(FILE).setDbKey(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void build_without_key_throws_NPE_if_component_arg_is_Null() { | public void build_without_key_throws_NPE_if_component_arg_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(FILE).setUuid("ABCD").build(); | |||||
assertThatThrownBy(() -> builder(FILE).setUuid("ABCD").build()) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void build_without_uuid_throws_NPE_if_component_arg_is_Null() { | public void build_without_uuid_throws_NPE_if_component_arg_is_Null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
builder(FILE).setDbKey(KEY).build(); | |||||
assertThatThrownBy(() -> builder(FILE).setDbKey(KEY).build()) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import java.util.function.UnaryOperator; | import java.util.function.UnaryOperator; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.ExternalResource; | import org.junit.rules.ExternalResource; | ||||
import org.sonar.ce.task.projectanalysis.analysis.Branch; | import org.sonar.ce.task.projectanalysis.analysis.Branch; | ||||
import org.sonar.core.component.ComponentKeys; | import org.sonar.core.component.ComponentKeys; | ||||
import static com.google.common.base.Preconditions.checkArgument; | import static com.google.common.base.Preconditions.checkArgument; | ||||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.junit.Assert.fail; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
private static final ProjectAttributes SOME_PROJECT_ATTRIBUTES = new ProjectAttributes( | private static final ProjectAttributes SOME_PROJECT_ATTRIBUTES = new ProjectAttributes( | ||||
randomAlphabetic(20), new Random().nextBoolean() ? null : randomAlphabetic(12), "1def5123"); | randomAlphabetic(20), new Random().nextBoolean() ? null : randomAlphabetic(12), "1def5123"); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public ScannerComponentProvider scannerComponentProvider = new ScannerComponentProvider(); | public ScannerComponentProvider scannerComponentProvider = new ScannerComponentProvider(); | ||||
.setType(FILE) | .setType(FILE) | ||||
.setProjectRelativePath("src/js/Foo.js")); | .setProjectRelativePath("src/js/Foo.js")); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("File 'src/js/Foo.js' has no line"); | |||||
call(project); | |||||
assertThatThrownBy(() -> call(project)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("File 'src/js/Foo.js' has no line"); | |||||
} | } | ||||
@Test | @Test | ||||
.setProjectRelativePath("src/js/Foo.js") | .setProjectRelativePath("src/js/Foo.js") | ||||
.setLines(0)); | .setLines(0)); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("File 'src/js/Foo.js' has no line"); | |||||
call(project); | |||||
assertThatThrownBy(() -> call(project)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("File 'src/js/Foo.js' has no line"); | |||||
} | } | ||||
@Test | @Test | ||||
.setProjectRelativePath("src/js/Foo.js") | .setProjectRelativePath("src/js/Foo.js") | ||||
.setLines(-10)); | .setLines(-10)); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("File 'src/js/Foo.js' has no line"); | |||||
call(project); | |||||
assertThatThrownBy(() -> call(project)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("File 'src/js/Foo.js' has no line"); | |||||
} | } | ||||
private static class ScannerComponentProvider extends ExternalResource implements Function<Integer, ScannerReport.Component> { | private static class ScannerComponentProvider extends ExternalResource implements Function<Integer, ScannerReport.Component> { |
import java.util.Set; | import java.util.Set; | ||||
import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.component.Component.Type; | import org.sonar.ce.task.projectanalysis.component.Component.Type; | ||||
import static com.google.common.collect.FluentIterable.from; | import static com.google.common.collect.FluentIterable.from; | ||||
import static java.util.Arrays.asList; | import static java.util.Arrays.asList; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class CrawlerDepthLimitTest { | public class CrawlerDepthLimitTest { | ||||
private static final Set<Type> REPORT_TYPES = Arrays.stream(Type.values()).filter(Type::isReportType).collect(Collectors.toSet()); | private static final Set<Type> REPORT_TYPES = Arrays.stream(Type.values()).filter(Type::isReportType).collect(Collectors.toSet()); | ||||
private static final Set<Type> VIEWS_TYPES = Arrays.stream(Type.values()).filter(Type::isViewsType).collect(Collectors.toSet()); | private static final Set<Type> VIEWS_TYPES = Arrays.stream(Type.values()).filter(Type::isViewsType).collect(Collectors.toSet()); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void PROJECT_isSameAs_only_PROJECT_type() { | public void PROJECT_isSameAs_only_PROJECT_type() { | ||||
@Test | @Test | ||||
@UseDataProvider("viewsTypes") | @UseDataProvider("viewsTypes") | ||||
public void reportMaxDepth_throws_IAE_if_type_is_views(Type viewsType) { | public void reportMaxDepth_throws_IAE_if_type_is_views(Type viewsType) { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("A Report max depth must be a report type"); | |||||
CrawlerDepthLimit.reportMaxDepth(viewsType); | |||||
assertThatThrownBy(() -> CrawlerDepthLimit.reportMaxDepth(viewsType)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("A Report max depth must be a report type"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
@UseDataProvider("reportTypes") | @UseDataProvider("reportTypes") | ||||
public void withViewsMaxDepth_throws_IAE_if_type_is_report(Type reportType) { | public void withViewsMaxDepth_throws_IAE_if_type_is_report(Type reportType) { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("A Views max depth must be a views type"); | |||||
CrawlerDepthLimit.reportMaxDepth(reportType).withViewsMaxDepth(reportType); | |||||
assertThatThrownBy(() -> CrawlerDepthLimit.reportMaxDepth(reportType).withViewsMaxDepth(reportType)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("A Views max depth must be a views type"); | |||||
} | } | ||||
@DataProvider | @DataProvider |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.db.component.BranchDto; | import org.sonar.db.component.BranchDto; | ||||
import org.sonar.db.component.BranchType; | import org.sonar.db.component.BranchType; | ||||
import org.sonar.scanner.protocol.output.ScannerReport; | import org.sonar.scanner.protocol.output.ScannerReport; | ||||
private static final String PROJECT_KEY = "P"; | private static final String PROJECT_KEY = "P"; | ||||
private static final ScannerReport.Component FILE = ScannerReport.Component.newBuilder().setType(ComponentType.FILE).setProjectRelativePath("src/Foo.js").build(); | private static final ScannerReport.Component FILE = ScannerReport.Component.newBuilder().setType(ComponentType.FILE).setProjectRelativePath("src/Foo.js").build(); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void default_branch_represents_the_project() { | public void default_branch_represents_the_project() { |
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import com.google.common.collect.ImmutableSet; | import com.google.common.collect.ImmutableSet; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class DisabledComponentsHolderImplTest { | public class DisabledComponentsHolderImplTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
DisabledComponentsHolderImpl underTest = new DisabledComponentsHolderImpl(); | DisabledComponentsHolderImpl underTest = new DisabledComponentsHolderImpl(); | ||||
public void setUuids_fails_if_called_twice() { | public void setUuids_fails_if_called_twice() { | ||||
underTest.setUuids(ImmutableSet.of("U1", "U2")); | underTest.setUuids(ImmutableSet.of("U1", "U2")); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("UUIDs have already been set in repository"); | |||||
underTest.setUuids(ImmutableSet.of("U1", "U2")); | |||||
assertThatThrownBy(() -> underTest.setUuids(ImmutableSet.of("U1", "U2"))) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("UUIDs have already been set in repository"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getUuids_fails_if_not_initialized() { | public void getUuids_fails_if_not_initialized() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("UUIDs have not been set in repository"); | |||||
underTest.getUuids(); | |||||
assertThatThrownBy(() -> underTest.getUuids()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("UUIDs have not been set in repository"); | |||||
} | } | ||||
} | } |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class FileAttributesTest { | public class FileAttributesTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void create_production_file() { | public void create_production_file() { | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_lines_is_0() { | public void fail_with_IAE_when_lines_is_0() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Number of lines must be greater than zero"); | |||||
new FileAttributes(true, "java", 0); | |||||
assertThatThrownBy(() -> new FileAttributes(true, "java", 0)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Number of lines must be greater than zero"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_lines_is_less_than_0() { | public void fail_with_IAE_when_lines_is_less_than_0() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Number of lines must be greater than zero"); | |||||
new FileAttributes(true, "java", -10); | |||||
assertThatThrownBy(() -> new FileAttributes(true, "java", -10)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Number of lines must be greater than zero"); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.impl.utils.TestSystem2; | import org.sonar.api.impl.utils.TestSystem2; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.verify; | import static org.mockito.Mockito.verify; | ||||
import static org.mockito.Mockito.verifyNoInteractions; | |||||
import static org.mockito.Mockito.verifyNoMoreInteractions; | import static org.mockito.Mockito.verifyNoMoreInteractions; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | ||||
public DbTester dbTester = DbTester.create(System2.INSTANCE); | public DbTester dbTester = DbTester.create(System2.INSTANCE); | ||||
@Rule | @Rule | ||||
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); | public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); | ||||
@Rule | |||||
public ExpectedException exception = ExpectedException.none(); | |||||
public TestSystem2 system2 = new TestSystem2(); | public TestSystem2 system2 = new TestSystem2(); | ||||
private ProjectPersister underTest = new ProjectPersister(dbTester.getDbClient(), treeRootHolder, system2); | private ProjectPersister underTest = new ProjectPersister(dbTester.getDbClient(), treeRootHolder, system2); |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | ||||
private final DepthTraversalTypeAwareCrawler directoryCrawler = new DepthTraversalTypeAwareCrawler(directoryVisitor); | private final DepthTraversalTypeAwareCrawler directoryCrawler = new DepthTraversalTypeAwareCrawler(directoryVisitor); | ||||
private final DepthTraversalTypeAwareCrawler fileCrawler = new DepthTraversalTypeAwareCrawler(fileVisitor); | private final DepthTraversalTypeAwareCrawler fileCrawler = new DepthTraversalTypeAwareCrawler(fileVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
fileCrawler.visit(null); | |||||
assertThatThrownBy(() -> fileCrawler.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | ||||
private final DepthTraversalTypeAwareCrawler directoryCrawler = new DepthTraversalTypeAwareCrawler(directoryVisitor); | private final DepthTraversalTypeAwareCrawler directoryCrawler = new DepthTraversalTypeAwareCrawler(directoryVisitor); | ||||
private final DepthTraversalTypeAwareCrawler fileCrawler = new DepthTraversalTypeAwareCrawler(fileVisitor); | private final DepthTraversalTypeAwareCrawler fileCrawler = new DepthTraversalTypeAwareCrawler(fileVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
fileCrawler.visit(null); | |||||
assertThatThrownBy(() -> fileCrawler.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.any; | ||||
import static org.mockito.ArgumentMatchers.eq; | import static org.mockito.ArgumentMatchers.eq; | ||||
import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||
public class ReportVisitorsCrawlerTest { | public class ReportVisitorsCrawlerTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private static final Component FILE_5 = component(FILE, 5); | private static final Component FILE_5 = component(FILE, 5); | ||||
private static final Component DIRECTORY_4 = component(DIRECTORY, 4, FILE_5); | private static final Component DIRECTORY_4 = component(DIRECTORY, 4, FILE_5); | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_visitor_is_not_path_aware_or_type_aware() { | public void fail_with_IAE_when_visitor_is_not_path_aware_or_type_aware() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only TypeAwareVisitor and PathAwareVisitor can be used"); | |||||
ComponentVisitor componentVisitor = new ComponentVisitor() { | |||||
@Override | |||||
public Order getOrder() { | |||||
return PRE_ORDER; | |||||
} | |||||
assertThatThrownBy(() -> { | |||||
ComponentVisitor componentVisitor = new ComponentVisitor() { | |||||
@Override | |||||
public Order getOrder() { | |||||
return PRE_ORDER; | |||||
} | |||||
@Override | |||||
public CrawlerDepthLimit getMaxDepth() { | |||||
@Override | |||||
public CrawlerDepthLimit getMaxDepth() { | |||||
return CrawlerDepthLimit.FILE; | return CrawlerDepthLimit.FILE; | ||||
} | } | ||||
}; | |||||
new VisitorsCrawler(Arrays.asList(componentVisitor)); | |||||
}; | |||||
new VisitorsCrawler(Arrays.asList(componentVisitor)); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only TypeAwareVisitor and PathAwareVisitor can be used"); | |||||
} | } | ||||
private static Component component(final Component.Type type, final int ref, final Component... children) { | private static Component component(final Component.Type type, final int ref, final Component... children) { |
import org.junit.Test; | import org.junit.Test; | ||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||
import static org.mockito.Mockito.never; | import static org.mockito.Mockito.never; | ||||
import static org.mockito.Mockito.spy; | import static org.mockito.Mockito.spy; | ||||
}); | }); | ||||
private final InOrder inOrder = inOrder(spyProjectVisitor, spyDirectoryVisitor, spyFileVisitor); | private final InOrder inOrder = inOrder(spyProjectVisitor, spyDirectoryVisitor, spyFileVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
VisitorsCrawler underTest = newVisitorsCrawler(spyFileVisitor); | VisitorsCrawler underTest = newVisitorsCrawler(spyFileVisitor); | ||||
underTest.visit(null); | |||||
assertThatThrownBy(() -> underTest.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Test; | import org.junit.Test; | ||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||
import static org.mockito.Mockito.never; | import static org.mockito.Mockito.never; | ||||
import static org.mockito.Mockito.spy; | import static org.mockito.Mockito.spy; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | ||||
import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; | import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; | ||||
public class ReportVisitorsCrawlerWithPreOrderTypeAwareVisitorTest { | public class ReportVisitorsCrawlerWithPreOrderTypeAwareVisitorTest { | ||||
private static final Component FILE_5 = component(FILE, 5); | private static final Component FILE_5 = component(FILE, 5); | ||||
}); | }); | ||||
private final InOrder inOrder = inOrder(spyProjectVisitor, spyDirectoryVisitor, spyFileVisitor); | private final InOrder inOrder = inOrder(spyProjectVisitor, spyDirectoryVisitor, spyFileVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
VisitorsCrawler underTest = newVisitorsCrawler(spyFileVisitor); | VisitorsCrawler underTest = newVisitorsCrawler(spyFileVisitor); | ||||
underTest.visit(null); | |||||
assertThatThrownBy(() -> underTest.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; | ||||
.build()) | .build()) | ||||
.build(); | .build(); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private TreeRootHolderImpl underTest = new TreeRootHolderImpl(); | private TreeRootHolderImpl underTest = new TreeRootHolderImpl(); | ||||
@Test | @Test | ||||
public void setRoots_throws_NPE_if_root_is_null() { | public void setRoots_throws_NPE_if_root_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("root can not be null"); | |||||
underTest.setRoots(null, DUMB_PROJECT); | |||||
assertThatThrownBy(() -> underTest.setRoots(null, DUMB_PROJECT)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("root can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void setRoots_throws_NPE_if_reportRoot_is_null() { | public void setRoots_throws_NPE_if_reportRoot_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("root can not be null"); | |||||
underTest.setRoots(DUMB_PROJECT, null); | |||||
assertThatThrownBy(() -> underTest.setRoots(DUMB_PROJECT, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessageContaining("root can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void setRoot_throws_ISE_when_called_twice() { | public void setRoot_throws_ISE_when_called_twice() { | ||||
underTest.setRoots(DUMB_PROJECT, DUMB_PROJECT); | underTest.setRoots(DUMB_PROJECT, DUMB_PROJECT); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("root can not be set twice in holder"); | |||||
underTest.setRoots(null, DUMB_PROJECT); | |||||
assertThatThrownBy(() -> underTest.setRoots(null, DUMB_PROJECT)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("root can not be set twice in holder"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getRoot_throws_ISE_if_root_has_not_been_set_yet() { | public void getRoot_throws_ISE_if_root_has_not_been_set_yet() { | ||||
expectNotInitialized_ISE(); | |||||
underTest.getRoot(); | |||||
assertThatThrownBy(() -> underTest.getRoot()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Holder has not been initialized yet"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getComponentByRef_throws_ISE_if_root_has_not_been_set() { | public void getComponentByRef_throws_ISE_if_root_has_not_been_set() { | ||||
expectNotInitialized_ISE(); | |||||
underTest.getComponentByRef(12); | |||||
assertThatThrownBy(() -> underTest.getComponentByRef(12)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Holder has not been initialized yet"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getComponentByRef_throws_IAE_if_holder_does_not_contain_specified_component() { | public void getComponentByRef_throws_IAE_if_holder_does_not_contain_specified_component() { | ||||
underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT); | underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Component with ref '6' can't be found"); | |||||
underTest.getComponentByRef(6); | |||||
assertThatThrownBy(() -> underTest.getComponentByRef(6)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Component with ref '6' can't be found"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getComponentByRef_throws_IAE_if_holder_contains_View_tree() { | public void getComponentByRef_throws_IAE_if_holder_contains_View_tree() { | ||||
underTest.setRoots(SOME_VIEWS_COMPONENT_TREE, DUMB_PROJECT); | underTest.setRoots(SOME_VIEWS_COMPONENT_TREE, DUMB_PROJECT); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Component with ref '1' can't be found"); | |||||
underTest.getComponentByRef(1); | |||||
assertThatThrownBy(() -> underTest.getComponentByRef(1)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Component with ref '1' can't be found"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getSize_throws_ISE_if_not_initialized() { | public void getSize_throws_ISE_if_not_initialized() { | ||||
expectNotInitialized_ISE(); | |||||
underTest.getSize(); | |||||
assertThatThrownBy(() -> underTest.getSize()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Holder has not been initialized yet"); | |||||
} | } | ||||
@Test | @Test | ||||
underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT); | underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT); | ||||
assertThat(underTest.getSize()).isEqualTo(3); | assertThat(underTest.getSize()).isEqualTo(3); | ||||
} | } | ||||
private void expectNotInitialized_ISE() { | |||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Holder has not been initialized yet"); | |||||
} | |||||
} | } |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER; | import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER; | ||||
public class TypeAwareVisitorAdapterTest { | public class TypeAwareVisitorAdapterTest { | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void non_null_max_depth_fast_fail() { | public void non_null_max_depth_fast_fail() { | ||||
new TypeAwareVisitorAdapter(null, POST_ORDER) { | |||||
}; | |||||
assertThatThrownBy(() -> { | |||||
new TypeAwareVisitorAdapter(null, POST_ORDER) { | |||||
}; | |||||
}) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
} | } |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.resources.Qualifiers; | import org.sonar.api.resources.Qualifiers; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION; | import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION; | ||||
import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO; | import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO; | ||||
public class ViewAttributesTest { | public class ViewAttributesTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private ViewAttributes underTest; | private ViewAttributes underTest; | ||||
@Test | @Test | ||||
public void fail_if_unknown_view_qualifier() { | public void fail_if_unknown_view_qualifier() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Qualifier 'TRK' is not supported"); | |||||
ViewAttributes.Type.fromQualifier(Qualifiers.PROJECT); | |||||
assertThatThrownBy(() -> ViewAttributes.Type.fromQualifier(Qualifiers.PROJECT)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Qualifier 'TRK' is not supported"); | |||||
} | } | ||||
} | } |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; | ||||
private final DepthTraversalTypeAwareCrawler subViewCrawler = new DepthTraversalTypeAwareCrawler(subViewVisitor); | private final DepthTraversalTypeAwareCrawler subViewCrawler = new DepthTraversalTypeAwareCrawler(subViewVisitor); | ||||
private final DepthTraversalTypeAwareCrawler projectViewCrawler = new DepthTraversalTypeAwareCrawler(projectViewVisitor); | private final DepthTraversalTypeAwareCrawler projectViewCrawler = new DepthTraversalTypeAwareCrawler(projectViewVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
projectViewCrawler.visit(null); | |||||
assertThatThrownBy(() -> projectViewCrawler.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; | ||||
private final DepthTraversalTypeAwareCrawler subViewCrawler = new DepthTraversalTypeAwareCrawler(subViewVisitor); | private final DepthTraversalTypeAwareCrawler subViewCrawler = new DepthTraversalTypeAwareCrawler(subViewVisitor); | ||||
private final DepthTraversalTypeAwareCrawler projectViewCrawler = new DepthTraversalTypeAwareCrawler(projectViewVisitor); | private final DepthTraversalTypeAwareCrawler projectViewCrawler = new DepthTraversalTypeAwareCrawler(projectViewVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
projectViewCrawler.visit(null); | |||||
assertThatThrownBy(() -> projectViewCrawler.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
package org.sonar.ce.task.projectanalysis.component; | package org.sonar.ce.task.projectanalysis.component; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.any; | ||||
import static org.mockito.ArgumentMatchers.eq; | import static org.mockito.ArgumentMatchers.eq; | ||||
import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||
public class ViewsVisitorsCrawlerTest { | public class ViewsVisitorsCrawlerTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private static final Component PROJECT_VIEW_5 = component(PROJECT_VIEW, 5); | private static final Component PROJECT_VIEW_5 = component(PROJECT_VIEW, 5); | ||||
private static final Component SUBVIEW_4 = component(SUBVIEW, 4, PROJECT_VIEW_5); | private static final Component SUBVIEW_4 = component(SUBVIEW, 4, PROJECT_VIEW_5); | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_visitor_is_not_path_aware_or_type_aware() { | public void fail_with_IAE_when_visitor_is_not_path_aware_or_type_aware() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only TypeAwareVisitor and PathAwareVisitor can be used"); | |||||
ComponentVisitor componentVisitor = new ComponentVisitor() { | |||||
@Override | |||||
public Order getOrder() { | |||||
return PRE_ORDER; | |||||
} | |||||
@Override | |||||
public CrawlerDepthLimit getMaxDepth() { | |||||
return CrawlerDepthLimit.PROJECT_VIEW; | |||||
} | |||||
}; | |||||
new VisitorsCrawler(Arrays.asList(componentVisitor)); | |||||
assertThatThrownBy(() -> { | |||||
ComponentVisitor componentVisitor = new ComponentVisitor() { | |||||
@Override | |||||
public Order getOrder() { | |||||
return PRE_ORDER; | |||||
} | |||||
@Override | |||||
public CrawlerDepthLimit getMaxDepth() { | |||||
return CrawlerDepthLimit.PROJECT_VIEW; | |||||
} | |||||
}; | |||||
new VisitorsCrawler(Arrays.asList(componentVisitor)); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only TypeAwareVisitor and PathAwareVisitor can be used"); | |||||
} | } | ||||
@Test | @Test |
import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW; | ||||
import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER; | import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class ViewsVisitorsCrawlerWithPostOrderTypeAwareVisitorTest { | public class ViewsVisitorsCrawlerWithPostOrderTypeAwareVisitorTest { | ||||
private static final Component PROJECT_VIEW_5 = component(PROJECT_VIEW, 5); | private static final Component PROJECT_VIEW_5 = component(PROJECT_VIEW, 5); | ||||
}); | }); | ||||
private final InOrder inOrder = inOrder(spyViewVisitor, spySubViewVisitor, spyProjectViewVisitor); | private final InOrder inOrder = inOrder(spyViewVisitor, spySubViewVisitor, spyProjectViewVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
VisitorsCrawler underTest = newVisitorsCrawler(spyProjectViewVisitor); | VisitorsCrawler underTest = newVisitorsCrawler(spyProjectViewVisitor); | ||||
underTest.visit(null); | |||||
assertThatThrownBy(() -> underTest.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Test; | import org.junit.Test; | ||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||
import static org.mockito.Mockito.never; | import static org.mockito.Mockito.never; | ||||
import static org.mockito.Mockito.spy; | import static org.mockito.Mockito.spy; | ||||
}); | }); | ||||
private final InOrder inOrder = inOrder(spyViewVisitor, spySubViewVisitor, spyProjectViewVisitor); | private final InOrder inOrder = inOrder(spyViewVisitor, spySubViewVisitor, spyProjectViewVisitor); | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void visit_null_Component_throws_NPE() { | public void visit_null_Component_throws_NPE() { | ||||
VisitorsCrawler underTest = newVisitorsCrawler(spyProjectViewVisitor); | VisitorsCrawler underTest = newVisitorsCrawler(spyProjectViewVisitor); | ||||
underTest.visit(null); | |||||
assertThatThrownBy(() -> underTest.visit(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.duplication; | package org.sonar.ce.task.projectanalysis.duplication; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class CrossProjectDuplicateTest { | public class CrossProjectDuplicateTest { | ||||
private static final String FILE_KEY_1 = "file key 1"; | private static final String FILE_KEY_1 = "file key 1"; | ||||
private static final String FILE_KEY_2 = "file key 2"; | private static final String FILE_KEY_2 = "file key 2"; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructors_throws_NPE_if_fileKey_is_null() { | public void constructors_throws_NPE_if_fileKey_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("fileKey can not be null"); | |||||
new CrossProjectDuplicate(null, new TextBlock(1, 1)); | |||||
assertThatThrownBy(() -> new CrossProjectDuplicate(null, new TextBlock(1, 1))) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("fileKey can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructors_throws_NPE_if_textBlock_is_null() { | public void constructors_throws_NPE_if_textBlock_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("textBlock of duplicate can not be null"); | |||||
new CrossProjectDuplicate(FILE_KEY_1, null); | |||||
assertThatThrownBy(() -> new CrossProjectDuplicate(FILE_KEY_1, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("textBlock of duplicate can not be null"); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.utils.log.LogTester; | import org.sonar.api.utils.log.LogTester; | ||||
import org.sonar.api.utils.log.LoggerLevel; | import org.sonar.api.utils.log.LoggerLevel; | ||||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | ||||
import org.sonar.ce.task.projectanalysis.analysis.Branch; | import org.sonar.ce.task.projectanalysis.analysis.Branch; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
public class CrossProjectDuplicationStatusHolderImplTest { | public class CrossProjectDuplicationStatusHolderImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public LogTester logTester = new LogTester(); | public LogTester logTester = new LogTester(); | ||||
@Rule | @Rule | ||||
@Test | @Test | ||||
public void isEnabled_throws_ISE_when_start_have_not_been_called_before() { | public void isEnabled_throws_ISE_when_start_have_not_been_called_before() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Flag hasn't been initialized, the start() should have been called before"); | |||||
underTest.isEnabled(); | |||||
assertThatThrownBy(() -> underTest.isEnabled()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessageContaining("Flag hasn't been initialized, the start() should have been called before"); | |||||
} | } | ||||
private static Branch newBranch(boolean supportsCrossProjectCpd) { | private static Branch newBranch(boolean supportsCrossProjectCpd) { |
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import org.assertj.core.api.Assertions; | import org.assertj.core.api.Assertions; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray; | import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
private static final Component FILE_COMPONENT_2 = ReportComponent.builder(Component.Type.FILE, 2).build(); | private static final Component FILE_COMPONENT_2 = ReportComponent.builder(Component.Type.FILE, 2).build(); | ||||
private static final Duplication SOME_DUPLICATION = createDuplication(1, 2); | private static final Duplication SOME_DUPLICATION = createDuplication(1, 2); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private DuplicationRepository underTest = new DuplicationRepositoryImpl(); | private DuplicationRepository underTest = new DuplicationRepositoryImpl(); | ||||
@Test | @Test | ||||
public void getDuplications_throws_NPE_if_Component_argument_is_null() { | public void getDuplications_throws_NPE_if_Component_argument_is_null() { | ||||
expectFileArgumentNPE(); | |||||
underTest.getDuplications(null); | |||||
assertThatThrownBy(() -> underTest.getDuplications(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("file can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
@UseDataProvider("allComponentTypesButFile") | @UseDataProvider("allComponentTypesButFile") | ||||
public void getDuplications_throws_IAE_if_Component_type_is_not_FILE(Component.Type type) { | public void getDuplications_throws_IAE_if_Component_type_is_not_FILE(Component.Type type) { | ||||
expectFileTypeIAE(); | |||||
Component component = mockComponentGetType(type); | |||||
underTest.getDuplications(component); | |||||
assertThatThrownBy(() -> { | |||||
Component component = mockComponentGetType(type); | |||||
underTest.getDuplications(component); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("type of file must be FILE"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void add_throws_NPE_if_file_argument_is_null() { | public void add_throws_NPE_if_file_argument_is_null() { | ||||
expectFileArgumentNPE(); | |||||
underTest.add(null, SOME_DUPLICATION); | |||||
assertThatThrownBy(() -> underTest.add(null, SOME_DUPLICATION)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("file can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void addDuplication_inner_throws_NPE_if_duplication_argument_is_null() { | public void addDuplication_inner_throws_NPE_if_duplication_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("duplication can not be null"); | |||||
underTest.add(FILE_COMPONENT_1, null); | |||||
assertThatThrownBy(() -> underTest.add(FILE_COMPONENT_1, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("duplication can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
@UseDataProvider("allComponentTypesButFile") | @UseDataProvider("allComponentTypesButFile") | ||||
public void addDuplication_inner_throws_IAE_if_file_type_is_not_FILE(Component.Type type) { | public void addDuplication_inner_throws_IAE_if_file_type_is_not_FILE(Component.Type type) { | ||||
expectFileTypeIAE(); | |||||
Component component = mockComponentGetType(type); | |||||
underTest.add(component, SOME_DUPLICATION); | |||||
assertThatThrownBy(() -> { | |||||
Component component = mockComponentGetType(type); | |||||
underTest.add(component, SOME_DUPLICATION); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("type of file must be FILE"); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(underTest.getDuplications(component)).isEmpty(); | assertThat(underTest.getDuplications(component)).isEmpty(); | ||||
} | } | ||||
private void expectFileArgumentNPE() { | |||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("file can not be null"); | |||||
} | |||||
private void expectFileTypeIAE() { | |||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("type of file must be FILE"); | |||||
} | |||||
private Component mockComponentGetType(Component.Type type) { | private Component mockComponentGetType(Component.Type type) { | ||||
Component component = mock(Component.class); | Component component = mock(Component.class); | ||||
when(component.getType()).thenReturn(type); | when(component.getType()).thenReturn(type); |
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
public class DuplicationTest { | public class DuplicationTest { | ||||
private static final String FILE_KEY_1 = "1"; | private static final String FILE_KEY_1 = "1"; | ||||
private static final String FILE_KEY_2 = "2"; | private static final String FILE_KEY_2 = "2"; | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_original_is_null() { | public void constructor_throws_NPE_if_original_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("original TextBlock can not be null"); | |||||
new Duplication(null, Collections.emptyList()); | |||||
assertThatThrownBy(() -> new Duplication(null, Collections.emptyList())) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("original TextBlock can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_duplicates_is_null() { | public void constructor_throws_NPE_if_duplicates_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("duplicates can not be null"); | |||||
new Duplication(SOME_ORIGINAL_TEXTBLOCK, null); | |||||
assertThatThrownBy(() -> new Duplication(SOME_ORIGINAL_TEXTBLOCK, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("duplicates can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_IAE_if_duplicates_is_empty() { | public void constructor_throws_IAE_if_duplicates_is_empty() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("duplicates can not be empty"); | |||||
new Duplication(SOME_ORIGINAL_TEXTBLOCK, Collections.emptyList()); | |||||
assertThatThrownBy(() -> new Duplication(SOME_ORIGINAL_TEXTBLOCK, Collections.emptyList())) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("duplicates can not be empty"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_NPE_if_duplicates_contains_null() { | public void constructor_throws_NPE_if_duplicates_contains_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("duplicates can not contain null"); | |||||
new Duplication(SOME_ORIGINAL_TEXTBLOCK, Arrays.asList(mock(Duplicate.class), null, mock(Duplicate.class))); | |||||
assertThatThrownBy(() -> new Duplication(SOME_ORIGINAL_TEXTBLOCK, Arrays.asList(mock(Duplicate.class), null, mock(Duplicate.class)))) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("duplicates can not contain null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_IAE_if_duplicates_contains_InnerDuplicate_of_original() { | public void constructor_throws_IAE_if_duplicates_contains_InnerDuplicate_of_original() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("TextBlock of an InnerDuplicate can not be the original TextBlock"); | |||||
new Duplication(SOME_ORIGINAL_TEXTBLOCK, Arrays.asList(mock(Duplicate.class), new InnerDuplicate(SOME_ORIGINAL_TEXTBLOCK), mock(Duplicate.class))); | |||||
assertThatThrownBy(() -> new Duplication(SOME_ORIGINAL_TEXTBLOCK, Arrays.asList(mock(Duplicate.class), new InnerDuplicate(SOME_ORIGINAL_TEXTBLOCK), mock(Duplicate.class)))) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("TextBlock of an InnerDuplicate can not be the original TextBlock"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_IAE_when_attempting_to_sort_Duplicate_of_unkown_type() { | public void constructor_throws_IAE_when_attempting_to_sort_Duplicate_of_unkown_type() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Unsupported type of Duplicate " + MyDuplicate.class.getName()); | |||||
new Duplication(SOME_ORIGINAL_TEXTBLOCK, Arrays.asList(new MyDuplicate(), new MyDuplicate())); | |||||
assertThatThrownBy(() -> new Duplication(SOME_ORIGINAL_TEXTBLOCK, Arrays.asList(new MyDuplicate(), new MyDuplicate()))) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Unsupported type of Duplicate " + MyDuplicate.class.getName()); | |||||
} | } | ||||
private static final class MyDuplicate implements Duplicate { | private static final class MyDuplicate implements Duplicate { |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.duplication; | package org.sonar.ce.task.projectanalysis.duplication; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class InProjectDuplicateTest { | public class InProjectDuplicateTest { | ||||
private static final Component FILE_1 = ReportComponent.builder(Component.Type.FILE, 1).build(); | private static final Component FILE_1 = ReportComponent.builder(Component.Type.FILE, 1).build(); | ||||
private static final Component FILE_2 = ReportComponent.builder(Component.Type.FILE, 2).build(); | private static final Component FILE_2 = ReportComponent.builder(Component.Type.FILE, 2).build(); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructors_throws_NPE_if_file_is_null() { | public void constructors_throws_NPE_if_file_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("file can not be null"); | |||||
new InProjectDuplicate(null, new TextBlock(1, 1)); | |||||
assertThatThrownBy(() -> new InProjectDuplicate(null, new TextBlock(1, 1))) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("file can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructors_throws_NPE_if_textBlock_is_null() { | public void constructors_throws_NPE_if_textBlock_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("textBlock of duplicate can not be null"); | |||||
new InProjectDuplicate(FILE_1, null); | |||||
assertThatThrownBy(() -> new InProjectDuplicate(FILE_1, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("textBlock of duplicate can not be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructors_throws_IAE_if_type_of_file_argument_is_not_FILE() { | public void constructors_throws_IAE_if_type_of_file_argument_is_not_FILE() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("file must be of type FILE"); | |||||
new InProjectDuplicate(ReportComponent.builder(Component.Type.PROJECT, 1).build(), new TextBlock(1, 1)); | |||||
assertThatThrownBy(() -> new InProjectDuplicate(ReportComponent.builder(Component.Type.PROJECT, 1).build(), new TextBlock(1, 1))) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("file must be of type FILE"); | |||||
} | } | ||||
@Test | @Test |
*/ | */ | ||||
package org.sonar.ce.task.projectanalysis.duplication; | package org.sonar.ce.task.projectanalysis.duplication; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class InnerDuplicateTest { | public class InnerDuplicateTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructors_throws_NPE_if_textBlock_is_null() { | public void constructors_throws_NPE_if_textBlock_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("textBlock of duplicate can not be null"); | |||||
new InnerDuplicate(null); | |||||
assertThatThrownBy(() -> new InnerDuplicate(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("textBlock of duplicate can not be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Random; | import java.util.Random; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class TextBlockTest { | public class TextBlockTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void constructor_throws_IAE_if_start_is_0() { | public void constructor_throws_IAE_if_start_is_0() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("First line index must be >= 1"); | |||||
new TextBlock(0, 2); | |||||
assertThatThrownBy(() -> new TextBlock(0, 2)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("First line index must be >= 1"); | |||||
} | } | ||||
@Test | @Test | ||||
public void constructor_throws_IAE_if_end_is_less_than_start() { | public void constructor_throws_IAE_if_end_is_less_than_start() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Last line index must be >= first line index"); | |||||
new TextBlock(1, 0); | |||||
assertThatThrownBy(() -> new TextBlock(1, 0)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Last line index must be >= first line index"); | |||||
} | } | ||||
@Test | @Test |
import org.sonar.ce.task.projectanalysis.component.ViewsComponent; | import org.sonar.ce.task.projectanalysis.component.ViewsComponent; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.assertj.core.api.Assertions.fail; | import static org.assertj.core.api.Assertions.fail; | ||||
public class EventRepositoryImplTest { | public class EventRepositoryImplTest { | ||||
assertThat(underTest.getEvents(COMPONENT_2)).extracting("name").containsExactly(EVENT_2.getName()); | assertThat(underTest.getEvents(COMPONENT_2)).extracting("name").containsExactly(EVENT_2.getName()); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void add_throws_NPE_if_component_arg_is_null() { | public void add_throws_NPE_if_component_arg_is_null() { | ||||
underTest.add(null, EVENT_1); | |||||
assertThatThrownBy(() -> underTest.add(null, EVENT_1)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void add_throws_NPE_if_even_arg_is_null() { | public void add_throws_NPE_if_even_arg_is_null() { | ||||
underTest.add(COMPONENT_1, null); | |||||
assertThatThrownBy(() -> underTest.add(COMPONENT_1, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Test; | import org.junit.Test; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class EventTest { | public class EventTest { | ||||
private static final String SOME_DATA = "some data"; | private static final String SOME_DATA = "some data"; | ||||
private static final String SOME_DESCRIPTION = "some description"; | private static final String SOME_DESCRIPTION = "some description"; | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void createAlert_fail_fast_null_check_on_null_name() { | public void createAlert_fail_fast_null_check_on_null_name() { | ||||
Event.createAlert(null, SOME_DATA, SOME_DESCRIPTION); | |||||
assertThatThrownBy(() -> Event.createAlert(null, SOME_DATA, SOME_DESCRIPTION)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void createProfile_fail_fast_null_check_on_null_name() { | public void createProfile_fail_fast_null_check_on_null_name() { | ||||
Event.createProfile(null, SOME_DATA, SOME_DESCRIPTION); | |||||
assertThatThrownBy(() -> Event.createProfile(null, SOME_DATA, SOME_DESCRIPTION)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; | import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder; | ||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class AddedFileRepositoryImplTest { | public class AddedFileRepositoryImplTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class); | private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class); | ||||
private AddedFileRepositoryImpl underTest = new AddedFileRepositoryImpl(analysisMetadataHolder); | private AddedFileRepositoryImpl underTest = new AddedFileRepositoryImpl(analysisMetadataHolder); | ||||
@Test | @Test | ||||
public void isAdded_fails_with_NPE_if_component_is_null() { | public void isAdded_fails_with_NPE_if_component_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("component can't be null"); | |||||
underTest.isAdded(null); | |||||
assertThatThrownBy(() -> underTest.isAdded(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("component can't be null"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void register_fails_with_NPE_if_component_is_null() { | public void register_fails_with_NPE_if_component_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("component can't be null"); | |||||
underTest.register(null); | |||||
assertThatThrownBy(() -> underTest.register(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("component can't be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void register_fails_with_IAE_if_component_is_not_a_file(Component.Type anyTypeButFile) { | public void register_fails_with_IAE_if_component_is_not_a_file(Component.Type anyTypeButFile) { | ||||
Component component = newComponent(anyTypeButFile); | Component component = newComponent(anyTypeButFile); | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("component must be a file"); | |||||
underTest.register(component); | |||||
assertThatThrownBy(() -> underTest.register(component)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("component must be a file"); | |||||
} | } | ||||
@DataProvider | @DataProvider | ||||
when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true); | when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(true); | ||||
Component component = newComponent(Component.Type.FILE); | Component component = newComponent(Component.Type.FILE); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("No file can be registered on first analysis"); | |||||
underTest.register(component); | |||||
assertThatThrownBy(() -> underTest.register(component)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("No file can be registered on first analysis"); | |||||
} | } | ||||
private static Component newComponent(Component.Type type) { | private static Component newComponent(Component.Type type) { |
package org.sonar.ce.task.projectanalysis.filemove; | package org.sonar.ce.task.projectanalysis.filemove; | ||||
import java.util.Random; | import java.util.Random; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ViewsComponent; | import org.sonar.ce.task.projectanalysis.component.ViewsComponent; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.assertj.core.api.Assertions.fail; | import static org.assertj.core.api.Assertions.fail; | ||||
import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder; | import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder; | ||||
}; | }; | ||||
private static final MovedFilesRepository.OriginalFile SOME_ORIGINAL_FILE = new MovedFilesRepository.OriginalFile("uuid for 100", "key for 100"); | private static final MovedFilesRepository.OriginalFile SOME_ORIGINAL_FILE = new MovedFilesRepository.OriginalFile("uuid for 100", "key for 100"); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private MutableMovedFilesRepositoryImpl underTest = new MutableMovedFilesRepositoryImpl(); | private MutableMovedFilesRepositoryImpl underTest = new MutableMovedFilesRepositoryImpl(); | ||||
@Test | @Test | ||||
public void setOriginalFile_throws_NPE_when_file_is_null() { | public void setOriginalFile_throws_NPE_when_file_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("file can't be null"); | |||||
underTest.setOriginalFile(null, SOME_ORIGINAL_FILE); | |||||
assertThatThrownBy(() -> underTest.setOriginalFile(null, SOME_ORIGINAL_FILE)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("file can't be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void setOriginalFile_throws_NPE_when_originalFile_is_null() { | public void setOriginalFile_throws_NPE_when_originalFile_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("originalFile can't be null"); | |||||
underTest.setOriginalFile(SOME_FILE, null); | |||||
assertThatThrownBy(() -> underTest.setOriginalFile(SOME_FILE, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("originalFile can't be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void setOriginalFile_throws_ISE_if_settings_another_originalFile() { | public void setOriginalFile_throws_ISE_if_settings_another_originalFile() { | ||||
underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE); | underTest.setOriginalFile(SOME_FILE, SOME_ORIGINAL_FILE); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Original file OriginalFile{uuid='uuid for 100', key='key for 100'} " + | |||||
"already registered for file ReportComponent{ref=1, key='key_1', type=FILE}"); | |||||
underTest.setOriginalFile(SOME_FILE, new MovedFilesRepository.OriginalFile("uudi", "key")); | |||||
assertThatThrownBy(() -> underTest.setOriginalFile(SOME_FILE, new MovedFilesRepository.OriginalFile("uudi", "key"))) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessageContaining("Original file OriginalFile{uuid='uuid for 100', key='key for 100'} " + | |||||
"already registered for file ReportComponent{ref=1, key='key_1', type=FILE}"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getOriginalFile_throws_NPE_when_file_is_null() { | public void getOriginalFile_throws_NPE_when_file_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("file can't be null"); | |||||
underTest.getOriginalFile(null); | |||||
assertThatThrownBy(() -> underTest.getOriginalFile(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("file can't be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.List; | import java.util.List; | ||||
import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||
import java.util.stream.IntStream; | import java.util.stream.IntStream; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static java.util.Arrays.asList; | import static java.util.Arrays.asList; | ||||
import static java.util.Collections.emptyList; | import static java.util.Collections.emptyList; | ||||
public class SourceSimilarityImplTest { | public class SourceSimilarityImplTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private SourceSimilarityImpl underTest = new SourceSimilarityImpl(); | private SourceSimilarityImpl underTest = new SourceSimilarityImpl(); | ||||
package org.sonar.ce.task.projectanalysis.formula; | package org.sonar.ce.task.projectanalysis.formula; | ||||
import java.util.Optional; | import java.util.Optional; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import org.sonar.ce.task.projectanalysis.measure.Measure; | import org.sonar.ce.task.projectanalysis.measure.Measure; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.mockito.ArgumentMatchers.anyString; | import static org.mockito.ArgumentMatchers.anyString; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
CreateMeasureContext createMeasureContext = new DumbCreateMeasureContext( | CreateMeasureContext createMeasureContext = new DumbCreateMeasureContext( | ||||
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class)); | ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class)); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void fail_with_NPE_when_building_formula_without_output_metric() { | public void fail_with_NPE_when_building_formula_without_output_metric() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Output metric key cannot be null"); | |||||
Builder.newBuilder() | |||||
.setOutputMetricKey(null) | |||||
.setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY) | |||||
.setByMetricKey(FUNCTIONS_KEY) | |||||
.build(); | |||||
assertThatThrownBy(() -> { | |||||
Builder.newBuilder() | |||||
.setOutputMetricKey(null) | |||||
.setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY) | |||||
.setByMetricKey(FUNCTIONS_KEY) | |||||
.build(); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Output metric key cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_building_formula_without_main_metric() { | public void fail_with_NPE_when_building_formula_without_main_metric() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Main metric Key cannot be null"); | |||||
Builder.newBuilder() | |||||
.setOutputMetricKey(FUNCTION_COMPLEXITY_KEY) | |||||
.setMainMetricKey(null) | |||||
.setByMetricKey(FUNCTIONS_KEY) | |||||
.build(); | |||||
assertThatThrownBy(() -> { | |||||
Builder.newBuilder() | |||||
.setOutputMetricKey(FUNCTION_COMPLEXITY_KEY) | |||||
.setMainMetricKey(null) | |||||
.setByMetricKey(FUNCTIONS_KEY) | |||||
.build(); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Main metric Key cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_building_formula_without_by_metric() { | public void fail_with_NPE_when_building_formula_without_by_metric() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("By metric Key cannot be null"); | |||||
Builder.newBuilder() | |||||
.setOutputMetricKey(FUNCTION_COMPLEXITY_KEY) | |||||
.setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY) | |||||
.setByMetricKey(null) | |||||
.build(); | |||||
assertThatThrownBy(() -> { | |||||
Builder.newBuilder() | |||||
.setOutputMetricKey(FUNCTION_COMPLEXITY_KEY) | |||||
.setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY) | |||||
.setByMetricKey(null) | |||||
.build(); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("By metric Key cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_aggregate_from_component_and_context_with_not_numeric_measures() { | public void fail_with_IAE_when_aggregate_from_component_and_context_with_not_numeric_measures() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Measure of type 'STRING' are not supported"); | |||||
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter(); | |||||
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10L); | |||||
when(counterInitializationContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create("data"))); | |||||
counter.initialize(counterInitializationContext); | |||||
BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext); | |||||
assertThatThrownBy(() -> { | |||||
AverageFormula.AverageCounter counter = BASIC_AVERAGE_FORMULA.createNewCounter(); | |||||
addMeasure(COMPLEXITY_IN_FUNCTIONS_KEY, 10L); | |||||
when(counterInitializationContext.getMeasure(FUNCTIONS_KEY)).thenReturn(Optional.of(Measure.newMeasureBuilder().create("data"))); | |||||
counter.initialize(counterInitializationContext); | |||||
BASIC_AVERAGE_FORMULA.createMeasure(counter, createMeasureContext); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Measure of type 'STRING' are not supported"); | |||||
} | } | ||||
@Test | @Test |
package org.sonar.ce.task.projectanalysis.formula; | package org.sonar.ce.task.projectanalysis.formula; | ||||
import java.util.Optional; | import java.util.Optional; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import org.sonar.ce.task.projectanalysis.measure.Measure; | import org.sonar.ce.task.projectanalysis.measure.Measure; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY; | import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY; | ||||
private static final DistributionFormula BASIC_DISTRIBUTION_FORMULA = new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY); | private static final DistributionFormula BASIC_DISTRIBUTION_FORMULA = new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class); | CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class); | ||||
CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext( | CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext( | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_creating_counter_with_null_metric() { | public void fail_with_NPE_when_creating_counter_with_null_metric() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Metric key cannot be null"); | |||||
new DistributionFormula(null); | |||||
assertThatThrownBy(() -> new DistributionFormula(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Metric key cannot be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Optional; | import java.util.Optional; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import org.sonar.ce.task.projectanalysis.formula.SumFormula.IntSumFormula; | import org.sonar.ce.task.projectanalysis.formula.SumFormula.IntSumFormula; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.api.measures.CoreMetrics.LINES_KEY; | import static org.sonar.api.measures.CoreMetrics.LINES_KEY; | ||||
private static final IntSumFormula INT_SUM_FORMULA_NULL_DEFAULT_INPUT_VALUE = createIntSumFormula(LINES_KEY, null); | private static final IntSumFormula INT_SUM_FORMULA_NULL_DEFAULT_INPUT_VALUE = createIntSumFormula(LINES_KEY, null); | ||||
private static final IntSumFormula INT_SUM_FORMULA_DEFAULT_INPUT_VALUE_15 = createIntSumFormula(LINES_KEY, 15); | private static final IntSumFormula INT_SUM_FORMULA_DEFAULT_INPUT_VALUE_15 = createIntSumFormula(LINES_KEY, 15); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext( | CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext( | ||||
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class)); | ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class)); | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_creating_formula_with_null_metric() { | public void fail_with_NPE_when_creating_formula_with_null_metric() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Metric key cannot be null"); | |||||
createIntSumFormula(null); | |||||
assertThatThrownBy(() -> createIntSumFormula(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Metric key cannot be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Optional; | import java.util.Optional; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ReportComponent; | import org.sonar.ce.task.projectanalysis.component.ReportComponent; | ||||
import org.sonar.ce.task.projectanalysis.formula.counter.LongSumCounter; | import org.sonar.ce.task.projectanalysis.formula.counter.LongSumCounter; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.api.measures.CoreMetrics.LINES_KEY; | import static org.sonar.api.measures.CoreMetrics.LINES_KEY; | ||||
private static final SumFormula.LongSumFormula LONG_SUM_FORMULA_NULL_DEFAULT_INPUT_VALUE = createLongSumFormula(LINES_KEY, null); | private static final SumFormula.LongSumFormula LONG_SUM_FORMULA_NULL_DEFAULT_INPUT_VALUE = createLongSumFormula(LINES_KEY, null); | ||||
private static final SumFormula.LongSumFormula LONG_SUM_FORMULA_DEFAULT_INPUT_VALUE_15 = createLongSumFormula(LINES_KEY, 15L); | private static final SumFormula.LongSumFormula LONG_SUM_FORMULA_DEFAULT_INPUT_VALUE_15 = createLongSumFormula(LINES_KEY, 15L); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext( | CreateMeasureContext projectCreateMeasureContext = new DumbCreateMeasureContext( | ||||
ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class)); | ReportComponent.builder(Component.Type.PROJECT, 1).build(), mock(Metric.class)); | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_creating_formula_with_null_metric() { | public void fail_with_NPE_when_creating_formula_with_null_metric() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Metric key cannot be null"); | |||||
createLongSumFormula(null); | |||||
assertThatThrownBy(() -> createLongSumFormula(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Metric key cannot be null"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Optional; | import java.util.Optional; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.measures.CoreMetrics; | import org.sonar.api.measures.CoreMetrics; | ||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler; | import org.sonar.ce.task.projectanalysis.component.PathAwareCrawler; | ||||
import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule; | import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.api.measures.CoreMetrics.LINES_KEY; | import static org.sonar.api.measures.CoreMetrics.LINES_KEY; | ||||
import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; | import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; | ||||
import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY; | import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY; | ||||
import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder; | import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder; | ||||
import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf; | import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.entryOf; | ||||
import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries; | import static org.sonar.ce.task.projectanalysis.measure.MeasureRepoEntry.toEntries; | ||||
import static org.sonar.test.ExceptionCauseMatcher.hasType; | |||||
public class ReportFormulaExecutorComponentVisitorTest { | public class ReportFormulaExecutorComponentVisitorTest { | ||||
private static final int ROOT_REF = 1; | private static final int ROOT_REF = 1; | ||||
.build()) | .build()) | ||||
.build(); | .build(); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); | public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); | ||||
@Rule | @Rule | ||||
treeRootHolder.setRoot(root); | treeRootHolder.setRoot(root); | ||||
measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(2)); | measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(2)); | ||||
expectedException.expectCause(hasType(UnsupportedOperationException.class) | |||||
.andMessage(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", FILE_1_REF, NCLOC_KEY))); | |||||
// expectedException.expectCause( | |||||
// hasType(UnsupportedOperationException.class) | |||||
// .andMessage(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", FILE_1_REF, NCLOC_KEY)) | |||||
// ); | |||||
new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) | |||||
.visit(root); | |||||
assertThatThrownBy(() -> new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())).visit(root)) | |||||
.hasCause( new UnsupportedOperationException(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", FILE_1_REF, NCLOC_KEY))); | |||||
} | } | ||||
@Test | @Test | ||||
treeRootHolder.setRoot(root); | treeRootHolder.setRoot(root); | ||||
measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(10)); | measureRepository.addRawMeasure(ROOT_REF, NCLOC_KEY, newMeasureBuilder().create(10)); | ||||
expectedException.expectCause(hasType(UnsupportedOperationException.class) | |||||
.andMessage(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", ROOT_REF, NCLOC_KEY))); | |||||
// expectedException.expectCause(hasType(UnsupportedOperationException.class) | |||||
// .andMessage(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", ROOT_REF, NCLOC_KEY))); | |||||
new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) | |||||
.visit(root); | |||||
assertThatThrownBy(() -> { | |||||
new PathAwareCrawler<>(formulaExecutorComponentVisitor(new FakeFormula())) | |||||
.visit(root); | |||||
}) | |||||
.hasCause(new UnsupportedOperationException(String.format("A measure can only be set once for Component (ref=%s), Metric (key=%s)", ROOT_REF, NCLOC_KEY))); | |||||
} | } | ||||
private FormulaExecutorComponentVisitor formulaExecutorComponentVisitor(Formula formula) { | private FormulaExecutorComponentVisitor formulaExecutorComponentVisitor(Formula formula) { |
import java.util.Optional; | import java.util.Optional; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.rules.ExternalResource; | import org.junit.rules.ExternalResource; | ||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.formula.CounterInitializationContext; | import org.sonar.ce.task.projectanalysis.formula.CounterInitializationContext; | ||||
import static com.google.common.base.Preconditions.checkNotNull; | import static com.google.common.base.Preconditions.checkNotNull; | ||||
import static com.google.common.base.Preconditions.checkState; | import static com.google.common.base.Preconditions.checkState; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.formula.coverage.CoverageUtils.getLongMeasureValue; | import static org.sonar.ce.task.projectanalysis.formula.coverage.CoverageUtils.getLongMeasureValue; | ||||
import static org.sonar.ce.task.projectanalysis.formula.coverage.CoverageUtils.getMeasureVariations; | import static org.sonar.ce.task.projectanalysis.formula.coverage.CoverageUtils.getMeasureVariations; | ||||
import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder; | import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder; | ||||
@Rule | @Rule | ||||
public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule(); | public CounterInitializationContextRule fileAggregateContext = new CounterInitializationContextRule(); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void verify_calculate_coverage() { | public void verify_calculate_coverage() { | ||||
@Test | @Test | ||||
public void getLongMeasureValue_throws_ISE_if_measure_is_DOUBLE() { | public void getLongMeasureValue_throws_ISE_if_measure_is_DOUBLE() { | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("value can not be converted to long because current value type is a DOUBLE"); | |||||
fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1)); | |||||
getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY); | |||||
assertThatThrownBy(() -> { | |||||
fileAggregateContext.put(SOME_METRIC_KEY, newMeasureBuilder().create(152d, 1)); | |||||
getLongMeasureValue(fileAggregateContext, SOME_METRIC_KEY); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("value can not be converted to long because current value type is a DOUBLE"); | |||||
} | } | ||||
@Test | @Test |
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.config.Configuration; | import org.sonar.api.config.Configuration; | ||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
@Rule | @Rule | ||||
public DbTester db = DbTester.create(System2.INSTANCE); | public DbTester db = DbTester.create(System2.INSTANCE); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private DbClient dbClient = db.getDbClient(); | private DbClient dbClient = db.getDbClient(); | ||||
private System2 system2 = mock(System2.class); | private System2 system2 = mock(System2.class); |
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.core.issue.DefaultIssue; | import org.sonar.core.issue.DefaultIssue; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder; | import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder; | ||||
public class ComponentIssuesRepositoryImplTest { | public class ComponentIssuesRepositoryImplTest { | ||||
@org.junit.Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
static final Component FILE_1 = builder(Component.Type.FILE, 1).build(); | static final Component FILE_1 = builder(Component.Type.FILE, 1).build(); | ||||
static final Component FILE_2 = builder(Component.Type.FILE, 2).build(); | static final Component FILE_2 = builder(Component.Type.FILE, 2).build(); | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_setting_issues_with_null_component() { | public void fail_with_NPE_when_setting_issues_with_null_component() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("component cannot be null"); | |||||
sut.setIssues(null, Arrays.asList(DUMB_ISSUE)); | |||||
assertThatThrownBy(() -> sut.setIssues(null, Arrays.asList(DUMB_ISSUE))) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("component cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_setting_issues_with_null_issues() { | public void fail_with_NPE_when_setting_issues_with_null_issues() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("issues cannot be null"); | |||||
sut.setIssues(FILE_1, null); | |||||
assertThatThrownBy(() -> sut.setIssues(FILE_1, null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("issues cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_IAE_when_getting_issues_on_different_component() { | public void fail_with_IAE_when_getting_issues_on_different_component() { | ||||
thrown.expect(IllegalArgumentException.class); | |||||
thrown.expectMessage("Only issues from component '1' are available, but wanted component is '2'."); | |||||
sut.setIssues(FILE_1, Arrays.asList(DUMB_ISSUE)); | |||||
sut.getIssues(FILE_2); | |||||
assertThatThrownBy(() -> { | |||||
sut.setIssues(FILE_1, Arrays.asList(DUMB_ISSUE)); | |||||
sut.getIssues(FILE_2); | |||||
}) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Only issues from component '1' are available, but wanted component is '2'."); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_getting_issues_but_issues_are_null() { | public void fail_with_ISE_when_getting_issues_but_issues_are_null() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Issues have not been initialized"); | |||||
sut.getIssues(FILE_1); | |||||
assertThatThrownBy(() -> { | |||||
sut.getIssues(FILE_1); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Issues have not been initialized"); | |||||
} | } | ||||
} | } |
package org.sonar.ce.task.projectanalysis.issue; | package org.sonar.ce.task.projectanalysis.issue; | ||||
import java.util.Set; | import java.util.Set; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import static com.google.common.collect.Sets.newHashSet; | import static com.google.common.collect.Sets.newHashSet; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class ComponentsWithUnprocessedIssuesTest { | public class ComponentsWithUnprocessedIssuesTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
ComponentsWithUnprocessedIssues sut = new ComponentsWithUnprocessedIssues(); | ComponentsWithUnprocessedIssues sut = new ComponentsWithUnprocessedIssues(); | ||||
@Test | @Test | ||||
public void fail_with_NPE_when_setting_null_uuids() { | public void fail_with_NPE_when_setting_null_uuids() { | ||||
thrown.expect(NullPointerException.class); | |||||
thrown.expectMessage("Uuids cannot be null"); | |||||
sut.setUuids(null); | |||||
assertThatThrownBy(() -> sut.setUuids(null)) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("Uuids cannot be null"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_setting_uuids_twice() { | public void fail_with_ISE_when_setting_uuids_twice() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Uuids have already been initialized"); | |||||
sut.setUuids(newHashSet("ABCD")); | |||||
sut.setUuids(newHashSet("EFGH")); | |||||
assertThatThrownBy(() -> { | |||||
sut.setUuids(newHashSet("ABCD")); | |||||
sut.setUuids(newHashSet("EFGH")); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Uuids have already been initialized"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_removing_uuid_and_not_initialized() { | public void fail_with_ISE_when_removing_uuid_and_not_initialized() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Uuids have not been initialized yet"); | |||||
sut.remove("ABCD"); | |||||
assertThatThrownBy(() -> sut.remove("ABCD")) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Uuids have not been initialized yet"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_with_ISE_when_getting_uuid_and_not_initialized() { | public void fail_with_ISE_when_getting_uuid_and_not_initialized() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Uuids have not been initialized yet"); | |||||
sut.getUuids(); | |||||
assertThatThrownBy(() -> sut.getUuids()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Uuids have not been initialized yet"); | |||||
} | } | ||||
} | } |
import org.sonar.db.rule.RuleTesting; | import org.sonar.db.rule.RuleTesting; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class DebtCalculatorTest { | public class DebtCalculatorTest { | ||||
assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(2); | assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(2); | ||||
} | } | ||||
@Test(expected = IllegalArgumentException.class) | |||||
@Test | |||||
public void effort_to_fix_must_not_be_set_with_constant_function() { | public void effort_to_fix_must_not_be_set_with_constant_function() { | ||||
int constant = 2; | int constant = 2; | ||||
issue.setGap(3.0); | issue.setGap(3.0); | ||||
rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, constant + "min")); | rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, constant + "min")); | ||||
underTest.calculate(issue); | |||||
assertThatThrownBy(() -> underTest.calculate(issue)) | |||||
.isInstanceOf(IllegalArgumentException.class); | |||||
} | } | ||||
@Test | @Test |
import org.apache.commons.lang.ArrayUtils; | import org.apache.commons.lang.ArrayUtils; | ||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.rule.RuleKey; | import org.sonar.api.rule.RuleKey; | ||||
import org.sonar.ce.task.projectanalysis.analysis.Analysis; | import org.sonar.ce.task.projectanalysis.analysis.Analysis; | ||||
import org.sonar.db.protobuf.DbIssues.Locations.Builder; | import org.sonar.db.protobuf.DbIssues.Locations.Builder; | ||||
import org.sonar.server.issue.IssueFieldsSetter; | import org.sonar.server.issue.IssueFieldsSetter; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.mockito.ArgumentMatchers.any; | import static org.mockito.ArgumentMatchers.any; | ||||
import static org.mockito.ArgumentMatchers.eq; | import static org.mockito.ArgumentMatchers.eq; | ||||
import static org.mockito.ArgumentMatchers.same; | import static org.mockito.ArgumentMatchers.same; | ||||
@org.junit.Rule | @org.junit.Rule | ||||
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); | public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); | ||||
@org.junit.Rule | |||||
public ExpectedException exception = ExpectedException.none(); | |||||
private ScmInfoRepository scmInfoRepository = mock(ScmInfoRepository.class); | private ScmInfoRepository scmInfoRepository = mock(ScmInfoRepository.class); | ||||
private IssueFieldsSetter issueUpdater = mock(IssueFieldsSetter.class); | private IssueFieldsSetter issueUpdater = mock(IssueFieldsSetter.class); | ||||
when(ruleRepository.findByKey(ruleKey)).thenReturn(Optional.empty()); | when(ruleRepository.findByKey(ruleKey)).thenReturn(Optional.empty()); | ||||
makeIssueNew(); | makeIssueNew(); | ||||
exception.expect(IllegalStateException.class); | |||||
exception.expectMessage("The rule with key 'reop:rule' raised an issue, but no rule with that key was found"); | |||||
run(); | |||||
assertThatThrownBy(this::run) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("The rule with key 'reop:rule' raised an issue, but no rule with that key was found"); | |||||
} | } | ||||
@Test | @Test |
import java.util.Optional; | import java.util.Optional; | ||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.mockito.ArgumentCaptor; | import org.mockito.ArgumentCaptor; | ||||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | ||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.server.issue.IssueFieldsSetter; | import org.sonar.server.issue.IssueFieldsSetter; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.any; | ||||
import static org.mockito.ArgumentMatchers.eq; | import static org.mockito.ArgumentMatchers.eq; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
.setUuid(FILE_UUID) | .setUuid(FILE_UUID) | ||||
.build(); | .build(); | ||||
@org.junit.Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@org.junit.Rule | @org.junit.Rule | ||||
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); | public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); | ||||
DefaultIssue issue = mockIssue("other component uuid"); | DefaultIssue issue = mockIssue("other component uuid"); | ||||
when(issue.toString()).thenReturn("[bad issue, bad!]"); | when(issue.toString()).thenReturn("[bad issue, bad!]"); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Issue [bad issue, bad!] for component ReportComponent{ref=1, key='key_1', type=FILE} " + | |||||
"has a different component key but no original file exist in MovedFilesRepository"); | |||||
underTest.onIssue(FILE, issue); | |||||
assertThatThrownBy(() -> underTest.onIssue(FILE, issue)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Issue [bad issue, bad!] for component ReportComponent{ref=1, key='key_1', type=FILE} " + | |||||
"has a different component key but no original file exist in MovedFilesRepository"); | |||||
} | } | ||||
@Test | @Test | ||||
when(movedFilesRepository.getOriginalFile(FILE)) | when(movedFilesRepository.getOriginalFile(FILE)) | ||||
.thenReturn(Optional.of(new MovedFilesRepository.OriginalFile("original uuid", "original key"))); | .thenReturn(Optional.of(new MovedFilesRepository.OriginalFile("original uuid", "original key"))); | ||||
expectedException.expect(IllegalStateException.class); | |||||
expectedException.expectMessage("Issue [bad issue, bad!] doesn't belong to file original uuid registered as original " + | |||||
"file of current file ReportComponent{ref=1, key='key_1', type=FILE}"); | |||||
underTest.onIssue(FILE, issue); | |||||
assertThatThrownBy(() -> underTest.onIssue(FILE, issue)) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Issue [bad issue, bad!] doesn't belong to file original uuid registered as original " + | |||||
"file of current file ReportComponent{ref=1, key='key_1', type=FILE}"); | |||||
} | } | ||||
@Test | @Test |
package org.sonar.ce.task.projectanalysis.issue; | package org.sonar.ce.task.projectanalysis.issue; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.scanner.protocol.output.ScannerReport; | import org.sonar.scanner.protocol.output.ScannerReport; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class NewAdHocRuleTest { | public class NewAdHocRuleTest { | ||||
@org.junit.Rule | |||||
public ExpectedException exception = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void fail_if_engine_id_is_not_set() { | public void fail_if_engine_id_is_not_set() { | ||||
exception.expect(IllegalArgumentException.class); | |||||
exception.expectMessage("'engine id' not expected to be null for an ad hoc rule"); | |||||
new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().build()); | |||||
assertThatThrownBy(() -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().build())) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("'engine id' not expected to be null for an ad hoc rule"); | |||||
} | } | ||||
@Test | @Test |
import com.google.common.collect.ImmutableList; | import com.google.common.collect.ImmutableList; | ||||
import com.google.common.collect.ImmutableSet; | import com.google.common.collect.ImmutableSet; | ||||
import java.util.Optional; | import java.util.Optional; | ||||
import org.assertj.core.api.ThrowableAssert.ThrowingCallable; | |||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.rule.RuleKey; | import org.sonar.api.rule.RuleKey; | ||||
import org.sonar.api.rule.RuleStatus; | import org.sonar.api.rule.RuleStatus; | ||||
import org.sonar.api.rules.RuleType; | import org.sonar.api.rules.RuleType; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; | |||||
import org.sonar.core.util.SequenceUuidFactory; | import org.sonar.core.util.SequenceUuidFactory; | ||||
import org.sonar.db.DbClient; | import org.sonar.db.DbClient; | ||||
import org.sonar.db.DbSession; | import org.sonar.db.DbSession; | ||||
import org.sonar.server.rule.index.RuleIndexer; | import org.sonar.server.rule.index.RuleIndexer; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.any; | ||||
import static org.mockito.ArgumentMatchers.anyBoolean; | import static org.mockito.ArgumentMatchers.anyBoolean; | ||||
import static org.mockito.ArgumentMatchers.eq; | |||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.reset; | import static org.mockito.Mockito.reset; | ||||
import static org.mockito.Mockito.verify; | import static org.mockito.Mockito.verify; | ||||
private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c"); | private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c"); | ||||
private static final String AC_RULE_UUID = "uuid-684"; | private static final String AC_RULE_UUID = "uuid-684"; | ||||
@org.junit.Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@org.junit.Rule | @org.junit.Rule | ||||
public DbTester db = DbTester.create(System2.INSTANCE); | public DbTester db = DbTester.create(System2.INSTANCE); | ||||
@Test | @Test | ||||
public void getByKey_throws_NPE_if_key_argument_is_null() { | public void getByKey_throws_NPE_if_key_argument_is_null() { | ||||
expectNullRuleKeyNPE(); | |||||
underTest.getByKey(null); | |||||
expectNullRuleKeyNPE(() -> underTest.getByKey(null)); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getByKey_throws_IAE_if_rules_does_not_exist_in_DB() { | public void getByKey_throws_IAE_if_rules_does_not_exist_in_DB() { | ||||
expectIAERuleNotFound(AC_RULE_KEY); | |||||
underTest.getByKey(AC_RULE_KEY); | |||||
expectIAERuleNotFound(() -> underTest.getByKey(AC_RULE_KEY), AC_RULE_KEY); | |||||
} | } | ||||
@Test | @Test | ||||
public void getByKey_throws_IAE_if_argument_is_deprecated_key_in_DB_of_non_existing_rule() { | public void getByKey_throws_IAE_if_argument_is_deprecated_key_in_DB_of_non_existing_rule() { | ||||
expectIAERuleNotFound(DEPRECATED_KEY_OF_NON_EXITING_RULE); | |||||
underTest.getByKey(DEPRECATED_KEY_OF_NON_EXITING_RULE); | |||||
expectIAERuleNotFound(() -> underTest.getByKey(DEPRECATED_KEY_OF_NON_EXITING_RULE), DEPRECATED_KEY_OF_NON_EXITING_RULE); | |||||
} | } | ||||
private void expectIAERuleNotFound(RuleKey ruleKey) { | |||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Can not find rule for key " + ruleKey.toString() + ". This rule does not exist in DB"); | |||||
private void expectIAERuleNotFound(ThrowingCallable callback, RuleKey ruleKey) { | |||||
assertThatThrownBy(callback) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Can not find rule for key " + ruleKey.toString() + ". This rule does not exist in DB"); | |||||
} | } | ||||
@Test | @Test | ||||
public void findByKey_throws_NPE_if_key_argument_is_null() { | public void findByKey_throws_NPE_if_key_argument_is_null() { | ||||
expectNullRuleKeyNPE(); | |||||
underTest.findByKey(null); | |||||
expectNullRuleKeyNPE(() -> underTest.findByKey(null)); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void getByUuid_throws_IAE_if_rules_does_not_exist_in_DB() { | public void getByUuid_throws_IAE_if_rules_does_not_exist_in_DB() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Can not find rule for uuid " + AC_RULE_UUID + ". This rule does not exist in DB"); | |||||
underTest.getByUuid(AC_RULE_UUID); | |||||
assertThatThrownBy(() -> underTest.getByUuid(AC_RULE_UUID)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Can not find rule for uuid " + AC_RULE_UUID + ". This rule does not exist in DB"); | |||||
} | } | ||||
@Test | @Test | ||||
verify(ruleIndexer).commitAndIndex(db.getSession(), ruleDefinitionDto.get().getUuid()); | verify(ruleIndexer).commitAndIndex(db.getSession(), ruleDefinitionDto.get().getUuid()); | ||||
} | } | ||||
private void expectNullRuleKeyNPE() { | |||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expectMessage("RuleKey can not be null"); | |||||
private void expectNullRuleKeyNPE(ThrowingCallable callback) { | |||||
assertThatThrownBy(callback) | |||||
.isInstanceOf(NullPointerException.class) | |||||
.hasMessage("RuleKey can not be null"); | |||||
} | } | ||||
private void verifyNoMethodCallTriggersCallToDB() { | private void verifyNoMethodCallTriggersCallToDB() { |
import com.google.common.collect.ImmutableMap; | import com.google.common.collect.ImmutableMap; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.measures.CoreMetrics; | import org.sonar.api.measures.CoreMetrics; | ||||
import org.sonar.api.rule.RuleKey; | import org.sonar.api.rule.RuleKey; | ||||
import org.sonar.api.rule.Severity; | import org.sonar.api.rule.Severity; | ||||
import org.sonar.server.rule.CommonRuleKeys; | import org.sonar.server.rule.CommonRuleKeys; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.component.ReportComponent.DUMB_PROJECT; | import static org.sonar.ce.task.projectanalysis.component.ReportComponent.DUMB_PROJECT; | ||||
public class CommentDensityRuleTest { | public class CommentDensityRuleTest { | ||||
.setFileAttributes(new FileAttributes(true, PLUGIN_KEY, 1)) | .setFileAttributes(new FileAttributes(true, PLUGIN_KEY, 1)) | ||||
.build(); | .build(); | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule(); | public ActiveRulesHolderRule activeRuleHolder = new ActiveRulesHolderRule(); | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void fail_if_min_density_is_100() { | public void fail_if_min_density_is_100() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Minimum density of rule [common-java:InsufficientCommentDensity] is incorrect. Got [100] but must be strictly less than 100."); | |||||
prepareForIssue("100", FILE, 0.0, 0, 1); | |||||
underTest.processFile(FILE, PLUGIN_KEY); | |||||
assertThatThrownBy(() -> { | |||||
prepareForIssue("100", FILE, 0.0, 0, 1); | |||||
underTest.processFile(FILE, PLUGIN_KEY); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Minimum density of rule [common-java:InsufficientCommentDensity] is incorrect. Got [100] but must be strictly less than 100."); | |||||
} | } | ||||
private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) { | private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) { |
package org.sonar.ce.task.projectanalysis.issue.commonrule; | package org.sonar.ce.task.projectanalysis.issue.commonrule; | ||||
import com.google.common.collect.ImmutableMap; | import com.google.common.collect.ImmutableMap; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.rule.Severity; | import org.sonar.api.rule.Severity; | ||||
import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule; | import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule; | ||||
import org.sonar.db.rule.RuleTesting; | import org.sonar.db.rule.RuleTesting; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class CommonRuleTest { | public class CommonRuleTest { | ||||
private static final String PLUGIN_KEY = "java"; | private static final String PLUGIN_KEY = "java"; | ||||
private static final String QP_KEY = "qp1"; | private static final String QP_KEY = "qp1"; | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void test_getMinDensityParam() { | public void test_getMinDensityParam() { | ||||
@Test | @Test | ||||
public void getMinDensityParam_fails_if_param_value_is_absent() { | public void getMinDensityParam_fails_if_param_value_is_absent() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Required parameter [minDensity] is missing on rule [xoo:x1]"); | |||||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, PLUGIN_KEY, QP_KEY); | |||||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||||
assertThatThrownBy(() -> { | |||||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, PLUGIN_KEY, QP_KEY); | |||||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Required parameter [minDensity] is missing on rule [xoo:x1]"); | |||||
} | } | ||||
@Test | @Test | ||||
public void getMinDensityParam_fails_if_param_value_is_negative() { | public void getMinDensityParam_fails_if_param_value_is_negative() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [-30.5] but must be between 0 and 100."); | |||||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, PLUGIN_KEY, QP_KEY); | |||||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||||
assertThatThrownBy(() -> { | |||||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, PLUGIN_KEY, QP_KEY); | |||||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Minimum density of rule [xoo:x1] is incorrect. Got [-30.5] but must be between 0 and 100."); | |||||
} | } | ||||
@Test | @Test | ||||
public void getMinDensityParam_fails_if_param_value_is_greater_than_100() { | public void getMinDensityParam_fails_if_param_value_is_greater_than_100() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [305] but must be between 0 and 100."); | |||||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, PLUGIN_KEY, QP_KEY); | |||||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||||
assertThatThrownBy(() -> { | |||||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, PLUGIN_KEY, QP_KEY); | |||||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Minimum density of rule [xoo:x1] is incorrect. Got [305] but must be between 0 and 100."); | |||||
} | } | ||||
} | } |
import java.util.List; | import java.util.List; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.api.config.internal.Settings; | |||||
import org.sonar.api.config.internal.MapSettings; | import org.sonar.api.config.internal.MapSettings; | ||||
import org.sonar.api.config.internal.Settings; | |||||
import org.sonar.api.rule.RuleKey; | import org.sonar.api.rule.RuleKey; | ||||
import org.sonar.ce.task.projectanalysis.component.Component; | import org.sonar.ce.task.projectanalysis.component.Component; | ||||
import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository; | import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository; | ||||
import static java.util.Arrays.asList; | import static java.util.Arrays.asList; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; | ||||
public class IssueFilterTest { | public class IssueFilterTest { | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
static final RuleKey XOO_X1 = RuleKey.of("xoo", "x1"); | static final RuleKey XOO_X1 = RuleKey.of("xoo", "x1"); | ||||
static final RuleKey XOO_X2 = RuleKey.of("xoo", "x2"); | static final RuleKey XOO_X2 = RuleKey.of("xoo", "x2"); | ||||
@Test | @Test | ||||
public void fail_when_only_rule_key_parameter() { | public void fail_when_only_rule_key_parameter() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("File path pattern cannot be empty. Please check 'sonar.issue.ignore.multicriteria' settings"); | |||||
newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.emptyList())); | |||||
assertThatThrownBy(() -> newIssueFilter(newSettings(asList("xoo:x1", ""), Collections.emptyList()))) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("File path pattern cannot be empty. Please check 'sonar.issue.ignore.multicriteria' settings"); | |||||
} | } | ||||
@Test | @Test | ||||
public void fail_when_only_path_parameter() { | public void fail_when_only_path_parameter() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("Rule key pattern cannot be empty. Please check 'sonar.issue.enforce.multicriteria' settings"); | |||||
newIssueFilter(newSettings(Collections.emptyList(), asList("", "**"))); | |||||
assertThatThrownBy(() -> newIssueFilter(newSettings(Collections.emptyList(), asList("", "**")))) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("Rule key pattern cannot be empty. Please check 'sonar.issue.enforce.multicriteria' settings"); | |||||
} | } | ||||
private IssueFilter newIssueFilter(MapSettings settings) { | private IssueFilter newIssueFilter(MapSettings settings) { |
import org.sonar.api.resources.Language; | import org.sonar.api.resources.Language; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
public class LanguageRepositoryImplTest { | public class LanguageRepositoryImplTest { | ||||
private static final String SOME_LANGUAGE_KEY = "SoMe language_Key"; | private static final String SOME_LANGUAGE_KEY = "SoMe language_Key"; | ||||
private static final Language SOME_LANGUAGE = createLanguage(SOME_LANGUAGE_KEY, "_name"); | private static final Language SOME_LANGUAGE = createLanguage(SOME_LANGUAGE_KEY, "_name"); | ||||
@Test(expected = IllegalArgumentException.class) | |||||
@Test | |||||
public void constructor_fails_is_language_have_the_same_key() { | public void constructor_fails_is_language_have_the_same_key() { | ||||
new LanguageRepositoryImpl(createLanguage(SOME_LANGUAGE_KEY, " 1"), createLanguage(SOME_LANGUAGE_KEY, " 2")); | |||||
assertThatThrownBy(() -> new LanguageRepositoryImpl(createLanguage(SOME_LANGUAGE_KEY, " 1"), createLanguage(SOME_LANGUAGE_KEY, " 2"))) | |||||
.isInstanceOf(IllegalArgumentException.class); | |||||
} | } | ||||
@Test | @Test |
import org.sonar.scanner.protocol.output.ScannerReport.Measure.StringValue; | import org.sonar.scanner.protocol.output.ScannerReport.Measure.StringValue; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class BatchMeasureToMeasureTest { | public class BatchMeasureToMeasureTest { | ||||
assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent(); | assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent(); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasure_throws_NPE_if_metric_argument_is_null() { | public void toMeasure_throws_NPE_if_metric_argument_is_null() { | ||||
underTest.toMeasure(EMPTY_BATCH_MEASURE, null); | |||||
assertThatThrownBy(() -> underTest.toMeasure(EMPTY_BATCH_MEASURE, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasure_throws_NPE_if_both_arguments_are_null() { | public void toMeasure_throws_NPE_if_both_arguments_are_null() { | ||||
underTest.toMeasure(null, null); | |||||
assertThatThrownBy(() -> underTest.toMeasure(null, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.Optional; | import java.util.Optional; | ||||
import org.assertj.core.data.Offset; | import org.assertj.core.data.Offset; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.measure.Measure.Level; | import org.sonar.ce.task.projectanalysis.measure.Measure.Level; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import org.sonar.db.measure.LiveMeasureDto; | import org.sonar.db.measure.LiveMeasureDto; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class LiveMeasureDtoToMeasureTest { | public class LiveMeasureDtoToMeasureTest { | ||||
private static final String SOME_ALERT_TEXT = "some alert text_be_careFul!"; | private static final String SOME_ALERT_TEXT = "some alert text_be_careFul!"; | ||||
private static final LiveMeasureDto EMPTY_MEASURE_DTO = new LiveMeasureDto(); | private static final LiveMeasureDto EMPTY_MEASURE_DTO = new LiveMeasureDto(); | ||||
@Rule | |||||
public final ExpectedException expectedException = ExpectedException.none(); | |||||
private LiveMeasureDtoToMeasure underTest = new LiveMeasureDtoToMeasure(); | private LiveMeasureDtoToMeasure underTest = new LiveMeasureDtoToMeasure(); | ||||
assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent(); | assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent(); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasure_throws_NPE_if_metric_argument_is_null() { | public void toMeasure_throws_NPE_if_metric_argument_is_null() { | ||||
underTest.toMeasure(EMPTY_MEASURE_DTO, null); | |||||
assertThatThrownBy(() -> underTest.toMeasure(EMPTY_MEASURE_DTO, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasure_throws_NPE_if_both_arguments_are_null() { | public void toMeasure_throws_NPE_if_both_arguments_are_null() { | ||||
underTest.toMeasure(null, null); | |||||
assertThatThrownBy(() -> underTest.toMeasure(null, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; | import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; | ||||
import static java.lang.String.format; | import static java.lang.String.format; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.junit.Assert.fail; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.verifyNoMoreInteractions; | import static org.mockito.Mockito.verifyNoMoreInteractions; | ||||
public class MapBasedRawMeasureRepositoryTest { | public class MapBasedRawMeasureRepositoryTest { | ||||
@Rule | @Rule | ||||
public DbTester dbTester = DbTester.create(System2.INSTANCE); | public DbTester dbTester = DbTester.create(System2.INSTANCE); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
private static final String FILE_COMPONENT_KEY = "file cpt key"; | private static final String FILE_COMPONENT_KEY = "file cpt key"; | ||||
private static final ReportComponent FILE_COMPONENT = ReportComponent.builder(Component.Type.FILE, 1) | private static final ReportComponent FILE_COMPONENT = ReportComponent.builder(Component.Type.FILE, 1) | ||||
when(metricRepository.getByKey(METRIC_KEY_2)).thenReturn(metric2); | when(metricRepository.getByKey(METRIC_KEY_2)).thenReturn(metric2); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void add_throws_NPE_if_Component_argument_is_null() { | public void add_throws_NPE_if_Component_argument_is_null() { | ||||
underTest.add(null, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.add(null, metric1, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void add_throws_NPE_if_Component_metric_is_null() { | public void add_throws_NPE_if_Component_metric_is_null() { | ||||
underTest.add(FILE_COMPONENT, null, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.add(FILE_COMPONENT, null, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void add_throws_NPE_if_Component_measure_is_null() { | public void add_throws_NPE_if_Component_measure_is_null() { | ||||
underTest.add(FILE_COMPONENT, metric1, null); | |||||
assertThatThrownBy(() -> underTest.add(FILE_COMPONENT, metric1, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = UnsupportedOperationException.class) | |||||
@Test | |||||
public void add_throws_UOE_if_measure_already_exists() { | public void add_throws_UOE_if_measure_already_exists() { | ||||
underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | ||||
underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE)) | |||||
.isInstanceOf(UnsupportedOperationException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void update_throws_NPE_if_Component_argument_is_null() { | public void update_throws_NPE_if_Component_argument_is_null() { | ||||
underTest.update(null, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.update(null, metric1, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void update_throws_NPE_if_Component_metric_is_null() { | public void update_throws_NPE_if_Component_metric_is_null() { | ||||
underTest.update(FILE_COMPONENT, null, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.update(FILE_COMPONENT, null, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void update_throws_NPE_if_Component_measure_is_null() { | public void update_throws_NPE_if_Component_measure_is_null() { | ||||
underTest.update(FILE_COMPONENT, metric1, null); | |||||
assertThatThrownBy(() -> underTest.update(FILE_COMPONENT, metric1, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = UnsupportedOperationException.class) | |||||
@Test | |||||
public void update_throws_UOE_if_measure_does_not_exists() { | public void update_throws_UOE_if_measure_does_not_exists() { | ||||
underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE)) | |||||
.isInstanceOf(UnsupportedOperationException.class); | |||||
} | } | ||||
private static final List<Measure> MEASURES = ImmutableList.of( | private static final List<Measure> MEASURES = ImmutableList.of( |
import com.google.common.collect.ImmutableList; | import com.google.common.collect.ImmutableList; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.sonar.ce.task.projectanalysis.api.measurecomputer.MeasureComputerWrapper; | import org.sonar.ce.task.projectanalysis.api.measurecomputer.MeasureComputerWrapper; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.mock; | ||||
public class MeasureComputersHolderImplTest { | public class MeasureComputersHolderImplTest { | ||||
@Rule | |||||
public ExpectedException thrown = ExpectedException.none(); | |||||
private MeasureComputersHolderImpl underTest = new MeasureComputersHolderImpl(); | private MeasureComputersHolderImpl underTest = new MeasureComputersHolderImpl(); | ||||
@Test | @Test | ||||
public void get_measure_computers_throws_ISE_if_not_initialized() { | public void get_measure_computers_throws_ISE_if_not_initialized() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Measure computers have not been initialized yet"); | |||||
underTest.getMeasureComputers(); | |||||
assertThatThrownBy(() -> underTest.getMeasureComputers()) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Measure computers have not been initialized yet"); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void set_measure_computers_throws_ISE_if_already_initialized() { | public void set_measure_computers_throws_ISE_if_already_initialized() { | ||||
thrown.expect(IllegalStateException.class); | |||||
thrown.expectMessage("Measure computers have already been initialized"); | |||||
MeasureComputerWrapper measureComputer = mock(MeasureComputerWrapper.class); | |||||
underTest.setMeasureComputers(Collections.singletonList(measureComputer)); | |||||
underTest.setMeasureComputers(Collections.singletonList(measureComputer)); | |||||
assertThatThrownBy(() -> { | |||||
MeasureComputerWrapper measureComputer = mock(MeasureComputerWrapper.class); | |||||
underTest.setMeasureComputers(Collections.singletonList(measureComputer)); | |||||
underTest.setMeasureComputers(Collections.singletonList(measureComputer)); | |||||
}) | |||||
.isInstanceOf(IllegalStateException.class) | |||||
.hasMessage("Measure computers have already been initialized"); | |||||
} | } | ||||
} | } |
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.Optional; | import java.util.Optional; | ||||
import org.assertj.core.data.Offset; | import org.assertj.core.data.Offset; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.measure.Measure.Level; | import org.sonar.ce.task.projectanalysis.measure.Measure.Level; | ||||
import org.sonar.ce.task.projectanalysis.metric.Metric; | import org.sonar.ce.task.projectanalysis.metric.Metric; | ||||
import org.sonar.db.measure.MeasureDto; | import org.sonar.db.measure.MeasureDto; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class MeasureDtoToMeasureTest { | public class MeasureDtoToMeasureTest { | ||||
private static final String SOME_ALERT_TEXT = "some alert text_be_careFul!"; | private static final String SOME_ALERT_TEXT = "some alert text_be_careFul!"; | ||||
private static final MeasureDto EMPTY_MEASURE_DTO = new MeasureDto(); | private static final MeasureDto EMPTY_MEASURE_DTO = new MeasureDto(); | ||||
@Rule | |||||
public final ExpectedException expectedException = ExpectedException.none(); | |||||
private MeasureDtoToMeasure underTest = new MeasureDtoToMeasure(); | private MeasureDtoToMeasure underTest = new MeasureDtoToMeasure(); | ||||
assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent(); | assertThat(underTest.toMeasure(null, SOME_INT_METRIC)).isNotPresent(); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasure_throws_NPE_if_metric_argument_is_null() { | public void toMeasure_throws_NPE_if_metric_argument_is_null() { | ||||
underTest.toMeasure(EMPTY_MEASURE_DTO, null); | |||||
assertThatThrownBy(() -> underTest.toMeasure(EMPTY_MEASURE_DTO, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasure_throws_NPE_if_both_arguments_are_null() { | public void toMeasure_throws_NPE_if_both_arguments_are_null() { | ||||
underTest.toMeasure(null, null); | |||||
assertThatThrownBy(() -> underTest.toMeasure(null, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT); | assertThat(measure.get().getQualityGateStatus().getText()).isEqualTo(SOME_ALERT_TEXT); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
public void toMeasure_for_LEVEL_Metric_ignores_data() { | public void toMeasure_for_LEVEL_Metric_ignores_data() { | ||||
MeasureDto measureDto = new MeasureDto().setAlertStatus(Level.ERROR.name()).setData(SOME_DATA); | MeasureDto measureDto = new MeasureDto().setAlertStatus(Level.ERROR.name()).setData(SOME_DATA); | ||||
Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LEVEL_METRIC); | Optional<Measure> measure = underTest.toMeasure(measureDto, SOME_LEVEL_METRIC); | ||||
assertThat(measure).isPresent(); | assertThat(measure).isPresent(); | ||||
measure.get().getStringValue(); | |||||
assertThatThrownBy(() ->measure.get().getStringValue()) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; | import org.sonar.ce.task.projectanalysis.batch.BatchReportReader; | ||||
import static com.google.common.collect.FluentIterable.from; | import static com.google.common.collect.FluentIterable.from; | ||||
import static java.lang.String.format; | import static java.lang.String.format; | ||||
import static org.assertj.core.api.Assertions.assertThat; | 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.junit.Assert.fail; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.verifyNoMoreInteractions; | import static org.mockito.Mockito.verifyNoMoreInteractions; | ||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class MeasureRepositoryImplTest { | public class MeasureRepositoryImplTest { | ||||
@Rule | |||||
public final ExpectedException expectedException = ExpectedException.none(); | |||||
@Rule | @Rule | ||||
public DbTester dbTester = DbTester.create(System2.INSTANCE); | public DbTester dbTester = DbTester.create(System2.INSTANCE); | ||||
@Test | @Test | ||||
public void add_throws_NPE_if_Component_argument_is_null() { | public void add_throws_NPE_if_Component_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
underTest.add(null, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.add(null, metric1, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void add_throws_NPE_if_Component_metric_is_null() { | public void add_throws_NPE_if_Component_metric_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
underTest.add(FILE_COMPONENT, null, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.add(FILE_COMPONENT, null, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void add_throws_NPE_if_Component_measure_is_null() { | public void add_throws_NPE_if_Component_measure_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
underTest.add(FILE_COMPONENT, metric1, null); | |||||
assertThatThrownBy(() -> underTest.add(FILE_COMPONENT, metric1, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void add_throws_UOE_if_measure_already_exists() { | public void add_throws_UOE_if_measure_already_exists() { | ||||
expectedException.expect(UnsupportedOperationException.class); | |||||
underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> { | |||||
underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
}) | |||||
.isInstanceOf(UnsupportedOperationException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void update_throws_NPE_if_Component_argument_is_null() { | public void update_throws_NPE_if_Component_argument_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
underTest.update(null, metric1, SOME_MEASURE); | |||||
} | } | ||||
@Test | @Test | ||||
public void update_throws_NPE_if_Component_metric_is_null() { | public void update_throws_NPE_if_Component_metric_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
underTest.update(FILE_COMPONENT, null, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.update(FILE_COMPONENT, null, SOME_MEASURE)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void update_throws_NPE_if_Component_measure_is_null() { | public void update_throws_NPE_if_Component_measure_is_null() { | ||||
expectedException.expect(NullPointerException.class); | |||||
expectedException.expect(NullPointerException.class); | |||||
underTest.update(FILE_COMPONENT, metric1, null); | |||||
assertThatThrownBy(() -> underTest.update(FILE_COMPONENT, metric1, null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
public void update_throws_UOE_if_measure_does_not_exists() { | public void update_throws_UOE_if_measure_does_not_exists() { | ||||
expectedException.expect(UnsupportedOperationException.class); | |||||
underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE); | |||||
assertThatThrownBy(() -> underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE)) | |||||
.isInstanceOf(UnsupportedOperationException.class); | |||||
} | } | ||||
private static final List<Measure> MEASURES = ImmutableList.of( | private static final List<Measure> MEASURES = ImmutableList.of( |
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||||
import java.util.List; | import java.util.List; | ||||
import org.junit.Rule; | |||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.junit.runner.RunWith; | import org.junit.runner.RunWith; | ||||
import org.sonar.ce.task.projectanalysis.measure.Measure.ValueType; | import org.sonar.ce.task.projectanalysis.measure.Measure.ValueType; | ||||
import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray; | import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder; | import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder; | ||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
private static final List<Measure> MEASURES = ImmutableList.of( | private static final List<Measure> MEASURES = ImmutableList.of( | ||||
INT_MEASURE, LONG_MEASURE, DOUBLE_MEASURE, STRING_MEASURE, TRUE_MEASURE, FALSE_MEASURE, NO_VALUE_MEASURE, LEVEL_MEASURE); | INT_MEASURE, LONG_MEASURE, DOUBLE_MEASURE, STRING_MEASURE, TRUE_MEASURE, FALSE_MEASURE, NO_VALUE_MEASURE, LEVEL_MEASURE); | ||||
@Rule | |||||
public final ExpectedException expectedException = ExpectedException.none(); | |||||
@DataProvider | @DataProvider | ||||
public static Object[][] all_but_INT_MEASURE() { | public static Object[][] all_but_INT_MEASURE() { | ||||
.toArray(Object[][]::new); | .toArray(Object[][]::new); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void create_from_String_throws_NPE_if_arg_is_null() { | public void create_from_String_throws_NPE_if_arg_is_null() { | ||||
newMeasureBuilder().create((String) null); | |||||
assertThatThrownBy(() -> newMeasureBuilder().create((String) null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(STRING_MEASURE.getValueType()).isEqualTo(ValueType.STRING); | assertThat(STRING_MEASURE.getValueType()).isEqualTo(ValueType.STRING); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all_but_INT_MEASURE") | @UseDataProvider("all_but_INT_MEASURE") | ||||
public void getIntValue_throws_ISE_for_all_value_types_except_INT(Measure measure) { | public void getIntValue_throws_ISE_for_all_value_types_except_INT(Measure measure) { | ||||
measure.getIntValue(); | |||||
assertThatThrownBy(measure::getIntValue) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(INT_MEASURE.getIntValue()).isEqualTo(1); | assertThat(INT_MEASURE.getIntValue()).isEqualTo(1); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all_but_LONG_MEASURE") | @UseDataProvider("all_but_LONG_MEASURE") | ||||
public void getLongValue_throws_ISE_for_all_value_types_except_LONG(Measure measure) { | public void getLongValue_throws_ISE_for_all_value_types_except_LONG(Measure measure) { | ||||
measure.getLongValue(); | |||||
assertThatThrownBy(measure::getLongValue) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(LONG_MEASURE.getLongValue()).isEqualTo(1); | assertThat(LONG_MEASURE.getLongValue()).isEqualTo(1); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all_but_DOUBLE_MEASURE") | @UseDataProvider("all_but_DOUBLE_MEASURE") | ||||
public void getDoubleValue_throws_ISE_for_all_value_types_except_DOUBLE(Measure measure) { | public void getDoubleValue_throws_ISE_for_all_value_types_except_DOUBLE(Measure measure) { | ||||
measure.getDoubleValue(); | |||||
assertThatThrownBy(measure::getDoubleValue) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(DOUBLE_MEASURE.getDoubleValue()).isEqualTo(1d); | assertThat(DOUBLE_MEASURE.getDoubleValue()).isEqualTo(1d); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all_but_BOOLEAN_MEASURE") | @UseDataProvider("all_but_BOOLEAN_MEASURE") | ||||
public void getBooleanValue_throws_ISE_for_all_value_types_except_BOOLEAN(Measure measure) { | public void getBooleanValue_throws_ISE_for_all_value_types_except_BOOLEAN(Measure measure) { | ||||
measure.getBooleanValue(); | |||||
assertThatThrownBy(measure::getBooleanValue) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(FALSE_MEASURE.getBooleanValue()).isFalse(); | assertThat(FALSE_MEASURE.getBooleanValue()).isFalse(); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all_but_STRING_MEASURE") | @UseDataProvider("all_but_STRING_MEASURE") | ||||
public void getStringValue_throws_ISE_for_all_value_types_except_STRING(Measure measure) { | public void getStringValue_throws_ISE_for_all_value_types_except_STRING(Measure measure) { | ||||
measure.getStringValue(); | |||||
assertThatThrownBy(measure::getStringValue) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all_but_LEVEL_MEASURE") | @UseDataProvider("all_but_LEVEL_MEASURE") | ||||
public void getLevelValue_throws_ISE_for_all_value_types_except_LEVEL(Measure measure) { | public void getLevelValue_throws_ISE_for_all_value_types_except_LEVEL(Measure measure) { | ||||
measure.getLevelValue(); | |||||
assertThatThrownBy(measure::getLevelValue) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(measure.hasQualityGateStatus()).isFalse(); | assertThat(measure.hasQualityGateStatus()).isFalse(); | ||||
} | } | ||||
@Test(expected = IllegalStateException.class) | |||||
@Test | |||||
@UseDataProvider("all") | @UseDataProvider("all") | ||||
public void getAlertStatus_throws_ISE_for_all_value_types_when_not_set(Measure measure) { | public void getAlertStatus_throws_ISE_for_all_value_types_when_not_set(Measure measure) { | ||||
measure.getQualityGateStatus(); | |||||
assertThatThrownBy(measure::getQualityGateStatus) | |||||
.isInstanceOf(IllegalStateException.class); | |||||
} | } | ||||
@Test | @Test | ||||
assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create(Measure.Level.OK).getQualityGateStatus()).isEqualTo(someStatus); | assertThat(newMeasureBuilder().setQualityGateStatus(someStatus).create(Measure.Level.OK).getQualityGateStatus()).isEqualTo(someStatus); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void newMeasureBuilder_setQualityGateStatus_throws_NPE_if_arg_is_null() { | public void newMeasureBuilder_setQualityGateStatus_throws_NPE_if_arg_is_null() { | ||||
newMeasureBuilder().setQualityGateStatus(null); | |||||
assertThatThrownBy(() -> newMeasureBuilder().setQualityGateStatus(null)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void updateMeasureBuilder_setQualityGateStatus_throws_NPE_if_arg_is_null() { | public void updateMeasureBuilder_setQualityGateStatus_throws_NPE_if_arg_is_null() { | ||||
Measure.updatedMeasureBuilder(newMeasureBuilder().createNoValue()).setQualityGateStatus(null); | |||||
assertThatThrownBy(() -> { | |||||
Measure.updatedMeasureBuilder(newMeasureBuilder().createNoValue()).setQualityGateStatus(null); | |||||
}) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = UnsupportedOperationException.class) | |||||
@Test | |||||
public void updateMeasureBuilder_setQualityGateStatus_throws_USO_if_measure_already_has_a_QualityGateStatus() { | public void updateMeasureBuilder_setQualityGateStatus_throws_USO_if_measure_already_has_a_QualityGateStatus() { | ||||
QualityGateStatus qualityGateStatus = new QualityGateStatus(Measure.Level.ERROR); | |||||
Measure.updatedMeasureBuilder(newMeasureBuilder().setQualityGateStatus(qualityGateStatus).createNoValue()).setQualityGateStatus(qualityGateStatus); | |||||
assertThatThrownBy(() -> { | |||||
QualityGateStatus qualityGateStatus = new QualityGateStatus(Measure.Level.ERROR); | |||||
Measure.updatedMeasureBuilder(newMeasureBuilder().setQualityGateStatus(qualityGateStatus).createNoValue()).setQualityGateStatus(qualityGateStatus); | |||||
}) | |||||
.isInstanceOf(UnsupportedOperationException.class); | |||||
} | } | ||||
@Test(expected = UnsupportedOperationException.class) | |||||
@Test | |||||
public void updateMeasureBuilder_setVariations_throws_USO_if_measure_already_has_Variations() { | public void updateMeasureBuilder_setVariations_throws_USO_if_measure_already_has_Variations() { | ||||
Measure.updatedMeasureBuilder(newMeasureBuilder().setVariation(1d).createNoValue()).setVariation(2d); | |||||
assertThatThrownBy(() -> Measure.updatedMeasureBuilder(newMeasureBuilder().setVariation(1d).createNoValue()).setVariation(2d)) | |||||
.isInstanceOf(UnsupportedOperationException.class); | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void create_with_double_value_throws_IAE_if_value_is_NaN() { | public void create_with_double_value_throws_IAE_if_value_is_NaN() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("NaN is not allowed as a Measure value"); | |||||
newMeasureBuilder().create(Double.NaN, 1); | |||||
assertThatThrownBy(() -> newMeasureBuilder().create(Double.NaN, 1)) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("NaN is not allowed as a Measure value"); | |||||
} | } | ||||
@Test | @Test | ||||
public void create_with_double_value_data_throws_IAE_if_value_is_NaN() { | public void create_with_double_value_data_throws_IAE_if_value_is_NaN() { | ||||
expectedException.expect(IllegalArgumentException.class); | |||||
expectedException.expectMessage("NaN is not allowed as a Measure value"); | |||||
newMeasureBuilder().create(Double.NaN, 1, "some data"); | |||||
assertThatThrownBy(() -> newMeasureBuilder().create(Double.NaN, 1, "some data")) | |||||
.isInstanceOf(IllegalArgumentException.class) | |||||
.hasMessage("NaN is not allowed as a Measure value"); | |||||
} | } | ||||
} | } |
import org.sonar.db.measure.MeasureDto; | import org.sonar.db.measure.MeasureDto; | ||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |||||
@RunWith(DataProviderRunner.class) | @RunWith(DataProviderRunner.class) | ||||
public class MeasureToMeasureDtoTest { | public class MeasureToMeasureDtoTest { | ||||
analysisMetadataHolder.setUuid(ANALYSIS_UUID); | analysisMetadataHolder.setUuid(ANALYSIS_UUID); | ||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasureDto_throws_NPE_if_Measure_arg_is_null() { | public void toMeasureDto_throws_NPE_if_Measure_arg_is_null() { | ||||
underTest.toMeasureDto(null, SOME_METRIC, SOME_COMPONENT); | |||||
assertThatThrownBy(() -> underTest.toMeasureDto(null, SOME_METRIC, SOME_COMPONENT)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@Test(expected = NullPointerException.class) | |||||
@Test | |||||
public void toMeasureDto_throws_NPE_if_Metric_arg_is_null() { | public void toMeasureDto_throws_NPE_if_Metric_arg_is_null() { | ||||
underTest.toMeasureDto(Measure.newMeasureBuilder().createNoValue(), null, SOME_COMPONENT); | |||||
assertThatThrownBy(() -> underTest.toMeasureDto(Measure.newMeasureBuilder().createNoValue(), null, SOME_COMPONENT)) | |||||
.isInstanceOf(NullPointerException.class); | |||||
} | } | ||||
@DataProvider | @DataProvider |
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | |||||
import org.mockito.ArgumentCaptor; | import org.mockito.ArgumentCaptor; | ||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import org.sonar.api.measures.CoreMetrics; | import org.sonar.api.measures.CoreMetrics; | ||||
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); | public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); | ||||
@Rule | @Rule | ||||
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); | public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); | ||||
@Rule | |||||
public ExpectedException expectedException = ExpectedException.none(); | |||||
@Test | @Test | ||||
public void execute_extensions() { | public void execute_extensions() { |