import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
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;
/**
return CoreMetrics.FILES;
}
- @SuppressWarnings("rawtypes")
public void decorate(Resource resource, DecoratorContext context) {
if (MeasureUtils.hasValue(context.getMeasure(CoreMetrics.FILES))) {
- checkRootProjectHasFiles(resource, context.getMeasure(CoreMetrics.FILES).getValue());
return;
}
} else {
Collection<Measure> childrenMeasures = context.getChildrenMeasures(CoreMetrics.FILES);
Double sum = MeasureUtils.sum(false, childrenMeasures);
- checkRootProjectHasFiles(resource, sum);
if (sum != null) {
context.saveMeasure(CoreMetrics.FILES, sum);
}
}
}
-
- @SuppressWarnings("rawtypes")
- private void checkRootProjectHasFiles(Resource resource, Double sum) {
- if (ResourceUtils.isRootProject(resource) && (sum == null || sum.doubleValue() == 0)) {
- 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();
- }
}
*/
package org.sonar.plugins.core.sensors;
-import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
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;
verify(context).saveMeasure(eq(CoreMetrics.FILES), eq(5.0));
}
- @Test
- public void shouldFailOnRootProjectIfNoFile() {
- 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 in its source folders:\n");
- thrown.expectMessage("- " + new File("target/temp").getAbsolutePath() + "\n");
- thrown.expectMessage("\nPlease check your project configuration.");
-
- decorator.decorate(project, context);
- }
-
- @Test
- public void shouldFailOnRootProjectIfNoFileMeasure() {
- Project project = createMockProject();
-
- thrown.expect(SonarException.class);
- thrown.expectMessage("Project \"Foo\" does not contain any file in its source folders");
-
- 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
- public void shouldNotFailOnModuleIfNoFile() {
- when(resource.getQualifier()).thenReturn(Qualifiers.MODULE);
- when(context.getChildrenMeasures(CoreMetrics.FILES)).thenReturn(Arrays.asList(new Measure(CoreMetrics.FILES, 0.0)));
-
- decorator.decorate(resource, context);
-
- verify(context).saveMeasure(eq(CoreMetrics.FILES), eq(0.0));
- }
-
}