*/
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;
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 {
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;
+ }
}
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
}\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
}\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
}\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