org.sonar.api.utils.ValidationMessages Java Examples

The following examples show how to use org.sonar.api.utils.ValidationMessages. 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: CheckstyleProfileImporterTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void before() {
    messages = ValidationMessages.create();

    /*
     * The mocked rule finder defines 3 rules :
     *
     * - JavadocCheck with 2 paramters format and ignore, default priority
     * is MAJOR
     * - EqualsHashCodeCheck without parameters, default priority
     * is BLOCKER
     * - MissingOverride with 1 parameter javaFiveCompatibility,
     * default priority is MINOR
     */
    importer = new CheckstyleProfileImporter(newRuleFinder());
}
 
Example #3
Source File: LuaProfileTest.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_create_sonar_way_profile() {
  ValidationMessages validation = ValidationMessages.create();

  RuleFinder ruleFinder = ruleFinder();
  LuaProfile definition = new LuaProfile(ruleFinder);
  RulesProfile profile = definition.createProfile(validation);

  assertThat(profile.getLanguage()).isEqualTo(Lua.KEY);
  assertThat(profile.getName()).isEqualTo(CheckList.SONAR_WAY_PROFILE);
  assertThat(profile.getActiveRulesByRepository(CheckList.REPOSITORY_KEY)).hasSize(15);
  assertThat(validation.hasErrors()).isFalse();
}
 
Example #4
Source File: LuaProfile.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages validation) {
  AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
  return annotationBasedProfileBuilder.build(
      CheckList.REPOSITORY_KEY,
      CheckList.SONAR_WAY_PROFILE,
      Lua.KEY,
      CheckList.getChecks(),
      validation);
}
 
Example #5
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;
}
 
Example #6
Source File: GherkinProfileTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_create_sonarqube_way_profile() {
  ValidationMessages validation = ValidationMessages.create();
  GherkinProfile definition = new GherkinProfile(universalRuleFinder());
  RulesProfile profile = definition.createProfile(validation);

  assertThat(profile.getName()).isEqualTo("SonarQube Way");
  assertThat(profile.getLanguage()).isEqualTo("gherkin");
  assertThat(profile.getActiveRulesByRepository("gherkin")).hasSize(36);
  assertThat(validation.hasErrors()).isFalse();
}
 
Example #7
Source File: GherkinProfile.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages messages) {
  AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
  return annotationBasedProfileBuilder.build(
    GherkinRulesDefinition.REPOSITORY_KEY,
    SONARQUBE_WAY_PROFILE_NAME,
    GherkinLanguage.KEY,
    GherkinRulesDefinition.getChecks(),
    messages);
}
 
Example #8
Source File: ApexProfileTest.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
@Test
public void testCreateSonarWayProfile() {
    ValidationMessages validation = ValidationMessages.create();

    RuleFinder ruleFinder = buildRuleFinder();
    ApexProfile definition = new ApexProfile(ruleFinder);
    RulesProfile profile = definition.createProfile(validation);

    assertThat(profile.getLanguage(), equalTo(Apex.KEY));
    assertThat(profile.getName(), equalTo(CheckList.SONAR_WAY_PROFILE));
    assertThat(validation.hasErrors(), is(FALSE));
}
 
Example #9
Source File: ApexProfile.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Returns a {@link RulesProfile} based on a list of classes annotated.
 *
 * @param validation validation message.
 * @return a rule profile.
 */
@Override
public RulesProfile createProfile(ValidationMessages validation) {
    AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
    return annotationBasedProfileBuilder.build(
            CheckList.REPOSITORY_KEY,
            CheckList.SONAR_WAY_PROFILE,
            Apex.KEY,
            CheckList.getChecks(),
            validation);
}
 
Example #10
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 #11
Source File: CheckstyleProfileImporter.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processModule(RulesProfile profile, String path, String moduleName,
        Map<String, String> properties, ValidationMessages messages) {
    if (isFilter(moduleName)) {
        messages.addWarningText("Checkstyle filters are not imported: " + moduleName);

    }
    else if (!isIgnored(moduleName)) {
        processRule(profile, path, moduleName, properties, messages);
    }
}
 
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 processTreewalker(RulesProfile profile, Module rootModule,
        Map<String, String> rootModuleProperties, ValidationMessages messages) {
    for (Module treewalkerModule : rootModule.modules) {
        final Map<String, String> treewalkerModuleProperties = new HashMap<>(
                rootModuleProperties);
        treewalkerModuleProperties.putAll(treewalkerModule.properties);

        processModule(profile, CHECKER_MODULE + "/" + TREEWALKER_MODULE + "/",
                treewalkerModule.name, treewalkerModuleProperties, messages);
    }
}
 
Example #13
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 #14
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 #15
Source File: ScssProfileTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_create_sonarqube_way_profile() {
  ValidationMessages validation = ValidationMessages.create();
  ScssProfile definition = new ScssProfile(universalRuleFinder());
  RulesProfile profile = definition.createProfile(validation);

  assertThat(profile.getName()).isEqualTo("SonarQube Way");
  assertThat(profile.getLanguage()).isEqualTo("scss");
  assertThat(profile.getActiveRulesByRepository("scss")).hasSize(84);
  assertThat(validation.hasErrors()).isFalse();
}
 
Example #16
Source File: LessProfileTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_create_sonarqube_way_profile() {
  ValidationMessages validation = ValidationMessages.create();
  LessProfile definition = new LessProfile(universalRuleFinder());
  RulesProfile profile = definition.createProfile(validation);

  assertThat(profile.getName()).isEqualTo("SonarQube Way");
  assertThat(profile.getLanguage()).isEqualTo("less");
  assertThat(profile.getActiveRulesByRepository("less")).hasSize(73);
  assertThat(validation.hasErrors()).isFalse();
}
 
Example #17
Source File: CssProfileTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_create_sonarqube_way_profile() {
  ValidationMessages validation = ValidationMessages.create();
  CssProfile definition = new CssProfile(universalRuleFinder());
  RulesProfile profile = definition.createProfile(validation);

  assertThat(profile.getName()).isEqualTo("SonarQube Way");
  assertThat(profile.getLanguage()).isEqualTo("css");
  assertThat(profile.getActiveRulesByRepository("css")).hasSize(73);
  assertThat(validation.hasErrors()).isFalse();
}
 
Example #18
Source File: ScssProfile.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages messages) {
  AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
  return annotationBasedProfileBuilder.build(
    CheckList.SCSS_REPOSITORY_KEY,
    SONARQUBE_WAY_PROFILE_NAME,
    ScssLanguage.KEY,
    CheckList.getScssChecks(),
    messages);
}
 
Example #19
Source File: LessProfile.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages messages) {
  AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
  return annotationBasedProfileBuilder.build(
    CheckList.LESS_REPOSITORY_KEY,
    SONARQUBE_WAY_PROFILE_NAME,
    LessLanguage.KEY,
    CheckList.getLessChecks(),
    messages);
}
 
Example #20
Source File: CssProfile.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages messages) {
  AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
  return annotationBasedProfileBuilder.build(
    CheckList.CSS_REPOSITORY_KEY,
    SONARQUBE_WAY_PROFILE_NAME,
    CssLanguage.KEY,
    CheckList.getCssChecks(),
    messages);
}
 
Example #21
Source File: PmdProfileExporterTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
static RulesProfile importProfile(String configuration) {
  PmdRulesDefinition definition = new PmdRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);
  RuleFinder ruleFinder = createRuleFinder(repository.rules());
  PmdProfileImporter importer = new PmdProfileImporter(ruleFinder);

  return importer.importProfile(new StringReader(configuration), ValidationMessages.create());
}
 
Example #22
Source File: PmdProfileImporter.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
protected PmdRuleset parsePmdRuleset(Reader pmdConfigurationFile, ValidationMessages messages) {
  try {
    SAXBuilder parser = new SAXBuilder();
    Document dom = parser.build(pmdConfigurationFile);
    Element eltResultset = dom.getRootElement();
    Namespace namespace = eltResultset.getNamespace();
    PmdRuleset pmdResultset = new PmdRuleset();
    for (Element eltRule : getChildren(eltResultset, "rule", namespace)) {
      PmdRule pmdRule = new PmdRule(eltRule.getAttributeValue("ref"));
      pmdRule.setClazz(eltRule.getAttributeValue("class"));
      pmdRule.setName(eltRule.getAttributeValue("name"));
      pmdRule.setMessage(eltRule.getAttributeValue("message"));
      parsePmdPriority(eltRule, pmdRule, namespace);
      parsePmdProperties(eltRule, pmdRule, namespace);
      pmdResultset.addRule(pmdRule);
    }
    return pmdResultset;
  } catch (Exception e) {
    String errorMessage = "The PMD configuration file is not valid";
    messages.addErrorText(errorMessage + " : " + e.getMessage());
    LOG.error(errorMessage, e);
    return new PmdRuleset();
  }
}
 
Example #23
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 #24
Source File: ColdFusionSonarWayProfileImporter.java    From sonar-coldfusion with Apache License 2.0 4 votes vote down vote up
@Override
public RulesProfile createProfile(ValidationMessages validation) {
    return xmlParser.parse(new InputStreamReader(getClass().getResourceAsStream(DEFAULT_PROFILE_PATH), Charsets.UTF_8), validation);
}
 
Example #25
Source File: PmdProfileImporterTest.java    From sonar-p3c-pmd with MIT License 4 votes vote down vote up
@Before
public void setUpImporter() {
  messages = ValidationMessages.create();
  importer = new PmdProfileImporter(createRuleFinder());
}
 
Example #26
Source File: PmdProfileImporter.java    From sonar-p3c-pmd with MIT License 4 votes vote down vote up
@Override
public RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages) {
  PmdRuleset pmdRuleset = parsePmdRuleset(pmdConfigurationFile, messages);
  return createRuleProfile(pmdRuleset, messages);
}