import java.util.List;
public final class AllTestsLineCoverageDecorator extends AbstractCoverageDecorator {
-
@DependsUpon
public List<Metric> dependsUponMetrics() {
return Arrays.asList(CoreMetrics.MERGED_UNCOVERED_LINES, CoreMetrics.MERGED_LINES_TO_COVER, CoreMetrics.NEW_MERGED_UNCOVERED_LINES,
import org.sonar.api.web.UserRole;
import org.sonar.api.web.WidgetCategory;
-@WidgetCategory({ "Tests" })
+@WidgetCategory("Tests")
@UserRole(UserRole.USER)
public class AllTestsCoverageWidget extends AbstractRubyTemplate implements RubyRailsWidget {
-
public String getId() {
return "merged-coverage";
}
protected String getTemplatePath() {
return "/org/sonar/plugins/core/widgets/merged_coverage.html.erb";
}
-}
\ No newline at end of file
+}
@WidgetCategory("Tests")
@UserRole(UserRole.USER)
public class CoverageWidget extends AbstractRubyTemplate implements RubyRailsWidget {
-
public String getId() {
return "code_coverage";
}
import org.sonar.api.web.UserRole;
import org.sonar.api.web.WidgetCategory;
-@WidgetCategory({ "Tests" })
+@WidgetCategory("Tests")
@UserRole(UserRole.USER)
public class ItCoverageWidget extends AbstractRubyTemplate implements RubyRailsWidget {
-
public String getId() {
return "it-coverage";
}
protected String getTemplatePath() {
return "/org/sonar/plugins/core/widgets/it_coverage.html.erb";
}
-}
\ No newline at end of file
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
+
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class AllTestsBranchCoverageDecoratorTest {
+ private final AllTestsBranchCoverageDecorator decorator = new AllTestsBranchCoverageDecorator();
+ private final Project resource = mock(Project.class);
+
+ @Before
+ public void setUp() {
+ when(resource.getScope()).thenReturn(Scopes.PROJECT);
+ when(resource.getQualifier()).thenReturn(Qualifiers.PROJECT);
+ }
+
+ @Test
+ public void shouldSaveBranchCoverage() {
+ DecoratorContext context = mockContext(20, 15);
+
+ decorator.decorate(resource, context);
+
+ verify(context).saveMeasure(CoreMetrics.MERGED_BRANCH_COVERAGE, 25.0);
+ }
+
+ @Test
+ public void shouldNotSaveBranchCoverageIfMissingConditions() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(resource, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_BRANCH_COVERAGE), anyDouble());
+ }
+
+ private static DecoratorContext mockContext(int conditions, int uncoveredConditions) {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.MERGED_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_CONDITIONS_TO_COVER, (double) conditions));
+ when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
+ return context;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class AllTestsCoverageDecoratorTest {
+ private final AllTestsCoverageDecorator decorator = new AllTestsCoverageDecorator();
+ private final Project project = mock(Project.class);
+
+ @Before
+ public void before() {
+ when(project.getScope()).thenReturn(Scopes.PROJECT);
+ }
+
+ @Test
+ public void noCoverageWhenStaticAnalysis() {
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
+ }
+
+ @Test
+ public void coverage() {
+ DecoratorContext context = mockContext(50, 40, 10, 8);
+
+ decorator.decorate(project, context);
+
+ // (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
+ verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 20.0);
+ }
+
+ @Test
+ public void coverageCanBe0() {
+ DecoratorContext context = mockContext(50, 50, 5, 5);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 0.0);
+ }
+
+ @Test
+ public void coverageCanBe100() {
+ DecoratorContext context = mockContext(50, 0, 5, 0);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 100.0);
+ }
+
+ @Test
+ public void noCoverageIfNoElements() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_COVERAGE), anyDouble());
+ }
+
+ private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.MERGED_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_LINES_TO_COVER, (double) lines));
+ when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, (double) uncoveredLines));
+ when(context.getMeasure(CoreMetrics.MERGED_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_CONDITIONS_TO_COVER, (double) conditions));
+ when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
+ return context;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.anyDouble;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class AllTestsLineCoverageDecoratorTest {
+ private final AllTestsLineCoverageDecorator decorator = new AllTestsLineCoverageDecorator();
+ private final Project project = mock(Project.class);
+
+ @Before
+ public void before() {
+ when(project.getScope()).thenReturn(Scopes.PROJECT);
+ }
+
+ @Test
+ public void should_depend_on_coverage_metrics() {
+ List<Metric> metrics = decorator.dependsUponMetrics();
+
+ assertThat(metrics).containsOnly(CoreMetrics.MERGED_UNCOVERED_LINES, CoreMetrics.MERGED_LINES_TO_COVER, CoreMetrics.NEW_MERGED_UNCOVERED_LINES,
+ CoreMetrics.NEW_MERGED_LINES_TO_COVER);
+ }
+
+ @Test
+ public void noCoverageWhenStaticAnalysis() {
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
+ assertThat(decorator.shouldExecuteOnProject(project)).isFalse();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+ assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+ assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
+ }
+
+ @Test
+ public void lineCoverage() {
+ DecoratorContext context = mockContext(50, 10);
+
+ decorator.decorate(project, context);
+
+ // 50-10 covered lines / 50 lines
+ verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 80.0);
+ }
+
+ @Test
+ public void zeroCoveredLines() {
+ DecoratorContext context = mockContext(50, 50);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 0.0);
+ }
+
+ @Test
+ public void allCoveredLines() {
+ DecoratorContext context = mockContext(50, 00);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 100.0);
+ }
+
+ @Test
+ public void noLineCoverageIfNoLines() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_LINE_COVERAGE), anyDouble());
+ }
+
+ private static DecoratorContext mockContext(int lines, int uncoveredLines) {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.MERGED_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_LINES_TO_COVER, (double) lines));
+ when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, (double) uncoveredLines));
+ return context;
+ }
+}
*/
package org.sonar.plugins.core.sensors;
-import org.sonar.api.resources.Resource;
-
+import org.junit.Before;
import org.junit.Test;
-import static org.mockito.Matchers.anyDouble;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
-public class BranchCoverageDecoratorTest {
-
- @Test
- public void shouldNotSaveBranchCoverageIfMissingConditions() {
- Project resource = mock(Project.class);
- when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
- when(resource.getQualifier()).thenReturn(Resource.QUALIFIER_SUBVIEW);
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
- DecoratorContext context = mockContext(null, null);
- new BranchCoverageDecorator().decorate(resource, context);
+public class BranchCoverageDecoratorTest {
+ private final BranchCoverageDecorator decorator = new BranchCoverageDecorator();
+ private final Project resource = mock(Project.class);
- verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
+ @Before
+ public void setUp() {
+ when(resource.getScope()).thenReturn(Scopes.PROJECT);
+ when(resource.getQualifier()).thenReturn(Qualifiers.PROJECT);
}
@Test
public void shouldSaveBranchCoverage() {
- Project resource = mock(Project.class);
- when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
- when(resource.getQualifier()).thenReturn(Resource.QUALIFIER_PROJECT);
-
DecoratorContext context = mockContext(20, 15);
- new BranchCoverageDecorator().decorate(resource, context);
+ decorator.decorate(resource, context);
+
verify(context).saveMeasure(CoreMetrics.BRANCH_COVERAGE, 25.0);
}
+ @Test
+ public void shouldNotSaveBranchCoverageIfMissingConditions() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(resource, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
+ }
- private DecoratorContext mockContext(Integer conditions, Integer uncoveredConditions) {
+ private static DecoratorContext mockContext(int conditions, int uncoveredConditions) {
DecoratorContext context = mock(DecoratorContext.class);
- if (conditions != null) {
- when(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.CONDITIONS_TO_COVER, conditions.doubleValue()));
- }
- if (uncoveredConditions != null) {
- when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, uncoveredConditions.doubleValue()));
- }
+ when(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.CONDITIONS_TO_COVER, (double) conditions));
+ when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) uncoveredConditions));
return context;
}
}
*/
package org.sonar.plugins.core.sensors;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-import org.sonar.api.resources.Resource;
-
import org.junit.Before;
import org.junit.Test;
-import static org.mockito.Mockito.*;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
-public class CoverageDecoratorTest {
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.anyDouble;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
- private Project project = null;
+public class CoverageDecoratorTest {
+ private final CoverageDecorator decorator = new CoverageDecorator();
+ private final Project project = mock(Project.class);
@Before
public void before() {
- project = mock(Project.class);
- when(project.getScope()).thenReturn(Resource.SCOPE_SET);
+ when(project.getScope()).thenReturn(Scopes.PROJECT);
}
@Test
public void noCoverageWhenStaticAnalysis() {
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
- assertThat(new CoverageDecorator().shouldExecuteOnProject(project), is(false));
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
- assertThat(new CoverageDecorator().shouldExecuteOnProject(project), is(true));
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
- assertThat(new CoverageDecorator().shouldExecuteOnProject(project), is(true));
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
}
@Test
public void coverage() {
DecoratorContext context = mockContext(50, 40, 10, 8);
- new CoverageDecorator().decorate(project, context);
+ decorator.decorate(project, context);
// (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
verify(context).saveMeasure(CoreMetrics.COVERAGE, 20.0);
@Test
public void coverageCanBe0() {
DecoratorContext context = mockContext(50, 50, 5, 5);
- new CoverageDecorator().decorate(project, context);
+
+ decorator.decorate(project, context);
verify(context).saveMeasure(CoreMetrics.COVERAGE, 0.0);
}
@Test
public void coverageCanBe100() {
DecoratorContext context = mockContext(50, 0, 5, 0);
- new CoverageDecorator().decorate(project, context);
+
+ decorator.decorate(project, context);
verify(context).saveMeasure(CoreMetrics.COVERAGE, 100.0);
}
@Test
public void noCoverageIfNoElements() {
- DecoratorContext context = mockContext(null, null, null, null);
- new CoverageDecorator().decorate(project, context);
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(project, context);
verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble());
}
- private DecoratorContext mockContext(Integer lines, Integer uncoveredLines, Integer conditions, Integer uncoveredConditions) {
+ private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
DecoratorContext context = mock(DecoratorContext.class);
- if (lines != null) {
- when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, lines.doubleValue()));
- }
- if (uncoveredLines != null) {
- when(context.getMeasure(CoreMetrics.UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.UNCOVERED_LINES, uncoveredLines.doubleValue()));
- }
- if (conditions != null) {
- when(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.CONDITIONS_TO_COVER, conditions.doubleValue()));
- }
- if (uncoveredConditions != null) {
- when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, uncoveredConditions.doubleValue()));
- }
+ when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, (double) lines));
+ when(context.getMeasure(CoreMetrics.UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.UNCOVERED_LINES, (double) uncoveredLines));
+ when(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.CONDITIONS_TO_COVER, (double) conditions));
+ when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) uncoveredConditions));
return context;
}
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
+
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ItBranchCoverageDecoratorTest {
+ private final ItBranchCoverageDecorator decorator = new ItBranchCoverageDecorator();
+ private final Project resource = mock(Project.class);
+
+ @Before
+ public void setUp() {
+ when(resource.getScope()).thenReturn(Scopes.PROJECT);
+ when(resource.getQualifier()).thenReturn(Qualifiers.PROJECT);
+ }
+
+ @Test
+ public void shouldSaveBranchCoverage() {
+ DecoratorContext context = mockContext(20, 15);
+
+ decorator.decorate(resource, context);
+
+ verify(context).saveMeasure(CoreMetrics.IT_BRANCH_COVERAGE, 25.0);
+ }
+
+ @Test
+ public void shouldNotSaveBranchCoverageIfMissingConditions() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(resource, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.IT_BRANCH_COVERAGE), anyDouble());
+ }
+
+ private static DecoratorContext mockContext(int conditions, int uncoveredConditions) {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.IT_CONDITIONS_TO_COVER, (double) conditions));
+ when(context.getMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
+ return context;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ItCoverageDecoratorTest {
+ private final ItCoverageDecorator decorator = new ItCoverageDecorator();
+ private final Project project = mock(Project.class);
+
+ @Before
+ public void before() {
+ when(project.getScope()).thenReturn(Scopes.PROJECT);
+ }
+
+ @Test
+ public void noCoverageWhenStaticAnalysis() {
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+ assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
+ }
+
+ @Test
+ public void coverage() {
+ DecoratorContext context = mockContext(50, 40, 10, 8);
+
+ decorator.decorate(project, context);
+
+ // (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
+ verify(context).saveMeasure(CoreMetrics.IT_COVERAGE, 20.0);
+ }
+
+ @Test
+ public void coverageCanBe0() {
+ DecoratorContext context = mockContext(50, 50, 5, 5);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.IT_COVERAGE, 0.0);
+ }
+
+ @Test
+ public void coverageCanBe100() {
+ DecoratorContext context = mockContext(50, 0, 5, 0);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.IT_COVERAGE, 100.0);
+ }
+
+ @Test
+ public void noCoverageIfNoElements() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.IT_COVERAGE), anyDouble());
+ }
+
+ 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));
+ when(context.getMeasure(CoreMetrics.IT_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.IT_UNCOVERED_LINES, (double) uncoveredLines));
+ when(context.getMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.IT_CONDITIONS_TO_COVER, (double) conditions));
+ when(context.getMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
+ return context;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.anyDouble;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ItLineCoverageDecoratorTest {
+ private final ItLineCoverageDecorator decorator = new ItLineCoverageDecorator();
+ private final Project project = mock(Project.class);
+
+ @Before
+ public void before() {
+ when(project.getScope()).thenReturn(Scopes.PROJECT);
+ }
+
+ @Test
+ public void should_depend_on_coverage_metrics() {
+ List<Metric> metrics = decorator.dependsUponMetrics();
+
+ assertThat(metrics).containsOnly(CoreMetrics.IT_UNCOVERED_LINES, CoreMetrics.IT_LINES_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_LINES, CoreMetrics.NEW_IT_LINES_TO_COVER);
+ }
+
+ @Test
+ public void noCoverageWhenStaticAnalysis() {
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
+ assertThat(decorator.shouldExecuteOnProject(project)).isFalse();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
+ assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
+
+ when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
+ assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
+ }
+
+ @Test
+ public void lineCoverage() {
+ DecoratorContext context = mockContext(50, 10);
+
+ decorator.decorate(project, context);
+
+ // 50-10 covered lines / 50 lines
+ verify(context).saveMeasure(CoreMetrics.IT_LINE_COVERAGE, 80.0);
+ }
+
+ @Test
+ public void zeroCoveredLines() {
+ DecoratorContext context = mockContext(50, 50);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.IT_LINE_COVERAGE, 0.0);
+ }
+
+ @Test
+ public void allCoveredLines() {
+ DecoratorContext context = mockContext(50, 00);
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.IT_LINE_COVERAGE, 100.0);
+ }
+
+ @Test
+ public void noLineCoverageIfNoLines() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.IT_LINE_COVERAGE), anyDouble());
+ }
+
+ private static DecoratorContext mockContext(int lines, int uncoveredLines) {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.IT_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.IT_LINES_TO_COVER, (double) lines));
+ when(context.getMeasure(CoreMetrics.IT_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.IT_UNCOVERED_LINES, (double) uncoveredLines));
+ return context;
+ }
+}
*/
package org.sonar.plugins.core.sensors;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-import org.sonar.api.resources.Resource;
-
import org.junit.Before;
import org.junit.Test;
-import static org.mockito.Mockito.*;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Scopes;
-public class LineCoverageDecoratorTest {
+import java.util.List;
- private Project project = null;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.anyDouble;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class LineCoverageDecoratorTest {
+ private final LineCoverageDecorator decorator = new LineCoverageDecorator();
+ private final Project project = mock(Project.class);
@Before
public void before() {
- project = mock(Project.class);
- when(project.getScope()).thenReturn(Resource.SCOPE_SET);
+ when(project.getScope()).thenReturn(Scopes.PROJECT);
+ }
+
+ @Test
+ public void should_depend_on_coverage_metrics() {
+ List<Metric> metrics = decorator.dependsUponMetrics();
+
+ assertThat(metrics).containsOnly(CoreMetrics.UNCOVERED_LINES, CoreMetrics.LINES_TO_COVER, CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.NEW_LINES_TO_COVER);
}
@Test
public void noCoverageWhenStaticAnalysis() {
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
- assertThat(new LineCoverageDecorator().shouldExecuteOnProject(project), is(false));
+ assertThat(decorator.shouldExecuteOnProject(project)).isFalse();
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
- assertThat(new LineCoverageDecorator().shouldExecuteOnProject(project), is(true));
+ assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
- assertThat(new LineCoverageDecorator().shouldExecuteOnProject(project), is(true));
+ assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
}
@Test
public void lineCoverage() {
DecoratorContext context = mockContext(50, 10);
- new LineCoverageDecorator().decorate(project, context);
+
+ decorator.decorate(project, context);
// 50-10 covered lines / 50 lines
verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 80.0);
}
-
- @Test
- public void noLineCoverageIfNoLines() {
- DecoratorContext context = mockContext(null, null);
- new LineCoverageDecorator().decorate(project, context);
-
- verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble());
- }
-
@Test
public void zeroCoveredLines() {
DecoratorContext context = mockContext(50, 50);
- new LineCoverageDecorator().decorate(project, context);
+
+ decorator.decorate(project, context);
verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 0.0);
}
@Test
public void allCoveredLines() {
DecoratorContext context = mockContext(50, 00);
- new LineCoverageDecorator().decorate(project, context);
+
+ decorator.decorate(project, context);
verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 100.0);
}
- private DecoratorContext mockContext(Integer lines, Integer uncoveredLines) {
+ @Test
+ public void noLineCoverageIfNoLines() {
+ DecoratorContext context = mock(DecoratorContext.class);
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble());
+ }
+
+ private static DecoratorContext mockContext(int lines, int uncoveredLines) {
DecoratorContext context = mock(DecoratorContext.class);
- if (lines != null) {
- when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, lines.doubleValue()));
- }
- if (uncoveredLines != null) {
- when(context.getMeasure(CoreMetrics.UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.UNCOVERED_LINES, uncoveredLines.doubleValue()));
- }
+ when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, (double) lines));
+ when(context.getMeasure(CoreMetrics.UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.UNCOVERED_LINES, (double) uncoveredLines));
return context;
}
-
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.widgets;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class AllTestsCoverageWidgetTest {
+ @Test
+ public void should_define_widget() {
+ AllTestsCoverageWidget widget = new AllTestsCoverageWidget();
+
+ assertThat(widget.getId()).isEqualTo("merged-coverage");
+ assertThat(widget.getTitle()).isEqualTo("All Tests Coverage");
+ }
+
+ @Test
+ public void should_use_relative_path_for_template() {
+ AllTestsCoverageWidget widget = new AllTestsCoverageWidget();
+
+ assertThat(widget.getTemplatePath()).startsWith("/");
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.widgets;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class CoverageWidgetTest {
+ @Test
+ public void should_define_widget() {
+ CoverageWidget widget = new CoverageWidget();
+
+ assertThat(widget.getId()).isEqualTo("code_coverage");
+ assertThat(widget.getTitle()).isEqualTo("Code coverage");
+ }
+
+ @Test
+ public void should_use_relative_path_for_template() {
+ CoverageWidget widget = new CoverageWidget();
+
+ assertThat(widget.getTemplatePath()).startsWith("/");
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.widgets;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ItCoverageWidgetTest {
+ @Test
+ public void should_define_widget() {
+ ItCoverageWidget widget = new ItCoverageWidget();
+
+ assertThat(widget.getId()).isEqualTo("it-coverage");
+ assertThat(widget.getTitle()).isEqualTo("Integration Test Coverage");
+ }
+
+ @Test
+ public void should_use_relative_path_for_template() {
+ ItCoverageWidget widget = new ItCoverageWidget();
+
+ assertThat(widget.getTemplatePath()).startsWith("/");
+ }
+}