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 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 shouldClearCacheAfterExecution() {
120 Violation violation1 = Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow);
121 Violation violation2 = Violation.create(rule2, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow);
122 when(context.getViolations()).thenReturn(Arrays.asList(violation1)).thenReturn(Arrays.asList(violation2));
124 decorator.decorate(resource, context);
125 decorator.decorate(resource, context);
127 verify(context, times(2)).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
128 verify(context, never()).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 2.0, 2.0)));
132 public void severityViolations() {
133 when(context.getViolations()).thenReturn(createViolations());
135 decorator.decorate(resource, context);
137 // remember : period1 is 5daysAgo, period2 is 10daysAgo
138 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_BLOCKER_VIOLATIONS, 0.0, 0.0)));
139 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
140 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_MAJOR_VIOLATIONS, 0.0, 1.0)));
141 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_MINOR_VIOLATIONS, 0.0, 1.0)));
142 verify(context).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_INFO_VIOLATIONS, 0.0, 0.0)));
146 public void ruleViolations() {
147 when(context.getViolations()).thenReturn(createViolations());
149 decorator.decorate(resource, context);
151 // remember : period1 is 5daysAgo, period2 is 10daysAgo
152 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, rule1, 1.0, 1.0)));
153 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_MAJOR_VIOLATIONS, rule2, 0.0, 1.0)));
154 verify(context).saveMeasure(argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_MINOR_VIOLATIONS, rule3, 0.0, 1.0)));
157 private List<Violation> createViolations() {
158 List<Violation> violations = Lists.newLinkedList();
159 violations.add(Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow));
160 violations.add(Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(tenDaysAgo));
161 violations.add(Violation.create(rule2, resource).setSeverity(RulePriority.MAJOR).setCreatedAt(fiveDaysAgo));
162 violations.add(Violation.create(rule2, resource).setSeverity(RulePriority.MAJOR).setCreatedAt(tenDaysAgo));
163 violations.add(Violation.create(rule3, resource).setSeverity(RulePriority.MINOR).setCreatedAt(fiveDaysAgo));
164 violations.add(Violation.create(rule3, resource).setSeverity(RulePriority.MINOR).setCreatedAt(tenDaysAgo));
168 private class IsVariationRuleMeasure extends BaseMatcher<Measure> {
169 private Metric metric = null;
170 private Rule rule = null;
171 private Double var1 = null;
172 private Double var2 = null;
174 public IsVariationRuleMeasure(Metric metric, Rule rule, Double var1, Double var2) {
175 this.metric = metric;
181 public boolean matches(Object o) {
182 if (!(o instanceof RuleMeasure)) {
185 RuleMeasure m = (RuleMeasure) o;
186 return ObjectUtils.equals(metric, m.getMetric()) &&
187 ObjectUtils.equals(rule, m.getRule()) &&
188 ObjectUtils.equals(var1, m.getVariation1()) &&
189 ObjectUtils.equals(var2, m.getVariation2());
192 public void describeTo(Description arg0) {
196 private class IsVariationMeasure extends BaseMatcher<Measure> {
197 private Metric metric = null;
198 private Double var1 = null;
199 private Double var2 = null;
201 public IsVariationMeasure(Metric metric, Double var1, Double var2) {
202 this.metric = metric;
207 public boolean matches(Object o) {
208 if (!(o instanceof Measure)) {
211 Measure m = (Measure) o;
212 return ObjectUtils.equals(metric, m.getMetric()) &&
213 ObjectUtils.equals(var1, m.getVariation1()) &&
214 ObjectUtils.equals(var2, m.getVariation2()) &&
215 !(m instanceof RuleMeasure);
218 public void describeTo(Description o) {