]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2487 Do not analyse bytecode, when property "sonar.skipDesign" set to "true"
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 31 May 2011 23:33:19 +0000 (03:33 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 1 Jun 2011 08:05:42 +0000 (12:05 +0400)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChecksBridgeTest.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java

index 318bded0ea87f54776d8e13d5369df13ae8bc54a..fc91ce2b39a3054e81f06b7ce46039bf6b306a58 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.plugins.squid;
 
-import org.apache.commons.io.FileUtils;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.batch.*;
 import org.sonar.api.checks.AnnotationCheckFactory;
@@ -34,6 +33,7 @@ import org.sonar.java.api.JavaUtils;
 import java.io.File;
 import java.nio.charset.Charset;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 @Phase(name = Phase.Name.PRE)
@@ -57,7 +57,6 @@ public class SquidSensor implements Sensor {
     return Java.KEY.equals(project.getLanguageKey());
   }
 
-  @SuppressWarnings("unchecked")
   public void analyse(Project project, SensorContext context) {
     analyzeMainSources(project, context);
     browseTestSources(project, context);
@@ -75,7 +74,7 @@ public class SquidSensor implements Sensor {
     AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, SquidConstants.REPOSITORY_KEY, SquidRuleRepository.getCheckClasses());
 
     SquidExecutor squidExecutor = new SquidExecutor(analyzePropertyAccessors, fieldNamesToExcludeFromLcom4Computation, factory, charset);
-    squidExecutor.scan(getMainSourceFiles(project), getMainBytecodeFiles(project));
+    squidExecutor.scan(getMainSourceFiles(project), getBytecodeFiles(project));
     squidExecutor.save(project, context, noSonarFilter);
     squidExecutor.flush();
   }
@@ -90,26 +89,16 @@ public class SquidSensor implements Sensor {
     return project.getFileSystem().mainFiles(Java.KEY);
   }
 
-  private Collection<File> getMainBytecodeFiles(Project project) {
-    Collection<File> bytecodeFiles = projectClasspath.getElements();
-    if (!hasProjectBytecodeFiles(project)) {
-      File classesDir = project.getFileSystem().getBuildOutputDir();
-      if (classesDir != null && classesDir.exists()) {
-        bytecodeFiles.remove(classesDir);
-      }
+  /**
+   * Visibility has been relaxed to make the code testable.
+   * 
+   * @return collection of jar-files and directories with classes for analysis
+   */
+  protected Collection<File> getBytecodeFiles(Project project) {
+    if (project.getConfiguration().getBoolean(CoreProperties.DESIGN_SKIP_DESIGN_PROPERTY, CoreProperties.DESIGN_SKIP_DESIGN_DEFAULT_VALUE)) {
+      return Collections.emptyList();
     }
-    return bytecodeFiles;
-  }
-
-  private boolean hasProjectBytecodeFiles(Project project) {
-    if (!project.getConfiguration()
-        .getBoolean(CoreProperties.DESIGN_SKIP_DESIGN_PROPERTY, CoreProperties.DESIGN_SKIP_DESIGN_DEFAULT_VALUE)) {
-      File classesDir = project.getFileSystem().getBuildOutputDir();
-      if (classesDir != null && classesDir.exists()) {
-        return !FileUtils.listFiles(classesDir, new String[]{"class"}, true).isEmpty();
-      }
-    }
-    return false;
+    return projectClasspath.getElements();
   }
 
   @Override
index f09b08e5a5d5ede812125858a5e027bc42315926..e4c99ecd65d058f0a7aafedcbfd1bcf9453b8be1 100644 (file)
@@ -35,10 +35,6 @@ public class ChidamberKemererBridge extends Bridge {
     context.saveMeasure(sonarFile, CoreMetrics.DEPTH_IN_TREE, squidFile.getDouble(Metric.DIT));
     context.saveMeasure(sonarFile, CoreMetrics.NUMBER_OF_CHILDREN, squidFile.getDouble(Metric.NOC));
     context.saveMeasure(sonarFile, CoreMetrics.RFC, squidFile.getDouble(Metric.RFC));
-
-    double lcom4 = squidFile.getDouble(Metric.LCOM4);
-    if (lcom4 > 0.0) {
-      context.saveMeasure(sonarFile, CoreMetrics.LCOM4, lcom4);
-    }
+    context.saveMeasure(sonarFile, CoreMetrics.LCOM4, squidFile.getDouble(Metric.LCOM4));
   }
 }
index 2ff7b38cf9ffae1119a2beccaba1417e96b567d8..4b4531af404a00571927db76a277eaab04bc26f7 100644 (file)
@@ -34,7 +34,7 @@ import org.sonar.api.resources.Scopes;
  */
 public final class ClassComplexityDistributionBuilder implements Decorator {
 
-  public static final Number[] LIMITS = {0, 5, 10, 20, 30, 60, 90};
+  private static final Number[] LIMITS = { 0, 5, 10, 20, 30, 60, 90 };
 
   @DependsUpon
   public Metric dependOnComplexity() {
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java
new file mode 100644 (file)
index 0000000..2ea1e4f
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.squid;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.sameInstance;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.*;
+
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.junit.Test;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.ProjectClasspath;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+
+public class SquidSensorTest {
+  @Test
+  public void testGetBytecodeFiles() {
+    ProjectClasspath projectClasspath = mock(ProjectClasspath.class);
+    when(projectClasspath.getElements()).thenReturn(Arrays.asList(new File("classes")));
+    SquidSensor sensor = new SquidSensor(null, null, projectClasspath, null);
+    Configuration configuration = new BaseConfiguration();
+    Project project = mock(Project.class);
+    when(project.getConfiguration()).thenReturn(configuration);
+
+    configuration.setProperty(CoreProperties.DESIGN_SKIP_DESIGN_PROPERTY, true);
+    assertThat(sensor.getBytecodeFiles(project).size(), is(0));
+
+    configuration.setProperty(CoreProperties.DESIGN_SKIP_DESIGN_PROPERTY, false);
+    assertThat(sensor.getBytecodeFiles(project).size(), is(1));
+    assertThat(sensor.getBytecodeFiles(project), sameInstance((Collection<File>) projectClasspath.getElements()));
+  }
+
+  @Test
+  public void onlyForJava() {
+    SquidSensor sensor = new SquidSensor(null, null, null, null);
+    Project project = mock(Project.class);
+    when(project.getLanguageKey()).thenReturn(Java.KEY).thenReturn("groovy");
+    assertThat(sensor.shouldExecuteOnProject(project), is(true));
+    assertThat(sensor.shouldExecuteOnProject(project), is(false));
+    verify(project, times(2)).getLanguageKey();
+    verifyNoMoreInteractions(project);
+  }
+}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChecksBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChecksBridgeTest.java
new file mode 100644 (file)
index 0000000..7638b6f
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.plugins.squid.bridges;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class ChecksBridgeTest {
+
+  @Test
+  public void notRequiresBytecode() {
+    assertThat(new ChecksBridge().needsBytecode(), is(false));
+  }
+
+}
index 42c51b64667f276fd189a3b88938aade3575d279..c032a434b31b6befd876166fd80d53016b853bfb 100644 (file)
  */
 package org.sonar.plugins.squid.bridges;
 
-import org.junit.Ignore;
-import org.junit.Test;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.JavaPackage;
-
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
 import static org.mockito.Matchers.anyDouble;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 
+import org.junit.Test;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.JavaPackage;
+
 public class ChidamberKemererBridgeTest extends BridgeTestCase {
 
+  @Test
+  public void requiresBytecode() {
+    assertThat(new ChidamberKemererBridge().needsBytecode(), is(true));
+  }
+
   @Test
   public void depthInTree() {
     verify(context).saveMeasure(new JavaFile("org.apache.struts.config.FormBeanConfig"), CoreMetrics.DEPTH_IN_TREE, 2.0);
index d946bcad9a403dff98e804fafcb82d7bed44f7ac..a5461b5f86d04001e86154d6a2ad20287c307210 100644 (file)
@@ -115,6 +115,12 @@ public interface CoreProperties {
   String CPD_SKIP_PROPERTY = "sonar.cpd.skip";
 
   /* Design */
+
+  /**
+   * Indicates whether Java bytecode analysis should be skipped.
+   * 
+   * @since 2.0
+   */
   String DESIGN_SKIP_DESIGN_PROPERTY = "sonar.skipDesign";
   boolean DESIGN_SKIP_DESIGN_DEFAULT_VALUE = false;