2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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 License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.sensors;
22 import com.google.common.collect.Maps;
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.commons.lang.time.DateUtils;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentMatcher;
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.measures.Metric;
32 import org.sonar.api.measures.RuleMeasure;
33 import org.sonar.api.resources.*;
34 import org.sonar.api.rules.Rule;
35 import org.sonar.api.rules.Violation;
36 import org.sonar.batch.components.PastSnapshot;
37 import org.sonar.batch.components.TimeMachineConfiguration;
38 import org.sonar.core.review.ReviewDao;
39 import org.sonar.core.review.ReviewDto;
41 import java.util.Arrays;
42 import java.util.Date;
45 import static org.fest.assertions.Assertions.assertThat;
46 import static org.mockito.Matchers.any;
47 import static org.mockito.Matchers.anyDouble;
48 import static org.mockito.Matchers.argThat;
49 import static org.mockito.Mockito.*;
51 public class ReviewsMeasuresDecoratorTest {
53 private ReviewsMeasuresDecorator decorator;
54 private DecoratorContext context;
56 private Date rightNow;
57 private Date tenDaysAgo;
58 private Date fiveDaysAgo;
62 ReviewDao reviewDao = mock(ReviewDao.class);
64 rightNow = new Date();
65 tenDaysAgo = DateUtils.addDays(rightNow, -10);
66 fiveDaysAgo = DateUtils.addDays(rightNow, -5);
68 PastSnapshot pastSnapshot = mock(PastSnapshot.class);
69 when(pastSnapshot.getIndex()).thenReturn(1);
70 when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo);
72 PastSnapshot pastSnapshot2 = mock(PastSnapshot.class);
73 when(pastSnapshot2.getIndex()).thenReturn(2);
74 when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo);
76 TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class);
77 when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2));
79 decorator = new ReviewsMeasuresDecorator(reviewDao, timeMachineConfiguration);
80 context = mock(DecoratorContext.class);
81 when(context.getMeasure(CoreMetrics.VIOLATIONS)).thenReturn(new Measure(CoreMetrics.VIOLATIONS, 35d));
85 public void shouldExecuteOnProject() {
86 ReviewsMeasuresDecorator decorator = new ReviewsMeasuresDecorator(null, null);
87 Project project = new Project("foo");
88 project.setLatestAnalysis(true);
89 assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
93 public void shouldNotDecoratePersistableResource() throws Exception {
94 ReviewsMeasuresDecorator decorator = new ReviewsMeasuresDecorator(null, null);
95 DecoratorContext context = mock(DecoratorContext.class);
96 Resource<?> resource = Method.createMethod("foo", null).setId(1);
97 decorator.decorate(resource, context);
98 verify(context, never()).saveMeasure(any(Metric.class), anyDouble());
105 public void shouldDecorateUnitTest() throws Exception {
106 DecoratorContext context = mock(DecoratorContext.class);
107 File resource = new File("foo");
108 resource.setQualifier(Qualifiers.UNIT_TEST_FILE);
110 decorator.decorate(resource, context);
111 verify(context, atLeast(1)).saveMeasure(any(Metric.class), anyDouble());
115 public void shouldTrackNewViolationsWithoutReview() throws Exception {
116 Resource<?> resource = new File("foo").setId(1);
117 Violation v1 = Violation.create((Rule) null, resource).setPermanentId(1); // test the null case for the created_at date
118 Violation v2 = Violation.create((Rule) null, resource).setPermanentId(2).setCreatedAt(rightNow);
119 Violation v3 = Violation.create((Rule) null, resource).setPermanentId(3).setCreatedAt(fiveDaysAgo);
120 Violation v4 = Violation.create((Rule) null, resource).setPermanentId(4).setCreatedAt(fiveDaysAgo);
121 Violation v5 = Violation.create((Rule) null, resource).setPermanentId(5).setCreatedAt(fiveDaysAgo);
122 Violation v6 = Violation.create((Rule) null, resource).setPermanentId(6).setCreatedAt(tenDaysAgo);
123 when(context.getViolations()).thenReturn(Arrays.asList(v1, v2, v3, v4, v5, v6));
125 Map<Integer, ReviewDto> openReviewsByViolationPermanentIds = Maps.newHashMap();
126 openReviewsByViolationPermanentIds.put(1, new ReviewDto());
127 openReviewsByViolationPermanentIds.put(3, new ReviewDto());
129 decorator.trackNewViolationsWithoutReview(context, openReviewsByViolationPermanentIds);
130 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_UNREVIEWED_VIOLATIONS, 1.0, 3.0)));
133 private class IsVariationMeasure extends ArgumentMatcher<Measure> {
134 private Metric metric = null;
135 private Double var1 = null;
136 private Double var2 = null;
138 public IsVariationMeasure(Metric metric, Double var1, Double var2) {
139 this.metric = metric;
144 public boolean matches(Object o) {
145 if (!(o instanceof Measure)) {
148 Measure m = (Measure) o;
149 return ObjectUtils.equals(metric, m.getMetric()) &&
150 ObjectUtils.equals(var1, m.getVariation1()) &&
151 ObjectUtils.equals(var2, m.getVariation2()) &&
152 !(m instanceof RuleMeasure);