/** * Every rules and active rules has to be added in builders before creating IssuePublisher */ private void initModuleIssues() { moduleIssues = new IssuePublisher(activeRulesBuilder.build(), filters, reportPublisher); }
private static ActiveRules load(ActiveRulesLoader loader, QualityProfiles qProfiles) { Collection<String> qProfileKeys = getKeys(qProfiles); Set<RuleKey> loadedRulesKey = new HashSet<>(); ActiveRulesBuilder builder = new ActiveRulesBuilder(); for (String qProfileKey : qProfileKeys) { Collection<LoadedActiveRule> qProfileRules = load(loader, qProfileKey); for (LoadedActiveRule r : qProfileRules) { if (!loadedRulesKey.contains(r.getRuleKey())) { loadedRulesKey.add(r.getRuleKey()); builder.addRule(transform(r, qProfileKey)); } } } return builder.build(); }
private SensorContextTester(Path moduleBaseDir) { this.settings = new MapSettings(); this.fs = new DefaultFileSystem(moduleBaseDir).setEncoding(Charset.defaultCharset()); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.project = new DefaultInputProject(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.runtime = SonarRuntimeImpl.forSonarQube(ApiVersion.load(System2.INSTANCE), SonarQubeSide.SCANNER); }
@Test public void no_rules() { ActiveRulesBuilder builder = new ActiveRulesBuilder(); ActiveRules rules = builder.build(); assertThat(rules.findAll()).isEmpty(); }
@Test public void no_checks_are_enabled() { CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithoutProperties.class); assertThat(checks.all()).isEmpty(); }
@Before public void prepare() throws Exception { activeRules = new ActiveRulesBuilder().build(); fs = new DefaultFileSystem(temp.newFolder().toPath()); MetricFinder metricFinder = mock(MetricFinder.class); when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC); when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION); settings = new MapSettings(); sensorStorage = mock(SensorStorage.class); analysisMode = mock(AnalysisMode.class); runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER); hierarchy = new DefaultInputModuleHierarchy(new DefaultInputModule(ProjectDefinition.create() .setWorkDir(temp.newFolder()) .setBaseDir(temp.newFolder()).setKey("foo"))); adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, analysisMode, sensorStorage, runtime); }
@Before public void prepare() throws Exception { fs = new DefaultFileSystem(temp.newFolder().toPath()); settings = new MapSettings(); optimizer = new ModuleSensorOptimizer(fs, new ActiveRulesBuilder().build(), settings.asConfig()); }
@Test public void class_name_as_check_key() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithoutProperties"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithoutProperties.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithoutProperties.class); assertThat(checks.all()).containsOnly(check); assertThat(checks.ruleKey(check)).isEqualTo(ruleKey); }
@Test public void param_as_primitive_fields() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("max", "300") .setParam("ignore", "true") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class); assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300); assertThat(((CheckWithPrimitiveProperties) check).isIgnore()).isTrue(); }
@Test public void param_as_string_field() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("pattern", "foo") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithStringProperty.class); assertThat(((CheckWithStringProperty) check).getPattern()).isEqualTo("foo"); }
@Test public void override_field_key() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithOverriddenPropertyKey"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("maximum", "300") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithOverriddenPropertyKey.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithOverriddenPropertyKey.class); assertThat(((CheckWithOverriddenPropertyKey) check).getMax()).isEqualTo(300); }
/** * SONAR-3164 */ @Test public void param_as_inherited_field() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("max", "300") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class); assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300); }
@Test public void use_template_rule_key() { RuleKey ruleKey = RuleKey.of("squid", "S0001_123"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setTemplateRuleKey("S0001") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class); Object check = checks.of(ruleKey); assertThat(check).isInstanceOf(CheckWithKey.class); assertThat(checks.of(ruleKey)).isSameAs(check); assertThat(checks.ruleKey(check)).isEqualTo(ruleKey); assertThat(checks.all()).containsOnly(check); }
@Test public void should_optimize_on_repository() { DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() .createIssuesForRuleRepositories("squid"); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); ActiveRules activeRules = new ActiveRulesBuilder() .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("repo1", "foo")).build()) .build(); optimizer = new ModuleSensorOptimizer(fs, activeRules, settings.asConfig()); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); activeRules = new ActiveRulesBuilder() .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("repo1", "foo")).build()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "rule")).build()) .build(); optimizer = new ModuleSensorOptimizer(fs, activeRules, settings.asConfig()); assertThat(optimizer.shouldExecute(descriptor)).isTrue(); }
@Test public void testActiveRules() { NewActiveRule activeRule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("foo", "bar")) .build(); ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).build(); tester.setActiveRules(activeRules); assertThat(tester.activeRules().findAll()).hasSize(1); }
/** * SONAR-2900 */ @Test public void checks_as_objects() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("pattern", "foo") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); CheckWithStringProperty check = new CheckWithStringProperty(); Checks checks = checkFactory.create("squid").addAnnotatedChecks(check); Object createdCheck = checks.of(ruleKey); assertThat(createdCheck).isSameAs(check); assertThat(((CheckWithStringProperty) createdCheck).getPattern()).isEqualTo("foo"); } }
.setParam("foo", null) .build()) .build();
@Test public void fail_if_field_type_is_not_supported() { thrown.expect(SonarException.class); RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithUnsupportedPropertyType"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("max", "300") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); checkFactory.create("squid").addAnnotatedChecks(CheckWithUnsupportedPropertyType.class); }
@Test public void fail_if_missing_field() { thrown.expect(IllegalStateException.class); thrown.expectMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty"); RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setParam("unknown", "foo") .build(); builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class); }
private SensorContextTester(Path moduleBaseDir) { this.settings = new MapSettings(); this.fs = new DefaultFileSystem(moduleBaseDir).setEncoding(Charset.defaultCharset()); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.project = new DefaultInputProject(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.runtime = SonarRuntimeImpl.forSonarQube(ApiVersion.load(System2.INSTANCE), SonarQubeSide.SCANNER); }