From 824b3b9facd472d989596a214edbbc660df2dc1d Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 5 Oct 2012 13:56:08 +0200 Subject: [PATCH] Add more tests --- .../core/sensors/CoverageDecoratorTest.java | 28 +++++++++++++++++++ .../core/sensors/ItCoverageDecoratorTest.java | 28 +++++++++++++++++++ .../sensors/OverallCoverageDecoratorTest.java | 28 +++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CoverageDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CoverageDecoratorTest.java index 0a9a0a9ddb4..16efef23eb3 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CoverageDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CoverageDecoratorTest.java @@ -105,6 +105,19 @@ public class CoverageDecoratorTest { verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble()); } + @Test + public void should_count_covered_elements_for_new_code() { + Measure newLines = measureWithVariation(1, 100.0); + Measure newUncoveredConditions = measureWithVariation(1, 10.0); + Measure newUncoveredLines = measureWithVariation(1, 5.0); + Measure newConditions = measureWithVariation(1, 1.0); + DecoratorContext context = mockNewContext(newLines, newUncoveredConditions, newUncoveredLines, newConditions); + + long count = decorator.countCoveredElementsForNewCode(context, 1); + + assertThat(count).isEqualTo(86).isEqualTo(100 + 1 - 10 - 5); + } + private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) { DecoratorContext context = mock(DecoratorContext.class); when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, (double) lines)); @@ -113,4 +126,19 @@ public class CoverageDecoratorTest { when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) uncoveredConditions)); return context; } + + private static DecoratorContext mockNewContext(Measure newLines, Measure newUncoveredConditions, Measure newUncoveredLines, Measure newConditions) { + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(newLines); + when(context.getMeasure(CoreMetrics.NEW_UNCOVERED_LINES)).thenReturn(newUncoveredLines); + when(context.getMeasure(CoreMetrics.NEW_UNCOVERED_CONDITIONS)).thenReturn(newUncoveredConditions); + when(context.getMeasure(CoreMetrics.NEW_CONDITIONS_TO_COVER)).thenReturn(newConditions); + return context; + } + + private static Measure measureWithVariation(int period, double variation) { + Measure measure = mock(Measure.class); + when(measure.getVariation(period)).thenReturn(variation); + return measure; + } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ItCoverageDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ItCoverageDecoratorTest.java index 1fc2089a376..b8e3afb2162 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ItCoverageDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/ItCoverageDecoratorTest.java @@ -105,6 +105,19 @@ public class ItCoverageDecoratorTest { verify(context, never()).saveMeasure(eq(CoreMetrics.IT_COVERAGE), anyDouble()); } + @Test + public void should_count_covered_elements_for_new_code() { + Measure newLines = measureWithVariation(1, 100.0); + Measure newUncoveredConditions = measureWithVariation(1, 10.0); + Measure newUncoveredLines = measureWithVariation(1, 5.0); + Measure newConditions = measureWithVariation(1, 1.0); + DecoratorContext context = mockNewContext(newLines, newUncoveredConditions, newUncoveredLines, newConditions); + + long count = decorator.countCoveredElementsForNewCode(context, 1); + + assertThat(count).isEqualTo(86).isEqualTo(100 + 1 - 10 - 5); + } + private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) { DecoratorContext context = mock(DecoratorContext.class); when(context.getMeasure(CoreMetrics.IT_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.IT_LINES_TO_COVER, (double) lines)); @@ -113,4 +126,19 @@ public class ItCoverageDecoratorTest { when(context.getMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, (double) uncoveredConditions)); return context; } + + private static DecoratorContext mockNewContext(Measure newLines, Measure newUncoveredConditions, Measure newUncoveredLines, Measure newConditions) { + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.NEW_IT_LINES_TO_COVER)).thenReturn(newLines); + when(context.getMeasure(CoreMetrics.NEW_IT_UNCOVERED_LINES)).thenReturn(newUncoveredLines); + when(context.getMeasure(CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS)).thenReturn(newUncoveredConditions); + when(context.getMeasure(CoreMetrics.NEW_IT_CONDITIONS_TO_COVER)).thenReturn(newConditions); + return context; + } + + private static Measure measureWithVariation(int period, double variation) { + Measure measure = mock(Measure.class); + when(measure.getVariation(period)).thenReturn(variation); + return measure; + } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/OverallCoverageDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/OverallCoverageDecoratorTest.java index 7ec96574cee..546f9b8e312 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/OverallCoverageDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/OverallCoverageDecoratorTest.java @@ -105,6 +105,19 @@ public class OverallCoverageDecoratorTest { verify(context, never()).saveMeasure(eq(CoreMetrics.OVERALL_COVERAGE), anyDouble()); } + @Test + public void should_count_covered_elements_for_new_code() { + Measure newLines = measureWithVariation(1, 100.0); + Measure newUncoveredConditions = measureWithVariation(1, 10.0); + Measure newUncoveredLines = measureWithVariation(1, 5.0); + Measure newConditions = measureWithVariation(1, 1.0); + DecoratorContext context = mockNewContext(newLines, newUncoveredConditions, newUncoveredLines, newConditions); + + long count = decorator.countCoveredElementsForNewCode(context, 1); + + assertThat(count).isEqualTo(86).isEqualTo(100 + 1 - 10 - 5); + } + private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) { DecoratorContext context = mock(DecoratorContext.class); when(context.getMeasure(CoreMetrics.OVERALL_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.OVERALL_LINES_TO_COVER, (double) lines)); @@ -113,4 +126,19 @@ public class OverallCoverageDecoratorTest { when(context.getMeasure(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS, (double) uncoveredConditions)); return context; } + + private static DecoratorContext mockNewContext(Measure newLines, Measure newUncoveredConditions, Measure newUncoveredLines, Measure newConditions) { + DecoratorContext context = mock(DecoratorContext.class); + when(context.getMeasure(CoreMetrics.NEW_OVERALL_LINES_TO_COVER)).thenReturn(newLines); + when(context.getMeasure(CoreMetrics.NEW_OVERALL_UNCOVERED_LINES)).thenReturn(newUncoveredLines); + when(context.getMeasure(CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS)).thenReturn(newUncoveredConditions); + when(context.getMeasure(CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER)).thenReturn(newConditions); + return context; + } + + private static Measure measureWithVariation(int period, double variation) { + Measure measure = mock(Measure.class); + when(measure.getVariation(period)).thenReturn(variation); + return measure; + } } -- 2.39.5