2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.core.sensors;
22 import org.sonar.api.resources.Resource;
24 import org.junit.Test;
25 import static org.mockito.Matchers.anyDouble;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.*;
28 import org.sonar.api.batch.DecoratorContext;
29 import org.sonar.api.measures.CoreMetrics;
30 import org.sonar.api.measures.Measure;
31 import org.sonar.api.resources.Project;
33 public class BranchCoverageDecoratorTest {
36 public void shouldNotSaveBranchCoverageIfMissingConditions() {
37 Project resource = mock(Project.class);
38 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
39 when(resource.getQualifier()).thenReturn(Resource.QUALIFIER_SUBVIEW);
41 DecoratorContext context = mockContext(null, null);
42 new BranchCoverageDecorator().decorate(resource, context);
44 verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
48 public void shouldSaveBranchCoverage() {
49 Project resource = mock(Project.class);
50 when(resource.getScope()).thenReturn(Resource.SCOPE_SET);
51 when(resource.getQualifier()).thenReturn(Resource.QUALIFIER_PROJECT);
53 DecoratorContext context = mockContext(20, 15);
55 new BranchCoverageDecorator().decorate(resource, context);
56 verify(context).saveMeasure(CoreMetrics.BRANCH_COVERAGE, 25.0);
60 private DecoratorContext mockContext(Integer conditions, Integer uncoveredConditions) {
61 DecoratorContext context = mock(DecoratorContext.class);
62 if (conditions != null) {
63 when(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.CONDITIONS_TO_COVER, conditions.doubleValue()));
65 if (uncoveredConditions != null) {
66 when(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, uncoveredConditions.doubleValue()));