Java Code Examples for org.sonar.api.measures.FileLinesContext

The following examples show how to use org.sonar.api.measures.FileLinesContext. These examples are extracted from open source projects. 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 Project: sonar-ansible   Source File: AbstractAnsibleSensorTest.java    License: 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 2
@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 3
Source Project: sonar-ansible   Source File: AnsibleExtraSensorTest.java    License: 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 4
Source Project: sonar-ansible   Source File: AnsibleSensorTest.java    License: 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 5
Source Project: sonar-esql-plugin   Source File: MetricsVisitor.java    License: Apache License 2.0 6 votes vote down vote up
private void saveLineMetrics(TreeVisitorContext context) {
	LineVisitor lineVisitor = new LineVisitor(context.getTopTree());
	Set<Integer> linesOfCode = lineVisitor.getLinesOfCode();

	saveMetricOnFile(CoreMetrics.NCLOC, lineVisitor.getLinesOfCodeNumber());

	CommentLineVisitor commentVisitor = new CommentLineVisitor(context.getTopTree(), ignoreHeaderComments);
	Set<Integer> commentLines = commentVisitor.getCommentLines();

	saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentVisitor.getCommentLineNumber());

	FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(this.inputFile);

	linesOfCode.forEach(line -> fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1));
	commentLines.forEach(line -> fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, 1));

	Set<Integer> executableLines = new ExecutableLineVisitor(context.getTopTree()).getExecutableLines();
	projectExecutableLines.put(inputFile, executableLines);

	executableLines.stream()
			.forEach(line -> fileLinesContext.setIntValue(CoreMetrics.EXECUTABLE_LINES_DATA_KEY, line, 1));
	fileLinesContext.save();
}
 
Example 6
Source Project: AEM-Rules-for-SonarQube   Source File: MetricsSaver.java    License: Apache License 2.0 5 votes vote down vote up
public static void saveLineLevelMeasures(InputFile inputFile, HtmlSourceCode htmlSourceCode, FileLinesContextFactory fileLinesContextFactory) {
    FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile);
    final int lineContainsCode = 1;

    for (Integer line : htmlSourceCode.getDetailedLinesOfCode()) {
        fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, lineContainsCode);
    }

    fileLinesContext.save();
}
 
Example 7
Source Project: sonar-esql-plugin   Source File: MetricsVisitorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  context = SensorContextTester.create(MODULE_BASE_DIR);
  context.fileSystem().add(INPUT_FILE);
  linesContext = mock(FileLinesContext.class);
  treeVisitorContext = mock(TreeVisitorContext.class);
  when(treeVisitorContext.getEsqlFile()).thenReturn(new EsqlFileImpl(INPUT_FILE));
  when(treeVisitorContext.getTopTree()).thenReturn(parse(INPUT_FILE.file()));
}
 
Example 8
@Override
public void leaveFile(AstNode astNode) {
  FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(
    fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(getContext().getFile().getAbsolutePath())));

  int fileLength = getContext().peekSourceCode().getInt(LuaMetric.LINES);
  for (int line = 1; line <= fileLength; line++) {
    fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0);
    fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0);
  }
  fileLinesContext.save();

  linesOfCode.clear();
  linesOfComments.clear();
}
 
Example 9
@Before
public void setUp() throws Exception {
  NewActiveRule ar = new ActiveRulesBuilder().create(RuleKey.of("lua", "S1125")).setSeverity("BLOCKER");
  ActiveRules activeRules = new DefaultActiveRules(Collections.singletonList(ar));
  CheckFactory checkFactory = new CheckFactory(activeRules);
  FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
  when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
  sensor = new LuaSquidSensor(checkFactory, fileLinesContextFactory);
  tester = SensorContextTester.create(TEST_DIR);
}
 
Example 10
Source Project: AEM-Rules-for-SonarQube   Source File: HtlSensorTest.java    License: Apache License 2.0 4 votes vote down vote up
private FileLinesContextFactory getMockedFileLinesContextFactory() {
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
    return fileLinesContextFactory;
}
 
Example 11
Source Project: sonar-esql-plugin   Source File: EsqlSensorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    fileLinesContextFactory = mock(FileLinesContextFactory.class);
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
}