import org.sonar.api.database.model.RuleFailureModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
-import java.util.Collections;
-import java.util.List;
-
public final class ViolationPersister {
private DatabaseSession session;
this.ruleFinder = ruleFinder;
}
- public List<RuleFailureModel> getPreviousViolations(Resource resource) {
- Snapshot snapshot = resourcePersister.getSnapshot(resource);
- Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
- if (previousLastSnapshot == null) {
- return Collections.emptyList();
- }
- return session.getResults(RuleFailureModel.class, "snapshotId", previousLastSnapshot.getId());
+ public void saveViolation(Project project, Violation violation) {
+ saveOrUpdateViolation(project, violation);
}
- public void saveViolation(Project project, Violation violation) {
- saveOrUpdateViolation(project, violation, null);
+ public RuleFailureModel selectPreviousViolation(Violation violation) {
+ Snapshot snapshot = resourcePersister.getSnapshot(violation.getResource());
+ Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true);
+ return session.getSingleResult(RuleFailureModel.class,
+ "snapshotId", previousLastSnapshot.getId(),
+ "line", violation.getLineId(),
+ "message", violation.getMessage());
}
- public void saveOrUpdateViolation(Project project, Violation violation, RuleFailureModel oldModel) {
+ public void saveOrUpdateViolation(Project project, Violation violation) {
Snapshot snapshot = resourcePersister.saveResource(project, violation.getResource());
- RuleFailureModel model;
- if (oldModel != null) {
+ RuleFailureModel model = selectPreviousViolation(violation);
+ if (model != null) {
// update
- model = session.reattach(RuleFailureModel.class, oldModel.getId());
- model = mergeModel(violation, oldModel);
+ model = mergeModel(violation, model);
} else {
// insert
model = createModel(violation);
*/
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;
+import static org.mockito.Mockito.when;
+
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.database.model.RuleFailureModel;
import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
-import java.util.List;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
public class ViolationPersisterTest extends AbstractDbUnitTestCase {
private ViolationPersister violationPersister;
violationPersister = new ViolationPersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory()));
}
- @Test
- public void shouldLoadViolations() {
- List<RuleFailureModel> models = violationPersister.getPreviousViolations(javaFile);
-
- assertThat(models, notNullValue());
- assertThat(models.size(), greaterThan(0));
- }
-
@Test
public void shouldSaveViolations() {
Violation violation1a = Violation.create(rule1, javaFile)
Violation violation2 = Violation.create(rule2, javaFile)
.setPriority(RulePriority.MINOR);
- violationPersister.saveOrUpdateViolation(new Project("project"), violation1a, null);
- violationPersister.saveOrUpdateViolation(new Project("project"), violation1b, null);
- violationPersister.saveOrUpdateViolation(new Project("project"), violation2, null);
+ violationPersister.saveViolation(new Project("project"), violation1a);
+ violationPersister.saveViolation(new Project("project"), violation1b);
+ violationPersister.saveViolation(new Project("project"), violation2);
checkTables("shouldInsertViolations", "rule_failures");
}
+ @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(20).setCost(55.6);
- RuleFailureModel oldModel = violationPersister.getPreviousViolations(javaFile).iterator().next();
+ .setPriority(RulePriority.CRITICAL).setLineId(10).setCost(55.6)
+ .setMessage("old message");
- violationPersister.saveOrUpdateViolation(new Project("project"), violation, oldModel);
+ violationPersister.saveOrUpdateViolation(new Project("project"), violation);
checkTables("shouldUpdateViolation", "rule_failures");
}