From f05ad5850ffd4792604f5dd9596ed35fd400e29a Mon Sep 17 00:00:00 2001 From: Godin Date: Thu, 2 Dec 2010 22:08:46 +0000 Subject: [PATCH] SONAR-1450: Improve unit tests --- .../ViolationPersisterDecorator.java | 8 ++-- .../timemachine/PastViolationsLoaderTest.java | 41 +++++++++++++++++++ .../ViolationPersisterDecoratorTest.java | 19 ++++++++- .../PastViolationsLoaderTest/shared.xml | 24 +++++++++++ 4 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest/shared.xml diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java index 8bdd4be37cb..f3ce8bd0bbe 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java @@ -39,8 +39,8 @@ public class ViolationPersisterDecorator implements Decorator { private PastViolationsLoader pastViolationsLoader; private ViolationPersister violationPersister; - private List checksums = Lists.newArrayList(); - private List pastChecksums = Lists.newArrayList(); + List checksums = Lists.newArrayList(); + List pastChecksums = Lists.newArrayList(); public ViolationPersisterDecorator(RuleFinder ruleFinder, PastViolationsLoader pastViolationsLoader, ViolationPersister violationPersister) { this.ruleFinder = ruleFinder; @@ -134,7 +134,7 @@ public class ViolationPersisterDecorator implements Decorator { /** * Search for past violation with same message and line. */ - RuleFailureModel selectPastViolationUsingLine(Violation violation, Collection pastViolations) { + private RuleFailureModel selectPastViolationUsingLine(Violation violation, Collection pastViolations) { for (RuleFailureModel pastViolation : pastViolations) { if (violation.getLineId() == pastViolation.getLine() && StringUtils.equals(violation.getMessage(), pastViolation.getMessage())) { return pastViolation; @@ -146,7 +146,7 @@ public class ViolationPersisterDecorator implements Decorator { /** * Search for past violation with same message and checksum. */ - RuleFailureModel selectPastViolationUsingChecksum(Violation violation, Collection pastViolations) { + private RuleFailureModel selectPastViolationUsingChecksum(Violation violation, Collection pastViolations) { String checksum = getChecksumForLine(checksums, violation.getLineId()); // skip violation, which not attached to line if (checksum == null) { diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java new file mode 100644 index 00000000000..ba0891da475 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java @@ -0,0 +1,41 @@ +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 violations = loader.getPastViolations(snapshot); + + assertThat(violations.size(), is(2)); + } + + @Test + public void shouldReturnEmptyList() { + List violations = loader.getPastViolations(null); + + assertThat(violations, notNullValue()); + assertThat(violations.size(), is(0)); + } + +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java index 2156c2f516d..d3c9606b377 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java @@ -55,7 +55,24 @@ public class ViolationPersisterDecoratorTest { } @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 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"); diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest/shared.xml new file mode 100644 index 00000000000..a660a0f062b --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest/shared.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + -- 2.39.5