diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-03-21 14:10:42 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-03-21 14:10:42 +0100 |
commit | b9b53575e822c1219161bae1644443840a54f6ca (patch) | |
tree | 6ac9e3a40ae267bd485f9be847a3fa57d735d767 /sonar-plugin-api/src/test/java/org/sonar | |
parent | 4de404dbcbac850c8ff10200fe14ce4703a27b09 (diff) | |
download | sonarqube-b9b53575e822c1219161bae1644443840a54f6ca.tar.gz sonarqube-b9b53575e822c1219161bae1644443840a54f6ca.zip |
SONAR-3900 Add new metrics "complexity in classes" and "complexity in methods" to compute "complexity/class" and "complexity/method"
Diffstat (limited to 'sonar-plugin-api/src/test/java/org/sonar')
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java | 77 |
1 files changed, 61 insertions, 16 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java index c8e9b09ca15..ae9767b4ef1 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java @@ -19,13 +19,13 @@ */ package org.sonar.api.measures; -import com.google.common.collect.Lists; import org.junit.Before; import org.junit.Test; import org.sonar.api.resources.JavaFile; import java.util.List; +import static com.google.common.collect.Lists.newArrayList; import static junit.framework.Assert.assertNull; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.hasItems; @@ -46,14 +46,14 @@ public class AverageFormulaTest { } @Test - public void testDependsUponMetrics() throws Exception { + public void test_depends_upon_metrics() throws Exception { AverageFormula formula = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS); assertThat(formula.dependsUponMetrics(), hasItems(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS)); } @Test - public void testAverageCalculation() { - List<FormulaData> childrenData = Lists.newArrayList(); + public void test_average_calculation() { + List<FormulaData> childrenData = newArrayList(); FormulaData data1 = mock(FormulaData.class); childrenData.add(data1); when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0)); @@ -72,23 +72,23 @@ public class AverageFormulaTest { } @Test - public void shouldNotComputeIfNotTargetMetric() { + public void should_not_compute_if_not_target_metric() { when(data.getMeasure(CoreMetrics.FUNCTION_COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTION_COMPLEXITY, 2.0)); Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context); assertNull(measure); } @Test - public void testWhenNoChildrenMesaures() { - List<FormulaData> childrenData = Lists.newArrayList(); + public void test_when_no_children_mesaures() { + List<FormulaData> childrenData = newArrayList(); when(data.getChildren()).thenReturn(childrenData); Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context); assertNull(measure); } @Test - public void testWhenNoComplexityMesaures() { - List<FormulaData> childrenData = Lists.newArrayList(); + public void test_when_no_complexity_mesaures() { + List<FormulaData> childrenData = newArrayList(); FormulaData data1 = mock(FormulaData.class); childrenData.add(data1); when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0)); @@ -100,8 +100,8 @@ public class AverageFormulaTest { } @Test - public void testWhenNoByMetricMesaures() { - List<FormulaData> childrenData = Lists.newArrayList(); + public void test_when_no_by_metric_mesaures() { + List<FormulaData> childrenData = newArrayList(); FormulaData data1 = mock(FormulaData.class); childrenData.add(data1); when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 43.0)); @@ -113,17 +113,17 @@ public class AverageFormulaTest { } @Test - public void testWhenMixedMetrics() { - List<FormulaData> childrenData = Lists.newArrayList(); + public void test_when_mixed_metrics() { + List<FormulaData> childrenData = newArrayList(); FormulaData data1 = mock(FormulaData.class); childrenData.add(data1); when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0)); - when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 107.0)); + when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 107.0)); FormulaData data2 = mock(FormulaData.class); childrenData.add(data2); when(data2.getMeasure(CoreMetrics.STATEMENTS)).thenReturn(new Measure(CoreMetrics.STATEMENTS, 127.0)); - when(data2.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 233.0)); + when(data2.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 233.0)); when(data.getChildren()).thenReturn(childrenData); @@ -133,7 +133,7 @@ public class AverageFormulaTest { } @Test - public void testCalculationForFIle() { + public void test_calculation_for_file() { when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0)); when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0)); when(context.getResource()).thenReturn(new JavaFile("foo")); @@ -141,4 +141,49 @@ public class AverageFormulaTest { Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context); assertThat(measure.getValue(), is(3.0)); } + + @Test + public void should_use_fallback_metric_when_no_data_on_main_metric_for_file() { + when(data.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(null); + when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0)); + when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0)); + when(context.getResource()).thenReturn(new JavaFile("foo")); + + Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS) + .setFallbackForMainMetric(CoreMetrics.COMPLEXITY) + .calculate(data, context); + assertThat(measure.getValue(), is(3.0)); + } + + @Test + public void should_use_main_metric_even_if_fallback_metric_provided() { + when(data.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 60.0)); + when(data.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 42.0)); + when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0)); + when(context.getResource()).thenReturn(new JavaFile("foo")); + + Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS) + .setFallbackForMainMetric(CoreMetrics.COMPLEXITY) + .calculate(data, context); + assertThat(measure.getValue(), is(3.0)); + } + + @Test + public void should_use_fallback_metric_when_no_data_on_main_metric_for_children() { + List<FormulaData> childrenData = newArrayList(); + FormulaData data1 = mock(FormulaData.class); + childrenData.add(data1); + when(data1.getMeasure(CoreMetrics.COMPLEXITY_IN_FUNCTIONS)).thenReturn(null); + when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 107.0)); + when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0)); + + when(data.getChildren()).thenReturn(childrenData); + + Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, CoreMetrics.FUNCTIONS) + .setFallbackForMainMetric(CoreMetrics.COMPLEXITY) + .calculate(data, context); + + assertThat(measure.getValue(), is(2.5)); + } + } |