diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-01 10:57:21 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-01 10:57:28 +0100 |
commit | 936f7f5f75e4acd3a58e12f67e69d9e24290c91c (patch) | |
tree | 6abd75dd50384cd602994d04d48fc48aec87569c /tests | |
parent | ee6efa4035d4f956bec561b9680f10d35068ea46 (diff) | |
download | sonarqube-936f7f5f75e4acd3a58e12f67e69d9e24290c91c.tar.gz sonarqube-936f7f5f75e4acd3a58e12f67e69d9e24290c91c.zip |
SONAR-2159 The total classes used in complexity distribution is different than the number of classes
Diffstat (limited to 'tests')
6 files changed, 82 insertions, 26 deletions
diff --git a/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/AnonymousClass.java b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/AnonymousClass.java new file mode 100644 index 00000000000..a7572a6d5a5 --- /dev/null +++ b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/AnonymousClass.java @@ -0,0 +1,26 @@ +package foo; + +import java.io.Serializable; +import java.lang.Runnable; + +// class complexity: 4 +public class AnonymousClass { + + // method complexity: 3 + public void anonymousClassWithComplexity() { + Runnable runnable = new Runnable() { + public void run() { + if (true) { + System.out.println("true"); + } + } + }; + } + + // method complexity: 1 + public void anonymousClassWithZeroComplexity() { + Serializable serializable = new Serializable() { + + }; + } +}
\ 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 index 9ed05abd626..900e5e5eac1 100644 --- 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 @@ -1,23 +1,23 @@ package foo; - +// class complexity: 5 public class ContainsInnerClasses { - // complexity: 1 + // method complexity: 1 public ContainsInnerClasses() { } - // complexity: 3 + // class complexity: 3 public static class InnerClass { private String field; - // complexity: 1 + // method complexity: 1 public InnerClass() { } - // complexity: 2 + // method complexity: 2 public InnerClass(String s) { if (s != null) { field = s; @@ -26,11 +26,11 @@ public class ContainsInnerClasses { } } -// complexity: 1 +// class complexity: 1 class PackageClass { private String field; - // complexity: 1 + // method complexity: 1 public PackageClass() { } 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 index 37fae842697..ded87d61848 100644 --- 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 @@ -1,11 +1,11 @@ package foo; -// complexity: 6 +// class complexity: 6 public class Helloworld { private String field = null; - // this is considered as a method + // this is considered as a method (bug http://jira.codehaus.org/browse/SONAR-2152) // complexity: 2 static { int i = 0; @@ -14,7 +14,7 @@ public class Helloworld { } } - // complexity: 1 + // method complexity: 1 public Helloworld(String s) { this.field = s; } @@ -31,7 +31,7 @@ public class Helloworld { this.field = s; } - // complexity: 3 + // method complexity: 3 public void sayHello() { for (int i = 0; i < 5; i++) { if (field != null) { diff --git a/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ZeroComplexity.java b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ZeroComplexity.java new file mode 100644 index 00000000000..ee6b3eb25f8 --- /dev/null +++ b/tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/ZeroComplexity.java @@ -0,0 +1,6 @@ +package foo; + +// complexity: 0 +public class ZeroComplexity { + +} 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 index e3e3ad1f157..96af69d950a 100644 --- 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 @@ -39,6 +39,21 @@ public class JavaComplexityIT { } @Test + public void testClasses() { + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.AnonymousClass", CoreMetrics.CLASSES_KEY).getIntValue(), is(1)); + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.ZeroComplexity", CoreMetrics.CLASSES_KEY).getIntValue(), is(1)); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.CLASSES_KEY).getIntValue(), is(6)); + } + + @Test + public void testMethods() { + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.AnonymousClass", CoreMetrics.FUNCTIONS_KEY).getIntValue(), is(2)); + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.ZeroComplexity", CoreMetrics.FUNCTIONS_KEY).getIntValue(), is(0)); + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.ContainsInnerClasses", CoreMetrics.FUNCTIONS_KEY).getIntValue(), is(4)); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.FUNCTIONS_KEY).getIntValue(), is(8)); + } + + @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)); @@ -46,12 +61,12 @@ public class JavaComplexityIT { @Test public void testPackageComplexity() { - assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(11)); + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(15)); } @Test public void testProjectComplexity() { - assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(11)); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.COMPLEXITY_KEY).getIntValue(), is(15)); } @Test @@ -64,9 +79,17 @@ public class JavaComplexityIT { // 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)); + // (1 + 3) / 2 = 2 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo.AnonymousClass", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(2.0)); + + // ContainsInnerClasses: 5/4 + // Helloworld: 6/2 + // AnonymousClass: 4/2 + // => 15/8=1.875 + // BUG http://jira.codehaus.org/browse/SONAR-2152 + // Should use sum of method complexity, not class complexity. + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(1.9)); + assertThat(getMeasure("org.sonar.tests:java-complexity", CoreMetrics.FUNCTION_COMPLEXITY_KEY).getValue(), is(1.9)); } @Test @@ -76,23 +99,24 @@ public class JavaComplexityIT { // 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)); + // 1 + 1 + 3 + 6 + 0 + 4 => 15/6 = 2.5 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.CLASS_COMPLEXITY_KEY).getValue(), is(2.5)); } @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")); + // 1 + 1 + 3 + 6 + 0 + 4 => 5 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=5;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=5;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")); + // Anonymous class : 1 + 3 + assertThat(getMeasure("org.sonar.tests:java-complexity:foo", CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY).getData(), is("1=5;2=3;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=5;2=3;4=0;6=0;8=0;10=0;12=0")); } @Test 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 2724b24db7f..d608b39bfd8 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 @@ -99,7 +99,7 @@ public class Struts139IT { public void sizeMetrics() { assertThat(getProjectMeasure("lines").getIntValue(), is(114621)); assertThat(getProjectMeasure("ncloc").getIntValue(), is(50080)); - assertThat(getProjectMeasure("functions").getIntValue(), is(4292)); + assertThat(getProjectMeasure("functions").getIntValue(), is(4234)); assertThat(getProjectMeasure("accessors").getIntValue(), is(1133)); assertThat(getProjectMeasure("classes").getIntValue(), is(518)); assertThat(getProjectMeasure("packages").getIntValue(), is(49)); @@ -134,8 +134,8 @@ public class Struts139IT { @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")); + assertThat(getCoreModuleMeasure("class_complexity_distribution").getData(), is("0=47;5=26;10=24;20=12;30=18;60=9;90=10")); + assertThat(getProjectMeasure("class_complexity_distribution").getData(), is("0=171;5=90;10=84;20=53;30=69;60=34;90=17")); } @Test |