]> source.dussan.org Git - sonarqube.git/blob
44538c856335073f25d792c008c03e892ad66dfb
[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   @Test
49   public void shouldGenerateCorrectChecksums() {
50     List<String> crlf = ViolationPersisterDecorator.getChecksums("Hello\r\nWorld");
51     List<String> lf = ViolationPersisterDecorator.getChecksums("Hello\nWorld");
52     List<String> cr = ViolationPersisterDecorator.getChecksums("Hello\rWorld");
53     assertThat(crlf.size(), is(2));
54     assertThat(crlf.get(0), not(equalTo(crlf.get(1))));
55     assertThat(lf, equalTo(crlf));
56     assertThat(cr, equalTo(crlf));
57
58     assertThat(ViolationPersisterDecorator.getChecksum("\tvoid  method()  {\n"),
59         equalTo(ViolationPersisterDecorator.getChecksum("  void method() {")));
60   }
61
62   @Test
63   public void checksumShouldHaveGreaterPriorityThanLine() {
64     RuleFailureModel pastViolation1 = newPastViolation("message", 1, 50, "checksum1");
65     RuleFailureModel pastViolation2 = newPastViolation("message", 3, 50, "checksum2");
66
67     Violation newViolation1 = newViolation("message", 3, 50, "checksum1");
68     Violation newViolation2 = newViolation("message", 5, 50, "checksum2");
69
70     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation1, newViolation2),
71         Lists.newArrayList(pastViolation1, pastViolation2));
72     assertThat(mapping.get(newViolation1), equalTo(pastViolation1));
73     assertThat(mapping.get(newViolation2), equalTo(pastViolation2));
74   }
75
76   @Test
77   public void sameRuleAndLineMessage() {
78     Violation newViolation = newViolation("message", 1, 50, "checksum1");
79     RuleFailureModel pastViolation = newPastViolation("message", 1, 50, "checksum2");
80
81     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
82     assertThat(mapping.get(newViolation), equalTo(pastViolation));
83   }
84
85   @Test
86   public void sameRuleAndMessageAndChecksumButDifferentLine() {
87     Violation newViolation = newViolation("message", 1, 50, "checksum1");
88     RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum1");
89
90     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
91     assertThat(mapping.get(newViolation), equalTo(pastViolation));
92   }
93
94   @Test
95   public void shouldCreateNewViolationWhenSameRuleSameMessageButDifferentLineAndChecksum() {
96     Violation newViolation = newViolation("message", 1, 50, "checksum1");
97     RuleFailureModel pastViolation = newPastViolation("message", 2, 50, "checksum2");
98
99     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
100     assertThat(mapping.get(newViolation), is(nullValue()));
101   }
102
103   @Test
104   public void shouldNotTrackViolationIfDifferentRule() {
105     Violation newViolation = newViolation("message", 1, 50, "checksum1");
106     RuleFailureModel pastViolation = newPastViolation("message", 1, 51, "checksum1");
107
108     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
109     assertThat(mapping.get(newViolation), is(nullValue()));
110   }
111
112   @Test
113   public void shouldCompareViolationsWithDatabaseFormat() {
114     // violation messages are trimmed and can be abbreviated when persisted in database.
115     // Comparing violation messages must use the same format.
116     Violation newViolation = newViolation("message", 1, 50, "checksum1");
117     RuleFailureModel pastViolation = newPastViolation("       message       ", 1, 50, "checksum2");
118
119     Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(pastViolation));
120     assertThat(mapping.get(newViolation), equalTo(pastViolation));
121   }
122
123   private Violation newViolation(String message, int lineId, int ruleId) {
124     Rule rule = Rule.create().setKey("rule");
125     rule.setId(ruleId);
126     Violation violation = Violation.create(rule, null).setLineId(lineId).setMessage(message);
127     return violation;
128   }
129
130   private Violation newViolation(String message, int lineId, int ruleId, String lineChecksum) {
131     Violation violation = newViolation(message, lineId, ruleId);
132     if (decorator.checksums == null) {
133       decorator.checksums = Lists.newArrayListWithExpectedSize(100);
134     }
135     for (int i = decorator.checksums.size() - 1; i < lineId; i++) {
136       decorator.checksums.add("");
137     }
138     decorator.checksums.set(lineId - 1, ViolationPersisterDecorator.getChecksum(lineChecksum));
139     return violation;
140   }
141
142   private RuleFailureModel newPastViolation(String message, int lineId, int ruleId) {
143     RuleFailureModel pastViolation = new RuleFailureModel();
144     pastViolation.setId(lineId + ruleId);
145     pastViolation.setLine(lineId);
146     pastViolation.setMessage(message);
147     pastViolation.setRuleId(ruleId);
148     return pastViolation;
149   }
150
151   private RuleFailureModel newPastViolation(String message, int lineId, int ruleId, String lineChecksum) {
152     RuleFailureModel pastViolation = newPastViolation(message, lineId, ruleId);
153     pastViolation.setChecksum(ViolationPersisterDecorator.getChecksum(lineChecksum));
154     return pastViolation;
155   }
156
157 }