1 package org.sonar.plugins.core.timemachine;
3 import static org.hamcrest.Matchers.equalTo;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.Matchers.not;
6 import static org.hamcrest.Matchers.nullValue;
7 import static org.junit.Assert.assertThat;
9 import com.google.common.collect.LinkedHashMultimap;
10 import com.google.common.collect.Multimap;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.sonar.api.database.model.RuleFailureModel;
14 import org.sonar.api.rules.Rule;
15 import org.sonar.api.rules.Violation;
17 import java.util.List;
19 public class ViolationPersisterDecoratorTest {
21 private ViolationPersisterDecorator decorator;
25 decorator = new ViolationPersisterDecorator(null, null, null);
29 public void testGetChecksums() {
30 List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
31 List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
32 assertThat(crlf.size(), is(2));
33 assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
34 assertThat(lf, equalTo(crlf));
36 List<String> spaces = ViolationPersisterDecorator.getChecksums("" +
37 "\tvoid method() {\n" +
39 assertThat(spaces.get(0), equalTo(spaces.get(1)));
43 public void sameRuleLineMessage() {
44 Rule rule = Rule.create().setKey("rule");
45 Violation violation = Violation.create(rule, null)
46 .setLineId(1).setMessage("message");
48 RuleFailureModel pastViolation = newPastViolation(rule, 1, "message");
50 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
51 pastViolationsByRule.put(rule, pastViolation);
53 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
54 assertThat(found, equalTo(pastViolation));
58 public void differentLine() {
59 Rule rule = Rule.create().setKey("rule");
60 Violation violation = Violation.create(rule, null)
61 .setLineId(1).setMessage("message");
63 RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
65 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
66 pastViolationsByRule.put(rule, pastViolation);
68 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
69 assertThat(found, nullValue());
73 public void differentRule() {
74 Rule rule = Rule.create().setKey("rule");
75 Violation violation = Violation.create(rule, null)
76 .setLineId(1).setMessage("message");
78 Rule anotherRule = Rule.create().setKey("anotherRule");
79 RuleFailureModel pastViolation = newPastViolation(anotherRule, 1, "message");
81 Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
82 pastViolationsByRule.put(anotherRule, pastViolation);
84 RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
85 assertThat(found, nullValue());
88 private RuleFailureModel newPastViolation(Rule rule, Integer line, String message) {
89 RuleFailureModel pastViolation = new RuleFailureModel();
90 pastViolation.setLine(line);
91 pastViolation.setMessage(message);