]> source.dussan.org Git - sonarqube.git/blob
0ac22acda204fcc75c88fe3d63fa1251e465b9c7
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2009 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.plugins.core.timemachine;
21
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;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Date;
43 import java.util.List;
44
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.*;
49
50 public class NewViolationsDecoratorTest {
51   private Rule rule1;
52   private Rule rule2;
53   private Rule rule3;
54
55   private NewViolationsDecorator decorator;
56   private DecoratorContext context;
57   private Resource resource;
58
59   private Date rightNow;
60   private Date tenDaysAgo;
61   private Date fiveDaysAgo;
62
63   @Before
64   public void setUp() {
65     rightNow = new Date();
66     tenDaysAgo = DateUtils.addDays(rightNow, -10);
67     fiveDaysAgo = DateUtils.addDays(rightNow, -5);
68
69     PastSnapshot pastSnapshot = mock(PastSnapshot.class);
70     when(pastSnapshot.getIndex()).thenReturn(1);
71     when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo);
72
73     PastSnapshot pastSnapshot2 = mock(PastSnapshot.class);
74     when(pastSnapshot2.getIndex()).thenReturn(2);
75     when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo);
76
77     TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class);
78     when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2));
79
80     context = mock(DecoratorContext.class);
81     resource = mock(Resource.class);
82     when(context.getResource()).thenReturn(resource);
83
84     decorator = new NewViolationsDecorator(timeMachineConfiguration);
85
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");
89   }
90
91   @Test
92   public void shouldExecuteIfLastAnalysis() {
93     Project project = mock(Project.class);
94
95     when(project.isLatestAnalysis()).thenReturn(false);
96     assertThat(decorator.shouldExecuteOnProject(project), is(false));
97
98     when(project.isLatestAnalysis()).thenReturn(true);
99     assertThat(decorator.shouldExecuteOnProject(project), is(true));
100   }
101
102   @Test
103   public void shouldBeDependedUponMetric() {
104     assertThat(decorator.generatesMetric().size(), is(6));
105   }
106
107   @Test
108   public void shouldCountViolationsAfterDate() {
109     List<Violation> violations = createViolations();
110
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
114   }
115
116   @Test
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);
121
122     assertThat(decorator.sumChildren(1, children), is(2));
123     assertThat(decorator.sumChildren(2, children), is(3));
124     assertThat(decorator.sumChildren(3, children), is(6));
125   }
126
127   @Test
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));
132
133     decorator.decorate(resource, context);
134     decorator.decorate(resource, context);
135
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)));
138   }
139
140   @Test
141   public void priorityViolations() {
142     when(context.getViolations()).thenReturn(createViolations());
143
144     decorator.decorate(resource, context);
145
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)));
152   }
153
154   @Test
155   public void ruleViolations() {
156     when(context.getViolations()).thenReturn(createViolations());
157
158     decorator.decorate(resource, context);
159
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)));
164   }
165
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));
174     return violations;
175   }
176
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;
183
184     public IsVariationRuleMeasure(Metric metric, Rule rule, RulePriority priority, Double var1, Double var2) {
185       this.metric = metric;
186       this.rule = rule;
187       this.priority = priority;
188       this.var1 = var1;
189       this.var2 = var2;
190     }
191
192     public boolean matches(Object o) {
193       if (!(o instanceof RuleMeasure)) {
194         return false;
195       }
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());
202     }
203
204     public void describeTo(Description arg0) {
205     }
206   }
207
208   private class IsVariationMeasure extends BaseMatcher<Measure> {
209     private Metric metric = null;
210     private Double var1 = null;
211     private Double var2 = null;
212
213     public IsVariationMeasure(Metric metric, Double var1, Double var2) {
214       this.metric = metric;
215       this.var1 = var1;
216       this.var2 = var2;
217     }
218
219     public boolean matches(Object o) {
220       if (!(o instanceof Measure)) {
221         return false;
222       }
223       Measure m = (Measure) o;
224       return ObjectUtils.equals(metric, m.getMetric()) &&
225           ObjectUtils.equals(var1, m.getVariation1()) &&
226           ObjectUtils.equals(var2, m.getVariation2());
227     }
228
229     public void describeTo(Description o) {
230     }
231   }
232 }