Java Code Examples for org.sonar.api.profiles.RulesProfile#create()

The following examples show how to use org.sonar.api.profiles.RulesProfile#create() . 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: 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 3
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void singleCheckstyleRulesToExport() {
    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);
    profile.activateRule(
            Rule.create("checkstyle",
                    "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
                    "Javadoc").setConfigKey("Checker/JavadocPackage"), RulePriority.MAJOR);
    profile.activateRule(
            Rule.create(
                    "checkstyle",
                    "com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck",
                    "Local Variable").setConfigKey(
                    "Checker/TreeWalker/Checker/TreeWalker/LocalFinalVariableName"),
            RulePriority.MINOR);

    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "singleCheckstyleRulesToExport.xml", sanitizeForTests(writer.toString()));
}
 
Example 4
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void addTheIdPropertyWhenManyInstancesWithTheSameConfigKey() {
    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    final Rule rule1 = Rule.create("checkstyle",
            "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
            .setConfigKey("Checker/JavadocPackage");
    final Rule rule2 = Rule
            .create("checkstyle",
                    "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345",
                    "Javadoc").setConfigKey("Checker/JavadocPackage").setParent(rule1);

    profile.activateRule(rule1, RulePriority.MAJOR);
    profile.activateRule(rule2, RulePriority.CRITICAL);

    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "addTheIdPropertyWhenManyInstancesWithTheSameConfigKey.xml",
            sanitizeForTests(writer.toString()));
}
 
Example 5
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void exportParameters() {
    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    final Rule rule = Rule.create("checkstyle",
            "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
            .setConfigKey("Checker/JavadocPackage");
    rule.createParameter("format");
    // not set in the profile and no default value => not exported in
    // checkstyle
    rule.createParameter("message");
    rule.createParameter("ignore");

    profile.activateRule(rule, RulePriority.MAJOR).setParameter("format", "abcde");

    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "exportParameters.xml", sanitizeForTests(writer.toString()));
}
 
Example 6
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void addCustomCheckerFilters() {
    initSettings(CheckstyleConstants.CHECKER_FILTERS_KEY,
            "<module name=\"SuppressionCommentFilter\">"
                    + "<property name=\"offCommentFormat\" value=\"BEGIN GENERATED CODE\"/>"
                    + "<property name=\"onCommentFormat\" value=\"END GENERATED CODE\"/>"
                    + "</module>" + "<module name=\"SuppressWithNearbyCommentFilter\">"
                    + "<property name=\"commentFormat\""
                    + " value=\"CHECKSTYLE IGNORE (\\w+) FOR NEXT (\\d+) LINES\"/>"
                    + "<property name=\"checkFormat\" value=\"$1\"/>"
                    + "<property name=\"messageFormat\" value=\"$2\"/>" + "</module>");

    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "addCustomFilters.xml", sanitizeForTests(writer.toString()));
}
 
Example 7
Source File: RubyRuleProfile.java    From sonar-ruby-plugin with MIT License 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages validationMessages) {
    RulesProfile profile = RulesProfile.create("Rubocop", Ruby.LANGUAGE_KEY);

    RubyRulesDefinition rules = new RubyRulesDefinition();

    activateRule(profile, RubyRulesDefinition.RUBY_LINT_UNKNOWN_RULE.key);

    for (RubocopRule coreRule : rules.getCoreRules()) {
        activateRule(profile, coreRule.key);
    }

    return profile;
}
 
Example 8
Source File: CheckstyleProfileImporter.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
    final SMInputFactory inputFactory = initStax();
    final RulesProfile profile = RulesProfile.create();
    try {
        final Module checkerModule = loadModule(inputFactory.rootElementCursor(reader)
                .advance());

        for (Module rootModule : checkerModule.modules) {
            final Map<String, String> rootModuleProperties = new HashMap<>(
                    checkerModule.properties);
            rootModuleProperties.putAll(rootModule.properties);

            if (StringUtils.equals(TREEWALKER_MODULE, rootModule.name)) {
                processTreewalker(profile, rootModule, rootModuleProperties, messages);
            }
            else {
                processModule(profile, CHECKER_MODULE + "/", rootModule.name,
                        rootModuleProperties, messages);
            }
        }

    }
    catch (XMLStreamException ex) {
        final String message = "XML is not valid: " + ex.getMessage();
        LOG.error(message, ex);
        messages.addErrorText(message);
    }
    return profile;
}
 
Example 9
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void alwaysSetSuppressionCommentFilter() {
    final RulesProfile profile = RulesProfile.create("sonar way", "java");

    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "alwaysSetSuppressionCommentFilter.xml",
            sanitizeForTests(writer.toString()));
}
 
Example 10
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void noCheckstyleRulesToExport() {
    final RulesProfile profile = RulesProfile.create("sonar way", "java");

    // this is a PMD rule
    profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);

    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "noCheckstyleRulesToExport.xml", sanitizeForTests(writer.toString()));
}
 
Example 11
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void ruleThrowsException() {
    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    try {
        new CheckstyleProfileExporter(settings).exportProfile(profile, new IoExceptionWriter());
        Assert.fail("IOException while writing should not be ignored");
    }
    catch (IllegalStateException ex) {
        Assertions.assertThat(ex.getMessage())
                .isEqualTo("Fail to export the profile " + profile);
    }
}
 
Example 12
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void addCustomTreewalkerFilters() {
    initSettings(CheckstyleConstants.TREEWALKER_FILTERS_KEY,
            "<module name=\"SuppressWithNearbyCommentFilter\"/>");

    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "addCustomTreewalkerFilters.xml", sanitizeForTests(writer.toString()));
}
 
Example 13
Source File: CheckstyleProfileExporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void addTabWidthProperty() {
    initSettings(CheckstyleConstants.CHECKER_TAB_WIDTH, "8");

    final RulesProfile profile = RulesProfile.create("sonar way", "java");
    final StringWriter writer = new StringWriter();
    new CheckstyleProfileExporter(settings).exportProfile(profile, writer);

    CheckstyleTestUtils.assertSimilarXmlWithResource(
            "/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
                    + "addTabWidthProperty.xml", sanitizeForTests(writer.toString()));
}
 
Example 14
Source File: CheckstyleConfigurationTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void getCheckstyleConfiguration() throws Exception {
    fileSystem.setEncoding(StandardCharsets.UTF_8);
    final MapSettings mapSettings = new MapSettings(new PropertyDefinitions(
            new CheckstylePlugin().getExtensions()));
    mapSettings.setProperty(CheckstyleConstants.CHECKER_FILTERS_KEY,
            CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE);
    final org.sonar.api.config.Configuration settings = new ConfigurationBridge(mapSettings);

    final RulesProfile profile = RulesProfile.create("sonar way", "java");

    final Rule rule = Rule.create("checkstyle", "CheckStyleRule1", "checkstyle rule one");
    rule.setConfigKey("checkstyle/rule1");
    profile.activateRule(rule, null);

    final CheckstyleConfiguration configuration = new CheckstyleConfiguration(settings,
            new CheckstyleProfileExporter(settings),
            new DefaultActiveRules(Collections.emptyList()), fileSystem);
    final Configuration checkstyleConfiguration = configuration.getCheckstyleConfiguration();
    assertThat(checkstyleConfiguration).isNotNull();
    assertThat(checkstyleConfiguration.getAttribute("charset")).isEqualTo("UTF-8");
    final File xmlFile = new File("checkstyle.xml");
    assertThat(xmlFile.exists()).isTrue();

    FileUtils.forceDelete(xmlFile);
}
 
Example 15
Source File: TypeScriptRuleProfile.java    From SonarTsPlugin with MIT License 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages validation) {
    RulesProfile profile = RulesProfile.create("TsLint", TypeScriptLanguage.LANGUAGE_KEY);

    TsRulesDefinition rules = new TsRulesDefinition();

    activateRule(profile, TsRulesDefinition.TSLINT_UNKNOWN_RULE.key);

    for (TsLintRule coreRule : rules.getCoreRules()) {
        activateRule(profile, coreRule.key);
    }

    return profile;
}