Search in sources :

Example 91 with InputFile

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

the class DefaultSensorStorageTest method shouldValidateMaxLine.

@Test
public void shouldValidateMaxLine() throws Exception {
    InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build();
    Map<Integer, Integer> map = ImmutableMap.of(11, 3);
    String data = KeyValueFormat.format(map);
    thrown.expect(IllegalStateException.class);
    underTest.validateCoverageMeasure(data, file);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 92 with InputFile

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

the class InputComponentStoreTest method should_add_input_file.

@Test
public void should_add_input_file() throws Exception {
    InputComponentStore cache = new InputComponentStore(new PathResolver());
    String rootModuleKey = "struts";
    File rootBaseDir = temp.newFolder();
    DefaultInputModule rootModule = TestInputFileBuilder.newDefaultInputModule(rootModuleKey, rootBaseDir);
    cache.put(rootModule);
    String subModuleKey = "struts-core";
    DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(subModuleKey, temp.newFolder());
    rootModule.definition().addSubProject(subModule.definition());
    cache.put(subModule);
    DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java").setModuleBaseDir(rootBaseDir.toPath()).setPublish(true).build();
    cache.put(fooFile);
    cache.put(new TestInputFileBuilder(subModuleKey, "src/main/java/Bar.java").setLanguage("bla").setPublish(false).setType(Type.MAIN).setStatus(Status.ADDED).setLines(2).setCharset(StandardCharsets.UTF_8).setModuleBaseDir(temp.newFolder().toPath()).build());
    DefaultInputFile loadedFile = (DefaultInputFile) cache.getFile(subModuleKey, "src/main/java/Bar.java");
    assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java");
    assertThat(loadedFile.charset()).isEqualTo(StandardCharsets.UTF_8);
    assertThat(cache.filesByModule(rootModuleKey)).hasSize(1);
    assertThat(cache.filesByModule(subModuleKey)).hasSize(1);
    assertThat(cache.allFiles()).hasSize(2);
    for (InputPath inputPath : cache.allFiles()) {
        assertThat(inputPath.relativePath()).startsWith("src/main/java/");
    }
    List<InputFile> toPublish = new LinkedList<>();
    cache.allFilesToPublish().forEach(toPublish::add);
    assertThat(toPublish).containsExactly(fooFile);
    cache.remove(fooFile);
    assertThat(cache.allFiles()).hasSize(1);
    cache.removeModule(rootModuleKey);
    assertThat(cache.filesByModule(rootModuleKey)).hasSize(0);
    assertThat(cache.filesByModule(subModuleKey)).hasSize(1);
    assertThat(cache.allFiles()).hasSize(1);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputPath(org.sonar.api.batch.fs.InputPath) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) PathResolver(org.sonar.api.scan.filesystem.PathResolver) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) LinkedList(java.util.LinkedList) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 93 with InputFile

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

the class InputComponentStoreTest method should_find_files_per_module_and_globally.

@Test
public void should_find_files_per_module_and_globally() throws IOException {
    InputComponentStoreTester tester = new InputComponentStoreTester();
    String mod1Key = "mod1";
    InputFile mod1File = tester.addFile(mod1Key, "src/main/java/Foo.java", "java");
    String mod2Key = "mod2";
    InputFile mod2File = tester.addFile(mod2Key, "src/main/groovy/Foo.groovy", "groovy");
    assertThat(tester.filesByModule(mod1Key)).containsExactly(mod1File);
    assertThat(tester.filesByModule(mod2Key)).containsExactly(mod2File);
    assertThat(tester.allFiles()).containsExactlyInAnyOrder(mod1File, mod2File);
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 94 with InputFile

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

the class DefaultSensorStorageTest method shouldSaveFileMeasureToSensorContext.

@Test
public void shouldSaveFileMeasureToSensorContext() {
    InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
    ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
    when(measureCache.put(eq(file.key()), eq(CoreMetrics.NCLOC_KEY), argumentCaptor.capture())).thenReturn(null);
    underTest.store(new DefaultMeasure().on(file).forMetric(CoreMetrics.NCLOC).withValue(10));
    DefaultMeasure m = argumentCaptor.getValue();
    assertThat(m.value()).isEqualTo(10);
    assertThat(m.metric()).isEqualTo(CoreMetrics.NCLOC);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 95 with InputFile

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

the class DefaultSensorStorageTest method shouldFailIfUnknownMetric.

@Test
public void shouldFailIfUnknownMetric() {
    InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
    thrown.expect(UnsupportedOperationException.class);
    thrown.expectMessage("Unknown metric: lines");
    underTest.store(new DefaultMeasure().on(file).forMetric(CoreMetrics.LINES).withValue(10));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)102 Test (org.junit.Test)61 File (java.io.File)45 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)39 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)20 TaskResult (org.sonar.scanner.mediumtest.TaskResult)19 IOException (java.io.IOException)10 FilePredicates (org.sonar.api.batch.fs.FilePredicates)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)7 List (java.util.List)6 FileSystem (org.sonar.api.batch.fs.FileSystem)6 BlameLine (org.sonar.api.batch.scm.BlameLine)6 ArrayList (java.util.ArrayList)4 PathResolver (org.sonar.api.scan.filesystem.PathResolver)4 DefaultBlameOutput (org.sonar.scanner.scm.DefaultBlameOutput)4 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)3 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)3 MutableTestPlan (org.sonar.api.test.MutableTestPlan)3 Block (org.sonar.duplications.block.Block)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2