From f543653d4fc293d7d3f0a025665ac0a5c275c6b4 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 14 Mar 2011 16:41:49 +0100 Subject: [PATCH] SONAR-2251 Unstable state of violations when stopping a build --- .../ViolationPersisterDecorator.java | 7 ++-- .../sonar/batch/index/ViolationPersister.java | 34 +++++++------------ .../batch/index/ViolationPersisterTest.java | 11 ------ .../shouldUpdateViolation-result.xml | 23 ------------- .../api/database/model/RuleFailureModel.java | 8 +++++ 5 files changed, 26 insertions(+), 57 deletions(-) delete mode 100644 sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.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 e1299a01365..57460299a54 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 @@ -93,11 +93,14 @@ public class ViolationPersisterDecorator implements Decorator { for (Violation violation : context.getViolations()) { RuleFailureModel pastViolation = selectPastViolation(violation, pastViolationsByRule); if (pastViolation != null) { - // remove violation from past, since would be updated and shouldn't affect other violations anymore + // remove violation, since would be updated and shouldn't affect other violations anymore pastViolationsByRule.remove(violation.getRule(), pastViolation); + violation.setCreatedAt(pastViolation.getCreatedAt()); + } else { + violation.setCreatedAt(null);//avoid plugins to set date } String checksum = getChecksumForLine(checksums, violation.getLineId()); - violationPersister.saveOrUpdateViolation(context.getProject(), violation, pastViolation, checksum); + violationPersister.saveViolation(context.getProject(), violation, checksum); } } 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 c840adbfa78..d6cbcf1ac27 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 @@ -40,38 +40,30 @@ public final class ViolationPersister { } void saveViolation(Project project, Violation violation) { - saveOrUpdateViolation(project, violation, null, null); + saveViolation(project, violation, null); } - public void saveOrUpdateViolation(Project project, Violation violation, RuleFailureModel model, String checksum) { + public void saveViolation(Project project, Violation violation, String checksum) { Snapshot snapshot = resourcePersister.saveResource(project, violation.getResource()); - if (model != null) { - // update - model = session.reattach(RuleFailureModel.class, model.getId()); - model = mergeModel(violation, model); - } else { - // insert - model = createModel(violation); - model.setCreatedAt(snapshot.getCreatedAt()); + if (violation.getCreatedAt()==null) { + violation.setCreatedAt(snapshot.getCreatedAt()); } + RuleFailureModel model = createModel(violation); model.setSnapshotId(snapshot.getId()); model.setChecksum(checksum); session.save(model); violation.setMessage(model.getMessage());// the message can be changed in the class RuleFailure (truncate + trim) - violation.setCreatedAt(model.getCreatedAt()); } private RuleFailureModel createModel(Violation violation) { - return mergeModel(violation, new RuleFailureModel()); - } - - private RuleFailureModel mergeModel(Violation violation, RuleFailureModel merge) { + RuleFailureModel model = new RuleFailureModel(); Rule rule = ruleFinder.findByKey(violation.getRule().getRepositoryKey(), violation.getRule().getKey()); - merge.setRuleId(rule.getId()); - merge.setPriority(violation.getSeverity()); - merge.setLine(violation.getLineId()); - merge.setMessage(violation.getMessage()); - merge.setCost(violation.getCost()); - return merge; + model.setRuleId(rule.getId()); + model.setPriority(violation.getSeverity()); + model.setCreatedAt(violation.getCreatedAt()); + model.setLine(violation.getLineId()); + model.setMessage(violation.getMessage()); + model.setCost(violation.getCost()); + return 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 7d90a707941..9b27d33b251 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 @@ -73,15 +73,4 @@ public class ViolationPersisterTest extends AbstractDbUnitTestCase { checkTables("shouldInsertViolations", "rule_failures"); } - @Test - public void shouldUpdateViolation() { - Violation violation = Violation.create(rule1, javaFile) - .setLineId(20).setCost(55.6).setSeverity(RulePriority.MINOR); - RuleFailureModel model = getSession().getSingleResult(RuleFailureModel.class, "id", 1); - - violationPersister.saveOrUpdateViolation(new Project("project"), violation, model, null); - - 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 deleted file mode 100644 index b54b33d0fe2..00000000000 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ViolationPersisterTest/shouldUpdateViolation-result.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/RuleFailureModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/RuleFailureModel.java index ae0f9531c89..fdcd25583e0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/RuleFailureModel.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/RuleFailureModel.java @@ -74,10 +74,18 @@ public class RuleFailureModel extends BaseIdentifiable { return StringUtils.abbreviate(StringUtils.trim(message), MESSAGE_COLUMN_SIZE); } + /** + * @deprecated since 2.7. Replace by getPriority() + */ + @Deprecated public RulePriority getLevel() { return priority; } + /** + * @deprecated since 2.7. Replace by setPriority() + */ + @Deprecated public void setLevel(RulePriority priority) { this.priority = priority; } -- 2.39.5