Search in sources :

Example 1 with DefaultInputDir

use of org.sonar.api.batch.fs.internal.DefaultInputDir in project sonarqube by SonarSource.

the class DefaultIssueTest method build_directory_issue.

@Test
public void build_directory_issue() {
    SensorStorage storage = mock(SensorStorage.class);
    DefaultIssue issue = new DefaultIssue(storage).at(new DefaultIssueLocation().on(new DefaultInputDir("foo", "src")).message("Wrong way!")).forRule(RuleKey.of("repo", "rule")).overrideSeverity(Severity.BLOCKER);
    assertThat(issue.primaryLocation().inputComponent()).isEqualTo(new DefaultInputDir("foo", "src"));
    assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
    assertThat(issue.primaryLocation().textRange()).isNull();
    assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!");
    assertThat(issue.overriddenSeverity()).isEqualTo(Severity.BLOCKER);
    issue.save();
    verify(storage).store(issue);
}
Also used : DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Example 2 with DefaultInputDir

use of org.sonar.api.batch.fs.internal.DefaultInputDir in project sonarqube by SonarSource.

the class ComponentsPublisherTest method add_components_with_links_and_branch.

@Test
public void add_components_with_links_and_branch() throws Exception {
    ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class);
    when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
    ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo").setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0").setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "my_branch").setName("Root project").setProperty(CoreProperties.LINKS_HOME_PAGE, "http://home").setDescription("Root description");
    DefaultInputModule root = new DefaultInputModule(rootDef, 1);
    ProjectDefinition module1Def = ProjectDefinition.create().setKey("module1").setName("Module1").setProperty(CoreProperties.LINKS_CI, "http://ci").setDescription("Module description");
    rootDef.addSubProject(module1Def);
    DefaultInputModule module1 = new DefaultInputModule(module1Def, 2);
    moduleHierarchy = mock(InputModuleHierarchy.class);
    when(moduleHierarchy.root()).thenReturn(root);
    when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1));
    tree.index(module1, root);
    DefaultInputDir dir = new DefaultInputDir("module1", "src", 3);
    tree.index(dir, module1);
    DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).build();
    tree.index(file, dir);
    ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree);
    publisher.publish(writer);
    ScannerReportReader reader = new ScannerReportReader(outputDir);
    Component rootProtobuf = reader.readComponent(1);
    assertThat(rootProtobuf.getVersion()).isEqualTo("1.0");
    assertThat(rootProtobuf.getLinkCount()).isEqualTo(1);
    assertThat(rootProtobuf.getLink(0).getType()).isEqualTo(ComponentLinkType.HOME);
    assertThat(rootProtobuf.getLink(0).getHref()).isEqualTo("http://home");
    Component module1Protobuf = reader.readComponent(2);
    assertThat(module1Protobuf.getVersion()).isEqualTo("1.0");
    assertThat(module1Protobuf.getLinkCount()).isEqualTo(1);
    assertThat(module1Protobuf.getLink(0).getType()).isEqualTo(ComponentLinkType.CI);
    assertThat(module1Protobuf.getLink(0).getHref()).isEqualTo("http://ci");
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) InputModuleHierarchy(org.sonar.api.batch.fs.internal.InputModuleHierarchy) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) ProjectAnalysisInfo(org.sonar.scanner.ProjectAnalysisInfo) ComponentsPublisher(org.sonar.scanner.report.ComponentsPublisher) Component(org.sonar.scanner.protocol.output.ScannerReport.Component) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir) ProjectDefinition(org.sonar.api.batch.bootstrap.ProjectDefinition) Test(org.junit.Test)

Example 3 with DefaultInputDir

use of org.sonar.api.batch.fs.internal.DefaultInputDir in project sonarqube by SonarSource.

the class ComponentsPublisherTest method add_components_without_version_and_name.

@Test
public void add_components_without_version_and_name() throws IOException {
    ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class);
    when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
    ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo").setDescription("Root description");
    DefaultInputModule root = new DefaultInputModule(rootDef, 1);
    ProjectDefinition module1Def = ProjectDefinition.create().setKey("module1").setDescription("Module description");
    rootDef.addSubProject(module1Def);
    DefaultInputModule module1 = new DefaultInputModule(module1Def, 2);
    moduleHierarchy = mock(InputModuleHierarchy.class);
    when(moduleHierarchy.root()).thenReturn(root);
    when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1));
    tree.index(module1, root);
    DefaultInputDir dir = new DefaultInputDir("module1", "src", 3);
    tree.index(dir, module1);
    DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).build();
    tree.index(file, dir);
    DefaultInputFile fileWithoutLang = new TestInputFileBuilder("module1", "src/make", 5).setLines(10).build();
    tree.index(fileWithoutLang, dir);
    DefaultInputFile testFile = new TestInputFileBuilder("module1", "test/FooTest.java", 6).setType(Type.TEST).setLines(4).build();
    tree.index(testFile, dir);
    ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree);
    publisher.publish(writer);
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue();
    // no such reference
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse();
    ScannerReportReader reader = new ScannerReportReader(outputDir);
    Component rootProtobuf = reader.readComponent(1);
    assertThat(rootProtobuf.getKey()).isEqualTo("foo");
    assertThat(rootProtobuf.getName()).isEqualTo("");
    assertThat(rootProtobuf.getDescription()).isEqualTo("Root description");
    assertThat(rootProtobuf.getVersion()).isEqualTo("");
    assertThat(rootProtobuf.getLinkCount()).isEqualTo(0);
    Component module1Protobuf = reader.readComponent(2);
    assertThat(module1Protobuf.getKey()).isEqualTo("module1");
    assertThat(module1Protobuf.getName()).isEqualTo("");
    assertThat(module1Protobuf.getDescription()).isEqualTo("Module description");
    assertThat(module1Protobuf.getVersion()).isEqualTo("");
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) InputModuleHierarchy(org.sonar.api.batch.fs.internal.InputModuleHierarchy) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) ProjectAnalysisInfo(org.sonar.scanner.ProjectAnalysisInfo) ComponentsPublisher(org.sonar.scanner.report.ComponentsPublisher) Component(org.sonar.scanner.protocol.output.ScannerReport.Component) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir) ProjectDefinition(org.sonar.api.batch.bootstrap.ProjectDefinition) Test(org.junit.Test)

Example 4 with DefaultInputDir

use of org.sonar.api.batch.fs.internal.DefaultInputDir in project sonarqube by SonarSource.

the class ComponentsPublisherTest method should_skip_dir_without_published_files.

@Test
public void should_skip_dir_without_published_files() {
    ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class);
    when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
    ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo").setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0").setName("Root project").setDescription("Root description");
    DefaultInputModule root = new DefaultInputModule(rootDef, 1);
    moduleHierarchy = mock(InputModuleHierarchy.class);
    when(moduleHierarchy.root()).thenReturn(root);
    when(moduleHierarchy.children(root)).thenReturn(Collections.emptyList());
    // dir with files
    DefaultInputDir dir = new DefaultInputDir("module1", "src", 2);
    tree.index(dir, root);
    // dir without files and issues
    DefaultInputDir dir2 = new DefaultInputDir("module1", "src2", 3);
    tree.index(dir2, root);
    // dir without files but has issues
    DefaultInputDir dir3 = new DefaultInputDir("module1", "src3", 4);
    tree.index(dir3, root);
    writeIssue(4);
    DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 5).setLines(2).build();
    tree.index(file, dir);
    DefaultInputFile file2 = new TestInputFileBuilder("module1", "src2/Foo2.java", 6).setPublish(false).setLines(2).build();
    tree.index(file2, dir2);
    DefaultInputFile file3 = new TestInputFileBuilder("module1", "src2/Foo3.java", 7).setPublish(false).setLines(2).build();
    tree.index(file3, dir3);
    ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree);
    publisher.publish(writer);
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue();
    // file was not marked for publishing and directory doesn't contain issues, so directory won't be included as well
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isFalse();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isFalse();
    assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputModuleHierarchy(org.sonar.api.batch.fs.internal.InputModuleHierarchy) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) ProjectAnalysisInfo(org.sonar.scanner.ProjectAnalysisInfo) ComponentsPublisher(org.sonar.scanner.report.ComponentsPublisher) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir) ProjectDefinition(org.sonar.api.batch.bootstrap.ProjectDefinition) Test(org.junit.Test)

Example 5 with DefaultInputDir

use of org.sonar.api.batch.fs.internal.DefaultInputDir in project sonarqube by SonarSource.

the class JSONReportTest method before.

@Before
public void before() throws Exception {
    moduleHierarchy = mock(InputModuleHierarchy.class);
    userRepository = mock(UserRepositoryLoader.class);
    File projectBaseDir = temp.newFolder();
    fs = new DefaultFileSystem(projectBaseDir.toPath());
    SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00"));
    when(server.getVersion()).thenReturn("3.6");
    InputComponentStore inputComponentStore = new InputComponentStore(new PathResolver());
    DefaultComponentTree inputComponentTree = new DefaultComponentTree();
    DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create().setBaseDir(projectBaseDir).setKey("struts"), 1);
    inputComponentStore.put(rootModule);
    DefaultInputModule moduleA = new DefaultInputModule("struts-core");
    inputComponentTree.index(moduleA, rootModule);
    DefaultInputModule moduleB = new DefaultInputModule("struts-ui");
    inputComponentTree.index(moduleB, rootModule);
    DefaultInputDir inputDir = new DefaultInputDir("struts", "src/main/java/org/apache/struts", TestInputFileBuilder.nextBatchId()).setModuleBaseDir(projectBaseDir.toPath());
    DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java").setModuleBaseDir(projectBaseDir.toPath()).build();
    inputFile.setStatus(InputFile.Status.CHANGED);
    inputFile.setPublish(true);
    inputComponentStore.put(inputFile);
    inputComponentStore.put(inputDir);
    inputComponentTree.index(inputDir, rootModule);
    inputComponentTree.index(inputFile, inputDir);
    when(moduleHierarchy.children(rootModule)).thenReturn(Arrays.asList(moduleA, moduleB));
    when(moduleHierarchy.parent(moduleA)).thenReturn(rootModule);
    when(moduleHierarchy.parent(moduleB)).thenReturn(rootModule);
    when(moduleHierarchy.relativePath(moduleA)).thenReturn("core");
    when(moduleHierarchy.relativePath(moduleB)).thenReturn("ui");
    RulesBuilder builder = new RulesBuilder();
    builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles");
    rules = builder.build();
    jsonReport = new JSONReport(moduleHierarchy, settings, fs, server, rules, issueCache, rootModule, inputComponentStore, userRepository, inputComponentTree);
}
Also used : PathResolver(org.sonar.api.scan.filesystem.PathResolver) RulesBuilder(org.sonar.api.batch.rule.internal.RulesBuilder) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputModuleHierarchy(org.sonar.api.batch.fs.internal.InputModuleHierarchy) UserRepositoryLoader(org.sonar.scanner.repository.user.UserRepositoryLoader) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) DefaultComponentTree(org.sonar.scanner.scan.DefaultComponentTree) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Before(org.junit.Before)

Aggregations

DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)10 Test (org.junit.Test)6 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)6 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)6 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)5 InputModuleHierarchy (org.sonar.api.batch.fs.internal.InputModuleHierarchy)5 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)4 ProjectAnalysisInfo (org.sonar.scanner.ProjectAnalysisInfo)4 ComponentsPublisher (org.sonar.scanner.report.ComponentsPublisher)4 Component (org.sonar.scanner.protocol.output.ScannerReport.Component)3 ScannerReportReader (org.sonar.scanner.protocol.output.ScannerReportReader)3 InputFile (org.sonar.api.batch.fs.InputFile)2 PathResolver (org.sonar.api.scan.filesystem.PathResolver)2 File (java.io.File)1 Path (java.nio.file.Path)1 Before (org.junit.Before)1 InputDir (org.sonar.api.batch.fs.InputDir)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1 RulesBuilder (org.sonar.api.batch.rule.internal.RulesBuilder)1 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)1