2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.plugins.core.timemachine;
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;
31 import java.util.Collections;
32 import java.util.Date;
35 import static org.fest.assertions.Assertions.assertThat;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class ViolationTrackingDecoratorTest {
41 private ViolationTrackingDecorator decorator;
42 private final Date analysisDate = DateUtils.parseDate("2010-12-25");
46 Project project = mock(Project.class);
47 when(project.getAnalysisDate()).thenReturn(analysisDate);
48 decorator = new ViolationTrackingDecorator(project, null, null);
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);
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();
64 public void checksumShouldHaveGreaterPriorityThanLine() {
65 RuleFailureModel referenceViolation1 = newReferenceViolation("message", 1, 50, "checksum1");
66 RuleFailureModel referenceViolation2 = newReferenceViolation("message", 3, 50, "checksum2");
68 Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
69 Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
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();
82 public void sameRuleAndNullLineAndChecksumButDifferentMessages() {
83 Violation newViolation = newViolation("new message", null, 50, "checksum1");
84 RuleFailureModel referenceViolation = newReferenceViolation("old message", null, 50, "checksum1");
86 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
87 assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
88 assertThat(newViolation.isNew()).isFalse();
92 public void sameRuleAndLineAndChecksumButDifferentMessages() {
93 Violation newViolation = newViolation("new message", 1, 50, "checksum1");
94 RuleFailureModel referenceViolation = newReferenceViolation("old message", 1, 50, "checksum1");
96 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
97 assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
98 assertThat(newViolation.isNew()).isFalse();
102 public void sameRuleAndLineMessage() {
103 Violation newViolation = newViolation("message", 1, 50, "checksum1");
104 RuleFailureModel referenceViolation = newReferenceViolation("message", 1, 50, "checksum2");
106 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
107 assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
108 assertThat(newViolation.isNew()).isFalse();
112 public void shouldIgnoreReferenceMeasureWithoutChecksum() {
113 Violation newViolation = newViolation("message", 1, 50, null);
114 RuleFailureModel referenceViolation = newReferenceViolation("message", 1, 51, null);
116 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
117 assertThat(decorator.getReferenceViolation(newViolation)).isNull();
118 assertThat(newViolation.isNew()).isTrue();
122 public void sameRuleAndMessageAndChecksumButDifferentLine() {
123 Violation newViolation = newViolation("message", 1, 50, "checksum1");
124 RuleFailureModel referenceViolation = newReferenceViolation("message", 2, 50, "checksum1");
126 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
127 assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
128 assertThat(newViolation.isNew()).isFalse();
135 public void sameChecksumAndRuleButDifferentLineAndDifferentMessage() {
136 Violation newViolation = newViolation("new message", 1, 50, "checksum1");
137 RuleFailureModel referenceViolation = newReferenceViolation("old message", 2, 50, "checksum1");
139 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
140 assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
141 assertThat(newViolation.isNew()).isFalse();
145 public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
146 Violation newViolation = newViolation("message", 1, 50, "checksum1");
147 RuleFailureModel referenceViolation = newReferenceViolation("message", 2, 50, "checksum2");
149 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
150 assertThat(decorator.getReferenceViolation(newViolation)).isNull();
151 assertThat(newViolation.isNew()).isTrue();
155 public void shouldNotTrackViolationIfDifferentRule() {
156 Violation newViolation = newViolation("message", 1, 50, "checksum1");
157 RuleFailureModel referenceViolation = newReferenceViolation("message", 1, 51, "checksum1");
159 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
160 assertThat(decorator.getReferenceViolation(newViolation)).isNull();
161 assertThat(newViolation.isNew()).isTrue();
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");
171 decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation));
172 assertThat(decorator.getReferenceViolation(newViolation)).isSameAs(referenceViolation);
173 assertThat(newViolation.isNew()).isFalse();
177 public void shouldSetDateOfNewViolations() {
178 Violation newViolation = newViolation("message", 1, 50, "checksum");
179 assertThat(newViolation.getCreatedAt()).isNull();
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();
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();
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();
201 private Violation newViolation(String message, Integer lineId, int ruleId) {
202 Rule rule = Rule.create().setKey("rule");
204 return Violation.create(rule, null).setLineId(lineId).setMessage(message);
207 private Violation newViolation(String message, Integer lineId, int ruleId, String lineChecksum) {
208 return newViolation(message, lineId, ruleId).setChecksum(lineChecksum);
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;
221 private int violationId = 0;