]> source.dussan.org Git - sonarqube.git/blob
daf343330c6dbb19563012285a089d9a82b4f878
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2011 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.plugins.core.timemachine;
21
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;
29
30 import java.util.List;
31
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;
37
38 public class ViolationPersisterDecoratorTest {
39
40   private ViolationPersisterDecorator decorator;
41
42   @Before
43   public void setUp() {
44     decorator = new ViolationPersisterDecorator(null, null, null);
45   }
46
47   @Test
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));
54
55     assertThat(ViolationPersisterDecorator.getChecksum("\tvoid  method()  {\n"),
56         equalTo(ViolationPersisterDecorator.getChecksum("  void method() {")));
57   }
58
59   @Test
60   public void sameRuleLineMessage() {
61     Rule rule = Rule.create().setKey("rule");
62     rule.setId(50);
63     Violation violation = Violation.create(rule, null)
64         .setLineId(1).setMessage("message");
65
66     RuleFailureModel pastViolation = newPastViolation(rule, 1, "message");
67
68     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
69     pastViolationsByRule.put(rule, pastViolation);
70
71     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
72     assertThat(found, equalTo(pastViolation));
73   }
74
75   @Test
76   public void sameRuleAndMessageButDifferentLine() {
77     Rule rule = Rule.create().setKey("rule");
78     rule.setId(50);
79     Violation violation = Violation.create(rule, null)
80         .setLineId(1).setMessage("message");
81     decorator.checksums = ViolationPersisterDecorator.getChecksums("violation");
82
83     RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
84     pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum("violation"));
85
86     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
87     pastViolationsByRule.put(rule, pastViolation);
88
89     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
90     assertThat(found, equalTo(pastViolation));
91   }
92
93   @Test
94   public void shouldCreateNewViolation() {
95     Rule rule = Rule.create().setKey("rule");
96     Violation violation = Violation.create(rule, null)
97         .setLineId(1).setMessage("message");
98
99     RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
100
101     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
102     pastViolationsByRule.put(rule, pastViolation);
103
104     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
105     assertThat(found, nullValue());
106   }
107
108   @Test
109   public void shouldNotTrackViolationIfDifferentRule() {
110     Rule rule = Rule.create().setKey("rule");
111     rule.setId(50);
112     Violation violation = Violation.create(rule, null)
113         .setLineId(1).setMessage("message");
114
115     Rule otherRule = Rule.create().setKey("anotherRule");
116     otherRule.setId(244);
117     RuleFailureModel pastViolationOnOtherRule = newPastViolation(otherRule, 1, "message");
118
119     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
120     pastViolationsByRule.put(otherRule, pastViolationOnOtherRule);
121
122     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
123     assertThat(found, nullValue());
124   }
125
126   @Test
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");
131     rule.setId(50);
132     Violation violation = Violation.create(rule, null)
133         .setLineId(30).setMessage("   message     "); // starts and ends with whitespaces
134
135     RuleFailureModel pastViolation = newPastViolation(rule, 30, "message"); // trimmed in database
136
137     Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
138     pastViolationsByRule.put(rule, pastViolation);
139
140     RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
141     assertThat(found, equalTo(pastViolation));
142   }
143
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;
150   }
151
152 }