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

The following examples show how to use org.sonar.api.profiles.RulesProfile#activateRule() . 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: 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 6
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 7
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 8
Source File: TSQLQualityProfile.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
private void activeRules(final RulesProfile profile, final String key, final InputStream file) {
	try {

		final JAXBContext jaxbContext = JAXBContext.newInstance(TSQLRules.class);
		final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
		final TSQLRules issues = (TSQLRules) jaxbUnmarshaller.unmarshal(file);
		for (final org.sonar.plugins.tsql.languages.TSQLRules.Rule rule : issues.rule) {
			profile.activateRule(Rule.create(key, rule.getKey()), null);
		}
	}

	catch (final Throwable e) {
		LOGGER.warn("Unexpected error occured while reading rules for " + key, e);
	}
}
 
Example 9
Source File: TSQLQualityProfile.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
private void activePluginRules(final RulesProfile profile) {
	try {
		final SqlRules rules = customPluginChecks.getRules();
		for (final org.sonar.plugins.tsql.checks.custom.Rule rule : rules.getRule()) {
			profile.activateRule(Rule.create(rules.getRepoKey(), rule.getKey()), null);
		}
	}

	catch (final Throwable e) {
		LOGGER.warn("Unexpected error occured while activating custom plugin rules", e);
	}
}
 
Example 10
Source File: RubyRuleProfile.java    From sonar-ruby-plugin with MIT License 4 votes vote down vote up
private static void activateRule(RulesProfile profile, String ruleKey) {
    profile.activateRule(Rule.create("rubocop", ruleKey), null);
}
 
Example 11
Source File: TypeScriptRuleProfile.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
private static void activateRule(RulesProfile profile, String ruleKey) {
    profile.activateRule(Rule.create(TsRulesDefinition.REPOSITORY_NAME, ruleKey), null);
}