diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-02-10 13:06:05 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-02-10 13:06:05 +0100 |
commit | 6fc365ecd87be80d1d989dea22b9f936d9ebafef (patch) | |
tree | 9043060c46ce50318eb171d9b4fde7043c5539fc /plugins/sonar-cpd-plugin | |
parent | b024967c94f74f6551494ae95bf4f5e75e762cfb (diff) | |
download | sonarqube-6fc365ecd87be80d1d989dea22b9f936d9ebafef.tar.gz sonarqube-6fc365ecd87be80d1d989dea22b9f936d9ebafef.zip |
SONAR-926 Update CPD plugin to not rely on project.getLanguage[Key]()
Diffstat (limited to 'plugins/sonar-cpd-plugin')
5 files changed, 32 insertions, 42 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java index 6e890a6fa18..7df821dd0a6 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java @@ -28,7 +28,7 @@ public abstract class CpdEngine implements BatchExtension { abstract boolean isLanguageSupported(String language); - abstract void analyse(Project project, SensorContext context); + abstract void analyse(Project project, String language, SensorContext context); protected void logExclusions(String[] exclusions, Logger logger) { if (exclusions.length > 0) { diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java index f55f1215fef..0488a9b63af 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java @@ -79,7 +79,7 @@ public class CpdSensor implements Sensor { continue; } LOG.info("{} is used for {}", engine, language); - engine.analyse(project, context); + engine.analyse(project, language, context); } } diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java index b093558edba..6512f6fc8fa 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java @@ -19,8 +19,6 @@ */ package org.sonar.plugins.cpd; -import org.sonar.api.scan.filesystem.InputFile; - import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; @@ -30,9 +28,9 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.CpdMapping; import org.sonar.api.batch.SensorContext; import org.sonar.api.config.Settings; -import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; import org.sonar.api.scan.filesystem.FileQuery; +import org.sonar.api.scan.filesystem.InputFile; import org.sonar.api.scan.filesystem.internal.DefaultInputFile; import org.sonar.api.utils.SonarException; import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; @@ -84,21 +82,21 @@ public class SonarBridgeEngine extends CpdEngine { } @Override - public void analyse(Project project, SensorContext context) { + public void analyse(Project project, String languageKey, SensorContext context) { String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS); logExclusions(cpdExclusions, LOG); - Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onSource().onLanguage(project.getLanguageKey()) + Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onMain().onLanguage(languageKey) .withExclusions(cpdExclusions)); if (!sourceFiles.iterator().hasNext()) { return; } - CpdMapping mapping = getMapping(project.getLanguage().getKey()); + CpdMapping mapping = getMapping(languageKey); // Create index SonarDuplicationsIndex index = indexFactory.create(project); - TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fileSystem.sourceCharset().name(), getBlockSize(project)); + TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fileSystem.sourceCharset().name(), getBlockSize(project, languageKey)); for (InputFile inputFile : sourceFiles) { LOG.debug("Populating index from {}", inputFile); String resourceEffectiveKey = inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY); @@ -107,7 +105,7 @@ public class SonarBridgeEngine extends CpdEngine { } // Detect - Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(project)); + Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(project, languageKey)); ExecutorService executorService = Executors.newSingleThreadExecutor(); try { @@ -137,8 +135,7 @@ public class SonarBridgeEngine extends CpdEngine { } @VisibleForTesting - int getBlockSize(Project project) { - String languageKey = project.getLanguageKey(); + int getBlockSize(Project project, String languageKey) { int blockSize = settings.getInt("sonar.cpd." + languageKey + ".minimumLines"); if (blockSize == 0) { blockSize = getDefaultBlockSize(languageKey); @@ -158,8 +155,8 @@ public class SonarBridgeEngine extends CpdEngine { } @VisibleForTesting - int getMinimumTokens(Project project) { - int minimumTokens = settings.getInt("sonar.cpd." + project.getLanguageKey() + ".minimumTokens"); + int getMinimumTokens(Project project, String languageKey) { + int minimumTokens = settings.getInt("sonar.cpd." + languageKey + ".minimumTokens"); if (minimumTokens == 0) { minimumTokens = settings.getInt(CoreProperties.CPD_MINIMUM_TOKENS_PROPERTY); } diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java index 62878f1467e..70c2731ae45 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java @@ -20,8 +20,6 @@ package org.sonar.plugins.cpd; -import org.sonar.api.scan.filesystem.InputFile; - import com.google.common.collect.Iterables; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringEscapeUtils; @@ -34,9 +32,9 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.PersistenceMode; import org.sonar.api.resources.Java; -import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; import org.sonar.api.scan.filesystem.FileQuery; +import org.sonar.api.scan.filesystem.InputFile; import org.sonar.api.scan.filesystem.internal.DefaultInputFile; import org.sonar.api.utils.SonarException; import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; @@ -98,10 +96,10 @@ public class SonarEngine extends CpdEngine { } @Override - public void analyse(Project project, SensorContext context) { + public void analyse(Project project, String languageKey, SensorContext context) { String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS); logExclusions(cpdExclusions, LOG); - Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onSource().onLanguage(project.getLanguageKey()).withExclusions(cpdExclusions)); + Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onMain().onLanguage(languageKey).withExclusions(cpdExclusions)); if (!sourceFiles.iterator().hasNext()) { return; } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarBridgeEngineTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarBridgeEngineTest.java index 826d1ed2ecd..814ef24d300 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarBridgeEngineTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarBridgeEngineTest.java @@ -19,7 +19,6 @@ */ package org.sonar.plugins.cpd; -import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -28,8 +27,6 @@ import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import static org.fest.assertions.Assertions.assertThat; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -64,58 +61,56 @@ public class SonarBridgeEngineTest { @Test public void shouldReturnDefaultBlockSize() { - assertThat(SonarBridgeEngine.getDefaultBlockSize("cobol"), is(30)); - assertThat(SonarBridgeEngine.getDefaultBlockSize("natur"), is(20)); - assertThat(SonarBridgeEngine.getDefaultBlockSize("abap"), is(20)); - assertThat(SonarBridgeEngine.getDefaultBlockSize("other"), is(10)); + assertThat(SonarBridgeEngine.getDefaultBlockSize("cobol")).isEqualTo(30); + assertThat(SonarBridgeEngine.getDefaultBlockSize("natur")).isEqualTo(20); + assertThat(SonarBridgeEngine.getDefaultBlockSize("abap")).isEqualTo(20); + assertThat(SonarBridgeEngine.getDefaultBlockSize("other")).isEqualTo(10); } @Test public void defaultBlockSize() { - Project project = newProject("foo", "java"); + Project project = newProject("foo"); - assertThat(engine.getBlockSize(project)).isEqualTo(10); + assertThat(engine.getBlockSize(project, "java")).isEqualTo(10); } @Test public void blockSizeForCobol() { - Project project = newProject("foo", "cobol"); + Project project = newProject("foo"); settings.setProperty("sonar.cpd.cobol.minimumLines", "42"); - assertThat(engine.getBlockSize(project)).isEqualTo(42); + assertThat(engine.getBlockSize(project, "cobol")).isEqualTo(42); } @Test public void defaultMinimumTokens() { - Project project = newProject("foo", "java"); + Project project = newProject("foo"); - assertThat(engine.getMinimumTokens(project), is(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE)); + assertThat(engine.getMinimumTokens(project, "java")).isEqualTo(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE); } @Test public void generalMinimumTokens() { - Project project = newProject("foo", "java"); + Project project = newProject("foo"); settings.setProperty("sonar.cpd.minimumTokens", 33); - assertThat(engine.getMinimumTokens(project), is(33)); + assertThat(engine.getMinimumTokens(project, "java")).isEqualTo(33); } @Test public void minimumTokensByLanguage() { - Project javaProject = newProject("foo", "java"); + Project javaProject = newProject("foo"); settings.setProperty("sonar.cpd.java.minimumTokens", "42"); settings.setProperty("sonar.cpd.php.minimumTokens", "33"); - assertThat(engine.getMinimumTokens(javaProject), is(42)); + assertThat(engine.getMinimumTokens(javaProject, "java")).isEqualTo(42); - Project phpProject = newProject("foo", "php"); + Project phpProject = newProject("foo"); settings.setProperty("sonar.cpd.java.minimumTokens", "42"); settings.setProperty("sonar.cpd.php.minimumTokens", "33"); - assertThat(engine.getMinimumTokens(phpProject), is(33)); + assertThat(engine.getMinimumTokens(phpProject, "php")).isEqualTo(33); } - private static Project newProject(String key, String language) { - PropertiesConfiguration conf = new PropertiesConfiguration(); - conf.setProperty("sonar.language", language); - return new Project(key).setConfiguration(conf).setAnalysisType(Project.AnalysisType.DYNAMIC); + private static Project newProject(String key) { + return new Project(key).setAnalysisType(Project.AnalysisType.DYNAMIC); } } |