]> source.dussan.org Git - sonarqube.git/blob
51f17c74271585cc6180df8fe3a936c798a80f4f
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2012 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.plugins.core.timemachine;
21
22 import org.hamcrest.BaseMatcher;
23 import org.hamcrest.Description;
24 import org.junit.Test;
25 import org.mockito.Matchers;
26 import org.sonar.api.batch.DecoratorContext;
27 import org.sonar.api.measures.CoreMetrics;
28 import org.sonar.api.measures.Measure;
29
30 import java.util.Arrays;
31 import java.util.Collections;
32
33 import static org.mockito.Mockito.*;
34
35 public class NewCoverageAggregatorTest {
36
37   @Test
38   public void shouldNotSaveDataWhenNoMeasures() {
39     NewCoverageAggregator aggregator = new NewCoverageAggregator();
40     DecoratorContext context = mock(DecoratorContext.class);
41     when(context.getChildrenMeasures(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(Collections.<Measure>emptyList());
42
43     aggregator.aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, 3);
44
45     verify(context, never()).saveMeasure(Matchers.<Measure>anyObject());
46   }
47
48   @Test
49   public void shouldNotsetZeroWhenNoValueOnPeriod() {
50     NewCoverageAggregator aggregator = new NewCoverageAggregator();
51     DecoratorContext context = mock(DecoratorContext.class);
52     when(context.getChildrenMeasures(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(Arrays.asList(newMeasure(null, 3.0, 2.0), newMeasure(null, 13.0, null)));
53
54     aggregator.aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, 3);
55
56     verify(context).saveMeasure(argThat(new BaseMatcher<Measure>() {
57       public boolean matches(Object o) {
58         Measure m = (Measure)o;
59         return m.getVariation1()==null;
60       }
61
62       public void describeTo(Description description) {
63
64       }
65     }));
66   }
67
68
69   @Test
70   public void shouldSumValues() {
71     NewCoverageAggregator aggregator = new NewCoverageAggregator();
72     DecoratorContext context = mock(DecoratorContext.class);
73     when(context.getChildrenMeasures(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(Arrays.asList(newMeasure(null, 3.0, 2.0), newMeasure(null, 13.0, null)));
74
75     aggregator.aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, 3);
76
77     verify(context).saveMeasure(argThat(new BaseMatcher<Measure>() {
78       public boolean matches(Object o) {
79         Measure m = (Measure)o;
80         return m.getVariation2()==16.0 && m.getVariation3()==2.0;
81       }
82
83       public void describeTo(Description description) {
84
85       }
86     }));
87   }
88
89
90   private Measure newMeasure(Double variation1, Double variation2, Double variation3) {
91     return new Measure(CoreMetrics.NEW_LINES_TO_COVER)
92         .setVariation1(variation1)
93         .setVariation2(variation2)
94         .setVariation3(variation3);
95   }
96 }