org.sonar.api.batch.rule.internal.ActiveRulesBuilder Java Examples

The following examples show how to use org.sonar.api.batch.rule.internal.ActiveRulesBuilder. 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: EsqlSensorTest.java    From sonar-esql-plugin with 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 #2
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #3
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #4
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #5
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #6
Source File: ScssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #8
Source File: LessAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #9
Source File: EsqlSensorTest.java    From sonar-esql-plugin with 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 #10
Source File: EsqlSensorTest.java    From sonar-esql-plugin with 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 #11
Source File: CssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #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 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 #14
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 #15
Source File: LuaSquidSensorTest.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 #16
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  String relativePath = "parsing-error.feature";
  inputFile(relativePath, Charsets.UTF_8);

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

  context.setActiveRules(activeRules);
  createGherkinSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(0);
}
 
Example #17
Source File: EsqlSensorTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void save_issue() throws Exception {
    inputFile("file.esql");

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

    checkFactory = new CheckFactory(activeRules);
    createSensor().execute(context);
    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(2);
}
 
Example #18
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 #19
Source File: ScssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 #20
Source File: ScssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #21
Source File: LessAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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
Source File: LessAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #23
Source File: EmbeddedCssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #24
Source File: EmbeddedCssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #25
Source File: CssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 #26
Source File: CssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #27
Source File: TsLintSensorTest.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.fakePathResolutions = new HashMap<String, String>();
    this.fakePathResolutions.put(TypeScriptPlugin.SETTING_TS_LINT_PATH, "/path/to/tslint");
    this.fakePathResolutions.put(TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH, "/path/to/tslint.json");
    this.fakePathResolutions.put(TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR, "/path/to/rules");

    this.settings = mock(Settings.class);
    when(this.settings.getInt(TypeScriptPlugin.SETTING_TS_LINT_TIMEOUT)).thenReturn(45000);
    when(this.settings.getBoolean(TypeScriptPlugin.SETTING_TS_LINT_ENABLED)).thenReturn(true);
    this.executor = mock(TsLintExecutor.class);
    this.parser = mock(TsLintParser.class);

    this.resolver = mock(PathResolver.class);
    this.sensor = spy(new TsLintSensor(settings, this.resolver, this.executor, this.parser));

    this.file = new DefaultInputFile("", "path/to/file")
                    .setLanguage(TypeScriptLanguage.LANGUAGE_KEY)
                    .setLines(1)
                    .setLastValidOffset(999)
                    .setOriginalLineOffsets(new int[] { 5 });

    this.typeDefFile = new DefaultInputFile("", "path/to/file.d.ts")
                    .setLanguage(TypeScriptLanguage.LANGUAGE_KEY)
                    .setLines(1)
                    .setLastValidOffset(999)
                    .setOriginalLineOffsets(new int[] { 5 });

    this.context = SensorContextTester.create(new File(""));
    this.context.fileSystem().add(this.file);
    this.context.fileSystem().add(this.typeDefFile);

    ActiveRulesBuilder rulesBuilder = new ActiveRulesBuilder();
    rulesBuilder.create(RuleKey.of(TsRulesDefinition.REPOSITORY_NAME, "rule name")).activate();

    this.context.setActiveRules(rulesBuilder.build());

    // Pretend all paths are absolute
    Answer<String> lookUpFakePath = new Answer<String>() {
        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return fakePathResolutions.get(invocation.<String>getArgument(1));
        }
    };

    doAnswer(lookUpFakePath).when(this.resolver).getPath(any(SensorContext.class), any(String.class), (String) any());

    this.configCaptor = ArgumentCaptor.forClass(TsLintExecutorConfig.class);
}