diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-11-23 22:01:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-23 22:01:21 +0100 |
commit | 4eb8c7f8657ebe5544fcd2283145d175713ab567 (patch) | |
tree | dd9e4078d15e052578874234bbb3761e20a448e4 | |
parent | e02d9d4a80eeb4f5cff53dd057e86df2c48d4f7e (diff) | |
download | sonarqube-4eb8c7f8657ebe5544fcd2283145d175713ab567.tar.gz sonarqube-4eb8c7f8657ebe5544fcd2283145d175713ab567.zip |
Support build of SonarQube with JDK 9
65 files changed, 319 insertions, 419 deletions
@@ -1016,6 +1016,11 @@ </dependency> <dependency> <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-core</artifactId> + <version>1.3</version> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> </dependency> diff --git a/server/sonar-ce/pom.xml b/server/sonar-ce/pom.xml index 301c2cb31f4..42dc89d5de0 100644 --- a/server/sonar-ce/pom.xml +++ b/server/sonar-ce/pom.xml @@ -53,12 +53,6 @@ <scope>test</scope> </dependency> <dependency> - <!-- TODO to be removed --> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java index 74b2a9503f7..753928597ed 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java @@ -19,23 +19,13 @@ */ package org.sonar.ce.queue; -import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Arrays.asList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.ce.container.ComputeEngineStatus.Status.STARTED; -import static org.sonar.ce.container.ComputeEngineStatus.Status.STOPPING; - +import com.google.common.collect.ImmutableSet; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.List; import java.util.Optional; import java.util.Random; - import javax.annotation.Nullable; - -import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -59,7 +49,13 @@ import org.sonar.server.computation.task.step.TypedException; import org.sonar.server.organization.DefaultOrganization; import org.sonar.server.organization.DefaultOrganizationProvider; -import com.google.common.collect.ImmutableSet; +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.ce.container.ComputeEngineStatus.Status.STARTED; +import static org.sonar.ce.container.ComputeEngineStatus.Status.STOPPING; public class InternalCeQueueImplTest { @@ -531,7 +527,7 @@ public class InternalCeQueueImplTest { CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get(); expectedException.expect(IllegalStateException.class); - expectedException.expectMessage(Matchers.startsWith("Task is in progress and can't be canceled")); + expectedException.expectMessage("Task is in progress and can't be canceled"); underTest.cancel(db.getSession(), queueDto); } diff --git a/server/sonar-db-dao/pom.xml b/server/sonar-db-dao/pom.xml index 8d308644fdd..f76bb60e10b 100644 --- a/server/sonar-db-dao/pom.xml +++ b/server/sonar-db-dao/pom.xml @@ -44,12 +44,6 @@ <scope>test</scope> </dependency> <dependency> - <!-- TODO to be removed --> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-guava</artifactId> <scope>test</scope> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java index b8639017123..999b160843e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/MyBatisTest.java @@ -27,7 +27,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.sonar.db.rule.RuleMapper; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; public class MyBatisTest { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java index 0295573904c..c163cb29b9d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/AnalysisPropertiesDaoTest.java @@ -157,7 +157,7 @@ public class AnalysisPropertiesDaoTest { assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(propertyDtos.size()); List<AnalysisPropertyDto> result = underTest.selectBySnapshotUuid(dbSession, snapshotUuid); - assertThat(result).containsExactlyInAnyOrder((AnalysisPropertyDto[]) propertyDtos.toArray()); + assertThat(result).containsExactlyInAnyOrder(propertyDtos.toArray(new AnalysisPropertyDto[0])); } private AnalysisPropertyDto insertAnalysisPropertyDto(int valueLength) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java index eb4a558fb17..de605579469 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeableAnalysisDtoTest.java @@ -23,9 +23,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class PurgeableAnalysisDtoTest { @Rule @@ -35,18 +33,18 @@ public class PurgeableAnalysisDtoTest { public void testEquals() { PurgeableAnalysisDto dto1 = new PurgeableAnalysisDto().setAnalysisUuid("u3"); PurgeableAnalysisDto dto2 = new PurgeableAnalysisDto().setAnalysisUuid("u4"); - assertThat(dto1.equals(dto2), is(false)); - assertThat(dto2.equals(dto1), is(false)); - assertThat(dto1.equals(dto1), is(true)); - assertThat(dto1.equals(new PurgeableAnalysisDto().setAnalysisUuid("u3")), is(true)); - assertThat(dto1.equals("bi_bop_a_lou_la"), is(false)); - assertThat(dto1.equals(null), is(false)); + assertThat(dto1.equals(dto2)).isFalse(); + assertThat(dto2.equals(dto1)).isFalse(); + assertThat(dto1.equals(dto1)).isTrue(); + assertThat(dto1.equals(new PurgeableAnalysisDto().setAnalysisUuid("u3"))).isTrue(); + assertThat(dto1.equals("bi_bop_a_lou_la")).isFalse(); + assertThat(dto1.equals(null)).isFalse(); } @Test public void testHasCode() { PurgeableAnalysisDto dto = new PurgeableAnalysisDto().setAnalysisUuid("u3"); - assertThat(dto.hashCode(), is(dto.hashCode())); + assertThat(dto.hashCode()).isEqualTo(dto.hashCode()); // no uuid => NPE dto = new PurgeableAnalysisDto(); @@ -58,6 +56,6 @@ public class PurgeableAnalysisDtoTest { @Test public void testToString() { PurgeableAnalysisDto dto = new PurgeableAnalysisDto().setAnalysisUuid("u3"); - assertThat(dto.toString().length(), greaterThan(0)); + assertThat(dto.toString()).isNotEmpty(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java b/server/sonar-server/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java index e49d5a9b784..95da611ad59 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java @@ -198,7 +198,7 @@ public class PermissionIndexerDao { if (projectUuids.isEmpty()) { sql = StringUtils.replace(SQL_TEMPLATE, "{projectsCondition}", ""); } else { - sql = StringUtils.replace(SQL_TEMPLATE, "{projectsCondition}", " AND (" + repeat("projects.uuid = ?", " or ", projectUuids.size()) + ")"); + sql = StringUtils.replace(SQL_TEMPLATE, "{projectsCondition}", " AND projects.uuid in (" + repeat("?", ", ", projectUuids.size()) + ")"); } PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(session, sql); int index = 1; diff --git a/server/sonar-server/src/test/java/org/sonar/elasticsearch/test/EsTestCluster.java b/server/sonar-server/src/test/java/org/sonar/elasticsearch/test/EsTestCluster.java index 91d403e4d37..c7287a7058a 100644 --- a/server/sonar-server/src/test/java/org/sonar/elasticsearch/test/EsTestCluster.java +++ b/server/sonar-server/src/test/java/org/sonar/elasticsearch/test/EsTestCluster.java @@ -100,7 +100,7 @@ import org.hamcrest.CoreMatchers; import org.hamcrest.MatcherAssert; import org.junit.Assert; -import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/AddTagsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/AddTagsActionTest.java index 5c9c31fd772..3d407a1477b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/AddTagsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/AddTagsActionTest.java @@ -20,53 +20,40 @@ package org.sonar.server.issue; import com.google.common.collect.ImmutableSet; -import java.util.Collection; +import java.util.HashMap; import java.util.Map; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.mockito.Matchers; +import org.mockito.Mockito; import org.sonar.core.issue.DefaultIssue; -import org.sonar.core.issue.IssueChangeContext; -import static com.google.common.collect.Maps.newHashMap; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class AddTagsActionTest { - private AddTagsAction action; - - private IssueFieldsSetter issueUpdater = mock(IssueFieldsSetter.class); - @Rule public ExpectedException throwable = ExpectedException.none(); - @Before - public void before() { - action = new AddTagsAction(issueUpdater); - } + private IssueFieldsSetter issueUpdater = new IssueFieldsSetter(); + private AddTagsAction underTest = new AddTagsAction(issueUpdater); @Test @SuppressWarnings("unchecked") public void should_execute() { - Map<String, Object> properties = newHashMap(); + Map<String, Object> properties = new HashMap<>(); properties.put("tags", "tag2,tag3"); DefaultIssue issue = mock(DefaultIssue.class); when(issue.tags()).thenReturn(ImmutableSet.of("tag1", "tag3")); - Action.Context context = mock(Action.Context.class); + Action.Context context = mock(Action.Context.class, Mockito.RETURNS_DEEP_STUBS); when(context.issue()).thenReturn(issue); - action.execute(properties, context); - verify(issueUpdater).setTags(eq(issue), - (Collection<String>) Matchers.argThat(org.hamcrest.Matchers.containsInAnyOrder("tag1", "tag2", "tag3")), - any(IssueChangeContext.class)); + underTest.execute(properties, context); + verify(issue).setTags(ImmutableSet.of("tag1", "tag2", "tag3")); } @Test @@ -74,7 +61,7 @@ public class AddTagsActionTest { throwable.expect(IllegalArgumentException.class); throwable.expectMessage("Tag 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'"); - Map<String, Object> properties = newHashMap(); + Map<String, Object> properties = new HashMap<>(); properties.put("tags", "th ag"); DefaultIssue issue = mock(DefaultIssue.class); @@ -83,6 +70,6 @@ public class AddTagsActionTest { Action.Context context = mock(Action.Context.class); when(context.issue()).thenReturn(issue); - action.execute(properties, context); + underTest.execute(properties, context); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/RemoveTagsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/RemoveTagsActionTest.java index fddefaa2f25..c81a2fdc2e8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/RemoveTagsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/RemoveTagsActionTest.java @@ -20,36 +20,25 @@ package org.sonar.server.issue; import com.google.common.collect.ImmutableSet; -import java.util.Collection; import java.util.Map; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.mockito.Matchers; +import org.mockito.Mockito; import org.sonar.core.issue.DefaultIssue; -import org.sonar.core.issue.IssueChangeContext; import static com.google.common.collect.Maps.newHashMap; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class RemoveTagsActionTest { - private RemoveTagsAction action; - - private IssueFieldsSetter issueUpdater = mock(IssueFieldsSetter.class); - @Rule public ExpectedException throwable = ExpectedException.none(); - @Before - public void before() { - action = new RemoveTagsAction(issueUpdater); - } + private IssueFieldsSetter issueUpdater = new IssueFieldsSetter(); + private RemoveTagsAction action = new RemoveTagsAction(issueUpdater); @Test @SuppressWarnings("unchecked") @@ -60,13 +49,11 @@ public class RemoveTagsActionTest { DefaultIssue issue = mock(DefaultIssue.class); when(issue.tags()).thenReturn(ImmutableSet.of("tag1", "tag3")); - Action.Context context = mock(Action.Context.class); + Action.Context context = mock(Action.Context.class, Mockito.RETURNS_DEEP_STUBS); when(context.issue()).thenReturn(issue); action.execute(properties, context); - verify(issueUpdater).setTags(eq(issue), - (Collection<String>) Matchers.argThat(org.hamcrest.Matchers.containsInAnyOrder("tag1")), - any(IssueChangeContext.class)); + verify(issue).setTags(ImmutableSet.of("tag1")); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/notification/NotificationDispatcherTest.java b/server/sonar-server/src/test/java/org/sonar/server/notification/NotificationDispatcherTest.java index 8b2f1bc150c..fed6d37ab6d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/notification/NotificationDispatcherTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/notification/NotificationDispatcherTest.java @@ -26,7 +26,7 @@ import org.mockito.MockitoAnnotations; import org.sonar.api.notifications.Notification; import org.sonar.api.notifications.NotificationChannel; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; diff --git a/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java index 0384a8f5747..33fabe367f1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/notification/email/AlertsEmailTemplateTest.java @@ -25,8 +25,8 @@ import org.sonar.api.config.EmailSettings; import org.sonar.api.notifications.Notification; import org.sonar.plugins.emailnotifications.api.EmailMessage; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java index 32414882dc1..f967be7f164 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java @@ -26,8 +26,8 @@ import org.sonar.api.database.model.User; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/UserSessionFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/UserSessionFilterTest.java index 79ef9e2d898..039d391c529 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/UserSessionFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/UserSessionFilterTest.java @@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; import org.mockito.InOrder; -import org.mockito.Mockito; +import org.sonar.core.platform.ComponentContainer; import org.sonar.db.DBSessions; import org.sonar.server.authentication.UserSessionInitializer; import org.sonar.server.organization.DefaultOrganizationCache; @@ -48,7 +48,8 @@ import static org.mockito.Mockito.when; public class UserSessionFilterTest { private UserSessionInitializer userSessionInitializer = mock(UserSessionInitializer.class); - private Platform platform = mock(Platform.class, Mockito.RETURNS_DEEP_STUBS); + private ComponentContainer container = new ComponentContainer(); + private Platform platform = mock(Platform.class); private HttpServletRequest request = mock(HttpServletRequest.class); private HttpServletResponse response = mock(HttpServletResponse.class); private FilterChain chain = mock(FilterChain.class); @@ -59,9 +60,8 @@ public class UserSessionFilterTest { @Before public void setUp() { - when(platform.getContainer().getComponentByType(DBSessions.class)).thenReturn(dbSessions); - when(platform.getContainer().getComponentByType(ThreadLocalSettings.class)).thenReturn(settings); - when(platform.getContainer().getComponentByType(DefaultOrganizationCache.class)).thenReturn(defaultOrganizationCache); + container.add(dbSessions, settings, defaultOrganizationCache); + when(platform.getContainer()).thenReturn(container); } @Test @@ -86,8 +86,6 @@ public class UserSessionFilterTest { @Test public void does_nothing_when_not_initialized() throws Exception { - mockNoUserSessionInitializer(); - underTest.doFilter(request, response, chain); verify(chain).doFilter(request, response); @@ -96,8 +94,6 @@ public class UserSessionFilterTest { @Test public void doFilter_loads_and_unloads_settings() throws Exception { - mockNoUserSessionInitializer(); - underTest.doFilter(request, response, chain); InOrder inOrder = inOrder(settings); @@ -108,7 +104,6 @@ public class UserSessionFilterTest { @Test public void doFilter_unloads_Settings_even_if_chain_throws_exception() throws Exception { - mockNoUserSessionInitializer(); RuntimeException thrown = mockChainDoFilterError(); try { @@ -122,8 +117,6 @@ public class UserSessionFilterTest { @Test public void doFilter_enables_and_disables_caching_in_DbSessions() throws Exception { - mockNoUserSessionInitializer(); - underTest.doFilter(request, response, chain); InOrder inOrder = inOrder(dbSessions); @@ -134,7 +127,6 @@ public class UserSessionFilterTest { @Test public void doFilter_disables_caching_in_DbSessions_even_if_chain_throws_exception() throws Exception { - mockNoUserSessionInitializer(); RuntimeException thrown = mockChainDoFilterError(); try { @@ -148,7 +140,6 @@ public class UserSessionFilterTest { @Test public void doFilter_unloads_Settings_even_if_DefaultOrganizationCache_unload_fails() throws Exception { - mockNoUserSessionInitializer(); RuntimeException thrown = new RuntimeException("Faking DefaultOrganizationCache.unload failing"); doThrow(thrown) .when(defaultOrganizationCache) @@ -178,8 +169,6 @@ public class UserSessionFilterTest { @Test public void doFilter_loads_and_unloads_DefaultOrganizationCache() throws Exception { - mockNoUserSessionInitializer(); - underTest.doFilter(request, response, chain); InOrder inOrder = inOrder(defaultOrganizationCache); @@ -190,7 +179,6 @@ public class UserSessionFilterTest { @Test public void doFilter_unloads_DefaultOrganizationCache_even_if_chain_throws_exception() throws Exception { - mockNoUserSessionInitializer(); RuntimeException thrown = mockChainDoFilterError(); try { @@ -204,7 +192,6 @@ public class UserSessionFilterTest { @Test public void doFilter_unloads_DefaultOrganizationCache_even_if_Settings_unload_fails() throws Exception { - mockNoUserSessionInitializer(); RuntimeException thrown = new RuntimeException("Faking Settings.unload failing"); doThrow(thrown) .when(settings) @@ -240,17 +227,13 @@ public class UserSessionFilterTest { // do not fail } - private void mockNoUserSessionInitializer() { - when(platform.getContainer().getComponentByType(UserSessionInitializer.class)).thenReturn(null); - } - private void mockUserSessionInitializer(boolean value) { - when(platform.getContainer().getComponentByType(UserSessionInitializer.class)).thenReturn(userSessionInitializer); + container.add(userSessionInitializer); when(userSessionInitializer.initUserSession(request, response)).thenReturn(value); } private RuntimeException mockUserSessionInitializerRemoveUserSessionFailing() { - when(platform.getContainer().getComponentByType(UserSessionInitializer.class)).thenReturn(userSessionInitializer); + container.add(userSessionInitializer); RuntimeException thrown = new RuntimeException("Faking UserSessionInitializer.removeUserSession failing"); doThrow(thrown) .when(userSessionInitializer) diff --git a/sonar-duplications/pom.xml b/sonar-duplications/pom.xml index 8d41b5acb8c..67ab7cc8488 100644 --- a/sonar-duplications/pom.xml +++ b/sonar-duplications/pom.xml @@ -39,11 +39,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> diff --git a/sonar-duplications/src/test/java/net/sourceforge/pmd/cpd/TokenEntryTest.java b/sonar-duplications/src/test/java/net/sourceforge/pmd/cpd/TokenEntryTest.java index 950da090b89..2d22ef7c6cb 100644 --- a/sonar-duplications/src/test/java/net/sourceforge/pmd/cpd/TokenEntryTest.java +++ b/sonar-duplications/src/test/java/net/sourceforge/pmd/cpd/TokenEntryTest.java @@ -22,8 +22,8 @@ package net.sourceforge.pmd.cpd; import org.junit.Before; import org.junit.Test; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertThat; public class TokenEntryTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTest.java index 41dd7677a2e..27fbc510958 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTest.java @@ -19,15 +19,14 @@ */ package org.sonar.duplications.block; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - import java.util.List; - import org.junit.Test; import org.sonar.duplications.statement.Statement; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + public class BlockChunkerTest extends BlockChunkerTestCase { @Override diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java index 86716386be7..9dfbb5eead6 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockChunkerTestCase.java @@ -25,10 +25,10 @@ import java.util.List; import org.junit.Test; import org.sonar.duplications.statement.Statement; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertThat; /** diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockTest.java index 2d4a09573f6..135dfad77f1 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/block/BlockTest.java @@ -21,8 +21,8 @@ package org.sonar.duplications.block; import org.junit.Test; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertThat; public class BlockTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/block/ByteArrayTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/block/ByteArrayTest.java index c0844194124..eae430e7071 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/block/ByteArrayTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/block/ByteArrayTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.block; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/CloneGroupMatcher.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/CloneGroupMatcher.java index 499f5ede79c..3a58e443968 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/CloneGroupMatcher.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/CloneGroupMatcher.java @@ -25,7 +25,7 @@ import org.hamcrest.TypeSafeMatcher; import org.sonar.duplications.index.CloneGroup; import org.sonar.duplications.index.ClonePart; -import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.CoreMatchers.hasItem; public class CloneGroupMatcher extends TypeSafeMatcher<CloneGroup> { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/ContainsInComparatorTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/ContainsInComparatorTest.java index c025a7ca2e7..ae2de81d325 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/ContainsInComparatorTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/ContainsInComparatorTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.detector; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; import java.util.Comparator; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java index ee3e8b32232..dc3e9bdfc2a 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/DetectorTestCase.java @@ -34,8 +34,8 @@ import org.sonar.duplications.index.MemoryCloneIndex; import org.sonar.duplications.junit.TestNamePrinter; import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/BlocksGroupTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/BlocksGroupTest.java index 68998fa8792..cee36b4620b 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/BlocksGroupTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/BlocksGroupTest.java @@ -22,7 +22,7 @@ package org.sonar.duplications.detector.original; import org.junit.Test; import org.sonar.duplications.block.Block; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class BlocksGroupTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/FilterTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/FilterTest.java index 4b9af7f5c81..868bd4254c1 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/FilterTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/original/FilterTest.java @@ -25,7 +25,7 @@ import org.sonar.duplications.index.ClonePart; import java.util.Arrays; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.*; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithmTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithmTest.java index 034a980b203..ff68df361e0 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithmTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/SuffixTreeCloneDetectionAlgorithmTest.java @@ -30,7 +30,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/index/DataUtilsTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/index/DataUtilsTest.java index c73de4abe9e..1b6fd0285fe 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/index/DataUtilsTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/index/DataUtilsTest.java @@ -19,8 +19,8 @@ */ package org.sonar.duplications.index; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.util.Arrays; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java index a96823135a4..08d33c791e0 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/index/PackedMemoryCloneIndexTest.java @@ -30,8 +30,8 @@ import java.util.Collection; import java.util.Iterator; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertThat; public class PackedMemoryCloneIndexTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/PmdBlockChunkerTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/PmdBlockChunkerTest.java index f2cf7746e86..a60c7b42373 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/PmdBlockChunkerTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/PmdBlockChunkerTest.java @@ -26,7 +26,7 @@ import org.sonar.duplications.block.ByteArray; import java.util.Arrays; import java.util.List; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class PmdBlockChunkerTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/TokenizerBridgeTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/TokenizerBridgeTest.java index f21a8195a83..db0c4929916 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/TokenizerBridgeTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/internal/pmd/TokenizerBridgeTest.java @@ -31,7 +31,7 @@ import net.sourceforge.pmd.cpd.Tokens; import org.junit.Before; import org.junit.Test; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; public class TokenizerBridgeTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java index 4f02f68ba83..3bfd9dbaa05 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaDuplicationsFunctionalTest.java @@ -35,7 +35,7 @@ import org.sonar.duplications.token.TokenChunker; import static java.util.Arrays.asList; import static java.util.stream.Collectors.joining; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; /** diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java index 3ee629b73f9..8b309de4773 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/java/JavaTokenProducerTest.java @@ -26,22 +26,20 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.apache.commons.io.IOUtils; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; import org.junit.Test; import org.sonar.duplications.DuplicationsTestUtil; import org.sonar.duplications.token.Token; import org.sonar.duplications.token.TokenChunker; import org.sonar.duplications.token.TokenQueue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class JavaTokenProducerTest { + private static final Token NUMERIC_LITTERAL = new Token("$NUMBER", 1, 0); + private static final Token STRING_LITTERAL = new Token("$CHARS", 1, 0); private final TokenChunker chunker = JavaTokenProducer.build(); /** @@ -49,7 +47,7 @@ public class JavaTokenProducerTest { */ @Test public void shouldIgnoreWhitespaces() { - assertThat(chunk(" \t\f\n\r"), isTokens()); + assertThat(chunk(" \t\f\n\r")).isEmpty(); } /** @@ -57,15 +55,15 @@ public class JavaTokenProducerTest { */ @Test public void shouldIgnoreEndOfLineComment() { - assertThat(chunk("// This is a comment"), isTokens()); - assertThat(chunk("// This is a comment \n and_this_is_not"), isTokens(new Token("and_this_is_not", 2, 1))); + assertThat(chunk("// This is a comment")).isEmpty(); + assertThat(chunk("// This is a comment \n and_this_is_not")).containsExactly(new Token("and_this_is_not", 2, 1)); } @Test public void shouldIgnoreTraditionalComment() { - assertThat(chunk("/* This is a comment \n and the second line */"), isTokens()); - assertThat(chunk("/** This is a javadoc \n and the second line */"), isTokens()); - assertThat(chunk("/* this \n comment /* \n // /** ends \n here: */"), isTokens()); + assertThat(chunk("/* This is a comment \n and the second line */")).isEmpty(); + assertThat(chunk("/** This is a javadoc \n and the second line */")).isEmpty(); + assertThat(chunk("/* this \n comment /* \n // /** ends \n here: */")).isEmpty(); } /** @@ -73,23 +71,24 @@ public class JavaTokenProducerTest { */ @Test public void shouldPreserveIdentifiers() { - assertThat(chunk("String"), isTokens(new Token("String", 1, 0))); - assertThat(chunk("i3"), isTokens(new Token("i3", 1, 0))); - assertThat(chunk("MAX_VALUE"), isTokens(new Token("MAX_VALUE", 1, 0))); - assertThat(chunk("isLetterOrDigit"), isTokens(new Token("isLetterOrDigit", 1, 0))); + assertThat(chunk("String")).containsExactly(new Token("String", 1, 0)); + assertThat(chunk("i3")).containsExactly(new Token("i3", 1, 0)); + assertThat(chunk("MAX_VALUE")).containsExactly(new Token("MAX_VALUE", 1, 0)); + assertThat(chunk("isLetterOrDigit")).containsExactly(new Token("isLetterOrDigit", 1, 0)); - assertThat(chunk("_"), isTokens(new Token("_", 1, 0))); - assertThat(chunk("_123_"), isTokens(new Token("_123_", 1, 0))); - assertThat(chunk("_Field"), isTokens(new Token("_Field", 1, 0))); - assertThat(chunk("_Field5"), isTokens(new Token("_Field5", 1, 0))); + assertThat(chunk("_")).containsExactly(new Token("_", 1, 0)); + assertThat(chunk("_123_")).containsExactly(new Token("_123_", 1, 0)); + assertThat(chunk("_Field")).containsExactly(new Token("_Field", 1, 0)); + assertThat(chunk("_Field5")).containsExactly(new Token("_Field5", 1, 0)); - assertThat(chunk("$"), isTokens(new Token("$", 1, 0))); - assertThat(chunk("$field"), isTokens(new Token("$field", 1, 0))); + assertThat(chunk("$")).containsExactly(new Token("$", 1, 0)); + assertThat(chunk("$field")).containsExactly(new Token("$field", 1, 0)); - assertThat(chunk("i2j"), isTokens(new Token("i2j", 1, 0))); - assertThat(chunk("from1to4"), isTokens(new Token("from1to4", 1, 0))); + assertThat(chunk("i2j")).containsExactly(new Token("i2j", 1, 0)); + assertThat(chunk("from1to4")).containsExactly(new Token("from1to4", 1, 0)); - assertThat("identifier with unicode", chunk("αβγ"), isTokens(new Token("αβγ", 1, 0))); + // identifier with unicode + assertThat(chunk("αβγ")).containsExactly(new Token("αβγ", 1, 0)); } /** @@ -97,7 +96,10 @@ public class JavaTokenProducerTest { */ @Test public void shouldPreserverKeywords() { - assertThat(chunk("private static final"), isTokens(new Token("private", 1, 0), new Token("static", 1, 8), new Token("final", 1, 15))); + assertThat(chunk("private static final")).containsExactly( + new Token("private", 1, 0), + new Token("static", 1, 8), + new Token("final", 1, 15)); } /** @@ -105,27 +107,27 @@ public class JavaTokenProducerTest { */ @Test public void shouldNormalizeDecimalIntegerLiteral() { - assertThat(chunk("543"), isNumericLiteral()); - assertThat(chunk("543l"), isNumericLiteral()); - assertThat(chunk("543L"), isNumericLiteral()); + assertThat(chunk("543")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("543l")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("543L")).containsExactly(NUMERIC_LITTERAL); } @Test public void shouldNormalizeOctalIntegerLiteral() { - assertThat(chunk("077"), isNumericLiteral()); - assertThat(chunk("077l"), isNumericLiteral()); - assertThat(chunk("077L"), isNumericLiteral()); + assertThat(chunk("077")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("077l")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("077L")).containsExactly(NUMERIC_LITTERAL); } @Test public void shouldNormalizeHexIntegerLiteral() { - assertThat(chunk("0xFF"), isNumericLiteral()); - assertThat(chunk("0xFFl"), isNumericLiteral()); - assertThat(chunk("0xFFL"), isNumericLiteral()); + assertThat(chunk("0xFF")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xFFl")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xFFL")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk("0XFF"), isNumericLiteral()); - assertThat(chunk("0XFFl"), isNumericLiteral()); - assertThat(chunk("0XFFL"), isNumericLiteral()); + assertThat(chunk("0XFF")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XFFl")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XFFL")).containsExactly(NUMERIC_LITTERAL); } /** @@ -133,13 +135,13 @@ public class JavaTokenProducerTest { */ @Test public void shouldNormalizeBinaryIntegerLiteral() { - assertThat(chunk("0b10"), isNumericLiteral()); - assertThat(chunk("0b10l"), isNumericLiteral()); - assertThat(chunk("0b10L"), isNumericLiteral()); + assertThat(chunk("0b10")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0b10l")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0b10L")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk("0B10"), isNumericLiteral()); - assertThat(chunk("0B10l"), isNumericLiteral()); - assertThat(chunk("0B10L"), isNumericLiteral()); + assertThat(chunk("0B10")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0B10l")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0B10L")).containsExactly(NUMERIC_LITTERAL); } /** @@ -148,72 +150,72 @@ public class JavaTokenProducerTest { @Test public void shouldNormalizeDecimalFloatingPointLiteral() { // with dot at the end - assertThat(chunk("1234."), isNumericLiteral()); - assertThat(chunk("1234.E1"), isNumericLiteral()); - assertThat(chunk("1234.e+1"), isNumericLiteral()); - assertThat(chunk("1234.E-1"), isNumericLiteral()); - assertThat(chunk("1234.f"), isNumericLiteral()); + assertThat(chunk("1234.")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234.E1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234.e+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234.E-1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234.f")).containsExactly(NUMERIC_LITTERAL); // with dot between - assertThat(chunk("12.34"), isNumericLiteral()); - assertThat(chunk("12.34E1"), isNumericLiteral()); - assertThat(chunk("12.34e+1"), isNumericLiteral()); - assertThat(chunk("12.34E-1"), isNumericLiteral()); + assertThat(chunk("12.34")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("12.34E1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("12.34e+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("12.34E-1")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk("12.34f"), isNumericLiteral()); - assertThat(chunk("12.34E1F"), isNumericLiteral()); - assertThat(chunk("12.34E+1d"), isNumericLiteral()); - assertThat(chunk("12.34e-1D"), isNumericLiteral()); + assertThat(chunk("12.34f")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("12.34E1F")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("12.34E+1d")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("12.34e-1D")).containsExactly(NUMERIC_LITTERAL); // with dot at the beginning - assertThat(chunk(".1234"), isNumericLiteral()); - assertThat(chunk(".1234e1"), isNumericLiteral()); - assertThat(chunk(".1234E+1"), isNumericLiteral()); - assertThat(chunk(".1234E-1"), isNumericLiteral()); + assertThat(chunk(".1234")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1234e1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1234E+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1234E-1")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk(".1234f"), isNumericLiteral()); - assertThat(chunk(".1234E1F"), isNumericLiteral()); - assertThat(chunk(".1234e+1d"), isNumericLiteral()); - assertThat(chunk(".1234E-1D"), isNumericLiteral()); + assertThat(chunk(".1234f")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1234E1F")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1234e+1d")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1234E-1D")).containsExactly(NUMERIC_LITTERAL); // without dot - assertThat(chunk("1234e1"), isNumericLiteral()); - assertThat(chunk("1234E+1"), isNumericLiteral()); - assertThat(chunk("1234E-1"), isNumericLiteral()); + assertThat(chunk("1234e1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234E+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234E-1")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk("1234E1f"), isNumericLiteral()); - assertThat(chunk("1234e+1d"), isNumericLiteral()); - assertThat(chunk("1234E-1D"), isNumericLiteral()); + assertThat(chunk("1234E1f")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234e+1d")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1234E-1D")).containsExactly(NUMERIC_LITTERAL); } @Test public void shouldNormalizeHexadecimalFloatingPointLiteral() { // with dot at the end - assertThat(chunk("0xAF."), isNumericLiteral()); - assertThat(chunk("0XAF.P1"), isNumericLiteral()); - assertThat(chunk("0xAF.p+1"), isNumericLiteral()); - assertThat(chunk("0XAF.p-1"), isNumericLiteral()); - assertThat(chunk("0xAF.f"), isNumericLiteral()); + assertThat(chunk("0xAF.")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAF.P1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAF.p+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAF.p-1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAF.f")).containsExactly(NUMERIC_LITTERAL); // with dot between - assertThat(chunk("0XAF.BC"), isNumericLiteral()); - assertThat(chunk("0xAF.BCP1"), isNumericLiteral()); - assertThat(chunk("0XAF.BCp+1"), isNumericLiteral()); - assertThat(chunk("0xAF.BCP-1"), isNumericLiteral()); + assertThat(chunk("0XAF.BC")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAF.BCP1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAF.BCp+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAF.BCP-1")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk("0xAF.BCf"), isNumericLiteral()); - assertThat(chunk("0xAF.BCp1F"), isNumericLiteral()); - assertThat(chunk("0XAF.BCP+1d"), isNumericLiteral()); - assertThat(chunk("0XAF.BCp-1D"), isNumericLiteral()); + assertThat(chunk("0xAF.BCf")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAF.BCp1F")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAF.BCP+1d")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAF.BCp-1D")).containsExactly(NUMERIC_LITTERAL); // without dot - assertThat(chunk("0xAFp1"), isNumericLiteral()); - assertThat(chunk("0XAFp+1"), isNumericLiteral()); - assertThat(chunk("0xAFp-1"), isNumericLiteral()); + assertThat(chunk("0xAFp1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAFp+1")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAFp-1")).containsExactly(NUMERIC_LITTERAL); - assertThat(chunk("0XAFp1f"), isNumericLiteral()); - assertThat(chunk("0xAFp+1d"), isNumericLiteral()); - assertThat(chunk("0XAFp-1D"), isNumericLiteral()); + assertThat(chunk("0XAFp1f")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xAFp+1d")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0XAFp-1D")).containsExactly(NUMERIC_LITTERAL); } /** @@ -221,19 +223,19 @@ public class JavaTokenProducerTest { */ @Test public void shouldNormalizeNumericLiteralsWithUnderscores() { - assertThat(chunk("54_3L"), isNumericLiteral()); - assertThat(chunk("07_7L"), isNumericLiteral()); - assertThat(chunk("0b1_0L"), isNumericLiteral()); - assertThat(chunk("0xF_FL"), isNumericLiteral()); - - assertThat(chunk("1_234."), isNumericLiteral()); - assertThat(chunk("1_2.3_4"), isNumericLiteral()); - assertThat(chunk(".1_234"), isNumericLiteral()); - assertThat(chunk("1_234e1_0"), isNumericLiteral()); - - assertThat(chunk("0xA_F."), isNumericLiteral()); - assertThat(chunk("0xA_F.B_C"), isNumericLiteral()); - assertThat(chunk("0x1.ffff_ffff_ffff_fP1_023"), isNumericLiteral()); + assertThat(chunk("54_3L")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("07_7L")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0b1_0L")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xF_FL")).containsExactly(NUMERIC_LITTERAL); + + assertThat(chunk("1_234.")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1_2.3_4")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk(".1_234")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("1_234e1_0")).containsExactly(NUMERIC_LITTERAL); + + assertThat(chunk("0xA_F.")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0xA_F.B_C")).containsExactly(NUMERIC_LITTERAL); + assertThat(chunk("0x1.ffff_ffff_ffff_fP1_023")).containsExactly(NUMERIC_LITTERAL); } /** @@ -241,7 +243,7 @@ public class JavaTokenProducerTest { */ @Test public void shouldPreserveBooleanLiterals() { - assertThat(chunk("true false"), isTokens(new Token("true", 1, 0), new Token("false", 1, 5))); + assertThat(chunk("true false")).containsExactly(new Token("true", 1, 0), new Token("false", 1, 5)); } /** @@ -249,11 +251,16 @@ public class JavaTokenProducerTest { */ @Test public void shouldNormalizeCharacterLiterals() { - assertThat("single character", chunk("'a'"), isStringLiteral()); - assertThat("escaped LF", chunk("'\\n'"), isStringLiteral()); - assertThat("escaped quote", chunk("'\\''"), isStringLiteral()); - assertThat("octal escape", chunk("'\\177'"), isStringLiteral()); - assertThat("unicode escape", chunk("'\\u03a9'"), isStringLiteral()); + // single character + assertThat(chunk("'a'")).containsExactly(STRING_LITTERAL); + // escaped LF + assertThat(chunk("'\\n'")).containsExactly(STRING_LITTERAL); + // escaped quote + assertThat(chunk("'\\''")).containsExactly(STRING_LITTERAL); + // octal escape + assertThat(chunk("'\\177'")).containsExactly(STRING_LITTERAL); + // unicode escape + assertThat(chunk("'\\u03a9'")).containsExactly(STRING_LITTERAL); } /** @@ -261,12 +268,18 @@ public class JavaTokenProducerTest { */ @Test public void shouldNormalizeStringLiterals() { - assertThat("regular string", chunk("\"string\""), isStringLiteral()); - assertThat("empty string", chunk("\"\""), isStringLiteral()); - assertThat("escaped LF", chunk("\"\\n\""), isStringLiteral()); - assertThat("escaped double quotes", chunk("\"string, which contains \\\"escaped double quotes\\\"\""), isStringLiteral()); - assertThat("octal escape", chunk("\"string \\177\""), isStringLiteral()); - assertThat("unicode escape", chunk("\"string \\u03a9\""), isStringLiteral()); + // regular string + assertThat(chunk("\"string\"")).containsExactly(STRING_LITTERAL); + // empty string + assertThat(chunk("\"\"")).containsExactly(STRING_LITTERAL); + // escaped LF + assertThat(chunk("\"\\n\"")).containsExactly(STRING_LITTERAL); + // escaped double quotes + assertThat(chunk("\"string, which contains \\\"escaped double quotes\\\"\"")).containsExactly(STRING_LITTERAL); + // octal escape + assertThat(chunk("\"string \\177\"")).containsExactly(STRING_LITTERAL); + // unicode escape + assertThat(chunk("\"string \\u03a9\"")).containsExactly(STRING_LITTERAL); } /** @@ -274,7 +287,7 @@ public class JavaTokenProducerTest { */ @Test public void shouldPreserverNullLiteral() { - assertThat(chunk("null"), isTokens(new Token("null", 1, 0))); + assertThat(chunk("null")).containsExactly(new Token("null", 1, 0)); } /** @@ -282,12 +295,12 @@ public class JavaTokenProducerTest { */ @Test public void shouldPreserveSeparators() { - assertThat(chunk("(){}[];,."), isTokens( + assertThat(chunk("(){}[];,.")).containsExactly( new Token("(", 1, 0), new Token(")", 1, 1), new Token("{", 1, 2), new Token("}", 1, 3), new Token("[", 1, 4), new Token("]", 1, 5), new Token(";", 1, 6), new Token(",", 1, 7), - new Token(".", 1, 8))); + new Token(".", 1, 8)); } /** @@ -295,17 +308,17 @@ public class JavaTokenProducerTest { */ @Test public void shouldPreserveOperators() { - assertThat(chunk("+="), isTokens(new Token("+", 1, 0), new Token("=", 1, 1))); - assertThat(chunk("--"), isTokens(new Token("-", 1, 0), new Token("-", 1, 1))); + assertThat(chunk("+=")).containsExactly(new Token("+", 1, 0), new Token("=", 1, 1)); + assertThat(chunk("--")).containsExactly(new Token("-", 1, 0), new Token("-", 1, 1)); } @Test public void realExamples() { File testFile = DuplicationsTestUtil.findFile("/java/MessageResources.java"); - assertThat(chunk(testFile).size(), Matchers.greaterThan(0)); + assertThat(chunk(testFile)).isNotEmpty(); testFile = DuplicationsTestUtil.findFile("/java/RequestUtils.java"); - assertThat(chunk(testFile).size(), Matchers.greaterThan(0)); + assertThat(chunk(testFile)).isNotEmpty(); } private TokenQueue chunk(File file) { @@ -320,21 +333,6 @@ public class JavaTokenProducerTest { } } - private static Matcher<List<Token>> isNumericLiteral() { - return isTokens(new Token("$NUMBER", 1, 0)); - } - - private static Matcher<List<Token>> isStringLiteral() { - return isTokens(new Token("$CHARS", 1, 0)); - } - - /** - * @return matcher for list of tokens - */ - private static Matcher<List<Token>> isTokens(Token... tokens) { - return is(Arrays.asList(tokens)); - } - private List<Token> chunk(String sourceCode) { List<Token> target = new ArrayList<>(); chunker.chunk(sourceCode).forEach(target::add); diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelDisptacherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelDisptacherTest.java index 0bc99930cfc..b710f2d00a2 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelDisptacherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelDisptacherTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyListOf; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelTest.java index 4bd873d4810..d44d344445e 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementChannelTest.java @@ -19,25 +19,23 @@ */ package org.sonar.duplications.statement; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -import org.mockito.Matchers; - import java.util.Arrays; import java.util.List; - import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.Matchers; import org.sonar.duplications.statement.matcher.AnyTokenMatcher; import org.sonar.duplications.statement.matcher.TokenMatcher; import org.sonar.duplications.token.Token; import org.sonar.duplications.token.TokenQueue; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + public class StatementChannelTest { @Test(expected = IllegalArgumentException.class) diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java index 96169310691..6b50fe3a45a 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.util.ArrayList; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/TokenMatcherFactoryTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/TokenMatcherFactoryTest.java index 305af7d05d8..e05a24c3b3a 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/TokenMatcherFactoryTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/TokenMatcherFactoryTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement; -import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/AnyTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/AnyTokenMatcherTest.java index 5443eba0f73..113928f37bc 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/AnyTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/AnyTokenMatcherTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement.matcher; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/BridgeTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/BridgeTokenMatcherTest.java index 89733e0f8ef..32fa219c3cc 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/BridgeTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/BridgeTokenMatcherTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement.matcher; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ExactTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ExactTokenMatcherTest.java index 684b67a2f74..bd4687fca2f 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ExactTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ExactTokenMatcherTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement.matcher; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java index a0746cbdd4f..30f35404864 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/ForgetLastTokenMatcherTest.java @@ -27,7 +27,7 @@ import org.junit.Test; import org.sonar.duplications.token.Token; import org.sonar.duplications.token.TokenQueue; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/OptTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/OptTokenMatcherTest.java index 9ffc1e0e78a..4d33697782b 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/OptTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/OptTokenMatcherTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement.matcher; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/UptoTokenMatcherTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/UptoTokenMatcherTest.java index 3e76a1b5b10..2e03a391a42 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/UptoTokenMatcherTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/matcher/UptoTokenMatcherTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.statement.matcher; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChannelTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChannelTest.java index 08eb2f861d7..0d00b892be2 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChannelTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChannelTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.token; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenQueueTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenQueueTest.java index 321235ca1ef..feee54254ca 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenQueueTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenQueueTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.token; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.util.ArrayList; diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java index 750cb0c7c5d..0111f0f76f5 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java @@ -23,8 +23,8 @@ import org.junit.Test; import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; public class TokenTest { diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/utils/SortedListsUtilsTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/utils/SortedListsUtilsTest.java index f1ba9d0cd4e..d7a6f8acf9a 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/utils/SortedListsUtilsTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/utils/SortedListsUtilsTest.java @@ -19,7 +19,7 @@ */ package org.sonar.duplications.utils; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.util.Arrays; diff --git a/sonar-duplications/test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java b/sonar-duplications/test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java index 8ba22b82a07..1b103c6eec2 100644 --- a/sonar-duplications/test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java +++ b/sonar-duplications/test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java @@ -3,8 +3,8 @@ */ package net.sourceforge.pmd.cpd.fork; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import java.nio.charset.Charset; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java deleted file mode 100644 index d72986ecd77..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsResource.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.test; - -import org.apache.commons.lang.ObjectUtils; -import org.mockito.ArgumentMatcher; -import org.sonar.api.resources.Resource; - -public class IsResource extends ArgumentMatcher<Resource> { - - private String scope; - private String qualifier; - private String key; - - public IsResource(String scope, String qualifier) { - this.scope = scope; - this.qualifier = qualifier; - } - - public IsResource(String scope, String qualifier, String key) { - this(scope, qualifier); - this.key = key; - } - - @Override - public boolean matches(Object o) { - Resource r = (Resource) o; - return ObjectUtils.equals(r.getScope(), scope) && ObjectUtils.equals(r.getQualifier(), qualifier) && r.getKey().equals(key); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java index ddc537947f5..71f09b96066 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeProfilerTest.java @@ -41,7 +41,7 @@ public class TimeProfilerTest { verify(logger).info(eq("{}..."), eq("Cycle analysis")); profiler.stop(); - verify(logger).info(eq("{} done: {} ms"), eq("Cycle analysis"), anyInt()); + verify(logger).info(eq("{} done: {} ms"), eq("Cycle analysis"), anyLong()); } @Test @@ -53,7 +53,7 @@ public class TimeProfilerTest { profiler.stop(); profiler.stop(); verify(logger, times(1)).info(anyString(), anyString()); // start() executes log() with 1 parameter - verify(logger, times(1)).info(anyString(), anyString(), anyInt()); // stop() executes log() with 3 parameters + verify(logger, times(1)).info(anyString(), anyString(), anyLong()); // stop() executes log() with 3 parameters } @Test @@ -64,7 +64,7 @@ public class TimeProfilerTest { profiler.start("New task"); profiler.stop(); profiler.stop(); - verify(logger, never()).info(eq("{} done: {} ms"), eq("Cycle analysis"), anyInt()); - verify(logger, times(1)).info(eq("{} done: {} ms"), eq("New task"), anyInt()); + verify(logger, never()).info(eq("{} done: {} ms"), eq("Cycle analysis"), anyLong()); + verify(logger, times(1)).info(eq("{} done: {} ms"), eq("New task"), anyLong()); } } diff --git a/sonar-scanner-engine/pom.xml b/sonar-scanner-engine/pom.xml index 086a9bec024..f92d507858e 100644 --- a/sonar-scanner-engine/pom.xml +++ b/sonar-scanner-engine/pom.xml @@ -109,11 +109,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>${project.groupId}</groupId> <artifactId>sonar-plugin-api</artifactId> <type>test-jar</type> diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java index 23c02f54f85..b9468dd8056 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilter.java @@ -41,9 +41,8 @@ public class IgnoreIssuesFilter implements IssueFilter { public boolean accept(FilterableIssue issue, IssueFilterChain chain) { if (hasMatchFor(issue)) { return false; - } else { - return chain.accept(issue); } + return chain.accept(issue); } private boolean hasMatchFor(FilterableIssue issue) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java index 60be7b5140e..6230c5ee925 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/QualityProfileProvider.java @@ -53,9 +53,8 @@ public class QualityProfileProvider extends ProviderAdapter { @CheckForNull private static String getSonarProfile(AnalysisProperties props) { - String profile = null; - if (props.properties().containsKey(ModuleQProfiles.SONAR_PROFILE_PROP)) { - profile = props.property(ModuleQProfiles.SONAR_PROFILE_PROP); + String profile = props.property(ModuleQProfiles.SONAR_PROFILE_PROP); + if (profile != null) { LOG.warn("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java index f3a47b46ec8..93d0e829a65 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java @@ -32,8 +32,8 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.scanner.scan.measure.MeasureCache; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java index 5dc8a09e511..6753f155734 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java @@ -19,8 +19,8 @@ */ package org.sonar.scanner.issue.ignore; -import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.sonar.api.rule.RuleKey; import org.sonar.api.scan.issue.filter.FilterableIssue; import org.sonar.api.scan.issue.filter.IssueFilterChain; @@ -35,32 +35,26 @@ import static org.mockito.Mockito.when; public class IgnoreIssuesFilterTest { - private PatternMatcher exclusionPatternMatcher; - private IgnoreIssuesFilter ignoreFilter; - private FilterableIssue issue; - private IssueFilterChain chain; - - @Before - public void init() { - exclusionPatternMatcher = mock(PatternMatcher.class); - issue = mock(FilterableIssue.class); - chain = mock(IssueFilterChain.class); - when(chain.accept(issue)).thenReturn(true); - - ignoreFilter = new IgnoreIssuesFilter(exclusionPatternMatcher); - } + private PatternMatcher exclusionPatternMatcher = mock(PatternMatcher.class); + private FilterableIssue issue = mock(FilterableIssue.class, Mockito.RETURNS_DEEP_STUBS); + private IssueFilterChain chain = mock(IssueFilterChain.class); + private IgnoreIssuesFilter underTest = new IgnoreIssuesFilter(exclusionPatternMatcher); @Test public void shouldPassToChainIfMatcherHasNoPatternForIssue() { - when(exclusionPatternMatcher.getMatchingPattern(anyString(), any(RuleKey.class), any(Integer.class))).thenReturn(null); - - assertThat(ignoreFilter.accept(issue, chain)).isTrue(); + when(exclusionPatternMatcher.getMatchingPattern(anyString(), any(RuleKey.class), any(Integer.class))) + .thenReturn(null); + when(chain.accept(issue)).thenReturn(true); + assertThat(underTest.accept(issue, chain)).isTrue(); } @Test - public void shouldAcceptOrRefuseIfMatcherHasPatternForIssue() { - when(exclusionPatternMatcher.getMatchingPattern(anyString(), any(RuleKey.class), any(Integer.class))).thenReturn(mock(IssuePattern.class)); + public void shouldRejectIfPatternMatches() { + IssuePattern pattern = mock(IssuePattern.class); + when(exclusionPatternMatcher.getMatchingPattern(anyString(), any(RuleKey.class), any(Integer.class))) + .thenReturn(pattern); - assertThat(ignoreFilter.accept(issue, chain)).isFalse(); + assertThat(underTest.accept(issue, chain)).isFalse(); } + } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/log/ExceptionHandlingMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/log/ExceptionHandlingMediumTest.java index 677cdfa53d7..a479662cd20 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/log/ExceptionHandlingMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/log/ExceptionHandlingMediumTest.java @@ -21,8 +21,8 @@ package org.sonar.scanner.mediumtest.log; import java.util.Collections; import java.util.Map; +import org.hamcrest.CoreMatchers; import org.hamcrest.Description; -import org.hamcrest.Matchers; import org.hamcrest.TypeSafeMatcher; import org.junit.BeforeClass; import org.junit.Rule; @@ -64,7 +64,7 @@ public class ExceptionHandlingMediumTest { loader.withCause = false; thrown.expect(MessageException.class); thrown.expectMessage("Error loading settings"); - thrown.expectCause(Matchers.nullValue(Throwable.class)); + thrown.expectCause(CoreMatchers.nullValue(Throwable.class)); batch.start(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java index 43a4ecbaaad..137a2bcbd05 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/QualityProfileProviderTest.java @@ -39,7 +39,6 @@ import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isNull; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -76,11 +75,11 @@ public class QualityProfileProviderTest { @Test public void testProvide() { - when(loader.load(eq("project"), isNull(String.class))).thenReturn(response); + when(loader.load("project", null)).thenReturn(response); ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); assertResponse(qps); - verify(loader).load(eq("project"), isNull(String.class)); + verify(loader).load("project", null); verifyNoMoreInteractions(loader); } @@ -88,6 +87,7 @@ public class QualityProfileProviderTest { public void testProjectDoesntExist() { when(projectRepo.exists()).thenReturn(false); when(loader.loadDefault(anyString())).thenReturn(response); + when(props.property(ModuleQProfiles.SONAR_PROFILE_PROP)).thenReturn("profile"); ModuleQProfiles qps = qualityProfileProvider.provide(key, loader, projectRepo, props); assertResponse(qps); @@ -127,7 +127,6 @@ public class QualityProfileProviderTest { } private void assertResponse(ModuleQProfiles qps) { - assertThat(qps.findAll()).hasSize(1); assertThat(qps.findAll()).extracting("key").containsExactly("profile"); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java index 51c36adc739..5e663c4b57a 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java @@ -36,6 +36,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class ModuleInputComponentStoreTest { @Rule @@ -120,10 +121,12 @@ public class ModuleInputComponentStoreTest { public void should_find_module_components_with_non_global_strategy() { InputComponentStore inputComponentStore = mock(InputComponentStore.class); SensorStrategy strategy = new SensorStrategy(); - ModuleInputComponentStore store = new ModuleInputComponentStore(mock(InputModule.class), inputComponentStore, strategy); + InputModule module = mock(InputModule.class); + when(module.key()).thenReturn("foo"); + ModuleInputComponentStore store = new ModuleInputComponentStore(module, inputComponentStore, strategy); store.inputFiles(); - verify(inputComponentStore).filesByModule(any(String.class)); + verify(inputComponentStore).filesByModule("foo"); String relativePath = "somepath"; store.inputFile(relativePath); diff --git a/sonar-testing-harness/pom.xml b/sonar-testing-harness/pom.xml index 1bfe094460e..d340723c1e9 100644 --- a/sonar-testing-harness/pom.xml +++ b/sonar-testing-harness/pom.xml @@ -24,6 +24,10 @@ <artifactId>junit</artifactId> </dependency> <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-core</artifactId> + </dependency> + <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> @@ -35,11 +39,6 @@ </dependency> <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedMatcherTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedMatcherTest.java index 26dcd8690e1..562b0cccc38 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedMatcherTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/i18n/BundleSynchronizedMatcherTest.java @@ -32,7 +32,11 @@ import java.io.InputStream; import java.util.SortedMap; import static junit.framework.TestCase.fail; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.*; public class BundleSynchronizedMatcherTest { diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/i18n/I18nMatchersTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/i18n/I18nMatchersTest.java index 149abda8f30..5ea1701bbbb 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/i18n/I18nMatchersTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/i18n/I18nMatchersTest.java @@ -23,7 +23,7 @@ import org.junit.Test; import java.io.File; -import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; diff --git a/tests/pom.xml b/tests/pom.xml index c36c4aaee20..535ee0bcc2a 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -83,8 +83,8 @@ <artifactId>assertj-core</artifactId> </dependency> <dependency> - <groupId>org.hamcrest</groupId> - <artifactId>hamcrest-all</artifactId> + <groupId>org.assertj</groupId> + <artifactId>assertj-guava</artifactId> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> diff --git a/tests/src/test/java/org/sonarqube/tests/plugins/checks/JavascriptCheck.java b/tests/src/test/java/org/sonarqube/tests/plugins/checks/JavascriptCheck.java index 40f74d64943..7fffd855060 100644 --- a/tests/src/test/java/org/sonarqube/tests/plugins/checks/JavascriptCheck.java +++ b/tests/src/test/java/org/sonarqube/tests/plugins/checks/JavascriptCheck.java @@ -30,6 +30,6 @@ public class JavascriptCheck implements Check { validation.mustHaveComments(SRC_DIR); validation.mustHaveComplexity(SRC_DIR); validation.mustHaveIssues(SRC_DIR + "/HasIssues.js"); - validation.mustHaveMeasuresGreaterThan(SRC_DIR + "/Person.js", 0, "coverage"); + validation.mustHaveMeasuresGreaterThanOrEquals(SRC_DIR + "/Person.js", 0, "coverage"); } } diff --git a/tests/src/test/java/org/sonarqube/tests/plugins/checks/Validation.java b/tests/src/test/java/org/sonarqube/tests/plugins/checks/Validation.java index 9b2f9a4ea21..0c1a76dba73 100644 --- a/tests/src/test/java/org/sonarqube/tests/plugins/checks/Validation.java +++ b/tests/src/test/java/org/sonarqube/tests/plugins/checks/Validation.java @@ -22,18 +22,19 @@ package org.sonarqube.tests.plugins.checks; import com.google.common.base.Joiner; import com.google.gson.Gson; import com.sonar.orchestrator.Orchestrator; -import org.sonarqube.tests.plugins.Project; import java.io.File; import java.util.List; import java.util.Map; -import org.hamcrest.Matchers; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; import org.junit.rules.ErrorCollector; +import org.sonarqube.tests.plugins.Project; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsResponse; import static java.util.Arrays.asList; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; import static util.ItUtils.getMeasuresAsDoubleByMetricKey; import static util.ItUtils.newAdminWsClient; @@ -57,22 +58,22 @@ public class Validation { public void mustHaveIssues(String path) { // TODO use the WS api/issues - mustHaveMeasuresGreaterThan(path, 1, "violations"); + mustHaveMeasuresGreaterThanOrEquals(path, 1, "violations"); } public void mustHaveComments(String path) { - mustHaveMeasuresGreaterThan(path, 0, "comment_lines", "comment_lines_density"); + mustHaveMeasuresGreaterThanOrEquals(path, 0, "comment_lines", "comment_lines_density"); } public void mustHaveComplexity(String path) { - mustHaveMeasuresGreaterThan(path, 0, "complexity"); + mustHaveMeasuresGreaterThanOrEquals(path, 0, "complexity"); } public void mustHaveSize(String path) { - mustHaveMeasuresGreaterThan(path, 0, "ncloc", "lines"); + mustHaveMeasuresGreaterThanOrEquals(path, 0, "ncloc", "lines"); } - public void mustHaveMeasuresGreaterThan(String path, int min, String... metricKeys) { + public void mustHaveMeasuresGreaterThanOrEquals(String path, int min, String... metricKeys) { for (String filePath : toFiles(path)) { fileMustHaveMeasures(filePath, metricKeys, min); } @@ -87,7 +88,17 @@ public class Validation { Double measure = measures.get(metricKey); errorCollector.checkThat("Measure " + metricKey + " is set on file " + filePath, measure, notNullValue()); if (measure != null) { - errorCollector.checkThat("Measure " + metricKey + " is positive on file " + filePath, measure.intValue(), Matchers.greaterThanOrEqualTo(min)); + errorCollector.checkThat("Measure " + metricKey + " is positive on file " + filePath, measure.intValue(), new TypeSafeMatcher<Integer>() { + @Override + protected boolean matchesSafely(Integer item) { + return item >= min; + } + + @Override + public void describeTo(Description description) { + description.appendText(metricKey).appendValue(min); + } + }); } } } @@ -111,7 +122,17 @@ public class Validation { errorCollector.checkThat("Source is set on file " + filePath, response.isSuccessful(), is(true)); Sources source = Sources.parse(response.content()); if (source != null) { - errorCollector.checkThat("Source is empty on file " + filePath, source.getSources().size(), Matchers.greaterThanOrEqualTo(minLines)); + errorCollector.checkThat("Source is empty on file " + filePath, source.getSources().size(), new TypeSafeMatcher<Integer>() { + @Override + protected boolean matchesSafely(Integer item) { + return item >= minLines; + } + + @Override + public void describeTo(Description description) { + description.appendValue(minLines); + } + }); } } } diff --git a/tests/src/test/java/util/ItUtils.java b/tests/src/test/java/util/ItUtils.java index f663c7d6b94..2e36b573136 100644 --- a/tests/src/test/java/util/ItUtils.java +++ b/tests/src/test/java/util/ItUtils.java @@ -335,7 +335,7 @@ public class ItUtils { private static Stream<Measure> getStreamMeasures(Orchestrator orchestrator, String componentKey, String... metricKeys) { return newWsClient(orchestrator).measures().component(new ComponentWsRequest() - .setComponentKey(componentKey) + .setComponent(componentKey) .setMetricKeys(asList(metricKeys))) .getComponent().getMeasuresList() .stream(); |