]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1450: Improve unit tests
authorGodin <mandrikov@gmail.com>
Thu, 2 Dec 2010 22:08:46 +0000 (22:08 +0000)
committerGodin <mandrikov@gmail.com>
Thu, 2 Dec 2010 22:08:46 +0000 (22:08 +0000)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest.java [new file with mode: 0644]
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationPersisterDecoratorTest.java
plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastViolationsLoaderTest/shared.xml [new file with mode: 0644]

index 8bdd4be37cbfe6c98e8ebd0a6f1c2979f4493e31..f3ce8bd0bbef0886b51490908658660341acc710 100644 (file)
@@ -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 (file)
index 0000000..ba0891d
--- /dev/null
@@ -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));
+  }
+
+}
index 2156c2f516d8a7f04c72473bf7fc4a3d2f2b215d..d3c9606b37755790321249575e108e9099c22bfc 100644 (file)
@@ -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 (file)
index 0000000..a660a0f
--- /dev/null
@@ -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>