2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
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 com.google.common.collect.Lists;
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.commons.lang.time.DateUtils;
25 import org.hamcrest.BaseMatcher;
26 import org.hamcrest.Description;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.sonar.api.batch.DecoratorContext;
30 import org.sonar.api.measures.CoreMetrics;
31 import org.sonar.api.measures.Measure;
32 import org.sonar.api.measures.Metric;
33 import org.sonar.api.measures.RuleMeasure;
34 import org.sonar.api.resources.Project;
35 import org.sonar.api.resources.Resource;
36 import org.sonar.api.rules.Rule;
37 import org.sonar.api.rules.RulePriority;
38 import org.sonar.api.rules.Violation;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Date;
43 import java.util.List;
45 import static org.hamcrest.Matchers.is;
46 import static org.junit.Assert.assertThat;
47 import static org.mockito.Matchers.argThat;
48 import static org.mockito.Mockito.*;
50 public class NewViolationsDecoratorTest {
55 private NewViolationsDecorator decorator;
56 private DecoratorContext context;
57 private Resource resource;
59 private Date rightNow;
60 private Date tenDaysAgo;
61 private Date fiveDaysAgo;
65 rightNow = new Date();
66 tenDaysAgo = DateUtils.addDays(rightNow, -10);
67 fiveDaysAgo = DateUtils.addDays(rightNow, -5);
69 PastSnapshot pastSnapshot = mock(PastSnapshot.class);
70 when(pastSnapshot.getIndex()).thenReturn(1);
71 when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo);
73 PastSnapshot pastSnapshot2 = mock(PastSnapshot.class);
74 when(pastSnapshot2.getIndex()).thenReturn(2);
75 when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo);
77 TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class);
78 when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2));
80 context = mock(DecoratorContext.class);
81 resource = mock(Resource.class);
82 when(context.getResource()).thenReturn(resource);
84 decorator = new NewViolationsDecorator(timeMachineConfiguration);
86 rule1 = Rule.create().setRepositoryKey("rule1").setKey("rule1").setName("name1");
87 rule2 = Rule.create().setRepositoryKey("rule2").setKey("rule2").setName("name2");
88 rule3 = Rule.create().setRepositoryKey("rule3").setKey("rule3").setName("name3");
92 public void shouldExecuteIfLastAnalysis() {
93 Project project = mock(Project.class);
95 when(project.isLatestAnalysis()).thenReturn(false);
96 assertThat(decorator.shouldExecuteOnProject(project), is(false));
98 when(project.isLatestAnalysis()).thenReturn(true);
99 assertThat(decorator.shouldExecuteOnProject(project), is(true));
103 public void shouldBeDependedUponMetric() {
104 assertThat(decorator.generatesMetric().size(), is(6));
108 public void shouldCountViolationsAfterDate() {
109 List<Violation> violations = createViolations();
111 assertThat(decorator.countViolations(null, fiveDaysAgo), is(0));
112 assertThat(decorator.countViolations(violations, fiveDaysAgo), is(1)); // 1 rightNow
113 assertThat(decorator.countViolations(violations, tenDaysAgo), is(3)); // 1 rightNow + 2 fiveDaysAgo
117 public void shouldSumChildren() {
118 Measure measure1 = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).setVariation2(1.0).setVariation3(3.0);
119 Measure measure2 = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).setVariation2(2.0).setVariation3(3.0);
120 List<Measure> children = Arrays.asList(measure1, measure2);
122 assertThat(decorator.sumChildren(1, children), is(2));
123 assertThat(decorator.sumChildren(2, children), is(3));
124 assertThat(decorator.sumChildren(3, children), is(6));
128 public void shouldClearCacheAfterExecution() {
129 Violation violation1 = Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow);
130 Violation violation2 = Violation.create(rule2, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow);
131 when(context.getViolations()).thenReturn(Arrays.asList(violation1)).thenReturn(Arrays.asList(violation2));
133 decorator.decorate(resource, context);
134 decorator.decorate(resource, context);
136 verify(context, times(2)).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
137 verify(context, never()).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 2.0, 2.0)));
141 public void priorityViolations() {
142 when(context.getViolations()).thenReturn(createViolations());
144 decorator.decorate(resource, context);
146 // remember : period1 is 5daysAgo, period2 is 10daysAgo
147 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_BLOCKER_VIOLATIONS, 0.0, 0.0)));
148 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
149 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_MAJOR_VIOLATIONS, 0.0, 1.0)));
150 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_MINOR_VIOLATIONS, 0.0, 1.0)));
151 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_INFO_VIOLATIONS, 0.0, 0.0)));
155 public void ruleViolations() {
156 when(context.getViolations()).thenReturn(createViolations());
158 decorator.decorate(resource, context);
160 // remember : period1 is 5daysAgo, period2 is 10daysAgo
161 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_VIOLATIONS, rule1, RulePriority.CRITICAL, 1.0, 1.0)));
162 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_VIOLATIONS, rule2, RulePriority.MAJOR, 0.0, 1.0)));
163 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_VIOLATIONS, rule3, RulePriority.MINOR, 0.0, 1.0)));
166 private List<Violation> createViolations() {
167 List<Violation> violations = Lists.newLinkedList();
168 violations.add(Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow));
169 violations.add(Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(tenDaysAgo));
170 violations.add(Violation.create(rule2, resource).setSeverity(RulePriority.MAJOR).setCreatedAt(fiveDaysAgo));
171 violations.add(Violation.create(rule2, resource).setSeverity(RulePriority.MAJOR).setCreatedAt(tenDaysAgo));
172 violations.add(Violation.create(rule3, resource).setSeverity(RulePriority.MINOR).setCreatedAt(fiveDaysAgo));
173 violations.add(Violation.create(rule3, resource).setSeverity(RulePriority.MINOR).setCreatedAt(tenDaysAgo));
177 private class IsVariationRuleMeasure extends BaseMatcher<Measure> {
178 private Metric metric = null;
179 private Rule rule = null;
180 private RulePriority priority = null;
181 private Double var1 = null;
182 private Double var2 = null;
184 public IsVariationRuleMeasure(Metric metric, Rule rule, RulePriority priority, Double var1, Double var2) {
185 this.metric = metric;
187 this.priority = priority;
192 public boolean matches(Object o) {
193 if (!(o instanceof RuleMeasure)) {
196 RuleMeasure m = (RuleMeasure) o;
197 return ObjectUtils.equals(metric, m.getMetric()) &&
198 ObjectUtils.equals(rule, m.getRule()) &&
199 ObjectUtils.equals(priority, m.getRulePriority()) &&
200 ObjectUtils.equals(var1, m.getVariation1()) &&
201 ObjectUtils.equals(var2, m.getVariation2());
204 public void describeTo(Description arg0) {
208 private class IsVariationMeasure extends BaseMatcher<Measure> {
209 private Metric metric = null;
210 private Double var1 = null;
211 private Double var2 = null;
213 public IsVariationMeasure(Metric metric, Double var1, Double var2) {
214 this.metric = metric;
219 public boolean matches(Object o) {
220 if (!(o instanceof Measure)) {
223 Measure m = (Measure) o;
224 return ObjectUtils.equals(metric, m.getMetric()) &&
225 ObjectUtils.equals(var1, m.getVariation1()) &&
226 ObjectUtils.equals(var2, m.getVariation2());
229 public void describeTo(Description o) {