diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-28 13:17:57 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-28 13:18:46 +0100 |
commit | b8eecc041ad4aa23a3bf5cf7c8847573b125d187 (patch) | |
tree | d0b719f881f8f324d574fa699bd6befb7608c058 /tests | |
parent | 220dd330306d76b7ff13baa07abf461688c92fc1 (diff) | |
download | sonarqube-b8eecc041ad4aa23a3bf5cf7c8847573b125d187.tar.gz sonarqube-b8eecc041ad4aa23a3bf5cf7c8847573b125d187.zip |
SONAR-2149 Resource filters are ignored in complexity distributions of Java projects
This issue implies SONAR-2153 : API: A decorator should override formulas
Diffstat (limited to 'tests')
8 files changed, 244 insertions, 1 deletions
diff --git a/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ExcludedResourceFilter.java b/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ExcludedResourceFilter.java new file mode 100644 index 00000000000..4c045cdb3b9 --- /dev/null +++ b/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ExcludedResourceFilter.java @@ -0,0 +1,13 @@ +package itests; + +import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.ResourceFilter; +import org.sonar.api.resources.Resource; +import org.sonar.api.resources.Scopes; + +public final class ExcludedResourceFilter implements ResourceFilter { + + public boolean isIgnored(Resource resource) { + return Scopes.isFile(resource) && StringUtils.contains(resource.getName(), "ExcludedByFilter"); + } +} diff --git a/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ITestsPlugin.java b/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ITestsPlugin.java index 6df7c15befb..5fd5809f3b3 100644 --- a/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ITestsPlugin.java +++ b/tests/integration/sonar-it-reference-plugin/src/main/java/itests/ITestsPlugin.java @@ -50,6 +50,7 @@ public class ITestsPlugin implements Plugin { extensions.add(SampleSensor.class);
extensions.add(LanguageWithoutRulesEngine.class);
extensions.add(ServerSideExtensionUsingExternalDependency.class);
+ extensions.add(ExcludedResourceFilter.class);
// web
extensions.add(SampleResourceTab.class);
diff --git a/tests/integration/tests/maven-projects/java-complexity/pom.xml b/tests/integration/tests/maven-projects/java-complexity/pom.xml new file mode 100644 index 00000000000..63afab1e8ab --- /dev/null +++ b/tests/integration/tests/maven-projects/java-complexity/pom.xml @@ -0,0 +1,8 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.sonar.tests</groupId> + <artifactId>java-complexity</artifactId> + <version>1.0-SNAPSHOT</version> + <name>java-complexity</name> +</project>
\ No newline at end of file diff --git a/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ContainsInnerClasses.java b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ContainsInnerClasses.java new file mode 100644 index 00000000000..9ed05abd626 --- /dev/null +++ b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ContainsInnerClasses.java @@ -0,0 +1,37 @@ +package foo; + + +public class ContainsInnerClasses { + + // complexity: 1 + public ContainsInnerClasses() { + + } + + // complexity: 3 + public static class InnerClass { + private String field; + + // complexity: 1 + public InnerClass() { + + } + + // complexity: 2 + public InnerClass(String s) { + if (s != null) { + field = s; + } + } + } +} + +// complexity: 1 +class PackageClass { + private String field; + + // complexity: 1 + public PackageClass() { + + } + } diff --git a/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ExcludedByFilter.java b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ExcludedByFilter.java new file mode 100644 index 00000000000..b5193444cd6 --- /dev/null +++ b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ExcludedByFilter.java @@ -0,0 +1,19 @@ +package foo; + +// this class is excluded by the resource filter defined in sonar-it-reference-plugin +public class ExcludedByFilter { + + public void say() { + int i=0; + if(i>5) { + System.out.println("say something"); + } + } + + public void cry() { + int i=0; + if(i<5) { + System.out.println("cry"); + } + } +} diff --git a/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/Helloworld.java b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/Helloworld.java new file mode 100644 index 00000000000..37fae842697 --- /dev/null +++ b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/Helloworld.java @@ -0,0 +1,42 @@ +package foo; + +// complexity: 6 +public class Helloworld { + + private String field = null; + + // this is considered as a method + // complexity: 2 + static { + int i = 0; + if (i > 5) { + System.out.println("hello from static block"); + } + } + + // complexity: 1 + public Helloworld(String s) { + this.field = s; + } + + // accessor + // complexity: 0 + public String getField() { + return field; + } + + // accessor + // complexity: 0 + public void setField(String s) { + this.field = s; + } + + // complexity: 3 + public void sayHello() { + for (int i = 0; i < 5; i++) { + if (field != null) { + System.out.println(field); + } + } + } +} diff --git a/tests/integration/tests/src/test/java/org/sonar/tests/integration/JavaComplexityIT.java b/tests/integration/tests/src/test/java/org/sonar/tests/integration/JavaComplexityIT.java new file mode 100644 index 00000000000..e3e3ad1f157 --- /dev/null +++ b/tests/integration/tests/src/test/java/org/sonar/tests/integration/JavaComplexityIT.java @@ -0,0 +1,107 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.tests.integration; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.wsclient.Sonar; +import org.sonar.wsclient.services.Measure; +import org.sonar.wsclient.services.ResourceQuery; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +public class JavaComplexityIT { + private static Sonar sonar; + + @BeforeClass + public static void buildServer() { + sonar = ITUtils.createSonarWsClient(); + } + + @Test + public void testFileComplexity() { + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.Helloworld", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(6)); + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.ContainsInnerClasses", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(5)); + } + + @Test + public void testPackageComplexity() { + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(11)); + } + + @Test + public void testProjectComplexity() { + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(11)); + } + + @Test + public void testAverageMethodComplexity() { + // complexity 6 / 2 methods + // BUG http://jira.codehaus.org/browse/SONAR-2152 + // => the complexity of the static block should not be included. Good value should be 4 / 2 = 2 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.Helloworld", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(3.0)); + + // complexity 5 / 4 methods. Real value is 1.25 but round up to 1.3 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.ContainsInnerClasses", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(1.3)); + + // 3.0 * 2 + 1.25 * 4 = 11 for 6 methods + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(1.8)); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(1.8)); + } + + @Test + public void testAverageClassComplexity() { + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.Helloworld", CoreMetrics.CLASS_COMPLEXITY_KEY).getValue(), is(6.0)); + + // 1 + 1 + 3 => complexity 5/3 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.ContainsInnerClasses", CoreMetrics.CLASS_COMPLEXITY_KEY).getValue(), is(1.7)); + + // 1 + 1 + 3 + 6 => 11/4 = 2.75 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.CLASS_COMPLEXITY_KEY).getValue(), is(2.8)); + } + + @Test + public void testDistributionOfClassComplexity() { + // 1 + 1 + 3 + 6 => 3 in range [0,5[ and 1 in range [5,10[ + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION_KEY).getData(), is("0=3;5=1;10=0;20=0;30=0;60=0;90=0")); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION_KEY).getData(), is("0=3;5=1;10=0;20=0;30=0;60=0;90=0")); + } + + @Test + public void testDistributionOfMethodComplexity() { + // ContainsInnerClasses: 1+ 1 + 2 + 1 + // Helloworld: 1 + 3 (static block is not a method) + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY).getData(), is("1=4;2=2;4=0;6=0;8=0;10=0;12=0")); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY).getData(), is("1=4;2=2;4=0;6=0;8=0;10=0;12=0")); + } + + @Test + public void shouldNotPersistDistributionOnFiles() { + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.Helloworld", CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION_KEY), nullValue()); + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.Helloworld", CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY), nullValue()); + } + + private Measure getMeasure(String resourceKey, String metricKey) { + return sonar.find(ResourceQuery.createForMetrics(resourceKey, metricKey)).getMeasure(metricKey); + } +} diff --git a/tests/integration/tests/src/test/java/org/sonar/tests/integration/Struts139IT.java b/tests/integration/tests/src/test/java/org/sonar/tests/integration/Struts139IT.java index bcffec6f59a..2724b24db7f 100644 --- a/tests/integration/tests/src/test/java/org/sonar/tests/integration/Struts139IT.java +++ b/tests/integration/tests/src/test/java/org/sonar/tests/integration/Struts139IT.java @@ -129,7 +129,23 @@ public class Struts139IT { assertThat(getProjectMeasure("statements").getIntValue(), is(21896)); assertThat(getProjectMeasure("class_complexity").getValue(), is(21.5)); assertThat(getProjectMeasure("function_complexity").getValue(), is(2.6)); - assertThat(getProjectMeasure("class_complexity_distribution").getData(), is("0=172;5=90;10=86;20=55;30=69;60=34;90=17")); + } + + @Test + public void classComplexityDistribution() throws Exception { + assertThat(sonar.find(ResourceQuery.createForMetrics("org.apache.struts:struts-core:org.apache.struts.config", "class_complexity_distribution")).getMeasure("class_complexity_distribution").getData(), is("0=10;5=3;10=2;20=1;30=4;60=4;90=1")); + assertThat(getCoreModuleMeasure("class_complexity_distribution").getData(), is("0=49;5=26;10=24;20=14;30=18;60=9;90=10")); + assertThat(getProjectMeasure("class_complexity_distribution").getData(), is("0=173;5=90;10=86;20=55;30=69;60=34;90=17")); + } + + @Test + public void functionComplexityDistribution() throws Exception { + assertThat(sonar.find(ResourceQuery.createForMetrics("org.apache.struts:struts-core:org.apache.struts.config", "function_complexity_distribution")).getMeasure("function_complexity_distribution").getData(), is("1=186;2=88;4=11;6=12;8=7;10=2;12=8")); + } + + @Test + public void shouldNotPersistComplexityDistributionsOnFiles() { + assertThat(sonar.find(ResourceQuery.createForMetrics("org.apache.struts:struts-core:org.apache.struts.config.ConfigRuleSet", "function_complexity_distribution", "class_complexity_distribution")).getMeasures().size(), is(0)); } @Test |