]> source.dussan.org Git - sonarqube.git/blob
2156c2f516d8a7f04c72473bf7fc4a3d2f2b215d
[sonarqube.git] /
1 package org.sonar.plugins.core.timemachine;
2
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;
8
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;
16
17 import java.util.List;
18
19 public class ViolationPersisterDecoratorTest {
20
21   private ViolationPersisterDecorator decorator;
22
23   @Before
24   public void setUp() {
25     decorator = new ViolationPersisterDecorator(null, null, null);
26   }
27
28   @Test
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));
35
36     List<String> spaces = ViolationPersisterDecorator.getChecksums("" +
37         "\tvoid  method()  {\n" +
38         "  void method() {");
39     assertThat(spaces.get(0), equalTo(spaces.get(1)));
40   }
41
42   @Test
43   public void sameRuleLineMessage() {
44     Rule rule = Rule.create().setKey("rule");
45     Violation violation = Violation.create(rule, null)
46         .setLineId(1).setMessage("message");
47
48     RuleFailureModel pastViolation = newPastViolation(rule, 1, "message");
49
50     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
51     pastViolationsByRule.put(rule, pastViolation);
52
53     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
54     assertThat(found, equalTo(pastViolation));
55   }
56
57   @Test
58   public void differentLine() {
59     Rule rule = Rule.create().setKey("rule");
60     Violation violation = Violation.create(rule, null)
61         .setLineId(1).setMessage("message");
62
63     RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
64
65     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
66     pastViolationsByRule.put(rule, pastViolation);
67
68     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
69     assertThat(found, nullValue());
70   }
71
72   @Test
73   public void differentRule() {
74     Rule rule = Rule.create().setKey("rule");
75     Violation violation = Violation.create(rule, null)
76         .setLineId(1).setMessage("message");
77
78     Rule anotherRule = Rule.create().setKey("anotherRule");
79     RuleFailureModel pastViolation = newPastViolation(anotherRule, 1, "message");
80
81     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
82     pastViolationsByRule.put(anotherRule, pastViolation);
83
84     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
85     assertThat(found, nullValue());
86   }
87
88   private RuleFailureModel newPastViolation(Rule rule, Integer line, String message) {
89     RuleFailureModel pastViolation = new RuleFailureModel();
90     pastViolation.setLine(line);
91     pastViolation.setMessage(message);
92     return pastViolation;
93   }
94
95 }