*/
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;
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)
return Java.KEY.equals(project.getLanguageKey());
}
- @SuppressWarnings("unchecked")
public void analyse(Project project, SensorContext context) {
analyzeMainSources(project, context);
browseTestSources(project, context);
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();
}
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
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));
}
}
*/
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() {
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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));
+ }
+
+}
*/
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);
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;