]> source.dussan.org Git - sonarqube.git/commitdiff
Add some unit tests
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 28 Jan 2011 13:34:50 +0000 (14:34 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 28 Jan 2011 13:34:50 +0000 (14:34 +0100)
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java
sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java

index 477643076d16ef42f40857995f0ec0dd28773c60..891cf45e928e745de73dab714b5412d03ce01241 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.plugins.squid.decorators;
 
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
 import org.junit.Test;
 import org.sonar.api.batch.DecoratorContext;
 import org.sonar.api.measures.CoreMetrics;
@@ -29,9 +31,14 @@ import org.sonar.api.resources.JavaPackage;
 import org.sonar.api.resources.Project;
 import org.sonar.java.api.JavaClass;
 
+import java.util.Arrays;
+import java.util.List;
+
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.argThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 public class ClassComplexityDistributionBuilderTest {
@@ -54,4 +61,43 @@ public class ClassComplexityDistributionBuilderTest {
     assertThat(builder.shouldExecuteOn(JavaClass.create("org.foo.Bar"), context), is(false));
   }
 
+  @Test
+  public void shouldCalculateDistributionOnFile() throws Exception {
+
+    List<DecoratorContext> children = Arrays.asList(
+        // first range
+        newClassChild("One", 2.0), newClassChild("Two", 1.0), newClassChild("Zero complexity", 0.0),
+
+        // second range
+        newClassChild("Three", 8.0),
+
+        // out of range
+        newClassChild("No complexity", null));
+
+    DecoratorContext context = mock(DecoratorContext.class);
+    when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 20.0));
+    when(context.getChildren()).thenReturn(children);
+
+    ClassComplexityDistributionBuilder builder = new ClassComplexityDistributionBuilder();
+    builder.decorate(JavaFile.fromRelativePath("org/foo/MyFile.java", false), context);
+
+    verify(context).saveMeasure(argThat(new BaseMatcher<Measure>() {
+      public boolean matches(Object o) {
+        Measure measure = (Measure) o;
+        return measure.getMetric().equals(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION) &&
+            measure.getData().equals("0=3;5=1;10=0;20=0;30=0;60=0;90=0");
+      }
+
+      public void describeTo(Description description) {
+
+      }
+    }));
+  }
+
+  private DecoratorContext newClassChild(String classname, Double complexity) {
+    DecoratorContext context = mock(DecoratorContext.class);
+    when(context.getResource()).thenReturn(JavaClass.create(classname));
+    when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, complexity));
+    return context;
+  }
 }
index 93d89b98abbd8b2fc5d1b45fbdbfd8ce14425eb1..6c6b6100c50b50ad3f6bc294c90c1d02bc50043d 100644 (file)
@@ -23,6 +23,9 @@ import com.google.common.collect.Lists;
 import org.junit.Before;\r
 import org.junit.Test;\r
 import org.sonar.api.resources.File;\r
+import org.sonar.api.resources.JavaFile;\r
+import org.sonar.api.resources.JavaPackage;\r
+import org.sonar.api.resources.Scopes;\r
 \r
 import java.util.Collections;\r
 import java.util.List;\r
@@ -62,7 +65,7 @@ public class SumChildDistributionFormulaTest {
   }\r
 \r
   @Test\r
-  public void doNotSumDifferentRanges() {\r
+  public void shouldNotSumDifferentRanges() {\r
     Metric m = new Metric("foo", Metric.ValueType.DATA);\r
     when(context.getTargetMetric()).thenReturn(m);\r
 \r
@@ -75,7 +78,7 @@ public class SumChildDistributionFormulaTest {
   }\r
 \r
   @Test\r
-  public void sumSameIntRanges() {\r
+  public void shouldSumSameIntRanges() {\r
     Metric m = new Metric("foo", Metric.ValueType.DATA);\r
     when(context.getTargetMetric()).thenReturn(m);\r
 \r
@@ -88,15 +91,51 @@ public class SumChildDistributionFormulaTest {
   }\r
 \r
   @Test\r
-  public void sumSameDoubleRanges() {\r
+  public void shouldSumSameDoubleRanges() {\r
+    initContextWithChildren();\r
+    assertThat(formula.calculate(data, context).getData(), is("0.5=3;2.5=6"));\r
+  }\r
+\r
+  @Test\r
+  public void shouldNotPersistWhenScopeLowerThanMinimun() throws Exception {\r
+    when(context.getResource()).thenReturn(JavaFile.fromRelativePath("org/Foo.java", false));\r
+\r
+    initContextWithChildren();\r
+    formula.setMinimumScopeToPersist(Scopes.DIRECTORY);\r
+\r
+    Measure distribution = formula.calculate(data, context);\r
+    assertThat(distribution.getPersistenceMode().useDatabase(), is(false));\r
+  }\r
+\r
+  @Test\r
+  public void shouldPersistWhenScopeEqualsMinimun() throws Exception {\r
+    when(context.getResource()).thenReturn(JavaFile.fromRelativePath("org/Foo.java", false));\r
+\r
+    initContextWithChildren();\r
+    formula.setMinimumScopeToPersist(Scopes.FILE);\r
+\r
+    Measure distribution = formula.calculate(data, context);\r
+    assertThat(distribution.getPersistenceMode().useDatabase(), is(true));\r
+  }\r
+\r
+  @Test\r
+  public void shouldPersistWhenScopeHigherThanMinimun() throws Exception {\r
+    when(context.getResource()).thenReturn(new JavaPackage("org.foo"));\r
+\r
+    initContextWithChildren();\r
+    formula.setMinimumScopeToPersist(Scopes.FILE);\r
+\r
+    Measure distribution = formula.calculate(data, context);\r
+    assertThat(distribution.getPersistenceMode().useDatabase(), is(true));\r
+  }\r
+\r
+  private void initContextWithChildren() {\r
     Metric m = new Metric("foo", Metric.ValueType.DATA);\r
     when(context.getTargetMetric()).thenReturn(m);\r
-\r
     List<Measure> list = Lists.newArrayList(\r
-      new Measure(m, "0.5=0;2.5=2"),\r
-      new Measure(m, "0.5=3;2.5=4")\r
+        new Measure(m, "0.5=0;2.5=2"),\r
+        new Measure(m, "0.5=3;2.5=4")\r
     );\r
     when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);\r
-    assertThat(formula.calculate(data, context).getData(), is("0.5=3;2.5=6"));\r
   }\r
 }\r