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 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.File;
35 import org.sonar.api.resources.Project;
36 import org.sonar.api.resources.Resource;
37 import org.sonar.api.rules.Rule;
38 import org.sonar.api.rules.RulePriority;
39 import org.sonar.api.rules.Violation;
40 import org.sonar.batch.components.PastSnapshot;
41 import org.sonar.batch.components.TimeMachineConfiguration;
43 import java.util.Arrays;
44 import java.util.Date;
45 import java.util.List;
47 import static org.hamcrest.Matchers.is;
48 import static org.junit.Assert.assertThat;
49 import static org.mockito.Matchers.argThat;
50 import static org.mockito.Mockito.*;
52 public class NewViolationsDecoratorTest {
57 private NewViolationsDecorator decorator;
58 private DecoratorContext context;
59 private Resource resource;
61 private Date rightNow;
62 private Date tenDaysAgo;
63 private Date fiveDaysAgo;
67 rightNow = new Date();
68 tenDaysAgo = DateUtils.addDays(rightNow, -10);
69 fiveDaysAgo = DateUtils.addDays(rightNow, -5);
71 PastSnapshot pastSnapshot = mock(PastSnapshot.class);
72 when(pastSnapshot.getIndex()).thenReturn(1);
73 when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo);
75 PastSnapshot pastSnapshot2 = mock(PastSnapshot.class);
76 when(pastSnapshot2.getIndex()).thenReturn(2);
77 when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo);
79 TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class);
80 when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2));
82 context = mock(DecoratorContext.class);
83 resource = new File("com/foo/bar");
84 when(context.getResource()).thenReturn(resource);
86 decorator = new NewViolationsDecorator(timeMachineConfiguration);
88 rule1 = Rule.create().setRepositoryKey("rule1").setKey("rule1").setName("name1");
89 rule2 = Rule.create().setRepositoryKey("rule2").setKey("rule2").setName("name2");
90 rule3 = Rule.create().setRepositoryKey("rule3").setKey("rule3").setName("name3");
94 public void shouldExecuteIfLastAnalysis() {
95 Project project = mock(Project.class);
97 when(project.isLatestAnalysis()).thenReturn(false);
98 assertThat(decorator.shouldExecuteOnProject(project), is(false));
100 when(project.isLatestAnalysis()).thenReturn(true);
101 assertThat(decorator.shouldExecuteOnProject(project), is(true));
105 public void shouldBeDependedUponMetric() {
106 assertThat(decorator.generatesMetric().size(), is(6));
110 public void shouldCountViolationsAfterDate() {
111 List<Violation> violations = createViolations();
113 assertThat(decorator.countViolations(null, fiveDaysAgo), is(0));
114 assertThat(decorator.countViolations(violations, fiveDaysAgo), is(1)); // 1 rightNow
115 assertThat(decorator.countViolations(violations, tenDaysAgo), is(3)); // 1 rightNow + 2 fiveDaysAgo
119 public void shouldSumChildren() {
120 Measure measure1 = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).setVariation2(1.0).setVariation3(3.0);
121 Measure measure2 = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).setVariation2(2.0).setVariation3(3.0);
122 List<Measure> children = Arrays.asList(measure1, measure2);
124 assertThat(decorator.sumChildren(1, children), is(2));
125 assertThat(decorator.sumChildren(2, children), is(3));
126 assertThat(decorator.sumChildren(3, children), is(6));
130 public void shouldClearCacheAfterExecution() {
131 Violation violation1 = Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow);
132 Violation violation2 = Violation.create(rule2, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow);
133 when(context.getViolations()).thenReturn(Arrays.asList(violation1)).thenReturn(Arrays.asList(violation2));
135 decorator.decorate(resource, context);
136 decorator.decorate(resource, context);
138 verify(context, times(2)).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
139 verify(context, never()).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 2.0, 2.0)));
143 public void priorityViolations() {
144 when(context.getViolations()).thenReturn(createViolations());
146 decorator.decorate(resource, context);
148 // remember : period1 is 5daysAgo, period2 is 10daysAgo
149 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_BLOCKER_VIOLATIONS, 0.0, 0.0)));
150 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
151 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_MAJOR_VIOLATIONS, 0.0, 1.0)));
152 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_MINOR_VIOLATIONS, 0.0, 1.0)));
153 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_INFO_VIOLATIONS, 0.0, 0.0)));
157 public void ruleViolations() {
158 when(context.getViolations()).thenReturn(createViolations());
160 decorator.decorate(resource, context);
162 // remember : period1 is 5daysAgo, period2 is 10daysAgo
163 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_VIOLATIONS, rule1, RulePriority.CRITICAL, 1.0, 1.0)));
164 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_VIOLATIONS, rule2, RulePriority.MAJOR, 0.0, 1.0)));
165 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_VIOLATIONS, rule3, RulePriority.MINOR, 0.0, 1.0)));
168 private List<Violation> createViolations() {
169 List<Violation> violations = Lists.newLinkedList();
170 violations.add(Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow));
171 violations.add(Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(tenDaysAgo));
172 violations.add(Violation.create(rule2, resource).setSeverity(RulePriority.MAJOR).setCreatedAt(fiveDaysAgo));
173 violations.add(Violation.create(rule2, resource).setSeverity(RulePriority.MAJOR).setCreatedAt(tenDaysAgo));
174 violations.add(Violation.create(rule3, resource).setSeverity(RulePriority.MINOR).setCreatedAt(fiveDaysAgo));
175 violations.add(Violation.create(rule3, resource).setSeverity(RulePriority.MINOR).setCreatedAt(tenDaysAgo));
179 private class IsVariationRuleMeasure extends BaseMatcher<Measure> {
180 private Metric metric = null;
181 private Rule rule = null;
182 private RulePriority priority = null;
183 private Double var1 = null;
184 private Double var2 = null;
186 public IsVariationRuleMeasure(Metric metric, Rule rule, RulePriority priority, Double var1, Double var2) {
187 this.metric = metric;
189 this.priority = priority;
194 public boolean matches(Object o) {
195 if (!(o instanceof RuleMeasure)) {
198 RuleMeasure m = (RuleMeasure) o;
199 return ObjectUtils.equals(metric, m.getMetric()) &&
200 ObjectUtils.equals(rule, m.getRule()) &&
201 ObjectUtils.equals(priority, m.getRulePriority()) &&
202 ObjectUtils.equals(var1, m.getVariation1()) &&
203 ObjectUtils.equals(var2, m.getVariation2());
206 public void describeTo(Description arg0) {
210 private class IsVariationMeasure extends BaseMatcher<Measure> {
211 private Metric metric = null;
212 private Double var1 = null;
213 private Double var2 = null;
215 public IsVariationMeasure(Metric metric, Double var1, Double var2) {
216 this.metric = metric;
221 public boolean matches(Object o) {
222 if (!(o instanceof Measure)) {
225 Measure m = (Measure) o;
226 return ObjectUtils.equals(metric, m.getMetric()) &&
227 ObjectUtils.equals(var1, m.getVariation1()) &&
228 ObjectUtils.equals(var2, m.getVariation2());
231 public void describeTo(Description o) {