2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 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.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.database.model.RuleFailureModel;
26 import org.sonar.api.rules.Rule;
27 import org.sonar.api.rules.Violation;
29 import java.util.List;
32 import static org.hamcrest.Matchers.*;
33 import static org.junit.Assert.assertThat;
35 public class ViolationPersisterDecoratorTest {
37 private ViolationPersisterDecorator decorator;
41 decorator = new ViolationPersisterDecorator(null, null);
45 * See http://jira.codehaus.org/browse/SONAR-2358
48 public void shouldGenerateCorrectChecksums() {
49 List<String> encoding = ViolationPersisterDecorator.getChecksums("Привет Мир");
50 assertThat(encoding.size(), is(1));
51 assertThat(encoding.get(0), is("5ba3a45e1299ede07f56e5531351be52"));
55 public void shouldSplitLinesAndIgnoreSpaces() {
56 List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
57 List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
58 List<String> cr = ViolationPersisterDecorator.getChecksums("Hello\rWorld");
59 assertThat(crlf.size(), is(2));
60 assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
61 assertThat(lf, equalTo(crlf));
62 assertThat(cr, equalTo(crlf));
64 assertThat(ViolationPersisterDecorator.getChecksum("\tvoid method() {\n"),
65 equalTo(ViolationPersisterDecorator.getChecksum(" void method() {")));
69 public void checksumShouldHaveGreaterPriorityThanLine() {
70 RuleFailureModel pastViolation1 = newPastViolation("message", 1, 50, "checksum1");
71 RuleFailureModel pastViolation2 = newPastViolation("message", 3, 50, "checksum2");
73 Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
74 Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
76 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation1, newViolation2),
77 Lists.newArrayList(pastViolation1, pastViolation2));
78 assertThat(mapping.get(newViolation1), equalTo(pastViolation1));
79 assertThat(mapping.get(newViolation2), equalTo(pastViolation2));
83 public void sameRuleAndLineMessage() {
84 Violation newViolation = newViolation("message", 1, 50, "checksum1");
85 RuleFailureModel pastViolation = newPastViolation("message", 1, 50, "checksum2");
87 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
88 assertThat(mapping.get(newViolation), equalTo(pastViolation));
92 public void pastMeasureHasNoChecksum() {
93 Violation newViolation = newViolation("message", 1, 50, "checksum1");
94 RuleFailureModel pastViolation = newPastViolation("message", 1, 51, null);
96 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
97 assertThat(mapping.get(newViolation), is(nullValue()));
101 public void sameRuleAndMessageAndChecksumButDifferentLine() {
102 Violation newViolation = newViolation("message", 1, 50, "checksum1");
103 RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum1");
105 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
106 assertThat(mapping.get(newViolation), equalTo(pastViolation));
110 public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
111 Violation newViolation = newViolation("message", 1, 50, "checksum1");
112 RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum2");
114 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
115 assertThat(mapping.get(newViolation), is(nullValue()));
119 public void shouldNotTrackViolationIfDifferentRule() {
120 Violation newViolation = newViolation("message", 1, 50, "checksum1");
121 RuleFailureModel pastViolation = newPastViolation("message", 1, 51, "checksum1");
123 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
124 assertThat(mapping.get(newViolation), is(nullValue()));
128 public void shouldCompareViolationsWithDatabaseFormat() {
129 // violation messages are trimmed and can be abbreviated when persisted in database.
130 // Comparing violation messages must use the same format.
131 Violation newViolation = newViolation("message", 1, 50, "checksum1");
132 RuleFailureModel pastViolation = newPastViolation(" message ", 1, 50, "checksum2");
134 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
135 assertThat(mapping.get(newViolation), equalTo(pastViolation));
138 private Violation newViolation(String message, int lineId, int ruleId) {
139 Rule rule = Rule.create().setKey("rule");
141 return Violation.create(rule, null).setLineId(lineId).setMessage(message);
144 private Violation newViolation(String message, int lineId, int ruleId, String lineChecksum) {
145 Violation violation = newViolation(message, lineId, ruleId);
146 if (decorator.checksums == null) {
147 decorator.checksums = Lists.newArrayListWithExpectedSize(100);
149 for (int i = decorator.checksums.size() - 1; i < lineId; i++) {
150 decorator.checksums.add("");
152 decorator.checksums.set(lineId - 1, ViolationPersisterDecorator.getChecksum(lineChecksum));
156 private RuleFailureModel newPastViolation(String message, int lineId, int ruleId) {
157 RuleFailureModel pastViolation = new RuleFailureModel();
158 pastViolation.setId(lineId + ruleId);
159 pastViolation.setLine(lineId);
160 pastViolation.setMessage(message);
161 pastViolation.setRuleId(ruleId);
162 return pastViolation;
165 private RuleFailureModel newPastViolation(String message, int lineId, int ruleId, String lineChecksum) {
166 RuleFailureModel pastViolation = newPastViolation(message, lineId, ruleId);
167 if (lineChecksum != null) {
168 pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
170 return pastViolation;