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