diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-28 14:34:50 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-28 14:34:50 +0100 |
commit | 715a8ca8c9b2ed5571e5f40bb0a0b69ad6313285 (patch) | |
tree | d1e153e518c84f389b7cef9ffbc0bacaf57be10a /plugins | |
parent | b8eecc041ad4aa23a3bf5cf7c8847573b125d187 (diff) | |
download | sonarqube-715a8ca8c9b2ed5571e5f40bb0a0b69ad6313285.tar.gz sonarqube-715a8ca8c9b2ed5571e5f40bb0a0b69ad6313285.zip |
Add some unit tests
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/decorators/ClassComplexityDistributionBuilderTest.java | 46 |
1 files changed, 46 insertions, 0 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; + } } |