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 public void shouldGenerateCorrectChecksums() {
50 List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
51 List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
52 List<String> cr = ViolationPersisterDecorator.getChecksums("Hello\rWorld");
53 assertThat(crlf.size(), is(2));
54 assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
55 assertThat(lf, equalTo(crlf));
56 assertThat(cr, equalTo(crlf));
58 assertThat(ViolationPersisterDecorator.getChecksum("\tvoid method() {\n"),
59 equalTo(ViolationPersisterDecorator.getChecksum(" void method() {")));
63 public void checksumShouldHaveGreaterPriorityThanLine() {
64 RuleFailureModel pastViolation1 = newPastViolation("message", 1, 50, "checksum1");
65 RuleFailureModel pastViolation2 = newPastViolation("message", 3, 50, "checksum2");
67 Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
68 Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
70 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation1, newViolation2),
71 Lists.newArrayList(pastViolation1, pastViolation2));
72 assertThat(mapping.get(newViolation1), equalTo(pastViolation1));
73 assertThat(mapping.get(newViolation2), equalTo(pastViolation2));
77 public void sameRuleAndLineMessage() {
78 Violation newViolation = newViolation("message", 1, 50, "checksum1");
79 RuleFailureModel pastViolation = newPastViolation("message", 1, 50, "checksum2");
81 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
82 assertThat(mapping.get(newViolation), equalTo(pastViolation));
86 public void sameRuleAndMessageAndChecksumButDifferentLine() {
87 Violation newViolation = newViolation("message", 1, 50, "checksum1");
88 RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum1");
90 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
91 assertThat(mapping.get(newViolation), equalTo(pastViolation));
95 public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
96 Violation newViolation = newViolation("message", 1, 50, "checksum1");
97 RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum2");
99 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
100 assertThat(mapping.get(newViolation), is(nullValue()));
104 public void shouldNotTrackViolationIfDifferentRule() {
105 Violation newViolation = newViolation("message", 1, 50, "checksum1");
106 RuleFailureModel pastViolation = newPastViolation("message", 1, 51, "checksum1");
108 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
109 assertThat(mapping.get(newViolation), is(nullValue()));
113 public void shouldCompareViolationsWithDatabaseFormat() {
114 // violation messages are trimmed and can be abbreviated when persisted in database.
115 // Comparing violation messages must use the same format.
116 Violation newViolation = newViolation("message", 1, 50, "checksum1");
117 RuleFailureModel pastViolation = newPastViolation(" message ", 1, 50, "checksum2");
119 Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
120 assertThat(mapping.get(newViolation), equalTo(pastViolation));
123 private Violation newViolation(String message, int lineId, int ruleId) {
124 Rule rule = Rule.create().setKey("rule");
126 Violation violation = Violation.create(rule, null).setLineId(lineId).setMessage(message);
130 private Violation newViolation(String message, int lineId, int ruleId, String lineChecksum) {
131 Violation violation = newViolation(message, lineId, ruleId);
132 if (decorator.checksums == null) {
133 decorator.checksums = Lists.newArrayListWithExpectedSize(100);
135 for (int i = decorator.checksums.size() - 1; i < lineId; i++) {
136 decorator.checksums.add("");
138 decorator.checksums.set(lineId - 1, ViolationPersisterDecorator.getChecksum(lineChecksum));
142 private RuleFailureModel newPastViolation(String message, int lineId, int ruleId) {
143 RuleFailureModel pastViolation = new RuleFailureModel();
144 pastViolation.setId(lineId + ruleId);
145 pastViolation.setLine(lineId);
146 pastViolation.setMessage(message);
147 pastViolation.setRuleId(ruleId);
148 return pastViolation;
151 private RuleFailureModel newPastViolation(String message, int lineId, int ruleId, String lineChecksum) {
152 RuleFailureModel pastViolation = newPastViolation(message, lineId, ruleId);
153 pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
154 return pastViolation;