From f742c257aed16bcc1794487462331ffafcb15c4a Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 11 Jul 2012 16:57:27 +0200 Subject: [PATCH] SONAR-3543 Display sources folder in error message --- .../plugins/core/sensors/FilesDecorator.java | 15 +++++++++- .../core/sensors/FilesDecoratorTest.java | 29 ++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FilesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FilesDecorator.java index 7f3b1097cdd..d03878878a7 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FilesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/FilesDecorator.java @@ -31,6 +31,7 @@ import org.sonar.api.resources.Resource; import org.sonar.api.resources.ResourceUtils; import org.sonar.api.utils.SonarException; +import java.io.File; import java.util.Collection; /** @@ -70,7 +71,19 @@ public final class FilesDecorator implements Decorator { @SuppressWarnings("rawtypes") private void checkRootProjectHasFiles(Resource resource, Double sum) { if (ResourceUtils.isRootProject(resource) && (sum == null || sum.doubleValue() == 0)) { - throw new SonarException("Project \"" + resource.getName() + "\" does not contain any file. Please check your project configuration."); + String sourceFoldersList = printSourceFoldersList((Project) resource); + throw new SonarException("Project \"" + resource.getName() + "\" does not contain any file in its source folders:\n" + + sourceFoldersList + "\nPlease check your project configuration."); } } + + private String printSourceFoldersList(Project project) { + StringBuilder result = new StringBuilder(); + for (File sourceDir : project.getFileSystem().getSourceDirs()) { + result.append(" - "); + result.append(sourceDir.getAbsolutePath()); + result.append("\n"); + } + return result.toString(); + } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FilesDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FilesDecoratorTest.java index c6bfd2bde3f..ba14e46d292 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FilesDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/FilesDecoratorTest.java @@ -19,6 +19,7 @@ */ package org.sonar.plugins.core.sensors; +import com.google.common.collect.Lists; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -29,10 +30,12 @@ import org.sonar.api.batch.DecoratorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.resources.Project; +import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; import org.sonar.api.utils.SonarException; +import java.io.File; import java.util.Arrays; import static org.fest.assertions.Assertions.assertThat; @@ -112,25 +115,35 @@ public class FilesDecoratorTest { @Test public void shouldFailOnRootProjectIfNoFile() { - when(resource.getQualifier()).thenReturn(Qualifiers.PROJECT); - when(resource.getName()).thenReturn("Foo"); + Project project = createMockProject(); when(context.getChildrenMeasures(CoreMetrics.FILES)).thenReturn(Arrays.asList(new Measure(CoreMetrics.FILES, 0.0))); thrown.expect(SonarException.class); - thrown.expectMessage("Project \"Foo\" does not contain any file. Please check your project configuration."); + thrown.expectMessage("Project \"Foo\" does not contain any file in its source folders:\n"); + thrown.expectMessage("- " + new File("target/temp").getAbsolutePath() + "\n"); + thrown.expectMessage("\nPlease check your project configuration."); - decorator.decorate(resource, context); + decorator.decorate(project, context); } @Test public void shouldFailOnRootProjectIfNoFileMeasure() { - when(resource.getQualifier()).thenReturn(Qualifiers.PROJECT); - when(resource.getName()).thenReturn("Foo"); + Project project = createMockProject(); thrown.expect(SonarException.class); - thrown.expectMessage("Project \"Foo\" does not contain any file. Please check your project configuration."); + thrown.expectMessage("Project \"Foo\" does not contain any file in its source folders"); - decorator.decorate(resource, context); + decorator.decorate(project, context); + } + + private Project createMockProject() { + Project project = mock(Project.class); + when(project.getQualifier()).thenReturn(Qualifiers.PROJECT); + when(project.getName()).thenReturn("Foo"); + ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); + when(fileSystem.getSourceDirs()).thenReturn(Lists.newArrayList(new File("target/temp"))); + when(project.getFileSystem()).thenReturn(fileSystem); + return project; } @Test -- 2.39.5