diff options
3 files changed, 29 insertions, 1 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java index bab5ee4a374..68aaa853f08 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecorator.java @@ -172,9 +172,11 @@ public class ViolationTrackingDecorator implements Decorator { if (pastViolation != null) { newViolation.setCreatedAt(pastViolation.getCreatedAt()); newViolation.setSwitchedOff(pastViolation.isSwitchedOff()); + newViolation.setNew(false); pastViolationsByRule.remove(newViolation.getRule().getId(), pastViolation); violationMap.put(newViolation, pastViolation); } else { + newViolation.setNew(true); newViolation.setCreatedAt(project.getAnalysisDate()); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecoratorTest.java index 848e990ecad..ba798f14b24 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/ViolationTrackingDecoratorTest.java @@ -81,6 +81,7 @@ public class ViolationTrackingDecoratorTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(refernceViolation)); assertThat(mapping.get(newViolation), equalTo(refernceViolation)); + assertThat(newViolation.isNew(), is(false)); } @Test @@ -117,6 +118,7 @@ public class ViolationTrackingDecoratorTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation)); assertThat(mapping.get(newViolation), is(nullValue())); + assertThat(newViolation.isNew(), is(true)); } @Test @@ -128,6 +130,7 @@ public class ViolationTrackingDecoratorTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.newArrayList(referenceViolation)); assertThat(mapping.get(newViolation), equalTo(referenceViolation)); + assertThat(newViolation.isNew(), is(false)); } @Test @@ -138,10 +141,11 @@ public class ViolationTrackingDecoratorTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Collections.<RuleFailureModel>emptyList()); assertThat(mapping.size(), is(0)); assertThat(newViolation.getCreatedAt(), is(analysisDate)); + assertThat(newViolation.isNew(), is(true)); } @Test - public void shouldCopyViolationDate() { + public void shouldCopyDateWhenNotNew() { Violation newViolation = newViolation("message", 1, 50, "checksum"); RuleFailureModel referenceViolation = newReferenceViolation("", 1, 50, "checksum"); Date referenceDate = DateUtils.parseDate("2009-05-18"); @@ -151,6 +155,7 @@ public class ViolationTrackingDecoratorTest { Map<Violation, RuleFailureModel> mapping = decorator.mapViolations(Lists.newArrayList(newViolation), Lists.<RuleFailureModel>newArrayList(referenceViolation)); assertThat(mapping.size(), is(1)); assertThat(newViolation.getCreatedAt(), is(referenceDate)); + assertThat(newViolation.isNew(), is(false)); } private Violation newViolation(String message, int lineId, int ruleId) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java index efab4077464..f4ab32762df 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Violation.java @@ -41,6 +41,7 @@ public class Violation { private Date createdAt; private boolean switchedOff=false; private String checksum; + private boolean isNew=false; /** * Creates of a violation from a rule. Will need to define the resource later on @@ -259,6 +260,26 @@ public class Violation { return this; } + /** + * A violation is considered as "new" if it has been created after the reference analysis + * (the "previous" analysis). + * This method must be used only by post-jobs and decorators depending on the barrier + * {@link org.sonar.api.batch.DecoratorBarriers#END_OF_VIOLATION_TRACKING} + * @since 2.9 + */ + public boolean isNew() { + return isNew; + } + + /** + * For internal use only. MUST NOT BE SET FROM PLUGINS. + * @since 2.9 + */ + public Violation setNew(boolean b) { + isNew = b; + return this; + } + @Override public String toString() { return ReflectionToStringBuilder.toString(this); |