]> source.dussan.org Git - sonarqube.git/blob
5c951c39f5f9f3ef681b6e895f150cf0aaeba5a1
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * SonarQube 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  * SonarQube 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 License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.plugins.core.timemachine;
21
22 import com.google.common.collect.Lists;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.database.model.RuleFailureModel;
26 import org.sonar.api.resources.Project;
27 import org.sonar.api.rules.Rule;
28 import org.sonar.api.rules.Violation;
29 import org.sonar.api.utils.DateUtils;
30
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.Map;
34
35 import static org.fest.assertions.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39 public class ViolationTrackingDecoratorTest {
40
41   private ViolationTrackingDecorator decorator;
42   private final Date analysisDate = DateUtils.parseDate("2010-12-25");
43
44   @Before
45   public void setUp() {
46     Project project = mock(Project.class);
47     when(project.getAnalysisDate()).thenReturn(analysisDate);
48     decorator = new ViolationTrackingDecorator(project, null, null);
49   }
50
51   @Test
52   public void permanentIdShouldBeThePrioritaryFieldToCheck() {
53     RuleFailureModel referenceViolation1 = newReferenceViolation("message", 10, 1, "checksum1").setPermanentId(100);
54     RuleFailureModel referenceViolation2 = newReferenceViolation("message", 18, 1, "checksum2").setPermanentId(200);
55     Violation newViolation = newViolation("message", 10, 1, "checksum1"); // exactly the fields of referenceViolation1
56     newViolation.setPermanentId(200);
57
58     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation1, referenceViolation2));
59     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation2); // same permanent id
60     assertThat(newViolation.isNew()).isFalse();
61   }
62
63   @Test
64   public void checksumShouldHaveGreaterPriorityThanLine() {
65     RuleFailureModel referenceViolation1 = newReferenceViolation("message", 1, 50, "checksum1");
66     RuleFailureModel referenceViolation2 = newReferenceViolation("message", 3, 50, "checksum2");
67
68     Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
69     Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
70
71     decorator.mapViolations(Lists.newArrayList(newViolation1, newViolation2), Lists.newArrayList(referenceViolation1, referenceViolation2));
72     assertThat(decorator.getReferenceViolation(newViolation1)).isSameAs(referenceViolation1);
73     assertThat(newViolation1.isNew()).isFalse();
74     assertThat(decorator.getReferenceViolation(newViolation2)).isSameAs(referenceViolation2);
75     assertThat(newViolation2.isNew()).isFalse();
76   }
77
78   /**
79    * SONAR-2928
80    */
81   @Test
82   public void sameRuleAndNullLineAndChecksumButDifferentMessages() {
83     Violation newViolation = newViolation("new message", null, 50, "checksum1");
84     RuleFailureModel referenceViolation = newReferenceViolation("old message", null, 50, "checksum1");
85
86     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
87     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
88     assertThat(newViolation.isNew()).isFalse();
89   }
90
91   @Test
92   public void sameRuleAndLineAndChecksumButDifferentMessages() {
93     Violation newViolation = newViolation("new message", 1, 50, "checksum1");
94     RuleFailureModel referenceViolation = newReferenceViolation("old message", 1, 50, "checksum1");
95
96     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
97     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
98     assertThat(newViolation.isNew()).isFalse();
99   }
100
101   @Test
102   public void sameRuleAndLineMessage() {
103     Violation newViolation = newViolation("message", 1, 50, "checksum1");
104     RuleFailureModel referenceViolation = newReferenceViolation("message", 1, 50, "checksum2");
105
106     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
107     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
108     assertThat(newViolation.isNew()).isFalse();
109   }
110
111   @Test
112   public void shouldIgnoreReferenceMeasureWithoutChecksum() {
113     Violation newViolation = newViolation("message", 1, 50, null);
114     RuleFailureModel referenceViolation = newReferenceViolation("message", 1, 51, null);
115
116     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
117     assertThat(decorator.getReferenceViolation(newViolation)).isNull();
118     assertThat(newViolation.isNew()).isTrue();
119   }
120
121   @Test
122   public void sameRuleAndMessageAndChecksumButDifferentLine() {
123     Violation newViolation = newViolation("message", 1, 50, "checksum1");
124     RuleFailureModel referenceViolation = newReferenceViolation("message", 2, 50, "checksum1");
125
126     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
127     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
128     assertThat(newViolation.isNew()).isFalse();
129   }
130
131   /**
132    * SONAR-2812
133    */
134   @Test
135   public void sameChecksumAndRuleButDifferentLineAndDifferentMessage() {
136     Violation newViolation = newViolation("new message", 1, 50, "checksum1");
137     RuleFailureModel referenceViolation = newReferenceViolation("old message", 2, 50, "checksum1");
138
139     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
140     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
141     assertThat(newViolation.isNew()).isFalse();
142   }
143
144   @Test
145   public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
146     Violation newViolation = newViolation("message", 1, 50, "checksum1");
147     RuleFailureModel referenceViolation = newReferenceViolation("message", 2, 50, "checksum2");
148
149     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
150     assertThat(decorator.getReferenceViolation(newViolation)).isNull();
151     assertThat(newViolation.isNew()).isTrue();
152   }
153
154   @Test
155   public void shouldNotTrackViolationIfDifferentRule() {
156     Violation newViolation = newViolation("message", 1, 50, "checksum1");
157     RuleFailureModel referenceViolation = newReferenceViolation("message", 1, 51, "checksum1");
158
159     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
160     assertThat(decorator.getReferenceViolation(newViolation)).isNull();
161     assertThat(newViolation.isNew()).isTrue();
162   }
163
164   @Test
165   public void shouldCompareViolationsWithDatabaseFormat() {
166     // violation messages are trimmed and can be abbreviated when persisted in database.
167     // Comparing violation messages must use the same format.
168     Violation newViolation = newViolation(" message ", 1, 50, "checksum1");
169     RuleFailureModel referenceViolation = newReferenceViolation("       message       ", 1, 50, "checksum2");
170
171     decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
172     assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
173     assertThat(newViolation.isNew()).isFalse();
174   }
175
176   @Test
177   public void shouldSetDateOfNewViolations() {
178     Violation newViolation = newViolation("message", 1, 50, "checksum");
179     assertThat(newViolation.getCreatedAt()).isNull();
180
181     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Collections.<RuleFailureModel> emptyList());
182     assertThat(mapping.size()).isEqualTo(0);
183     assertThat(newViolation.getCreatedAt()).isEqualTo(analysisDate);
184     assertThat(newViolation.isNew()).isTrue();
185   }
186
187   @Test
188   public void shouldCopyDateWhenNotNew() {
189     Violation newViolation = newViolation("message", 1, 50, "checksum");
190     RuleFailureModel referenceViolation = newReferenceViolation("", 1, 50, "checksum");
191     Date referenceDate = DateUtils.parseDate("2009-05-18");
192     referenceViolation.setCreatedAt(referenceDate);
193     assertThat(newViolation.getCreatedAt()).isNull();
194
195     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.<RuleFailureModel> newArrayList(referenceViolation));
196     assertThat(mapping.size()).isEqualTo(1);
197     assertThat(newViolation.getCreatedAt()).isEqualTo(referenceDate);
198     assertThat(newViolation.isNew()).isFalse();
199   }
200
201   private Violation newViolation(String message, Integer lineId, int ruleId) {
202     Rule rule = Rule.create().setKey("rule");
203     rule.setId(ruleId);
204     return Violation.create(rule, null).setLineId(lineId).setMessage(message);
205   }
206
207   private Violation newViolation(String message, Integer lineId, int ruleId, String lineChecksum) {
208     return newViolation(message, lineId, ruleId).setChecksum(lineChecksum);
209   }
210
211   private RuleFailureModel newReferenceViolation(String message, Integer lineId, int ruleId, String lineChecksum) {
212     RuleFailureModel referenceViolation = new RuleFailureModel();
213     referenceViolation.setId(violationId++);
214     referenceViolation.setLine(lineId);
215     referenceViolation.setMessage(message);
216     referenceViolation.setRuleId(ruleId);
217     referenceViolation.setChecksum(lineChecksum);
218     return referenceViolation;
219   }
220
221   private int violationId = 0;
222
223 }