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.timemachine;
22 import org.hamcrest.BaseMatcher;
23 import org.hamcrest.Description;
24 import org.junit.Test;
25 import org.sonar.api.batch.DecoratorContext;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.measures.Metric;
29 import org.sonar.api.utils.DateUtils;
31 import java.text.ParseException;
32 import java.text.SimpleDateFormat;
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.List;
37 import static org.mockito.Mockito.*;
39 public class NewCoverageFileAnalyzerTest {
42 public void shouldDoNothingIfNoScmData() throws ParseException {
43 DecoratorContext context = mock(DecoratorContext.class);
44 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA))
45 .thenReturn(new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=10"));
47 AbstractNewCoverageFileAnalyzer decorator = newDecorator();
48 decorator.doDecorate(context);
49 verify(context, never()).saveMeasure((Measure) anyObject());
53 public void shouldDoNothingIfNoCoverageData() throws ParseException {
54 DecoratorContext context = mock(DecoratorContext.class);
55 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE))
56 .thenReturn(new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2008-05-18T00:00:00+0000"));
58 AbstractNewCoverageFileAnalyzer decorator = newDecorator();
59 decorator.doDecorate(context);
61 verify(context, never()).saveMeasure((Measure) anyObject());
65 public void shouldGetNewLines() throws ParseException {
66 DecoratorContext context = mock(DecoratorContext.class);
67 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn(
68 new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3"));
69 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE)).thenReturn(
70 new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2007-01-15T00:00:00+0000;11=2011-01-01T00:00:00+0000"));
72 AbstractNewCoverageFileAnalyzer decorator = newDecorator();
73 decorator.doDecorate(context);
75 // line 11 has been updated after date1 (2009-12-25). This line is covered.
76 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 1, 1.0)));
77 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 1, 0.0)));
79 // no line have been updated after date3 (2011-02-18)
80 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 3, 0.0)));
81 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 3, 0.0)));
83 // no data on other periods
84 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 2, null)));
85 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 4, null)));
86 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 5, null)));
87 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 2, null)));
88 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 4, null)));
89 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 5, null)));
93 public void shouldGetNewConditions() throws ParseException {
94 DecoratorContext context = mock(DecoratorContext.class);
95 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn(
96 new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3"));
97 when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn(
98 new Measure(CoreMetrics.CONDITIONS_BY_LINE, "11=4"));
99 when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn(
100 new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "11=1"));
101 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE)).thenReturn(
102 new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2007-01-15T00:00:00+0000;11=2011-01-01T00:00:00+0000"));
104 AbstractNewCoverageFileAnalyzer decorator = newDecorator();
105 decorator.doDecorate(context);
107 // line 11 has been updated after date1 (2009-12-25). This line has 1 covered condition amongst 4
108 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, 4.0)));
109 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, 3.0)));
111 // no line have been updated after date3 (2011-02-18)
112 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 3, 0.0)));
113 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 3, 0.0)));
115 // no data on other periods
116 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 2, null)));
117 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 4, null)));
118 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 5, null)));
119 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 2, null)));
120 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 4, null)));
121 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 5, null)));
125 public void shouldNotGetNewConditionsWhenNewLineHasNoConditions() throws ParseException {
126 DecoratorContext context = mock(DecoratorContext.class);
127 when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn(
128 new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3"));
129 when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn(
130 new Measure(CoreMetrics.CONDITIONS_BY_LINE, "10=1"));
131 when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn(
132 new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "10=1"));
133 when(context.getMeasure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE)).thenReturn(
134 new Measure(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, "10=2007-01-15T00:00:00+0000;11=2011-01-01T00:00:00+0000"));
136 AbstractNewCoverageFileAnalyzer decorator = newDecorator();
137 decorator.doDecorate(context);
139 // line 11 has been updated after date1 (2009-12-25) but it has no conditions
140 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, 0.0)));
141 verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, 0.0)));
145 static class VariationMatcher extends BaseMatcher<Measure> {
150 VariationMatcher(Metric metric, int index, Double variation) {
151 this.metric = metric;
153 this.variation = variation;
156 public boolean matches(Object o) {
157 Measure m = (Measure)o;
158 if (m.getMetric().equals(metric)) {
159 if ((variation==null && m.getVariation(index)==null) ||
160 (variation!=null && variation.equals(m.getVariation(index)))) {
167 public void describeTo(Description description) {
172 private AbstractNewCoverageFileAnalyzer newDecorator() throws ParseException {
173 List<AbstractNewCoverageFileAnalyzer.PeriodStruct> structs = Arrays.asList(
174 new AbstractNewCoverageFileAnalyzer.PeriodStruct(1, newDate("2009-12-25")),
175 new AbstractNewCoverageFileAnalyzer.PeriodStruct(3, newDate("2011-02-18")));
176 return new NewCoverageFileAnalyzer(structs);
179 private Date newDate(String s) throws ParseException {
180 return new SimpleDateFormat(DateUtils.DATE_FORMAT).parse(s);