Java Code Examples for org.sonar.api.batch.fs.InputFile.Type

The following examples show how to use org.sonar.api.batch.fs.InputFile.Type. 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-p3c-pmd   Source File: PmdExecutorTest.java    License: MIT License 6 votes vote down vote up
@Test
public void should_execute_pmd_on_source_files_and_test_files() throws Exception {
  InputFile srcFile = file("src/Class.java", Type.MAIN);
  InputFile tstFile = file("test/ClassTest.java", Type.TEST);
  setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml");
  setupPmdRuleSet(PmdConstants.TEST_REPOSITORY_KEY, "junit.xml");
  fileSystem.add(srcFile);
  fileSystem.add(tstFile);

  Report report = pmdExecutor.execute();

  assertThat(report).isNotNull();

  // setting java source version to the default value
  settings.removeProperty(PmdConstants.JAVA_SOURCE_VERSION);
  report = pmdExecutor.execute();

  assertThat(report).isNotNull();
}
 
Example 2
Source Project: sonar-p3c-pmd   Source File: PmdExecutorTest.java    License: MIT License 6 votes vote down vote up
@Test
public void unknown_pmd_ruleset() throws Exception {
  String profileContent = "content";
  when(pmdProfileExporter.exportProfile(PmdConstants.REPOSITORY_KEY, rulesProfile)).thenReturn(profileContent);
  when(pmdConfiguration.dumpXmlRuleSet(PmdConstants.REPOSITORY_KEY, profileContent)).thenReturn(new File("unknown"));

  InputFile srcFile = file("src/Class.java", Type.MAIN);
  fileSystem.add(srcFile);

  try {
    pmdExecutor.execute();
    Assert.fail("Expected an exception");
  } catch (IllegalStateException e) {
    assertThat(e.getCause()).isInstanceOf(RuleSetNotFoundException.class);
  }
}
 
Example 3
Source Project: sonar-esql-plugin   Source File: EsqlSensor.java    License: Apache License 2.0 6 votes vote down vote up
public EsqlSensor(
        CheckFactory checkFactory, FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem, NoSonarFilter noSonarFilter,
        @Nullable CustomEsqlRulesDefinition[] customRulesDefinition
) {

    this.checks = EsqlChecks.createEsqlCheck(checkFactory)
            .addChecks(CheckList.REPOSITORY_KEY, CheckList.getChecks())
            .addCustomChecks(customRulesDefinition);
    this.fileLinesContextFactory = fileLinesContextFactory;
    this.fileSystem = fileSystem;
    this.noSonarFilter = noSonarFilter;
    this.mainFilePredicate = fileSystem.predicates().and(
            fileSystem.predicates().hasType(InputFile.Type.MAIN),
            fileSystem.predicates().hasLanguage(EsqlLanguage.KEY));
    this.parser = EsqlParserBuilder.createParser();
}
 
Example 4
Source Project: sonar-esql-plugin   Source File: TraceSensorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws FileNotFoundException {
	settings = new MapSettings();
	settings.setProperty("sonar.esql.trace.reportPaths", "TraceSensorTest/trace.txt");
	context = SensorContextTester.create(moduleBaseDir);
	context.setSettings(settings);

	InputFile inputFile1 = inputFile("file1.esql", Type.MAIN);
	InputFile inputFile2 = inputFile("file2.esql", Type.MAIN);
	InputFile inputFile3 = inputFile("file3.esql", Type.MAIN);
	// inputFile("tests/file1.esql", Type.TEST);

	linesOfCode = new HashMap<>();
	linesOfCode.put(inputFile1, ImmutableSet.of(1, 2, 3, 4));
	linesOfCode.put(inputFile2, null);
	linesOfCode.put(inputFile3, ImmutableSet.of(1, 2, 3, 4));

	sensor = new TraceSensor();

}
 
Example 5
Source Project: sonar-p3c-pmd   Source File: PmdExecutor.java    License: MIT License 5 votes vote down vote up
private Report executePmd(URLClassLoader classLoader) {
  Report report = new Report();

  RuleContext context = new RuleContext();
  context.setReport(report);

  PmdTemplate pmdFactory = createPmdTemplate(classLoader);
  executeRules(pmdFactory, context, javaFiles(Type.MAIN), PmdConstants.REPOSITORY_KEY);
  executeRules(pmdFactory, context, javaFiles(Type.TEST), PmdConstants.TEST_REPOSITORY_KEY);

  pmdConfiguration.dumpXmlReport(report);

  return report;
}
 
Example 6
Source Project: sonar-p3c-pmd   Source File: PmdSensor.java    License: MIT License 5 votes vote down vote up
private boolean hasFilesToCheck(Type type, String repositoryKey) {
  FilePredicates predicates = fs.predicates();
  Iterable<File> files = fs.files(predicates.and(
    predicates.hasLanguage(Java.KEY),
    predicates.hasType(type)));
  return !Iterables.isEmpty(files) && !profile.getActiveRulesByRepository(repositoryKey).isEmpty();
}
 
Example 7
Source Project: sonar-p3c-pmd   Source File: PmdSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void should_execute_on_project_without_main_files() {
  addOneJavaFile(Type.TEST);

  boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project);

  assertThat(shouldExecute).isTrue();
}
 
Example 8
Source Project: sonar-p3c-pmd   Source File: PmdSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void should_execute_on_project_without_test_files() {
  addOneJavaFile(Type.MAIN);

  boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project);

  assertThat(shouldExecute).isTrue();
}
 
Example 9
Source Project: sonar-p3c-pmd   Source File: PmdSensorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void should_not_execute_on_project_without_active_rules() {
  addOneJavaFile(Type.MAIN);
  addOneJavaFile(Type.TEST);

  when(profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty()).thenReturn(true);
  when(profile.getActiveRulesByRepository(PmdConstants.TEST_REPOSITORY_KEY).isEmpty()).thenReturn(true);

  boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project);

  assertThat(shouldExecute).isFalse();
}
 
Example 10
Source Project: sonar-p3c-pmd   Source File: PmdExecutorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void should_dump_ruleset_as_xml() throws Exception {
  InputFile srcFile = file("src/Class.java", Type.MAIN);
  InputFile tstFile = file("test/ClassTest.java", Type.TEST);
  setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml");
  setupPmdRuleSet(PmdConstants.TEST_REPOSITORY_KEY, "junit.xml");
  fileSystem.add(srcFile);
  fileSystem.add(tstFile);

  pmdExecutor.execute();

  verify(pmdConfiguration).dumpXmlRuleSet(PmdConstants.REPOSITORY_KEY, PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/simple.xml"));
  verify(pmdConfiguration).dumpXmlRuleSet(PmdConstants.TEST_REPOSITORY_KEY, PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/junit.xml"));
}
 
Example 11
Source Project: sonar-p3c-pmd   Source File: PmdExecutorTest.java    License: MIT License 5 votes vote down vote up
@Test
public void should_ignore_empty_test_dir() throws Exception {
  InputFile srcFile = file("src/Class.java", Type.MAIN);
  doReturn(pmdTemplate).when(pmdExecutor).createPmdTemplate(any(URLClassLoader.class));
  setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml");
  fileSystem.add(srcFile);

  pmdExecutor.execute();

  verify(pmdTemplate).process(eq(srcFile.file()), any(RuleSets.class), any(RuleContext.class));
  verifyNoMoreInteractions(pmdTemplate);
}
 
Example 12
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(CssLanguage.KEY)
    .name("CSS Analyzer Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example 13
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(LessLanguage.KEY)
    .name("Less Analyzer Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example 14
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(ScssLanguage.KEY)
    .name("SCSS Analyzer Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example 15
public GherkinSquidSensor(FileSystem fileSystem, CheckFactory checkFactory, @Nullable CustomGherkinRulesDefinition[] customRulesDefinition) {
  this.fileSystem = fileSystem;

  this.mainFilePredicate = fileSystem.predicates().and(
    fileSystem.predicates().hasType(Type.MAIN),
    fileSystem.predicates().hasLanguage(GherkinLanguage.KEY));

  this.checks = GherkinChecks.createGherkinChecks(checkFactory)
    .addChecks(GherkinRulesDefinition.REPOSITORY_KEY, GherkinRulesDefinition.getChecks())
    .addCustomChecks(customRulesDefinition);
}
 
Example 16
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(GherkinLanguage.KEY)
    .name("Gherkin Squid Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example 17
Source Project: sonar-esql-plugin   Source File: HighlighterVisitorTest.java    License: Apache License 2.0 5 votes vote down vote up
private void initFile(String text) throws IOException {
	File file = tempFolder.newFile();
	inputFile = new TestInputFileBuilder("moduleKey", file.getName())
			.setLanguage("esql")
			.setType(Type.MAIN)
			.setCharset(CHARSET)
			.initMetadata(text)
			.build();

	when(visitorContext.getEsqlFile()).thenReturn(new EsqlFileImpl(inputFile));
}
 
Example 18
Source Project: sonar-esql-plugin   Source File: EsqlSensor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
    descriptor
            .onlyOnLanguage(EsqlLanguage.KEY)
            .name("ESQL Squid Sensor")
            .onlyOnFileType(Type.MAIN);
}
 
Example 19
Source Project: sonar-p3c-pmd   Source File: PmdExecutor.java    License: MIT License 4 votes vote down vote up
public Iterable<File> javaFiles(Type fileType) {
  FilePredicates predicates = fs.predicates();
  return fs.files(predicates.and(
    predicates.hasLanguage(Java.KEY),
    predicates.hasType(fileType)));
}
 
Example 20
Source Project: sonar-p3c-pmd   Source File: PmdSensor.java    License: MIT License 4 votes vote down vote up
@Override
public boolean shouldExecuteOnProject(Project project) {
  return (hasFilesToCheck(Type.MAIN, PmdConstants.REPOSITORY_KEY))
    || (hasFilesToCheck(Type.TEST, PmdConstants.TEST_REPOSITORY_KEY));
}
 
Example 21
Source Project: sonar-p3c-pmd   Source File: PmdSensorTest.java    License: MIT License 4 votes vote down vote up
private void addOneJavaFile(Type type) {
  File file = new File("x");
  fs.add(new DefaultInputFile(
    file.getName()).setAbsolutePath(file.getAbsolutePath()).setLanguage("java").setType(type));
}
 
Example 22
Source Project: sonar-p3c-pmd   Source File: PmdExecutorTest.java    License: MIT License 4 votes vote down vote up
static InputFile file(String path, Type type) {
  return new DefaultInputFile(path)
    .setAbsolutePath(new File(path).getAbsolutePath())
    .setType(type)
    .setLanguage(Java.KEY);
}
 
Example 23
private FilePredicate mainFilePredicate(FileSystem fileSystem) {
  return fileSystem.predicates()
    .and(
      fileSystem.predicates().hasType(InputFile.Type.MAIN),
      fileSystem.predicates().hasLanguage(CssLanguage.KEY));
}
 
Example 24
private FilePredicate mainFilePredicate(FileSystem fileSystem) {
  return fileSystem.predicates()
    .and(
      fileSystem.predicates().hasType(InputFile.Type.MAIN),
      fileSystem.predicates().hasLanguage(LessLanguage.KEY));
}
 
Example 25
private FilePredicate mainFilePredicate(FileSystem fileSystem) {
  return fileSystem.predicates()
    .and(
      fileSystem.predicates().hasType(Type.MAIN),
      fileSystem.predicates().hasLanguage(ScssLanguage.KEY));
}
 
Example 26
Source Project: AEM-Rules-for-SonarQube   Source File: HtlSensor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
    descriptor
        .name(Htl.NAME)
        .onlyOnFileType(Type.MAIN);
}