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 static org.hamcrest.Matchers.equalTo;
23 import static org.hamcrest.Matchers.is;
24 import static org.hamcrest.Matchers.not;
25 import static org.hamcrest.Matchers.nullValue;
26 import static org.junit.Assert.assertThat;
28 import java.util.List;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.sonar.api.database.model.RuleFailureModel;
34 import org.sonar.api.rules.Rule;
35 import org.sonar.api.rules.Violation;
37 import com.google.common.collect.Lists;
39 public class ViolationPersisterDecoratorTest {
41 private ViolationPersisterDecorator decorator;
45 decorator = new ViolationPersisterDecorator(null, null);
49 * See http://jira.codehaus.org/browse/SONAR-2358
52 public void shouldGenerateCorrectChecksums() {
53 List<String> encoding = ViolationPersisterDecorator.getChecksums("Привет Мир");
54 assertThat(encoding.size(), is(1));
55 assertThat(encoding.get(0), is("5ba3a45e1299ede07f56e5531351be52"));
59 public void shouldSplitLinesAndIgnoreSpaces() {
60 List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
61 List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
62 List<String> cr = ViolationPersisterDecorator.getChecksums("Hello\rWorld");
63 assertThat(crlf.size(), is(2));
64 assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
65 assertThat(lf, equalTo(crlf));
66 assertThat(cr, equalTo(crlf));
68 assertThat(ViolationPersisterDecorator.getChecksum("\tvoid method() {\n"),
69 equalTo(ViolationPersisterDecorator.getChecksum(" void method() {")));
73 public void checksumShouldHaveGreaterPriorityThanLine() {
74 RuleFailureModel pastViolation1 = newPastViolation("message", 1, 50, "checksum1");
75 RuleFailureModel pastViolation2 = newPastViolation("message", 3, 50, "checksum2");
77 Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
78 Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
80 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation1, newViolation2),
81 Lists.newArrayList(pastViolation1, pastViolation2));
82 assertThat(mapping.get(newViolation1), equalTo(pastViolation1));
83 assertThat(mapping.get(newViolation2), equalTo(pastViolation2));
87 public void sameRuleAndLineMessage() {
88 Violation newViolation = newViolation("message", 1, 50, "checksum1");
89 RuleFailureModel pastViolation = newPastViolation("message", 1, 50, "checksum2");
91 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
92 assertThat(mapping.get(newViolation), equalTo(pastViolation));
96 public void sameRuleAndMessageAndChecksumButDifferentLine() {
97 Violation newViolation = newViolation("message", 1, 50, "checksum1");
98 RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum1");
100 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
101 assertThat(mapping.get(newViolation), equalTo(pastViolation));
105 public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
106 Violation newViolation = newViolation("message", 1, 50, "checksum1");
107 RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum2");
109 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
110 assertThat(mapping.get(newViolation), is(nullValue()));
114 public void shouldNotTrackViolationIfDifferentRule() {
115 Violation newViolation = newViolation("message", 1, 50, "checksum1");
116 RuleFailureModel pastViolation = newPastViolation("message", 1, 51, "checksum1");
118 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
119 assertThat(mapping.get(newViolation), is(nullValue()));
123 public void shouldCompareViolationsWithDatabaseFormat() {
124 // violation messages are trimmed and can be abbreviated when persisted in database.
125 // Comparing violation messages must use the same format.
126 Violation newViolation = newViolation("message", 1, 50, "checksum1");
127 RuleFailureModel pastViolation = newPastViolation(" message ", 1, 50, "checksum2");
129 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
130 assertThat(mapping.get(newViolation), equalTo(pastViolation));
133 private Violation newViolation(String message, int lineId, int ruleId) {
134 Rule rule = Rule.create().setKey("rule");
136 Violation violation = Violation.create(rule, null).setLineId(lineId).setMessage(message);
140 private Violation newViolation(String message, int lineId, int ruleId, String lineChecksum) {
141 Violation violation = newViolation(message, lineId, ruleId);
142 if (decorator.checksums == null) {
143 decorator.checksums = Lists.newArrayListWithExpectedSize(100);
145 for (int i = decorator.checksums.size() - 1; i < lineId; i++) {
146 decorator.checksums.add("");
148 decorator.checksums.set(lineId - 1, ViolationPersisterDecorator.getChecksum(lineChecksum));
152 private RuleFailureModel newPastViolation(String message, int lineId, int ruleId) {
153 RuleFailureModel pastViolation = new RuleFailureModel();
154 pastViolation.setId(lineId + ruleId);
155 pastViolation.setLine(lineId);
156 pastViolation.setMessage(message);
157 pastViolation.setRuleId(ruleId);
158 return pastViolation;
161 private RuleFailureModel newPastViolation(String message, int lineId, int ruleId, String lineChecksum) {
162 RuleFailureModel pastViolation = newPastViolation(message, lineId, ruleId);
163 pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
164 return pastViolation;