]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2278 Improve exception handling of decorators
authorsimonbrandhof <simon.brandhof@gmail.com>
Tue, 19 Apr 2011 09:55:28 +0000 (11:55 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Tue, 19 Apr 2011 09:55:28 +0000 (11:55 +0200)
sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java
sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java

index 4d4607accd695f22247c2f96f70537694a6255e6..1605a1623d410c160947bc472126bcfc3df72de4 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.batch.DecoratorContext;
 import org.sonar.api.batch.SonarIndex;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.SonarException;
 import org.sonar.batch.DecoratorsSelector;
 import org.sonar.batch.DefaultDecoratorContext;
 import org.sonar.batch.events.EventBus;
@@ -53,7 +54,7 @@ public class DecoratorsExecutor implements BatchComponent {
     eventBus.fireEvent(new DecoratorsPhaseEvent(Lists.newArrayList(decorators), false));
   }
 
-  private DecoratorContext decorateResource(Resource resource, Collection<Decorator> decorators, boolean executeDecorators) {
+  DecoratorContext decorateResource(Resource resource, Collection<Decorator> decorators, boolean executeDecorators) {
     List<DecoratorContext> childrenContexts = Lists.newArrayList();
     for (Resource child : index.getChildren(resource)) {
       boolean isModule = (child instanceof Project);
@@ -64,12 +65,22 @@ public class DecoratorsExecutor implements BatchComponent {
     DefaultDecoratorContext context = new DefaultDecoratorContext(resource, index, childrenContexts);
     if (executeDecorators) {
       for (Decorator decorator : decorators) {
-        eventBus.fireEvent(new DecoratorExecutionEvent(decorator, true));
-        decorator.decorate(resource, context);
-        eventBus.fireEvent(new DecoratorExecutionEvent(decorator, false));
+        executeDecorator(decorator, context, resource);
       }
     }
     return context;
   }
 
+  void executeDecorator(Decorator decorator, DefaultDecoratorContext context, Resource resource) {
+    try {
+      eventBus.fireEvent(new DecoratorExecutionEvent(decorator, true));
+      decorator.decorate(resource, context);
+      eventBus.fireEvent(new DecoratorExecutionEvent(decorator, false));
+      
+    } catch (Exception e) {
+      // SONAR-2278 the resource should not be lost in exception stacktrace.
+      throw new SonarException("Fail to decorate '" + resource + "'", e);
+    }
+  }
+
 }
index 396ce2b4e36d5f655871f29a9c9c23d4a099e2d1..fc86a6d4c275c27f026a8847a6b6fdc717bdcdb1 100644 (file)
  */
 package org.sonar.batch.phases;
 
-import static org.hamcrest.number.OrderingComparisons.greaterThanOrEqualTo;
-import static org.hamcrest.number.OrderingComparisons.lessThan;
-import static org.junit.Assert.assertThat;
-
 import org.junit.Test;
+import org.sonar.api.batch.BatchExtensionDictionnary;
 import org.sonar.api.batch.Decorator;
 import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.batch.SonarIndex;
+import org.sonar.api.resources.File;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.SonarException;
+import org.sonar.batch.DefaultDecoratorContext;
+import org.sonar.batch.events.EventBus;
+
+import static org.hamcrest.number.OrderingComparisons.greaterThanOrEqualTo;
+import static org.hamcrest.number.OrderingComparisons.lessThan;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import static org.junit.matchers.JUnitMatchers.containsString;
+import static org.mockito.Matchers.any;
+
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
 
 public class DecoratorsExecutorTest {
 
@@ -49,6 +61,21 @@ public class DecoratorsExecutorTest {
     assertThat(profiler.getMessage().indexOf("Decorator1"), lessThan(profiler.getMessage().indexOf("Decorator2")));
   }
 
+  @Test
+  public void exceptionShouldIncludeResource() {
+    Decorator decorator = mock(Decorator.class);
+    doThrow(new SonarException()).when(decorator).decorate(any(Resource.class), any(DecoratorContext.class));
+
+    DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), mock(SonarIndex.class), mock(EventBus.class));
+    try {
+      executor.executeDecorator(decorator, mock(DefaultDecoratorContext.class), new File("org/foo/Bar.java"));
+      fail("Exception has not been thrown");
+
+    } catch (SonarException e) {
+      assertThat(e.getMessage(), containsString("org/foo/Bar.java"));
+    }
+  }
+
   static class Decorator1 implements Decorator {
     public void decorate(Resource resource, DecoratorContext context) {
     }