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.LinkedHashMultimap;
23 import com.google.common.collect.Multimap;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.database.model.RuleFailureModel;
27 import org.sonar.api.rules.Rule;
28 import org.sonar.api.rules.Violation;
30 import java.util.List;
32 import static org.hamcrest.Matchers.equalTo;
33 import static org.hamcrest.Matchers.is;
34 import static org.hamcrest.Matchers.not;
35 import static org.hamcrest.Matchers.nullValue;
36 import static org.junit.Assert.assertThat;
38 public class ViolationPersisterDecoratorTest {
40 private ViolationPersisterDecorator decorator;
44 decorator = new ViolationPersisterDecorator(null, null, null);
48 public void shouldGenerateCorrectChecksums() {
49 List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
50 List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
51 assertThat(crlf.size(), is(2));
52 assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
53 assertThat(lf, equalTo(crlf));
55 assertThat(ViolationPersisterDecorator.getChecksum("\tvoid method() {\n"),
56 equalTo(ViolationPersisterDecorator.getChecksum(" void method() {")));
60 public void sameRuleLineMessage() {
61 Rule rule = Rule.create().setKey("rule");
63 Violation violation = Violation.create(rule, null)
64 .setLineId(1).setMessage("message");
66 RuleFailureModel pastViolation = newPastViolation(rule, 1, "message");
68 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
69 pastViolationsByRule.put(rule, pastViolation);
71 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
72 assertThat(found, equalTo(pastViolation));
76 public void sameRuleAndMessageButDifferentLine() {
77 Rule rule = Rule.create().setKey("rule");
79 Violation violation = Violation.create(rule, null)
80 .setLineId(1).setMessage("message");
81 decorator.checksums = ViolationPersisterDecorator.getChecksums("violation");
83 RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
84 pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum("violation"));
86 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
87 pastViolationsByRule.put(rule, pastViolation);
89 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
90 assertThat(found, equalTo(pastViolation));
94 public void shouldCreateNewViolation() {
95 Rule rule = Rule.create().setKey("rule");
96 Violation violation = Violation.create(rule, null)
97 .setLineId(1).setMessage("message");
99 RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
101 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
102 pastViolationsByRule.put(rule, pastViolation);
104 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
105 assertThat(found, nullValue());
109 public void shouldNotTrackViolationIfDifferentRule() {
110 Rule rule = Rule.create().setKey("rule");
112 Violation violation = Violation.create(rule, null)
113 .setLineId(1).setMessage("message");
115 Rule otherRule = Rule.create().setKey("anotherRule");
116 otherRule.setId(244);
117 RuleFailureModel pastViolationOnOtherRule = newPastViolation(otherRule, 1, "message");
119 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
120 pastViolationsByRule.put(otherRule, pastViolationOnOtherRule);
122 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
123 assertThat(found, nullValue());
127 public void shouldCompareViolationsWithDatabaseFormat() {
128 // violation messages are trimmed and can be abbreviated when persisted in database.
129 // Comparing violation messages must use the same format.
130 Rule rule = Rule.create().setKey("rule");
132 Violation violation = Violation.create(rule, null)
133 .setLineId(30).setMessage(" message "); // starts and ends with whitespaces
135 RuleFailureModel pastViolation = newPastViolation(rule, 30, "message"); // trimmed in database
137 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
138 pastViolationsByRule.put(rule, pastViolation);
140 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
141 assertThat(found, equalTo(pastViolation));
144 private RuleFailureModel newPastViolation(Rule rule, Integer line, String message) {
145 RuleFailureModel pastViolation = new RuleFailureModel();
146 pastViolation.setLine(line);
147 pastViolation.setMessage(message);
148 pastViolation.setRuleId(rule.getId());
149 return pastViolation;