org.sonar.api.rules.ActiveRule Java Examples

The following examples show how to use org.sonar.api.rules.ActiveRule. 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: PmdProfileImporter.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) {
  RulesProfile profile = RulesProfile.create();
  for (PmdRule pmdRule : pmdRuleset.getPmdRules()) {
    String ruleClassName = pmdRule.getClazz();
    if (PmdConstants.XPATH_CLASS.equals(ruleClassName)) {
      messages.addWarningText("PMD XPath rule '" + pmdRule.getName()
        + "' can't be imported automatically. The rule must be created manually through the SonarQube web interface.");
    } else {
      String ruleRef = pmdRule.getRef();
      if (ruleRef == null) {
        messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + ruleClassName + "'");
      } else {
        Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(ruleRef));
        if (rule != null) {
          ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority()));
          setParameters(activeRule, pmdRule, rule, messages);
        } else {
          messages.addWarningText("Unable to import unknown PMD rule '" + ruleRef + "'");
        }
      }
    }
  }
  return profile;
}
 
Example #2
Source File: PmdProfileExporter.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
private PmdRuleset createPmdRuleset(String repositoryKey, List<ActiveRule> activeRules, String profileName) {
  PmdRuleset ruleset = new PmdRuleset(profileName);
  for (ActiveRule activeRule : activeRules) {
    if (activeRule.getRule().getRepositoryKey().equals(repositoryKey)) {
      String configKey = activeRule.getRule().getConfigKey();
      PmdRule rule = new PmdRule(configKey, PmdLevelUtils.toLevel(activeRule.getSeverity()));
      if ((activeRule.getActiveRuleParams() != null) && !activeRule.getActiveRuleParams().isEmpty()) {
        List<PmdProperty> properties = new ArrayList<>();
        for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
          properties.add(new PmdProperty(activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue()));
        }
        rule.setProperties(properties);
      }
      ruleset.addRule(rule);
      processXPathRule(activeRule.getRuleKey(), rule);
    }
  }
  return ruleset;
}
 
Example #3
Source File: PmdProfileExporterTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
@Test
public void should_export_xPath_rule() {
  Rule rule = Rule.create(PmdConstants.REPOSITORY_KEY, "MyOwnRule", "This is my own xpath rule.")
    .setConfigKey(PmdConstants.XPATH_CLASS)
    .setRepositoryKey(PmdConstants.REPOSITORY_KEY);
  rule.createParameter(PmdConstants.XPATH_EXPRESSION_PARAM);
  rule.createParameter(PmdConstants.XPATH_MESSAGE_PARAM);

  RulesProfile profile = RulesProfile.create();
  ActiveRule xpath = profile.activateRule(rule, null);
  xpath.setParameter(PmdConstants.XPATH_EXPRESSION_PARAM, "//FieldDeclaration");
  xpath.setParameter(PmdConstants.XPATH_MESSAGE_PARAM, "This is bad");

  String exportedXml = exporter.exportProfile(PmdConstants.REPOSITORY_KEY, profile);

  assertThat(exportedXml).satisfies(equalsIgnoreEOL(PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/export_xpath_rules.xml")));
}
 
Example #4
Source File: CheckstyleProfileExporter.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #5
Source File: CheckstyleProfileExporter.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void exportProfile(RulesProfile profile, Writer writer) {
    try {
        final List<ActiveRule> activeRules = profile
                .getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY);
        if (activeRules != null) {
            final List<ActiveRuleWrapper> activeRuleWrappers = new ArrayList<>();
            for (ActiveRule activeRule : activeRules) {
                activeRuleWrappers.add(new ActiveRuleWrapperServerImpl(activeRule));
            }
            final Map<String, List<ActiveRuleWrapper>> activeRulesByConfigKey =
                    arrangeByConfigKey(activeRuleWrappers);
            generateXml(writer, activeRulesByConfigKey);
        }
    }
    catch (IOException ex) {
        throw new IllegalStateException("Fail to export the profile " + profile, ex);
    }
}
 
Example #6
Source File: TypeScriptRuleProfileTest.java    From SonarTsPlugin with MIT License 5 votes vote down vote up
@Test
public void definesUnexpectedRules() {
    RulesProfile profile = this.ruleProfile.createProfile(this.validationMessages);

    for (ActiveRule rule : profile.getActiveRules()) {
        assertTrue("Unexpected rule in plugin: " + rule.getRuleKey(), this.expectedRuleNames.contains(rule.getRuleKey()));
    }
}
 
Example #7
Source File: CheckstyleProfileImporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void priorityIsOptional() {
    final Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent(
            "/org/sonar/plugins/checkstyle/"
                    + "CheckstyleProfileImporterTest/simple.xml"));
    final RulesProfile profile = importer.importProfile(reader, messages);

    final ActiveRule activeRule = profile.getActiveRuleByConfigKey("checkstyle",
            "Checker/TreeWalker/EqualsHashCode");
    // reuse the rule default priority
    assertThat(activeRule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
}
 
Example #8
Source File: CheckstyleProfileImporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void importPriorities() {
    final Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml"));
    final RulesProfile profile = importer.importProfile(reader, messages);

    final ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle",
            "Checker/JavadocPackage");
    assertThat(javadocCheck.getSeverity()).isEqualTo(RulePriority.BLOCKER);
}
 
Example #9
Source File: CheckstyleProfileImporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void propertiesShouldBeInherited() {
    final Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/"
            + "inheritance_of_properties.xml"));
    final RulesProfile profile = importer.importProfile(reader, messages);

    final ActiveRule activeRule = profile.getActiveRuleByConfigKey("checkstyle",
            "Checker/TreeWalker/MissingOverride");
    assertThat(activeRule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
    assertThat(activeRule.getParameter("javaFiveCompatibility")).isEqualTo("true");
}
 
Example #10
Source File: CheckstyleProfileImporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void importParameters() {
    final Reader reader = new StringReader(CheckstyleTestUtils.getResourceContent(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml"));
    final RulesProfile profile = importer.importProfile(reader, messages);

    final ActiveRule javadocCheck = profile.getActiveRuleByConfigKey("checkstyle",
            "Checker/JavadocPackage");
    assertThat(javadocCheck.getActiveRuleParams()).hasSize(2);
    assertThat(javadocCheck.getParameter("format")).isEqualTo("abcde");
    assertThat(javadocCheck.getParameter("ignore")).isEqualTo("true");
    // checkstyle internal parameter
    assertThat(javadocCheck.getParameter("severity")).isNull();
}
 
Example #11
Source File: CheckstyleProfileImporter.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void activateProperties(ActiveRule activeRule, Map<String, String> properties) {
    for (Map.Entry<String, String> property : properties.entrySet()) {
        if (StringUtils.equals("severity", property.getKey())) {
            activeRule.setSeverity(CheckstyleSeverityUtils.fromSeverity(property.getValue()));

        }
        else if (!StringUtils.equals("id", property.getKey())) {
            activeRule.setParameter(property.getKey(), property.getValue());
        }
    }
}
 
Example #12
Source File: CheckstyleProfileImporter.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processRule(RulesProfile profile, String path, String moduleName,
        Map<String, String> properties, ValidationMessages messages) {
    final Rule rule;
    final String id = properties.get("id");
    final String warning;
    if (StringUtils.isNotBlank(id)) {
        rule = ruleFinder.find(RuleQuery.create()
                .withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withKey(id));
        warning = "Checkstyle rule with key '" + id + "' not found";

    }
    else {
        final String configKey = path + moduleName;
        rule = ruleFinder
                .find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY)
                        .withConfigKey(configKey));
        warning = "Checkstyle rule with config key '" + configKey + "' not found";
    }

    if (rule == null) {
        messages.addWarningText(warning);

    }
    else {
        final ActiveRule activeRule = profile.activateRule(rule, null);
        activateProperties(activeRule, properties);
    }
}
 
Example #13
Source File: PmdProfileImporterTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_deal_with_unsupported_property() {
  Reader reader = read("/org/sonar/plugins/pmd/simple.xml");

  RulesProfile profile = importer.importProfile(reader, messages);
  ActiveRule check = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/CouplingBetweenObjects");

  assertThat(check.getParameter("threshold")).isNull();
  assertThat(messages.getWarnings()).hasSize(2);
}
 
Example #14
Source File: PmdProfileImporterTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_import_priority() {
  Reader reader = read("/org/sonar/plugins/pmd/simple.xml");

  RulesProfile profile = importer.importProfile(reader, messages);

  ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/design.xml/UseNotifyAllInsteadOfNotify");
  assertThat(activeRule.getSeverity()).isSameAs(RulePriority.MINOR);

  activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/CouplingBetweenObjects");
  assertThat(activeRule.getSeverity()).isSameAs(RulePriority.CRITICAL);
}
 
Example #15
Source File: PmdProfileImporterTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_import_default_priority() {
  Reader reader = read("/org/sonar/plugins/pmd/simple.xml");

  RulesProfile profile = importer.importProfile(reader, messages);
  ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports");

  assertThat(activeRule.getSeverity()).isSameAs(RulePriority.BLOCKER);
}
 
Example #16
Source File: PmdProfileImporterTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_import_parameter() {
  Reader reader = read("/org/sonar/plugins/pmd/simple.xml");

  RulesProfile profile = importer.importProfile(reader, messages);
  ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports");

  assertThat(activeRule.getParameter("minimum")).isEqualTo("30");
}
 
Example #17
Source File: PmdProfileImporter.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
private static void setParameters(ActiveRule activeRule, PmdRule pmdRule, Rule rule, ValidationMessages messages) {
  for (PmdProperty prop : pmdRule.getProperties()) {
    String paramName = prop.getName();
    if (rule.getParam(paramName) == null) {
      messages.addWarningText("The property '" + paramName + "' is not supported in the pmd rule: " + pmdRule.getRef());
    } else {
      activeRule.setParameter(paramName, prop.getValue());
    }
  }
}
 
Example #18
Source File: ActiveRuleWrapperServerImpl.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ActiveRuleWrapperServerImpl(ActiveRule activeRule) {
    this.activeRule = activeRule;
}