diff options
2 files changed, 92 insertions, 7 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java index 477643076d1..891cf45e928 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java @@ -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; + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java index 93d89b98abb..6c6b6100c50 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/SumChildDistributionFormulaTest.java @@ -23,6 +23,9 @@ import com.google.common.collect.Lists; import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.File;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.JavaPackage;
+import org.sonar.api.resources.Scopes;
import java.util.Collections;
import java.util.List;
@@ -62,7 +65,7 @@ public class SumChildDistributionFormulaTest { }
@Test
- public void doNotSumDifferentRanges() {
+ public void shouldNotSumDifferentRanges() {
Metric m = new Metric("foo", Metric.ValueType.DATA);
when(context.getTargetMetric()).thenReturn(m);
@@ -75,7 +78,7 @@ public class SumChildDistributionFormulaTest { }
@Test
- public void sumSameIntRanges() {
+ public void shouldSumSameIntRanges() {
Metric m = new Metric("foo", Metric.ValueType.DATA);
when(context.getTargetMetric()).thenReturn(m);
@@ -88,15 +91,51 @@ public class SumChildDistributionFormulaTest { }
@Test
- public void sumSameDoubleRanges() {
+ public void shouldSumSameDoubleRanges() {
+ initContextWithChildren();
+ assertThat(formula.calculate(data, context).getData(), is("0.5=3;2.5=6"));
+ }
+
+ @Test
+ public void shouldNotPersistWhenScopeLowerThanMinimun() throws Exception {
+ when(context.getResource()).thenReturn(JavaFile.fromRelativePath("org/Foo.java", false));
+
+ initContextWithChildren();
+ formula.setMinimumScopeToPersist(Scopes.DIRECTORY);
+
+ Measure distribution = formula.calculate(data, context);
+ assertThat(distribution.getPersistenceMode().useDatabase(), is(false));
+ }
+
+ @Test
+ public void shouldPersistWhenScopeEqualsMinimun() throws Exception {
+ when(context.getResource()).thenReturn(JavaFile.fromRelativePath("org/Foo.java", false));
+
+ initContextWithChildren();
+ formula.setMinimumScopeToPersist(Scopes.FILE);
+
+ Measure distribution = formula.calculate(data, context);
+ assertThat(distribution.getPersistenceMode().useDatabase(), is(true));
+ }
+
+ @Test
+ public void shouldPersistWhenScopeHigherThanMinimun() throws Exception {
+ when(context.getResource()).thenReturn(new JavaPackage("org.foo"));
+
+ initContextWithChildren();
+ formula.setMinimumScopeToPersist(Scopes.FILE);
+
+ Measure distribution = formula.calculate(data, context);
+ assertThat(distribution.getPersistenceMode().useDatabase(), is(true));
+ }
+
+ private void initContextWithChildren() {
Metric m = new Metric("foo", Metric.ValueType.DATA);
when(context.getTargetMetric()).thenReturn(m);
-
List<Measure> list = Lists.newArrayList(
- new Measure(m, "0.5=0;2.5=2"),
- new Measure(m, "0.5=3;2.5=4")
+ new Measure(m, "0.5=0;2.5=2"),
+ new Measure(m, "0.5=3;2.5=4")
);
when(data.getChildrenMeasures(new Metric("foo"))).thenReturn(list);
- assertThat(formula.calculate(data, context).getData(), is("0.5=3;2.5=6"));
}
}
|