org.sonar.api.batch.fs.internal.DefaultFileSystem Java Examples

The following examples show how to use org.sonar.api.batch.fs.internal.DefaultFileSystem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CssSyntaxHighlighterVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("css")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);

  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new CssSyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example #2
Source File: LuaSquidSensorTest.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void analyse2() throws FileNotFoundException {
  DefaultFileSystem fs = new DefaultFileSystem(TEST_DIR);
  tester.setFileSystem(fs);
  DefaultInputFile inputFile = new DefaultInputFile("key", "timeFormatter.lua")
    .setType(InputFile.Type.MAIN)
    .setLanguage(Lua.KEY)
    .initMetadata(new FileMetadata().readMetadata(new FileReader(new File(TEST_DIR, "timeFormatter.lua"))));
  fs.add(inputFile);

  sensor.execute(tester);

  String componentKey = inputFile.key();
  assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY_IN_CLASSES).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.COMMENT_LINES).value()).isEqualTo(59);
  assertThat(tester.measure(componentKey, CoreMetrics.STATEMENTS).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.FUNCTIONS).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");
  assertThat(tester.measure(componentKey, CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0");



  assertThat(tester.allIssues()).hasSize(0);
}
 
Example #3
Source File: CloverSensorTest.java    From sonar-clover with Apache License 2.0 6 votes vote down vote up
@Test
public void should_process_file() throws Exception {
  final String cloverFilePath = "org/sonar/plugins/clover/CloverXmlReportParserTest/clover_2_6_0.xml";
  final File cloverFile = TestUtils.getResource(cloverFilePath);
  final MapSettings settings = new MapSettings();
  settings.setProperty(CloverSensor.REPORT_PATH_PROPERTY, cloverFile.getAbsolutePath());

  final DefaultFileSystem fs = context.fileSystem();
  fs.add(new TestInputFileBuilder("", cloverFile.getAbsolutePath()).build());

  final Configuration configuration = new ConfigurationBridge(settings);
  final CloverSensor sensor = new CloverSensor(configuration, fs, new PathResolver());

  sensor.execute(context);

  assertThat(logTester.logs(LoggerLevel.INFO)).contains("Parsing " + cloverFile.getCanonicalPath());
  assertThat(logTester.logs(LoggerLevel.WARN).stream().anyMatch(s -> s.contains("14 files in Clover report did not match any file in SonarQube Index"))).isEqualTo(true);
}
 
Example #4
Source File: SyntaxHighlighterVisitorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("gherkin")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);
  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new SyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example #5
Source File: CpdVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("css")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);

  visitorContext = mock(TreeVisitorContext.class);
  cpdVisitor = new CpdVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example #6
Source File: ClojureSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
private SensorContextTester prepareContext() {
    DefaultFileSystem fileSystem = new DefaultFileSystem(temporaryFolder.getRoot());
    SensorContextTester context = SensorContextTester.create(temporaryFolder.getRoot());
    context.setFileSystem(fileSystem);

    DefaultInputFile inputFile1 = TestInputFileBuilder.create("module", "foo.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata("firstLine\nsecondLine")
            .build();
    DefaultInputFile inputFile2 = TestInputFileBuilder.create("module", "bar.clj")
            .setLanguage(Clojure.KEY)
            .initMetadata("firstLine\nsecondLine\nthirdLine")
            .build();
    context.fileSystem().add(inputFile1).add(inputFile2);

    return context;
}
 
Example #7
Source File: ScssSyntaxHighlighterVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("scss")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);

  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new ScssSyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example #8
Source File: LessSyntaxHighlighterVisitorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  DefaultFileSystem fileSystem = new DefaultFileSystem(tempFolder.getRoot());
  fileSystem.setEncoding(Charsets.UTF_8);
  file = tempFolder.newFile();
  inputFile = new DefaultInputFile("moduleKey", file.getName())
    .setLanguage("less")
    .setType(InputFile.Type.MAIN);
  fileSystem.add(inputFile);

  sensorContext = SensorContextTester.create(tempFolder.getRoot());
  sensorContext.setFileSystem(fileSystem);

  visitorContext = mock(TreeVisitorContext.class);
  highlighterVisitor = new LessSyntaxHighlighterVisitor(sensorContext);
  when(visitorContext.getFile()).thenReturn(file);
}
 
Example #9
Source File: AnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    Path baseDir = Paths.get("src/test/resources");
    context = SensorContextTester.create(baseDir);

    DefaultFileSystem fs = new DefaultFileSystem(baseDir);
    fs.setWorkDir(temporaryFolder.newFolder("temp").toPath());
    context.setFileSystem(fs);

    ActiveRules activeRules = new ActiveRulesBuilder()
            .create(ruleKey1)
            .activate()
            .create(ruleKey2)
            .activate()
            .create(ruleKey3)
            .activate()
            .build();
    context.setActiveRules(activeRules);

    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(mock(FileLinesContext.class));

    sensor = new AnsibleSensor(fs);
}
 
Example #10
Source File: AnsibleExtraSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    context = Utils.getSensorContext();

    DefaultFileSystem fs = Utils.getFileSystem();
    fs.setWorkDir(temporaryFolder.newFolder("temp").toPath());
    context.setFileSystem(fs);

    ActiveRules activeRules = new ActiveRulesBuilder()
            .create(ruleKey1)
            .activate()
            .create(ruleKey2)
            .activate()
            .build();
    context.setActiveRules(activeRules);

    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(mock(FileLinesContext.class));

    sensor = new AnsibleExtraSensor(context.fileSystem());
}
 
Example #11
Source File: AbstractAnsibleSensorNoRuleTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoActiveRule() throws IOException {
    SensorContextTester context = Utils.getSensorContext();

    DefaultFileSystem fs = Utils.getFileSystem();
    fs.setWorkDir(temporaryFolder.newFolder("temp").toPath());
    context.setFileSystem(fs);

    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(mock(FileLinesContext.class));

    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    InputFile playbook2 = Utils.getInputFile("playbooks/playbook2.yml");
    InputFile playbook3 = Utils.getInputFile("playbooks/playbook3.yml");
    context.fileSystem().add(playbook1).add(playbook2).add(playbook3);

    MySensor sensor = new MySensor(fs);

    sensor.executeWithAnsibleLint(context, null);
    assertEquals(1, logTester.logs(LoggerLevel.INFO).size());
    assertEquals("No active rules found for this plugin, skipping.", logTester.logs(LoggerLevel.INFO).get(0));
    assertEquals(0, context.allIssues().size());
}
 
Example #12
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    context = Utils.getSensorContext();

    DefaultFileSystem fs = Utils.getFileSystem();
    fs.setWorkDir(temporaryFolder.newFolder("temp").toPath());
    context.setFileSystem(fs);

    ActiveRules activeRules = new ActiveRulesBuilder()
            .create(ruleKey1)
            .activate()
            .create(ruleKey2)
            .activate()
            .create(ruleKey3)
            .activate()
            .build();
    context.setActiveRules(activeRules);

    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(mock(FileLinesContext.class));

    sensor = new MySensor(fs);
}
 
Example #13
Source File: AnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    File moduleBaseDir = new File("src/main/resources");

    try {
        AnsibleSensor sensor = new AnsibleSensor(new DefaultFileSystem(moduleBaseDir));
        DummySensorDescriptor descriptor = new DummySensorDescriptor();
        sensor.describe(descriptor);
        assertEquals("Ansible Lint Sensor", descriptor.sensorName);
        assertEquals(YamlLanguage.KEY, descriptor.languageKey);
    } catch (Exception e) {
        fail("Sensor constructor should not fail: " + e.getMessage());
    }
}
 
Example #14
Source File: CheckstyleConfigurationTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void beforeClass() {
    fileSystem = new DefaultFileSystem(new File(""));
    fileSystem.setWorkDir(new File(".").toPath());

    final File file = new File("mainFile");
    final DefaultInputFile inputFile = new DefaultInputFile(
            new DefaultIndexedFile("",
                    new File("").toPath(),
                    file.getName(),
                    "java"),
            DefaultInputFile::checkMetadata);
    fileSystem.add(inputFile);

    final File testFile = new File("testFile");
    final DefaultInputFile testInputFile = new DefaultInputFile(
            new DefaultIndexedFile(testFile.toPath(),
                    "",
                    "testFile",
                    "testFile",
                    InputFile.Type.TEST,
                    "java",
                    TestInputFileBuilder.nextBatchId(),
                    new SensorStrategy()),
            DefaultInputFile::checkMetadata);
    fileSystem.add(testInputFile);
}
 
Example #15
Source File: ApexSquidSensorTest.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
@Before
public void setUp() {
    ActiveRules activeRules = (new ActiveRulesBuilder())
            .create(RuleKey.of(CheckList.REPOSITORY_KEY, "PrintStatementUsage"))
            .setName("Print Statement Usage")
            .activate()
            .build();
    CheckFactory checkFactory = new CheckFactory(activeRules);
    perspectives = mock(ResourcePerspectives.class);
    fileSystem = new DefaultFileSystem(new File("."));
    squidSensor = new ApexSquidSensor(fileSystem, perspectives, checkFactory);
}
 
Example #16
Source File: ColdfusionSensorTest.java    From sonar-coldfusion with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicCFMAnalysis() {
    DefaultFileSystem fileSystem = new DefaultFileSystem(tmpFolder.getRoot());
    fileSystem.setEncoding(Charsets.UTF_8);
    fileSystem.setWorkDir(tmpFolder.getRoot().toPath());

    context.setFileSystem(fileSystem);
    context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.create(7, 6), SonarQubeSide.SCANNER));

    context.settings().appendProperty("sonar.projectBaseDir", baseDir.getPath());
    addFilesToFs();

    CommandExecutor commandExecutor = CommandExecutor.create();
    String javaHome = System.getProperty("java.home");
    Assert.assertTrue(javaHome!=null && !javaHome.equals(""));

    if(OSValidator.isWindows()) {
        context.settings().appendProperty(ColdFusionPlugin.CFLINT_JAVA, javaHome + "/bin/java.exe");
    } else {
        context.settings().appendProperty(ColdFusionPlugin.CFLINT_JAVA, javaHome + "/bin/java");
    }

    ColdFusionSensor sensor = new ColdFusionSensor(context.fileSystem(), rulesProfile);
    sensor.execute(context);

    Integer nloc = 0;
    Integer comments = 0;
    Integer complexity = 0;
    for (InputFile o : context.fileSystem().inputFiles()) {
        Measure<Integer> measureNloc = context.measure(o.key(),CoreMetrics.NCLOC.key());
        Measure<Integer> measureComment = context.measure(o.key(),CoreMetrics.COMMENT_LINES.key());
        Measure<Integer> measureComplexity = context.measure(o.key(),CoreMetrics.COMPLEXITY.key());
        nloc+=measureNloc.value();
        comments+=measureComment.value();
        complexity+=measureComplexity.value();
    }
    assertThat(nloc).isEqualTo(56);
    assertThat(comments).isEqualTo(9);
    assertThat(complexity).isEqualTo(10);
}
 
Example #17
Source File: LuaSquidSensorTest.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void analyse() throws FileNotFoundException {
  DefaultFileSystem fs = new DefaultFileSystem(TEST_DIR);
  tester.setFileSystem(fs);
  DefaultInputFile inputFile = new DefaultInputFile("key", "smallFile.lua")
    .setType(InputFile.Type.MAIN)
    .setLanguage(Lua.KEY)
    .initMetadata(new FileMetadata().readMetadata(new FileReader(new File(TEST_DIR, "smallFile.lua"))));
  fs.add(inputFile);
  inputFile = new DefaultInputFile("key", "bom.lua")
    .setType(InputFile.Type.MAIN)
    .setLanguage(Lua.KEY)
    .initMetadata(new FileMetadata().readMetadata(new FileReader(new File(TEST_DIR, "bom.lua"))));
  fs.add(inputFile);

  sensor.execute(tester);

  String componentKey = "key:smallFile.lua";
  assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY_IN_CLASSES).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.NCLOC).value()).isEqualTo(4);
  assertThat(tester.measure(componentKey, CoreMetrics.COMMENT_LINES).value()).isEqualTo(1);
  assertThat(tester.measure(componentKey, CoreMetrics.STATEMENTS).value()).isEqualTo(0);
  assertThat(tester.measure(componentKey, CoreMetrics.FUNCTIONS).value()).isEqualTo(3);

  assertThat(tester.measure(componentKey, CoreMetrics.COMPLEXITY).value()).isEqualTo(4);
  assertThat(tester.measure(componentKey, CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");
  assertThat(tester.measure(componentKey, CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("1=2;2=0;4=1;6=0;8=0;10=0;12=0");

 
}
 
Example #18
Source File: Utils.java    From sonar-ansible with Apache License 2.0 4 votes vote down vote up
public static DefaultFileSystem getFileSystem() {
    return new DefaultFileSystem(BASE_DIR);
}