Java Code Examples for org.sonar.api.batch.rule.ActiveRules

The following examples show how to use org.sonar.api.batch.rule.ActiveRules. 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
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 3
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 4
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  inputFile("parsingError.css");

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createCssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example 5
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  inputFile("parsingError.less");

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.LESS_REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createLessSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example 6
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  inputFile("parsingError.scss");

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.SCSS_REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createScssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example 7
public void exportProfile(ActiveRules activeRules, Writer writer) {
    try {
        final List<ActiveRuleWrapper> activeRuleWrappers = new ArrayList<>();
        for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules
                .findByRepository(CheckstyleConstants.REPOSITORY_KEY)) {
            activeRuleWrappers.add(new ActiveRuleWrapperScannerImpl(activeRule));
        }
        final Map<String, List<ActiveRuleWrapper>> activeRulesByConfigKey =
                arrangeByConfigKey(activeRuleWrappers);
        generateXml(writer, activeRulesByConfigKey);
    }
    catch (IOException ex) {
        throw new IllegalStateException("Fail to export active rules.", ex);
    }

}
 
Example 8
@Test
public void should_execute_and_save_issues_on_UTF8_with_BOM_file() {
  inputFile("my-feature-bom.feature", Charsets.UTF_8);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, CommentConventionCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, MissingNewlineAtEndOfFileCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createGherkinSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 9
@Test
public void should_execute_and_save_issues_on_UTF8_file() {
  inputFile("my-feature.feature", Charsets.UTF_8);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, CommentConventionCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, MissingNewlineAtEndOfFileCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createGherkinSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 10
@Test
public void should_execute_and_save_issues_on_UTF8_with_BOM_file_french() {
  inputFile("my-feature-bom-fr.feature", Charsets.UTF_8);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, CommentConventionCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, MissingNewlineAtEndOfFileCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createGherkinSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 11
@Test
public void should_execute_and_save_issues_on_UTF8_file_french() {
  inputFile("my-feature-fr.feature", Charsets.UTF_8);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, CommentConventionCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, MissingNewlineAtEndOfFileCheck.class.getAnnotation(Rule.class).key()))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createGherkinSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 12
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  String relativePath = "parsing-error.feature";
  inputFile(relativePath, Charsets.UTF_8);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createGherkinSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example 13
Source Project: sonar-esql-plugin   Source File: EsqlSensorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void parsing_error() {
    String relativePath = "cpd/parsingError.esql";
    inputFile(relativePath);

    String parsingErrorCheckKey = "ParsingError";

    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setName("ParsingError").setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, parsingErrorCheckKey)).build())
            .build();

    checkFactory = new CheckFactory(activeRules);

    context.setActiveRules(activeRules);
    createSensor().execute(context);
    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(1);
    Issue issue = issues.iterator().next();
    assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(3);
    assertThat(issue.primaryLocation().message()).isEqualTo("Parse error");

    assertThat(context.allAnalysisErrors()).hasSize(1);
}
 
Example 14
Source Project: sonar-esql-plugin   Source File: EsqlSensorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void analysis_with_issues_should_not_add_error_to_context() {
    inputFile("file.esql");

    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "MissingNewlineAtEndOfFile")).build())
            .build();
    checkFactory = new CheckFactory(activeRules);

    createSensor().execute(context);

    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(1);

    assertThat(context.allAnalysisErrors()).isEmpty();
}
 
Example 15
Source Project: sonar-esql-plugin   Source File: EsqlSensorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void custom_rule() throws Exception {
    inputFile("file.esql");
    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("customKey", "key")).build()).build();
    checkFactory = new CheckFactory(activeRules);
    createSensorWithCustomRules().execute(context);

    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(1);
    Issue issue = issues.iterator().next();
    assertThat(issue.gap()).isEqualTo(42);
    assertThat(issue.primaryLocation().message()).isEqualTo("Message of custom rule");
    assertThat(issue.primaryLocation().textRange())
            .isEqualTo(new DefaultTextRange(new DefaultTextPointer(1, 0), new DefaultTextPointer(1, 24)));
}
 
Example 16
private void should_execute_and_save_issues(String fileName) {
  inputFile(fileName);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "S1135"))
    .activate()
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "important"))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createCssSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 17
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  inputFile("parsingError.css");

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createCssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).isEmpty();
}
 
Example 18
private void should_execute_and_save_issues(String fileName) {
  inputFile(fileName);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "S1135"))
    .activate()
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "important"))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createEmbeddedCssSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(2);
}
 
Example 19
private void should_not_execute_and_save_issues(String fileName) {
  inputFile(fileName);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "S1135"))
    .activate()
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "important"))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createEmbeddedCssSquidSensor().execute(context);

  assertThat(context.allIssues()).isEmpty();
}
 
Example 20
private void should_execute_and_save_issues(String fileName) {
  inputFile(fileName);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.LESS_REPOSITORY_KEY, "less-variable-naming-convention"))
    .activate()
    .create(RuleKey.of(CheckList.LESS_REPOSITORY_KEY, "important"))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createLessSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 21
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  inputFile("parsingError.less");

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createLessSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).isEmpty();
}
 
Example 22
private void should_execute_and_save_issues(String fileName) {
  inputFile(fileName);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.SCSS_REPOSITORY_KEY, "scss-variable-naming-convention"))
    .activate()
    .create(RuleKey.of(CheckList.SCSS_REPOSITORY_KEY, "important"))
    .activate()
    .build();
  checkFactory = new CheckFactory(activeRules);

  createScssSquidSensor().execute(context);

  assertThat(context.allIssues()).hasSize(3);
}
 
Example 23
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  inputFile("parsingError.scss");

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createScssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).isEmpty();
}
 
Example 24
public CheckstyleConfiguration(
        org.sonar.api.config.Configuration conf,
        CheckstyleProfileExporter confExporter,
        ActiveRules activeRules,
        FileSystem fileSystem) {
    this.conf = conf;
    this.confExporter = confExporter;
    this.activeRules = activeRules;
    this.fileSystem = fileSystem;
}
 
Example 25
@Before
public void before() {
    fileSystem = mock(FileSystem.class);
    context = mock(SensorContext.class);
    inputFile = mock(InputFile.class);
    ruleFinder = mock(ActiveRules.class);

    final FilePredicates predicates = mock(FilePredicates.class);
    final FilePredicate filePredicate = mock(FilePredicate.class);
    when(fileSystem.inputFile(any(FilePredicate.class))).thenReturn(inputFile);
    when(fileSystem.predicates()).thenReturn(predicates);
    when(predicates.hasAbsolutePath(anyString())).thenReturn(filePredicate);
}
 
Example 26
@Test
public void singleCheckstyleActiveRulesToExport() {
    final ActiveRules activeRules = Mockito.mock(ActiveRules.class);
    Mockito.when(activeRules.findByRepository(CheckstyleConstants.REPOSITORY_KEY))
            .thenReturn(Collections.singletonList(new TestActiveRule()));

    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(activeRules, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "singleCheckstyleActiveRulesToExport.xml",
            sanitizeForTests(writer.toString()));
}
 
Example 27
@Override
public void exportProfile(ActiveRules activeRules, Writer writer) {
    try {
        writer.write("<conf/>");
    }
    catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example 28
Source Project: enforce-sonarqube-plugin   Source File: ApexSquidSensorTest.java    License: 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 29
Source Project: sonar-coldfusion   Source File: ColdFusionSensor.java    License: Apache License 2.0 5 votes vote down vote up
public ColdFusionSensor(FileSystem fs, ActiveRules ruleProfile) {
    Preconditions.checkNotNull(fs);
    Preconditions.checkNotNull(ruleProfile);

    this.fs = fs;
    this.ruleProfile = ruleProfile;
}
 
Example 30
public void exportProfile(ActiveRules activeRules, Writer writer) {
    try {
        new CFLintConfigExporter(activeRules.findByRepository(ColdFusionPlugin.REPOSITORY_KEY)).save(writer);
    } catch (IOException | XMLStreamException e) {
        Throwables.propagate(e);
    }

}