]> source.dussan.org Git - sonarqube.git/blob
f0c88e7b52fd2992eb1205c41e644541154ed635
[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 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;
27
28 import java.util.List;
29 import java.util.Map;
30
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;
36
37 import com.google.common.collect.Lists;
38
39 public class ViolationPersisterDecoratorTest {
40
41   private ViolationPersisterDecorator decorator;
42
43   @Before
44   public void setUp() {
45     decorator = new ViolationPersisterDecorator(null, null);
46   }
47
48   /**
49    * See http://jira.codehaus.org/browse/SONAR-2358
50    */
51   @Test
52   public void shouldGenerateCorrectChecksums() {
53     List<String> encoding = ViolationPersisterDecorator.getChecksums("Привет Мир");
54     assertThat(encoding.size(), is(1));
55     assertThat(encoding.get(0), is("5ba3a45e1299ede07f56e5531351be52"));
56   }
57
58   @Test
59   public void shouldSplitLinesAndIgnoreSpaces() {
60     List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
61     List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
62     List<String> cr = ViolationPersisterDecorator.getChecksums("Hello\rWorld");
63     assertThat(crlf.size(), is(2));
64     assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
65     assertThat(lf, equalTo(crlf));
66     assertThat(cr, equalTo(crlf));
67
68     assertThat(ViolationPersisterDecorator.getChecksum("\tvoid  method()  {\n"),
69         equalTo(ViolationPersisterDecorator.getChecksum("  void method() {")));
70   }
71
72   @Test
73   public void checksumShouldHaveGreaterPriorityThanLine() {
74     RuleFailureModel pastViolation1 = newPastViolation("message", 1, 50, "checksum1");
75     RuleFailureModel pastViolation2 = newPastViolation("message", 3, 50, "checksum2");
76
77     Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
78     Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
79
80     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation1, newViolation2),
81         Lists.newArrayList(pastViolation1, pastViolation2));
82     assertThat(mapping.get(newViolation1), equalTo(pastViolation1));
83     assertThat(mapping.get(newViolation2), equalTo(pastViolation2));
84   }
85
86   @Test
87   public void sameRuleAndLineMessage() {
88     Violation newViolation = newViolation("message", 1, 50, "checksum1");
89     RuleFailureModel pastViolation = newPastViolation("message", 1, 50, "checksum2");
90
91     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
92     assertThat(mapping.get(newViolation), equalTo(pastViolation));
93   }
94
95   @Test
96   public void sameRuleAndMessageAndChecksumButDifferentLine() {
97     Violation newViolation = newViolation("message", 1, 50, "checksum1");
98     RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum1");
99
100     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
101     assertThat(mapping.get(newViolation), equalTo(pastViolation));
102   }
103
104   @Test
105   public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
106     Violation newViolation = newViolation("message", 1, 50, "checksum1");
107     RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum2");
108
109     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
110     assertThat(mapping.get(newViolation), is(nullValue()));
111   }
112
113   @Test
114   public void shouldNotTrackViolationIfDifferentRule() {
115     Violation newViolation = newViolation("message", 1, 50, "checksum1");
116     RuleFailureModel pastViolation = newPastViolation("message", 1, 51, "checksum1");
117
118     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
119     assertThat(mapping.get(newViolation), is(nullValue()));
120   }
121
122   @Test
123   public void shouldCompareViolationsWithDatabaseFormat() {
124     // violation messages are trimmed and can be abbreviated when persisted in database.
125     // Comparing violation messages must use the same format.
126     Violation newViolation = newViolation("message", 1, 50, "checksum1");
127     RuleFailureModel pastViolation = newPastViolation("       message       ", 1, 50, "checksum2");
128
129     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
130     assertThat(mapping.get(newViolation), equalTo(pastViolation));
131   }
132
133   private Violation newViolation(String message, int lineId, int ruleId) {
134     Rule rule = Rule.create().setKey("rule");
135     rule.setId(ruleId);
136     Violation violation = Violation.create(rule, null).setLineId(lineId).setMessage(message);
137     return violation;
138   }
139
140   private Violation newViolation(String message, int lineId, int ruleId, String lineChecksum) {
141     Violation violation = newViolation(message, lineId, ruleId);
142     if (decorator.checksums == null) {
143       decorator.checksums = Lists.newArrayListWithExpectedSize(100);
144     }
145     for (int i = decorator.checksums.size() - 1; i < lineId; i++) {
146       decorator.checksums.add("");
147     }
148     decorator.checksums.set(lineId - 1, ViolationPersisterDecorator.getChecksum(lineChecksum));
149     return violation;
150   }
151
152   private RuleFailureModel newPastViolation(String message, int lineId, int ruleId) {
153     RuleFailureModel pastViolation = new RuleFailureModel();
154     pastViolation.setId(lineId + ruleId);
155     pastViolation.setLine(lineId);
156     pastViolation.setMessage(message);
157     pastViolation.setRuleId(ruleId);
158     return pastViolation;
159   }
160
161   private RuleFailureModel newPastViolation(String message, int lineId, int ruleId, String lineChecksum) {
162     RuleFailureModel pastViolation = newPastViolation(message, lineId, ruleId);
163     pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
164     return pastViolation;
165   }
166
167 }