private PastViolationsLoader pastViolationsLoader;
private ViolationPersister violationPersister;
- private List<String> checksums = Lists.newArrayList();
- private List<String> pastChecksums = Lists.newArrayList();
+ List<String> checksums = Lists.newArrayList();
+ List<String> pastChecksums = Lists.newArrayList();
public ViolationPersisterDecorator(RuleFinder ruleFinder, PastViolationsLoader pastViolationsLoader, ViolationPersister violationPersister) {
this.ruleFinder = ruleFinder;
/**
* Search for past violation with same message and line.
*/
- RuleFailureModel selectPastViolationUsingLine(Violation violation, Collection<RuleFailureModel> pastViolations) {
+ private RuleFailureModel selectPastViolationUsingLine(Violation violation, Collection<RuleFailureModel> pastViolations) {
for (RuleFailureModel pastViolation : pastViolations) {
if (violation.getLineId() == pastViolation.getLine() && StringUtils.equals(violation.getMessage(), pastViolation.getMessage())) {
return pastViolation;
/**
* Search for past violation with same message and checksum.
*/
- RuleFailureModel selectPastViolationUsingChecksum(Violation violation, Collection<RuleFailureModel> pastViolations) {
+ private RuleFailureModel selectPastViolationUsingChecksum(Violation violation, Collection<RuleFailureModel> pastViolations) {
String checksum = getChecksumForLine(checksums, violation.getLineId());
// skip violation, which not attached to line
if (checksum == null) {
--- /dev/null
+package org.sonar.plugins.core.timemachine;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.database.model.RuleFailureModel;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import java.util.List;
+
+public class PastViolationsLoaderTest extends AbstractDbUnitTestCase {
+
+ private PastViolationsLoader loader;
+
+ @Before
+ public void setUp() {
+ setupData("shared");
+ loader = new PastViolationsLoader(getSession(), null);
+ }
+
+ @Test
+ public void shouldGetPastResourceViolations() {
+ Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
+ List<RuleFailureModel> violations = loader.getPastViolations(snapshot);
+
+ assertThat(violations.size(), is(2));
+ }
+
+ @Test
+ public void shouldReturnEmptyList() {
+ List<RuleFailureModel> violations = loader.getPastViolations(null);
+
+ assertThat(violations, notNullValue());
+ assertThat(violations.size(), is(0));
+ }
+
+}
}
@Test
- public void differentLine() {
+ public void sameRuleAndMessageButDifferentLine() {
+ Rule rule = Rule.create().setKey("rule");
+ Violation violation = Violation.create(rule, null)
+ .setLineId(1).setMessage("message");
+ decorator.checksums = ViolationPersisterDecorator.getChecksums("violation");
+
+ RuleFailureModel pastViolation = newPastViolation(rule, 2, "message");
+ decorator.pastChecksums = ViolationPersisterDecorator.getChecksums("line\nviolation");
+
+ Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
+ pastViolationsByRule.put(rule, pastViolation);
+
+ RuleFailureModel found = decorator.selectPastViolation(violation, pastViolationsByRule);
+ assertThat(found, equalTo(pastViolation));
+ }
+
+ @Test
+ public void newViolation() {
Rule rule = Rule.create().setKey("rule");
Violation violation = Violation.create(rule, null)
.setLineId(1).setMessage("message");
--- /dev/null
+<dataset>
+
+ <rules_categories id="1" name="Efficiency" description="[null]"/>
+ <rules_categories id="6" name="Usability" description="[null]"/>
+
+ <rules id="30" name="Check Header" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
+ plugin_config_key="Checker/Treewalker/HeaderCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="true"
+ cardinality="SINGLE" parent_id="[null]"/>
+
+ <rules id="31" name="Equals Avoid Null" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck"
+ plugin_config_key="Checker/TreeWalker/EqualsAvoidNull" plugin_name="checkstyle" description="[null]" priority="4" enabled="true"
+ cardinality="SINGLE" parent_id="[null]"/>
+
+ <projects id="200" scope="FIL" qualifier="CLA" kee="project:org.foo.Bar" root_id="[null]"
+ name="Bar" long_name="org.foo.Bar" description="[null]"
+ enabled="true" language="java" copy_resource_id="[null]" profile_id="[null]"/>
+
+ <snapshots var_mode_1="[null]" var_label_1="[null]" var_mode_2="[null]" var_label_2="[null]" var_mode_3="[null]" var_label_3="[null]" id="1000" project_id="200" parent_snapshot_id="[null]" root_project_id="100" root_snapshot_id="[null]"
+ scope="FIL" qualifier="CLA" created_at="2008-11-01 13:58:00.00" version="[null]" path=""
+ status="U" islast="false" depth="3" />
+
+ <RULE_FAILURES ID="1" SNAPSHOT_ID="1000" RULE_ID="30" FAILURE_LEVEL="3" MESSAGE="old message" LINE="10" COST="[null]" created_at="2008-11-01 13:58:00.00" />
+ <RULE_FAILURES ID="2" SNAPSHOT_ID="1000" RULE_ID="30" FAILURE_LEVEL="3" MESSAGE="old message" LINE="10" COST="[null]" created_at="2008-11-01 13:58:00.00" />
+</dataset>