2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 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.timemachine;
22 import org.apache.commons.lang.NumberUtils;
23 import org.hamcrest.BaseMatcher;
24 import org.hamcrest.Description;
25 import org.junit.Test;
26 import org.sonar.api.batch.DecoratorContext;
27 import org.sonar.api.measures.CoreMetrics;
28 import org.sonar.api.measures.Measure;
29 import org.sonar.api.measures.Metric;
30 import org.sonar.api.utils.DateUtils;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.Arrays;
35 import java.util.Date;
36 import java.util.List;
38 import static org.mockito.Mockito.*;
40 public class NewCoverageDecoratorTest {
43 public void shouldDoNothingIfNoScmData() throws ParseException {
44 DecoratorContext context = mock(DecoratorContext.class);
45 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA))
46 .thenReturn(new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=10"));
48 NewCoverageDecorator decorator = newDecorator();
49 decorator.doDecorate(context);
50 verify(context, never()).saveMeasure((Measure) anyObject());
54 public void shouldDoNothingIfNoCoverageData() throws ParseException {
55 DecoratorContext context = mock(DecoratorContext.class);
56 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE))
57 .thenReturn(new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2008-05-18T00:00:00+0000"));
59 NewCoverageDecorator decorator = newDecorator();
60 decorator.doDecorate(context);
62 verify(context, never()).saveMeasure((Measure) anyObject());
66 public void shouldGetNewLines() throws ParseException {
67 DecoratorContext context = mock(DecoratorContext.class);
68 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn(
69 new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3"));
70 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE)).thenReturn(
71 new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2007-01-15T00:00:00+0000;11=2011-01-01T00:00:00+0000"));
73 NewCoverageDecorator decorator = newDecorator();
74 decorator.doDecorate(context);
76 // line 11 has been updated after date1 (2009-12-25). This line is covered.
77 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 1, 1.0)));
78 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 1, 0.0)));
80 // no line have been updated after date3 (2011-02-18)
81 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 3, 0.0)));
82 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 3, 0.0)));
84 // no data on other periods
85 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 2, null)));
86 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 4, null)));
87 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 5, null)));
88 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 2, null)));
89 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 4, null)));
90 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 5, null)));
94 public void shouldGetNewConditions() throws ParseException {
95 DecoratorContext context = mock(DecoratorContext.class);
96 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn(
97 new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3"));
98 when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn(
99 new Measure(CoreMetrics.CONDITIONS_BY_LINE, "11=4"));
100 when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn(
101 new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "11=1"));
102 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE)).thenReturn(
103 new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2007-01-15T00:00:00+0000;11=2011-01-01T00:00:00+0000"));
105 NewCoverageDecorator decorator = newDecorator();
106 decorator.doDecorate(context);
108 // line 11 has been updated after date1 (2009-12-25). This line has 1 covered condition amongst 4
109 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, 4.0)));
110 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, 3.0)));
112 // no line have been updated after date3 (2011-02-18)
113 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 3, 0.0)));
114 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 3, 0.0)));
116 // no data on other periods
117 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 2, null)));
118 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 4, null)));
119 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 5, null)));
120 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 2, null)));
121 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 4, null)));
122 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 5, null)));
126 public void shouldNotGetNewConditionsWhenNewLineHasNoConditions() throws ParseException {
127 DecoratorContext context = mock(DecoratorContext.class);
128 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn(
129 new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3"));
130 when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn(
131 new Measure(CoreMetrics.CONDITIONS_BY_LINE, "10=1"));
132 when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn(
133 new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "10=1"));
134 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE)).thenReturn(
135 new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2007-01-15T00:00:00+0000;11=2011-01-01T00:00:00+0000"));
137 NewCoverageDecorator decorator = newDecorator();
138 decorator.doDecorate(context);
140 // line 11 has been updated after date1 (2009-12-25) but it has no conditions
141 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, 0.0)));
142 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, 0.0)));
146 static class VariationMatcher extends BaseMatcher<Measure> {
151 VariationMatcher(Metric metric, int index, Double variation) {
152 this.metric = metric;
154 this.variation = variation;
157 public boolean matches(Object o) {
158 Measure m = (Measure)o;
159 if (m.getMetric().equals(metric)) {
160 if ((variation==null && m.getVariation(index)==null) ||
161 (variation!=null && variation.equals(m.getVariation(index)))) {
168 public void describeTo(Description description) {
173 private NewCoverageDecorator newDecorator() throws ParseException {
174 List<NewCoverageDecorator.PeriodStruct> structs = Arrays.asList(
175 new NewCoverageDecorator.PeriodStruct(1, newDate("2009-12-25")),
176 new NewCoverageDecorator.PeriodStruct(3, newDate("2011-02-18")));
177 return new NewCoverageDecorator(structs);
180 private Date newDate(String s) throws ParseException {
181 return new SimpleDateFormat(DateUtils.DATE_FORMAT).parse(s);