diff options
author | Godin <mandrikov@gmail.com> | 2010-12-02 22:08:46 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-12-02 22:08:46 +0000 |
commit | f05ad5850ffd4792604f5dd9596ed35fd400e29a (patch) | |
tree | 921fa2677ef0377f5b17da093031559f48d746cf /plugins | |
parent | f884e85619d113a65dfa81c286af4c996a056367 (diff) | |
download | sonarqube-f05ad5850ffd4792604f5dd9596ed35fd400e29a.tar.gz sonarqube-f05ad5850ffd4792604f5dd9596ed35fd400e29a.zip |
SONAR-1450: Improve unit tests
Diffstat (limited to 'plugins')
4 files changed, 87 insertions, 5 deletions
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<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; @@ -134,7 +134,7 @@ public class ViolationPersisterDecorator implements Decorator { /** * 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; @@ -146,7 +146,7 @@ public class ViolationPersisterDecorator implements Decorator { /** * 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) { 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<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)); + } + +} 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<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"); 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 @@ +<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> |