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

The following examples show how to use org.sonar.api.batch.fs.InputFile.Type. 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: TraceSensorTest.java    From sonar-esql-plugin with 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 #2
Source File: EsqlSensor.java    From sonar-esql-plugin with 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 #3
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with 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 #4
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with 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 #5
Source File: CssAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(CssLanguage.KEY)
    .name("CSS Analyzer Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example #6
Source File: EsqlSensor.java    From sonar-esql-plugin with 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 #7
Source File: HighlighterVisitorTest.java    From sonar-esql-plugin with 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 #8
Source File: GherkinSquidSensor.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(GherkinLanguage.KEY)
    .name("Gherkin Squid Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example #9
Source File: GherkinSquidSensor.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #10
Source File: ScssAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(ScssLanguage.KEY)
    .name("SCSS Analyzer Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example #11
Source File: LessAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
  descriptor
    .onlyOnLanguage(LessLanguage.KEY)
    .name("Less Analyzer Sensor")
    .onlyOnFileType(Type.MAIN);
}
 
Example #12
Source File: PmdExecutor.java    From sonar-p3c-pmd with 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 #13
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with 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 #14
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with 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 #15
Source File: PmdSensorTest.java    From sonar-p3c-pmd with 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 #16
Source File: PmdSensorTest.java    From sonar-p3c-pmd with 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 #17
Source File: PmdSensorTest.java    From sonar-p3c-pmd with 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 #18
Source File: PmdSensor.java    From sonar-p3c-pmd with 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 #19
Source File: PmdExecutorTest.java    From sonar-p3c-pmd with 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 #20
Source File: CssAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
private FilePredicate mainFilePredicate(FileSystem fileSystem) {
  return fileSystem.predicates()
    .and(
      fileSystem.predicates().hasType(InputFile.Type.MAIN),
      fileSystem.predicates().hasLanguage(CssLanguage.KEY));
}
 
Example #21
Source File: LessAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
private FilePredicate mainFilePredicate(FileSystem fileSystem) {
  return fileSystem.predicates()
    .and(
      fileSystem.predicates().hasType(InputFile.Type.MAIN),
      fileSystem.predicates().hasLanguage(LessLanguage.KEY));
}
 
Example #22
Source File: PmdSensorTest.java    From sonar-p3c-pmd with 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 #23
Source File: ScssAnalyzerSensor.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 4 votes vote down vote up
private FilePredicate mainFilePredicate(FileSystem fileSystem) {
  return fileSystem.predicates()
    .and(
      fileSystem.predicates().hasType(Type.MAIN),
      fileSystem.predicates().hasLanguage(ScssLanguage.KEY));
}
 
Example #24
Source File: HtlSensor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void describe(SensorDescriptor descriptor) {
    descriptor
        .name(Htl.NAME)
        .onlyOnFileType(Type.MAIN);
}
 
Example #25
Source File: PmdSensor.java    From sonar-p3c-pmd with 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 #26
Source File: PmdExecutor.java    From sonar-p3c-pmd with 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)));
}