]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3108 Add new property to exclude files from duplication detection
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Fri, 15 Feb 2013 12:51:23 +0000 (13:51 +0100)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Fri, 15 Feb 2013 12:52:12 +0000 (13:52 +0100)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdPlugin.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/CpdSensorTest.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java

index 81ac5d09f36e46e7bc3e6a75017a0381ccf77258..c6f59261437dbc238a227f96ed8d9773865dd0d7 100644 (file)
@@ -50,18 +50,28 @@ import java.util.List;
     // not displayed in UI
     project = false, module = false, global = false,
     category = CoreProperties.CATEGORY_DUPLICATIONS,
-    type = PropertyType.BOOLEAN)
+    type = PropertyType.BOOLEAN),
+  @Property(
+    key = CoreProperties.CPD_EXCLUSIONS,
+    defaultValue = "",
+    name = "Duplication exclusions",
+    description = "Patterns used to exclude some source files from the duplication detection mechanism.",
+    project = true,
+    module = true,
+    global = true,
+    category = CoreProperties.CATEGORY_DUPLICATIONS,
+    multiValues = true)
 })
 public final class CpdPlugin extends SonarPlugin {
 
   public List getExtensions() {
     return ImmutableList.of(
-      CpdSensor.class,
-      SumDuplicationsDecorator.class,
-      DuplicationDensityDecorator.class,
-      IndexFactory.class,
-      SonarEngine.class,
-      SonarBridgeEngine.class);
+        CpdSensor.class,
+        SumDuplicationsDecorator.class,
+        DuplicationDensityDecorator.class,
+        IndexFactory.class,
+        SonarEngine.class,
+        SonarBridgeEngine.class);
   }
 
 }
index d62fd253ef26d8087aed780f8af37bb914c944f7..0b05e83f4ad14305e5d55f23af607fa879ad2b59 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.scan.filesystem.FileQuery;
 import org.sonar.api.scan.filesystem.ModuleFileSystem;
 import org.sonar.api.utils.SonarException;
 import org.sonar.duplications.DuplicationPredicates;
@@ -81,7 +82,8 @@ public class SonarBridgeEngine extends CpdEngine {
 
   @Override
   public void analyse(Project project, SensorContext context) {
-    List<File> sourceFiles = fileSystem.sourceFilesOfLang(project.getLanguageKey());
+    String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
+    List<File> sourceFiles = fileSystem.files(FileQuery.onSource().onLanguage(project.getLanguageKey()).withExclusions(cpdExclusions));
     if (sourceFiles.isEmpty()) {
       return;
     }
index a00c906e5143991cfdf77daa4e4602f6f31cda7f..e9e4c589081d175b9a444e2c6f726249a9b2db81 100644 (file)
  */
 package org.sonar.plugins.cpd;
 
+import org.sonar.api.CoreProperties;
+
+import org.sonar.api.config.Settings;
+
 import com.google.common.collect.Iterables;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringEscapeUtils;
@@ -34,6 +38,7 @@ import org.sonar.api.resources.JavaFile;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.scan.filesystem.FileQuery;
 import org.sonar.api.scan.filesystem.ModuleFileSystem;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.api.utils.SonarException;
@@ -81,15 +86,15 @@ public class SonarEngine extends CpdEngine {
   private static final int TIMEOUT = 5 * 60;
 
   private final IndexFactory indexFactory;
-
   private final ModuleFileSystem fileSystem;
-
   private final PathResolver pathResolver;
+  private final Settings settings;
 
-  public SonarEngine(IndexFactory indexFactory, ModuleFileSystem moduleFileSystem, PathResolver pathResolver) {
+  public SonarEngine(IndexFactory indexFactory, ModuleFileSystem moduleFileSystem, PathResolver pathResolver, Settings settings) {
     this.indexFactory = indexFactory;
     this.fileSystem = moduleFileSystem;
     this.pathResolver = pathResolver;
+    this.settings = settings;
   }
 
   @Override
@@ -107,7 +112,8 @@ public class SonarEngine extends CpdEngine {
 
   @Override
   public void analyse(Project project, SensorContext context) {
-    List<File> sourceFiles = fileSystem.sourceFilesOfLang(project.getLanguageKey());
+    String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
+    List<File> sourceFiles = fileSystem.files(FileQuery.onSource().onLanguage(project.getLanguageKey()).withExclusions(cpdExclusions));
     if (sourceFiles.isEmpty()) {
       return;
     }
index 8d2ab27bb3d9bc8c3bbb2ed792cdfee9719ad347..c04dacaa92e382b72467c371b21bf843ed950742 100644 (file)
@@ -42,7 +42,7 @@ public class CpdSensorTest {
   @Before
   public void setUp() {
     IndexFactory indexFactory = mock(IndexFactory.class);
-    sonarEngine = new SonarEngine(indexFactory, null, null);
+    sonarEngine = new SonarEngine(indexFactory, null, null, null);
     sonarBridgeEngine = new SonarBridgeEngine(indexFactory, null, null);
     settings = new Settings(new PropertyDefinitions(CpdPlugin.class));
     sensor = new CpdSensor(sonarEngine, sonarBridgeEngine, settings);
index e3a054324a46519018f42f35fa10043047c1899d..fbb7a0b7d322f9fbaca3f7228cd4458e646e0da9 100644 (file)
@@ -67,7 +67,7 @@ public class SonarEngineTest {
     when(pathResolver.relativePath(anyCollection(), any(java.io.File.class))).thenReturn(relativePath);
     when(relativePath.path()).thenReturn("com/foo/Bar.java");
 
-    SonarEngine engine = new SonarEngine(null, fileSystem, pathResolver);
+    SonarEngine engine = new SonarEngine(null, fileSystem, pathResolver, null);
     Resource<?> resource = engine.getResource(new java.io.File(""));
 
     assertThat(resource.getKey()).isEqualTo("com.foo.Bar");
index 67d20ac2f59e992c2a277915b765d532aafb2b2c..fdacc52dfbd98822f4c51faca599c18e1ba7f47e 100644 (file)
@@ -243,6 +243,11 @@ public interface CoreProperties {
    */
   boolean CPD_CROSS_RPOJECT_DEFAULT_VALUE = false;
 
+  /**
+   * @since 3.5
+   */
+  String CPD_EXCLUSIONS = "sonar.cpd.exclusions";
+
   /* Design */
 
   /**