From 592cb1f45be74a17310ab7bb623c3e0e54dbea9f Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 16 Jul 2012 15:43:31 +0200 Subject: [PATCH] Use ArgumentMatcher --- .../core/sensors/ProjectLinksSensorTest.java | 24 +++++++++---------- .../NewCoverageFileAnalyzerTest.java | 17 ++++++------- .../squid/bridges/DesignBridgeTest.java | 13 +++++----- .../phases/MavenPluginsConfiguratorTest.java | 16 ++++++------- .../java/org/sonar/api/test/IsResource.java | 12 ++++------ .../org/sonar/api/test/IsRuleMeasure.java | 10 +++----- .../java/org/sonar/api/test/IsViolation.java | 10 +++----- .../configuration/PropertiesBackupTest.java | 19 +++++++-------- .../platform/PersistentSettingsTest.java | 13 +++++----- .../platform/SettingsChangeNotifierTest.java | 9 +++---- .../startup/ServerMetadataPersisterTest.java | 14 +++++------ .../SetDefaultProjectPermissionsTest.java | 15 ++++++------ 12 files changed, 76 insertions(+), 96 deletions(-) diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java index f3012ab9395..0fd9f9505fb 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ProjectLinksSensorTest.java @@ -19,21 +19,24 @@ */ package org.sonar.plugins.core.sensors; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; - import org.apache.commons.lang.StringUtils; import org.apache.maven.project.MavenProject; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectLink; import org.sonar.api.test.MavenTestUtils; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + public class ProjectLinksSensorTest { @Test @@ -77,7 +80,7 @@ public class ProjectLinksSensorTest { verify(context).deleteLink(ProjectLinksSensor.KEY_SCM_DEVELOPER_CONNECTION); } - private class MatchLink extends BaseMatcher { + private class MatchLink extends ArgumentMatcher { private String key; private String name; private String url; @@ -88,14 +91,11 @@ public class ProjectLinksSensorTest { this.url = url; } + @Override public boolean matches(Object o) { ProjectLink link = (ProjectLink) o; return StringUtils.equals(link.getHref(), url) && StringUtils.equals(link.getKey(), key) && StringUtils.equals(link.getName(), name); } - - public void describeTo(Description description) { - - } } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzerTest.java index 708c53a53c3..e3c44deea6d 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzerTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewCoverageFileAnalyzerTest.java @@ -19,9 +19,8 @@ */ package org.sonar.plugins.core.timemachine; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; @@ -34,7 +33,12 @@ import java.util.Arrays; import java.util.Date; import java.util.List; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class NewCoverageFileAnalyzerTest { @@ -142,7 +146,7 @@ public class NewCoverageFileAnalyzerTest { } - static class VariationMatcher extends BaseMatcher { + static class VariationMatcher extends ArgumentMatcher { Metric metric; int index; Double variation; @@ -153,6 +157,7 @@ public class NewCoverageFileAnalyzerTest { this.variation = variation; } + @Override public boolean matches(Object o) { Measure m = (Measure)o; if (m.getMetric().equals(metric)) { @@ -163,10 +168,6 @@ public class NewCoverageFileAnalyzerTest { } return false; } - - public void describeTo(Description description) { - - } } private AbstractNewCoverageFileAnalyzer newDecorator() throws ParseException { diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/DesignBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/DesignBridgeTest.java index 6db7a515b14..0142f234bfb 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/DesignBridgeTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/DesignBridgeTest.java @@ -19,16 +19,17 @@ */ package org.sonar.plugins.squid.bridges; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.design.Dependency; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.JavaPackage; import static org.hamcrest.number.OrderingComparisons.greaterThan; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.anyDouble; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -64,7 +65,7 @@ public class DesignBridgeTest extends BridgeTestCase { verify(context).saveMeasure(eq(new JavaPackage("org.apache.struts.config")), eq(CoreMetrics.FILE_EDGES_WEIGHT), argThat(greaterThan(1.0))); } - static class DependencyMatcher extends BaseMatcher { + static class DependencyMatcher extends ArgumentMatcher { private String from; private String to; @@ -73,6 +74,7 @@ public class DesignBridgeTest extends BridgeTestCase { this.to = to; } + @Override public boolean matches(Object o) { if (!(o instanceof Dependency)) { return false; @@ -80,8 +82,5 @@ public class DesignBridgeTest extends BridgeTestCase { Dependency dep = (Dependency) o; return from.equals(dep.getFrom().getKey()) && to.equals(dep.getTo().getKey()); } - - public void describeTo(Description description) { - } } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/MavenPluginsConfiguratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/MavenPluginsConfiguratorTest.java index 32f1643c291..bddf0031a1b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/MavenPluginsConfiguratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/MavenPluginsConfiguratorTest.java @@ -19,9 +19,8 @@ */ package org.sonar.batch.phases; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.batch.BatchExtensionDictionnary; import org.sonar.api.batch.maven.MavenPlugin; import org.sonar.api.batch.maven.MavenPluginHandler; @@ -30,8 +29,12 @@ import org.sonar.api.test.MavenTestUtils; import java.util.Arrays; +import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; +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 MavenPluginsConfiguratorTest { @@ -57,19 +60,16 @@ public class MavenPluginsConfiguratorTest { verify(handler2).configure(eq(project), argThat(new IsMavenPlugin("myartifact2"))); } - private class IsMavenPlugin extends BaseMatcher { + private class IsMavenPlugin extends ArgumentMatcher { private String artifactId; public IsMavenPlugin(String artifactId) { this.artifactId = artifactId; } + @Override public boolean matches(Object o) { return artifactId.equals(((MavenPlugin) o).getPlugin().getArtifactId()); } - - public void describeTo(Description description) { - - } } } 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 index 9d6474bf087..ea7cc4e39d2 100644 --- 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 @@ -21,11 +21,10 @@ package org.sonar.api.test; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; +import org.mockito.ArgumentMatcher; import org.sonar.api.resources.Resource; -public class IsResource extends BaseMatcher { +public class IsResource extends ArgumentMatcher { private String scope; private String qualifier; @@ -41,13 +40,10 @@ public class IsResource extends BaseMatcher { this.key = key; } + @Override public boolean matches(Object o) { Resource r = (Resource) o; boolean keyMatch = (key != null) ? StringUtils.equals(r.getKey(), key) : true; return ObjectUtils.equals(r.getScope(), scope) && ObjectUtils.equals(r.getQualifier(), qualifier) && keyMatch; } - - public void describeTo(Description description) { - - } -} \ No newline at end of file +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsRuleMeasure.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsRuleMeasure.java index 01a403effd5..55716acf9ef 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsRuleMeasure.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsRuleMeasure.java @@ -21,14 +21,13 @@ package org.sonar.api.test; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.math.NumberUtils; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; +import org.mockito.ArgumentMatcher; import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric; import org.sonar.api.measures.RuleMeasure; import org.sonar.api.rules.Rule; -public class IsRuleMeasure extends BaseMatcher { +public class IsRuleMeasure extends ArgumentMatcher { private Metric metric = null; private Rule rule = null; @@ -40,6 +39,7 @@ public class IsRuleMeasure extends BaseMatcher { this.value = value; } + @Override public boolean matches(Object o) { if (!(o instanceof RuleMeasure)) { return false; @@ -49,8 +49,4 @@ public class IsRuleMeasure extends BaseMatcher { ObjectUtils.equals(rule, m.getRule()) && NumberUtils.compare(value, m.getValue()) == 0; } - - public void describeTo(Description description) { - - } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsViolation.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsViolation.java index a22211a2322..bdadc7628ad 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/test/IsViolation.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/IsViolation.java @@ -19,13 +19,12 @@ */ package org.sonar.api.test; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; +import org.mockito.ArgumentMatcher; import org.sonar.api.resources.Resource; import org.sonar.api.rules.Rule; import org.sonar.api.rules.Violation; -public class IsViolation extends BaseMatcher { +public class IsViolation extends ArgumentMatcher { private Rule rule; private String message; @@ -46,6 +45,7 @@ public class IsViolation extends BaseMatcher { this.lineId = lineId; } + @Override public boolean matches(Object o) { Violation violation = (Violation) o; if (lineId != null && !lineId.equals(violation.getLineId())) { @@ -66,8 +66,4 @@ public class IsViolation extends BaseMatcher { return true; } - - public void describeTo(Description description) { - - } } diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java index 5e91f5fe5b7..9468788be60 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/PropertiesBackupTest.java @@ -20,10 +20,9 @@ package org.sonar.server.configuration; import com.google.common.collect.ImmutableMap; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.CoreProperties; import org.sonar.api.database.configuration.Property; import org.sonar.jpa.test.AbstractDbUnitTestCase; @@ -35,7 +34,9 @@ import java.util.Map; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class PropertiesBackupTest extends AbstractDbUnitTestCase { @@ -77,14 +78,12 @@ public class PropertiesBackupTest extends AbstractDbUnitTestCase { backup.importXml(config); - verify(persistentSettings).saveProperties(argThat(new BaseMatcher>() { + verify(persistentSettings).saveProperties(argThat(new ArgumentMatcher>() { + @Override public boolean matches(Object o) { Map map = (Map) o; return map.get("key1").equals("value1") && map.get("key2").equals("value2"); } - - public void describeTo(Description description) { - } })); } @@ -98,14 +97,12 @@ public class PropertiesBackupTest extends AbstractDbUnitTestCase { config.setProperties(newProperties); backup.importXml(config); - verify(persistentSettings).saveProperties(argThat(new BaseMatcher>() { + verify(persistentSettings).saveProperties(argThat(new ArgumentMatcher>() { + @Override public boolean matches(Object o) { Map map = (Map) o; return map.get(CoreProperties.SERVER_ID).equals("111"); } - - public void describeTo(Description description) { - } })); } } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java index a4346e5866a..423041986b4 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java @@ -21,10 +21,9 @@ package org.sonar.server.platform; import com.google.common.collect.ImmutableMap; import org.apache.commons.configuration.PropertiesConfiguration; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.config.PropertyDefinitions; import org.sonar.core.properties.PropertiesDao; import org.sonar.core.properties.PropertyDto; @@ -35,7 +34,9 @@ import java.util.Arrays; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class PersistentSettingsTest { @@ -71,14 +72,12 @@ public class PersistentSettingsTest { // kept in memory cache and persisted in db assertThat(settings.getString("foo")).isEqualTo("bar"); - verify(dao).setProperty(argThat(new BaseMatcher() { + verify(dao).setProperty(argThat(new ArgumentMatcher() { + @Override public boolean matches(Object o) { PropertyDto dto = (PropertyDto) o; return dto.getKey().equals("foo"); } - - public void describeTo(Description description) { - } })); } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java b/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java index 05755d6661d..326fddda5c2 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/SettingsChangeNotifierTest.java @@ -19,9 +19,8 @@ */ package org.sonar.server.platform; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.config.GlobalPropertyChangeHandler; import static org.fest.assertions.Assertions.assertThat; @@ -37,14 +36,12 @@ public class SettingsChangeNotifierTest { notifier.onGlobalPropertyChange("foo", "bar"); - verify(handler).onChange(argThat(new BaseMatcher() { + verify(handler).onChange(argThat(new ArgumentMatcher() { + @Override public boolean matches(Object o) { GlobalPropertyChangeHandler.PropertyChange change = (GlobalPropertyChangeHandler.PropertyChange) o; return change.getKey().equals("foo") && change.getNewValue().equals("bar"); } - - public void describeTo(Description description) { - } })); } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/ServerMetadataPersisterTest.java b/sonar-server/src/test/java/org/sonar/server/startup/ServerMetadataPersisterTest.java index ba00c5c61cd..67dd9290b6c 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/ServerMetadataPersisterTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/ServerMetadataPersisterTest.java @@ -19,11 +19,10 @@ */ package org.sonar.server.startup; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.CoreProperties; import org.sonar.api.platform.Server; import org.sonar.server.platform.PersistentSettings; @@ -35,7 +34,9 @@ import java.util.Map; import java.util.TimeZone; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class ServerMetadataPersisterTest { @@ -65,7 +66,8 @@ public class ServerMetadataPersisterTest { ServerMetadataPersister persister = new ServerMetadataPersister(server, persistentSettings); persister.start(); - verify(persistentSettings).saveProperties(argThat(new BaseMatcher>() { + verify(persistentSettings).saveProperties(argThat(new ArgumentMatcher>() { + @Override public boolean matches(Object o) { Map map = (Map) o; return map.get(CoreProperties.SERVER_ID).equals("123") @@ -73,11 +75,7 @@ public class ServerMetadataPersisterTest { && map.get(CoreProperties.SERVER_STARTTIME).equals("2010-05-18T17:59:00+0000") && map.size() == 3; } - - public void describeTo(Description description) { - } })); } - } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java index 0f7ef2329f1..3b2cba14604 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java @@ -19,16 +19,19 @@ */ package org.sonar.server.startup; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.junit.Test; +import org.mockito.ArgumentMatcher; import org.sonar.api.config.Settings; import org.sonar.server.platform.PersistentSettings; import java.util.Map; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class SetDefaultProjectPermissionsTest { @Test @@ -39,14 +42,12 @@ public class SetDefaultProjectPermissionsTest { new SetDefaultProjectPermissions(persistentSettings).start(); - verify(persistentSettings).saveProperties(argThat(new BaseMatcher>() { + verify(persistentSettings).saveProperties(argThat(new ArgumentMatcher>() { + @Override public boolean matches(Object o) { Map map = (Map) o; return map.size() == 9 && map.get("sonar.role.admin.TRK.defaultGroups").equals("sonar-administrators"); } - - public void describeTo(Description description) { - } })); } -- 2.39.5