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;
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);
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);
+ }
+ }
+
}
*/
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 {
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) {
}