use of org.sonar.api.batch.rule.ActiveRules in project sonarqube by SonarSource.
the class ActiveRulesBuilderTest method build_rules.
@Test
public void build_rules() {
ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S0001")).setName("My Rule").setSeverity(Severity.CRITICAL).setInternalKey("__S0001__").setParam("min", "20").activate().create(RuleKey.of("squid", "S0002")).activate().create(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).activate().build();
assertThat(activeRules.findAll()).hasSize(3);
assertThat(activeRules.findByRepository("squid")).hasSize(2);
assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
assertThat(activeRules.findByRepository("unknown")).isEmpty();
ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
assertThat(squid1.internalKey()).isEqualTo("__S0001__");
assertThat(squid1.params()).hasSize(1);
assertThat(squid1.param("min")).isEqualTo("20");
ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
assertThat(squid2.params()).isEmpty();
ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
assertThat(findbugsRule.internalKey()).isNull();
assertThat(findbugsRule.params()).isEmpty();
}
use of org.sonar.api.batch.rule.ActiveRules in project sonarqube by SonarSource.
the class ActiveRulesBuilderTest method no_rules.
@Test
public void no_rules() {
ActiveRulesBuilder builder = new ActiveRulesBuilder();
ActiveRules rules = builder.build();
assertThat(rules.findAll()).isEmpty();
}
use of org.sonar.api.batch.rule.ActiveRules in project sonar-web by SonarSource.
the class WebSensorTest method setUp.
@Before
public void setUp() throws Exception {
WebRulesDefinition rulesDefinition = new WebRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
RulesDefinition.Repository repository = context.repository(WebRulesDefinition.REPOSITORY_KEY);
List<NewActiveRule> ar = new ArrayList<>();
for (RulesDefinition.Rule rule : repository.rules()) {
ar.add(new ActiveRulesBuilder().create(RuleKey.of(WebRulesDefinition.REPOSITORY_KEY, rule.key())));
}
ActiveRules activeRules = new DefaultActiveRules(ar);
CheckFactory checkFactory = new CheckFactory(activeRules);
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
sensor = new WebSensor(new NoSonarFilter(), fileLinesContextFactory, checkFactory);
tester = SensorContextTester.create(TEST_DIR);
}
use of org.sonar.api.batch.rule.ActiveRules in project sonar-java by SonarSource.
the class SonarComponentsTest method ucfg_activation_should_rely_on_active_rules.
@Test
public void ucfg_activation_should_rely_on_active_rules() {
File file = new File("src/test/files/ParseError.java");
SensorContextTester sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null);
sonarComponents.setRuleRepositoryKey("squid");
sonarComponents.setSensorContext(sensorContext);
// no security rules available
JavaRules.ruleKeys = new HashSet<>();
assertThat(sonarComponents.shouldGenerateUCFG()).isFalse();
ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S3649")).activate().build();
// one security rule available
JavaRules.ruleKeys = new HashSet<>(Arrays.asList("S3649"));
sensorContext.setActiveRules(activeRules);
assertThat(sonarComponents.shouldGenerateUCFG()).isTrue();
}
use of org.sonar.api.batch.rule.ActiveRules in project sonar-web by SonarSource.
the class HtmlSensorTest method setUp.
@Before
public void setUp() {
HtmlRulesDefinition rulesDefinition = new HtmlRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
RulesDefinition.Repository repository = context.repository(HtmlRulesDefinition.REPOSITORY_KEY);
List<NewActiveRule> ar = new ArrayList<>();
for (RulesDefinition.Rule rule : repository.rules()) {
ar.add(new NewActiveRule.Builder().setRuleKey(RuleKey.of(HtmlRulesDefinition.REPOSITORY_KEY, rule.key())).build());
}
ActiveRules activeRules = new DefaultActiveRules(ar);
CheckFactory checkFactory = new CheckFactory(activeRules);
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
final SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 9), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
sensor = new HtmlSensor(sonarRuntime, new NoSonarFilter(), fileLinesContextFactory, checkFactory);
tester = SensorContextTester.create(TEST_DIR).setRuntime(sonarRuntime);
}
Aggregations