aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorGodin <mandrikov@gmail.com>2010-12-02 13:52:57 +0000
committerGodin <mandrikov@gmail.com>2010-12-02 13:52:57 +0000
commit1a51c6bf53bf1f526c3c39aa86569b042f8be810 (patch)
tree67cc94b281a089e8b7ea4828f4fbd72cfbdc428d /sonar-batch
parentf384c82819754a165cefc588cd8338faa078e003 (diff)
downloadsonarqube-1a51c6bf53bf1f526c3c39aa86569b042f8be810.tar.gz
sonarqube-1a51c6bf53bf1f526c3c39aa86569b042f8be810.zip
SONAR-1450: Extract ViolationPersisterDecorator from ViolationPersister
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java1
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java27
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java29
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.xml2
4 files changed, 9 insertions, 50 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
index 59c28501c5b..a0191b39669 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
@@ -400,7 +400,6 @@ public final class DefaultIndex extends SonarIndex {
private void doAddViolation(Violation violation, Bucket bucket) {
bucket.addViolation(violation);
- persistence.saveViolation(currentProject, violation);
}
//
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
index 3050f55234e..37c6fdc8b57 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
@@ -27,8 +27,6 @@ import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
-import java.util.List;
-
public final class ViolationPersister {
private DatabaseSession session;
@@ -41,30 +39,15 @@ public final class ViolationPersister {
this.ruleFinder = ruleFinder;
}
+ /**
+ * @deprecated Use {@link #saveOrUpdateViolation(Project, Violation, RuleFailureModel)} instead.
+ */
public void saveViolation(Project project, Violation violation) {
- saveOrUpdateViolation(project, violation);
- }
-
- public RuleFailureModel selectPreviousViolation(Violation violation) {
- Snapshot snapshot = resourcePersister.getSnapshot(violation.getResource());
- Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
- if (previousLastSnapshot == null) {
- return null;
- }
- // Can be several violations on line with same message: for example - "'3' is a magic number"
- List<RuleFailureModel> models = session.getResults(RuleFailureModel.class,
- "snapshotId", previousLastSnapshot.getId(),
- "line", violation.getLineId(),
- "message", violation.getMessage());
- if (models != null && !models.isEmpty()) {
- return models.get(0);
- }
- return null;
+ saveOrUpdateViolation(project, violation, null);
}
- public void saveOrUpdateViolation(Project project, Violation violation) {
+ public void saveOrUpdateViolation(Project project, Violation violation, RuleFailureModel model) {
Snapshot snapshot = resourcePersister.saveResource(project, violation.getResource());
- RuleFailureModel model = selectPreviousViolation(violation);
if (model != null) {
// update
model = mergeModel(violation, model);
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
index 79eb4dd1e35..5d3fbc795cb 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
@@ -21,7 +21,6 @@ package org.sonar.batch.index;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
-import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@@ -76,34 +75,12 @@ public class ViolationPersisterTest extends AbstractDbUnitTestCase {
}
@Test
- public void shouldSelectPreviousViolation() {
- Violation violation = Violation.create(rule1, javaFile)
- .setPriority(RulePriority.CRITICAL).setLineId(10)
- .setMessage("old message");
-
- RuleFailureModel model = violationPersister.selectPreviousViolation(violation);
-
- assertThat(model, notNullValue());
- }
-
- @Test
- public void noPreviousViolation() {
- Violation violation = Violation.create(rule1, javaFile)
- .setPriority(RulePriority.CRITICAL).setLineId(10)
- .setMessage("new message");
-
- RuleFailureModel model = violationPersister.selectPreviousViolation(violation);
-
- assertThat(model, nullValue());
- }
-
- @Test
public void shouldUpdateViolation() {
Violation violation = Violation.create(rule1, javaFile)
- .setPriority(RulePriority.CRITICAL).setLineId(10).setCost(55.6)
- .setMessage("old message");
+ .setLineId(20).setCost(55.6);
+ RuleFailureModel model = getSession().getSingleResult(RuleFailureModel.class, "id", 1);
- violationPersister.saveOrUpdateViolation(new Project("project"), violation);
+ violationPersister.saveOrUpdateViolation(new Project("project"), violation, model);
assertThat(violation.getCreatedAt(), notNullValue());
checkTables("shouldUpdateViolation", "rule_failures");
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.xml
index 1830e9a2c56..8fff57c6eea 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.xml
@@ -18,6 +18,6 @@
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="55.6" created_at="2008-11-01 13:58:00.00" />
+ <RULE_FAILURES ID="1" SNAPSHOT_ID="1000" RULE_ID="30" FAILURE_LEVEL="3" MESSAGE="old message" LINE="20" COST="55.6" 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>