]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 Update CPD plugin to not rely on project.getLanguage[Key]()
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 10 Feb 2014 12:06:05 +0000 (13:06 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 10 Feb 2014 12:06:05 +0000 (13:06 +0100)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarBridgeEngineTest.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/CpdMapping.java

index 6e890a6fa181e73c29832180159fed75c7e4c639..7df821dd0a6967da80e426335f2db7d6b28179c2 100644 (file)
@@ -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) {
index f55f1215fef2ba39e86c3a0e3300532bb0f5a6d7..0488a9b63af567cefe9edf96f2a0a6917d47a791 100644 (file)
@@ -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);
     }
   }
 
index b093558edba2d905c5c79da10329e599a8c6317b..6512f6fc8fab945ea81814bc8c079ce4cc77d9f0 100644 (file)
@@ -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);
     }
index 62878f1467e83c8da6e04b680cb51e3b6211e942..70c2731ae450484c76ef3814350f47eb3cdad575 100644 (file)
@@ -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;
     }
index 826d1ed2ecd75b151f82cd7eb7c06132807363a2..814ef24d300de051115b46b39fdad55c3c5159e9 100644 (file)
@@ -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);
   }
 }
index c000683c4e83bcc83e31232174fe39b35997496c..458eab2bee6c367e72fb4a6376c2d4b90671d89f 100644 (file)
@@ -37,7 +37,7 @@ public interface CpdMapping extends BatchExtension {
   Language getLanguage();
 
   /**
-   * @deprecated since 4.2
+   * @deprecated since 4.2 not used anymore
    */
   @Deprecated
   Resource createResource(File file, List<File> sourceDirs);