From 7632e96e2a93083bcba01499c363d7e0004b90c7 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Mon, 19 Jan 2015 10:37:37 +0100 Subject: [PATCH] Sonar 5.0 support Removed deprecations --- .../plugins/objectivec/core/ObjectiveC.java | 11 ++--- .../objectivec/coverage/CoberturaSensor.java | 18 +++++---- .../objectivec/violations/OCLintSensor.java | 17 ++++---- .../coverage/CoberturaSensorTest.java | 40 ++++++++++++++----- .../violations/OCLintSensorTest.java | 38 +++++++++++++----- 5 files changed, 86 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/sonar/plugins/objectivec/core/ObjectiveC.java b/src/main/java/org/sonar/plugins/objectivec/core/ObjectiveC.java index a5aa5025..25e3904d 100644 --- a/src/main/java/org/sonar/plugins/objectivec/core/ObjectiveC.java +++ b/src/main/java/org/sonar/plugins/objectivec/core/ObjectiveC.java @@ -21,8 +21,8 @@ import java.util.List; -import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; +import org.sonar.api.config.Settings; import org.sonar.api.resources.AbstractLanguage; import org.sonar.plugins.objectivec.ObjectiveCPlugin; @@ -32,15 +32,16 @@ public class ObjectiveC extends AbstractLanguage { public static final String KEY = "objc"; - private Configuration configuration; + private Settings settings; + + public ObjectiveC(Settings settings) { - public ObjectiveC(Configuration configuration) { super(KEY, "Objective-C"); - this.configuration = configuration; + this.settings = settings; } public String[] getFileSuffixes() { - String[] suffixes = filterEmptyStrings(configuration.getStringArray(ObjectiveCPlugin.FILE_SUFFIXES_KEY)); + String[] suffixes = filterEmptyStrings(settings.getStringArray(ObjectiveCPlugin.FILE_SUFFIXES_KEY)); if (suffixes == null || suffixes.length == 0) { suffixes = StringUtils.split(ObjectiveCPlugin.FILE_SUFFIXES_DEFVALUE, ","); } diff --git a/src/main/java/org/sonar/plugins/objectivec/coverage/CoberturaSensor.java b/src/main/java/org/sonar/plugins/objectivec/coverage/CoberturaSensor.java index 314a607a..0a900b2b 100644 --- a/src/main/java/org/sonar/plugins/objectivec/coverage/CoberturaSensor.java +++ b/src/main/java/org/sonar/plugins/objectivec/coverage/CoberturaSensor.java @@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.config.Settings; import org.sonar.api.measures.CoverageMeasuresBuilder; import org.sonar.api.resources.Project; @@ -42,17 +43,20 @@ public final class CoberturaSensor implements Sensor { private final ReportFilesFinder reportFilesFinder; private final CoberturaParser parser = new CoberturaParser(); - public CoberturaSensor() { - this(null); - } + private final Settings conf; + private final FileSystem fileSystem; + + public CoberturaSensor(final FileSystem fileSystem, final Settings config) { - public CoberturaSensor(final Settings config) { - reportFilesFinder = new ReportFilesFinder(config, REPORT_PATTERN_KEY, - DEFAULT_REPORT_PATTERN); + this.conf = config; + this.fileSystem = fileSystem; + + reportFilesFinder = new ReportFilesFinder(config, REPORT_PATTERN_KEY, DEFAULT_REPORT_PATTERN); } public boolean shouldExecuteOnProject(final Project project) { - return ObjectiveC.KEY.equals(project.getLanguageKey()); + + return project.isRoot() && fileSystem.languages().contains(ObjectiveC.KEY); } public void analyse(final Project project, final SensorContext context) { diff --git a/src/main/java/org/sonar/plugins/objectivec/violations/OCLintSensor.java b/src/main/java/org/sonar/plugins/objectivec/violations/OCLintSensor.java index 58a0704e..4847905f 100644 --- a/src/main/java/org/sonar/plugins/objectivec/violations/OCLintSensor.java +++ b/src/main/java/org/sonar/plugins/objectivec/violations/OCLintSensor.java @@ -25,29 +25,30 @@ import org.slf4j.LoggerFactory; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.api.rules.Violation; import org.sonar.plugins.objectivec.ObjectiveCPlugin; import org.sonar.plugins.objectivec.core.ObjectiveC; -import org.sonar.plugins.objectivec.violations.OCLintParser; public final class OCLintSensor implements Sensor { public static final String REPORT_PATH_KEY = ObjectiveCPlugin.PROPERTY_PREFIX + ".oclint.report"; public static final String DEFAULT_REPORT_PATH = "sonar-reports/oclint.xml"; - private final Settings conf; - public OCLintSensor() { - this(null); - } + private final Settings conf; + private final FileSystem fileSystem; - public OCLintSensor(final Settings config) { - conf = config; + public OCLintSensor(final FileSystem moduleFileSystem, final Settings config) { + this.conf = config; + this.fileSystem = moduleFileSystem; } public boolean shouldExecuteOnProject(final Project project) { - return ObjectiveC.KEY.equals(project.getLanguageKey()); + + return project.isRoot() && fileSystem.languages().contains(ObjectiveC.KEY); + } public void analyse(final Project project, final SensorContext context) { diff --git a/src/test/java/org/sonar/plugins/objectivec/coverage/CoberturaSensorTest.java b/src/test/java/org/sonar/plugins/objectivec/coverage/CoberturaSensorTest.java index 228d929a..84922956 100644 --- a/src/test/java/org/sonar/plugins/objectivec/coverage/CoberturaSensorTest.java +++ b/src/test/java/org/sonar/plugins/objectivec/coverage/CoberturaSensorTest.java @@ -21,21 +21,39 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import org.apache.commons.configuration.PropertiesConfiguration; +import org.junit.Before; import org.junit.Test; +import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; +import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.objectivec.core.ObjectiveC; +import java.util.SortedSet; +import java.util.TreeSet; + public final class CoberturaSensorTest { + private Settings settings; + + @Before + public void setUp() { + settings = new Settings(); + } + @Test public void shouldExecuteOnProjectShouldBeTrueWhenProjectIsObjc() { final Project project = new Project("Test"); - final CoberturaSensor testedSensor = new CoberturaSensor(); - final PropertiesConfiguration config = new PropertiesConfiguration(); - config.addProperty("sonar.language", ObjectiveC.KEY); - project.setConfiguration(config); + + FileSystem fileSystem = mock(FileSystem.class); + SortedSet languages = new TreeSet(); + languages.add(ObjectiveC.KEY); + when(fileSystem.languages()).thenReturn(languages); + + final CoberturaSensor testedSensor = new CoberturaSensor(fileSystem, settings); assertTrue(testedSensor.shouldExecuteOnProject(project)); } @@ -43,10 +61,14 @@ public void shouldExecuteOnProjectShouldBeTrueWhenProjectIsObjc() { @Test public void shouldExecuteOnProjectShouldBeFalseWhenProjectIsSomethingElse() { final Project project = new Project("Test"); - final CoberturaSensor testedSensor = new CoberturaSensor(); - final PropertiesConfiguration config = new PropertiesConfiguration(); - config.addProperty("sonar.language", "Test"); - project.setConfiguration(config); + settings.setProperty("sonar.language", "Test"); + + FileSystem fileSystem = mock(FileSystem.class); + SortedSet languages = new TreeSet(); + languages.add("Test"); + when(fileSystem.languages()).thenReturn(languages); + + final CoberturaSensor testedSensor = new CoberturaSensor(fileSystem, settings); assertFalse(testedSensor.shouldExecuteOnProject(project)); } diff --git a/src/test/java/org/sonar/plugins/objectivec/violations/OCLintSensorTest.java b/src/test/java/org/sonar/plugins/objectivec/violations/OCLintSensorTest.java index 01c125ba..52bd2ec8 100644 --- a/src/test/java/org/sonar/plugins/objectivec/violations/OCLintSensorTest.java +++ b/src/test/java/org/sonar/plugins/objectivec/violations/OCLintSensorTest.java @@ -21,21 +21,38 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import org.apache.commons.configuration.PropertiesConfiguration; +import org.junit.Before; import org.junit.Test; +import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.plugins.objectivec.core.ObjectiveC; +import java.util.SortedSet; +import java.util.TreeSet; + public final class OCLintSensorTest { + private Settings settings; + + @Before + public void setUp() { + settings = new Settings(); + } + @Test public void shouldExecuteOnProjectShouldBeTrueWhenProjectIsObjc() { final Project project = new Project("Test"); - final OCLintSensor testedSensor = new OCLintSensor(); - final PropertiesConfiguration config = new PropertiesConfiguration(); - config.addProperty("sonar.language", ObjectiveC.KEY); - project.setConfiguration(config); + + FileSystem fileSystem = mock(FileSystem.class); + SortedSet languages = new TreeSet(); + languages.add(ObjectiveC.KEY); + when(fileSystem.languages()).thenReturn(languages); + + final OCLintSensor testedSensor = new OCLintSensor(fileSystem, settings); assertTrue(testedSensor.shouldExecuteOnProject(project)); } @@ -43,10 +60,13 @@ public void shouldExecuteOnProjectShouldBeTrueWhenProjectIsObjc() { @Test public void shouldExecuteOnProjectShouldBeFalseWhenProjectIsSomethingElse() { final Project project = new Project("Test"); - final OCLintSensor testedSensor = new OCLintSensor(); - final PropertiesConfiguration config = new PropertiesConfiguration(); - config.addProperty("sonar.language", "Test"); - project.setConfiguration(config); + + FileSystem fileSystem = mock(FileSystem.class); + SortedSet languages = new TreeSet(); + languages.add("Test"); + when(fileSystem.languages()).thenReturn(languages); + + final OCLintSensor testedSensor = new OCLintSensor(fileSystem, settings); assertFalse(testedSensor.shouldExecuteOnProject(project)); }