@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(); }
.setInternalKey(rule.internalKey()); for (Param param : rule.params()) { newAr.setParam(param.key(), param.defaultValue());
private static void createNewActiveRule(ActiveRulesBuilder builder, ActiveRule activeRule, Sonarlint.Rules storageRules, String language, Rules rules) { RuleKey ruleKey = RuleKey.of(activeRule.getRepo(), activeRule.getKey()); Rule rule = rules.find(ruleKey); Sonarlint.Rules.Rule storageRule; try { storageRule = storageRules.getRulesByKeyOrThrow(ruleKey.toString()); } catch (IllegalArgumentException e) { throw new MessageException("Unknown active rule in the quality profile of the project. Please update the SonarQube server binding."); } Builder newActiveRule = new NewActiveRule.Builder() .setRuleKey(ruleKey) .setLanguage(language) .setName(rule.name()) .setInternalKey(rule.internalKey()) .setSeverity(activeRule.getSeverity()); if (!StringUtils.isEmpty(storageRule.getTemplateKey())) { RuleKey templateRuleKey = RuleKey.parse(storageRule.getTemplateKey()); newActiveRule.setTemplateRuleKey(templateRuleKey.rule()); } for (Map.Entry<String, String> param : activeRule.getParamsMap().entrySet()) { newActiveRule.setParam(param.getKey(), param.getValue()); } builder.addRule(newActiveRule.build()); }
private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey) { NewActiveRule.Builder builder = new NewActiveRule.Builder(); builder .setRuleKey(activeRule.getRuleKey()) .setName(activeRule.getName()) .setSeverity(activeRule.getSeverity()) .setCreatedAt(activeRule.getCreatedAt()) .setUpdatedAt(activeRule.getUpdatedAt()) .setLanguage(activeRule.getLanguage()) .setInternalKey(activeRule.getInternalKey()) .setTemplateRuleKey(activeRule.getTemplateRuleKey()) .setQProfileKey(qProfileKey); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { builder.setParam(params.getKey(), params.getValue()); } } return builder.build(); }
private static void registerProfile(ActiveRulesBuilder builder, String language, Map.Entry<String, Collection<RulesProfile>> entry) { for (RulesProfile rp : entry.getValue()) { for (ActiveRule ar : rp.getActiveRules()) { Builder newAr = new NewActiveRule.Builder() .setRuleKey(RuleKey.of(ar.getRepositoryKey(), ar.getRuleKey())) .setLanguage(language) .setName(ar.getRule().getName()) .setSeverity(ar.getSeverity().name()) .setInternalKey(ar.getConfigKey()); for (ActiveRuleParam param : ar.getActiveRuleParams()) { newAr.setParam(param.getKey(), param.getValue()); } builder.addRule(newAr.build()); } } }
private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey) { NewActiveRule.Builder builder = new NewActiveRule.Builder(); builder .setRuleKey(activeRule.getRuleKey()) .setName(activeRule.getName()) .setSeverity(activeRule.getSeverity()) .setCreatedAt(activeRule.getCreatedAt()) .setUpdatedAt(activeRule.getUpdatedAt()) .setLanguage(activeRule.getLanguage()) .setInternalKey(activeRule.getInternalKey()) .setTemplateRuleKey(activeRule.getTemplateRuleKey()) .setQProfileKey(qProfileKey); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { builder.setParam(params.getKey(), params.getValue()); } } return builder.build(); }
/** * 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"); } }
/** * 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 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); }
@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 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); }
@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); }