From 36276f260deb8c81018f6f5063010f1f739c7ab4 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 1 Jun 2011 03:33:19 +0400 Subject: [PATCH] SONAR-2487 Do not analyse bytecode, when property "sonar.skipDesign" set to "true" --- .../org/sonar/plugins/squid/SquidSensor.java | 33 +++------ .../squid/bridges/ChidamberKemererBridge.java | 6 +- .../ClassComplexityDistributionBuilder.java | 2 +- .../sonar/plugins/squid/SquidSensorTest.java | 67 +++++++++++++++++++ .../squid/bridges/ChecksBridgeTest.java | 34 ++++++++++ .../bridges/ChidamberKemererBridgeTest.java | 18 +++-- .../java/org/sonar/api/CoreProperties.java | 6 ++ 7 files changed, 132 insertions(+), 34 deletions(-) create mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java create mode 100644 plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChecksBridgeTest.java diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java index 318bded0ea8..fc91ce2b39a 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java @@ -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 getMainBytecodeFiles(Project project) { - Collection 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 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 diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java index f09b08e5a5d..e4c99ecd65d 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java @@ -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)); } } diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java index 2ff7b38cf9f..4b4531af404 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilder.java @@ -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 index 00000000000..2ea1e4f9d26 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java @@ -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) 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 index 00000000000..7638b6fd56b --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChecksBridgeTest.java @@ -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)); + } + +} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java index 42c51b64667..c032a434b31 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java @@ -19,19 +19,25 @@ */ 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); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index d946bcad9a4..a5461b5f86d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -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; -- 2.39.5