]> source.dussan.org Git - sonarqube.git/blob
8dc10282cb3c1ef3d87113398a82cc459627c3e5
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2012 SonarSource
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.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;
42
43 import java.util.Arrays;
44 import java.util.Date;
45 import java.util.List;
46
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.*;
51
52 public class NewViolationsDecoratorTest {
53   private Rule rule1;
54   private Rule rule2;
55   private Rule rule3;
56
57   private NewViolationsDecorator decorator;
58   private DecoratorContext context;
59   private Resource resource;
60
61   private Date rightNow;
62   private Date tenDaysAgo;
63   private Date fiveDaysAgo;
64
65   @Before
66   public void setUp() {
67     rightNow = new Date();
68     tenDaysAgo = DateUtils.addDays(rightNow, -10);
69     fiveDaysAgo = DateUtils.addDays(rightNow, -5);
70
71     PastSnapshot pastSnapshot = mock(PastSnapshot.class);
72     when(pastSnapshot.getIndex()).thenReturn(1);
73     when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo);
74
75     PastSnapshot pastSnapshot2 = mock(PastSnapshot.class);
76     when(pastSnapshot2.getIndex()).thenReturn(2);
77     when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo);
78
79     TimeMachineConfiguration timeMachineConfiguration = mock(TimeMachineConfiguration.class);
80     when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2));
81
82     context = mock(DecoratorContext.class);
83     resource = new File("com/foo/bar");
84     when(context.getResource()).thenReturn(resource);
85
86     decorator = new NewViolationsDecorator(timeMachineConfiguration);
87
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");
91   }
92
93   @Test
94   public void shouldExecuteIfLastAnalysis() {
95     Project project = mock(Project.class);
96
97     when(project.isLatestAnalysis()).thenReturn(false);
98     assertThat(decorator.shouldExecuteOnProject(project), is(false));
99
100     when(project.isLatestAnalysis()).thenReturn(true);
101     assertThat(decorator.shouldExecuteOnProject(project), is(true));
102   }
103
104   @Test
105   public void shouldBeDependedUponMetric() {
106     assertThat(decorator.generatesMetric().size(), is(6));
107   }
108
109   @Test
110   public void shouldCountViolationsAfterDate() {
111     List<Violation> violations = createViolations();
112
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
116   }
117
118   @Test
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));
123
124     decorator.decorate(resource, context);
125     decorator.decorate(resource, context);
126
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)));
129   }
130
131   @Test
132   public void severityViolations() {
133     when(context.getViolations()).thenReturn(createViolations());
134
135     decorator.decorate(resource, context);
136
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)));
143   }
144
145   @Test
146   public void ruleViolations() {
147     when(context.getViolations()).thenReturn(createViolations());
148
149     decorator.decorate(resource, context);
150
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)));
155   }
156
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));
165     return violations;
166   }
167
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;
173
174     public IsVariationRuleMeasure(Metric metric, Rule rule, Double var1, Double var2) {
175       this.metric = metric;
176       this.rule = rule;
177       this.var1 = var1;
178       this.var2 = var2;
179     }
180
181     public boolean matches(Object o) {
182       if (!(o instanceof RuleMeasure)) {
183         return false;
184       }
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());
190     }
191
192     public void describeTo(Description arg0) {
193     }
194   }
195
196   private class IsVariationMeasure extends BaseMatcher<Measure> {
197     private Metric metric = null;
198     private Double var1 = null;
199     private Double var2 = null;
200
201     public IsVariationMeasure(Metric metric, Double var1, Double var2) {
202       this.metric = metric;
203       this.var1 = var1;
204       this.var2 = var2;
205     }
206
207     public boolean matches(Object o) {
208       if (!(o instanceof Measure)) {
209         return false;
210       }
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);
216     }
217
218     public void describeTo(Description o) {
219     }
220   }
221 }