]> source.dussan.org Git - sonarqube.git/commitdiff
Add unit tests for NewViolationsDecorator
authorGodin <mandrikov@gmail.com>
Mon, 6 Dec 2010 15:33:53 +0000 (15:33 +0000)
committerGodin <mandrikov@gmail.com>
Mon, 6 Dec 2010 15:33:53 +0000 (15:33 +0000)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java

index e3294c0de25d79c510007f78742cc1afdd473245..14edff0e40fe8339c3eb650ef171e9c906169536 100644 (file)
@@ -61,11 +61,11 @@ public class NewViolationsDecorator implements Decorator {
   }
 
   public void decorate(Resource resource, DecoratorContext context) {
-    clearCache();
     prepareCurrentResourceViolations(context);
     saveNewViolations(context);
     saveNewViolationsByPriority(context);
     saveNewViolationsByRule(context);
+    clearCache();
   }
 
   private void clearCache() {
index 520b5551a55425e00cae96a0e70ed61c5351bdc7..ce1400de6abf75168cd68ddf5f6d98dae8a21e96 100644 (file)
@@ -44,9 +44,7 @@ import java.util.List;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 import static org.mockito.Matchers.argThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 public class NewViolationsDecoratorTest {
   private Rule rule1;
@@ -125,6 +123,19 @@ public class NewViolationsDecoratorTest {
     assertThat(decorator.sumChildren(3, children), is(6));
   }
 
+  @Test
+  public void shouldClearCacheAfterExecution() {
+    Violation violation1 = Violation.create(rule1, resource).setPriority(RulePriority.CRITICAL).setCreatedAt(rightNow);
+    Violation violation2 = Violation.create(rule2, resource).setPriority(RulePriority.CRITICAL).setCreatedAt(rightNow);
+    when(context.getViolations()).thenReturn(Arrays.asList(violation1)).thenReturn(Arrays.asList(violation2));
+
+    decorator.decorate(resource, context);
+    decorator.decorate(resource, context);
+
+    verify(context, times(2)).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0)));
+    verify(context, never()).saveMeasure(argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 2.0, 2.0)));
+  }
+
   @Test
   public void priorityViolations() {
     when(context.getViolations()).thenReturn(createViolations());