Exemplo n.º 1
0
 @Test
 public void shouldCreateProfile() {
   ProfileDefinition sonarWay =
       new SonarWayProfile(new XMLProfileParser(newRuleFinder(), mock(MetricFinder.class)));
   ValidationMessages validation = ValidationMessages.create();
   RulesProfile profile = sonarWay.createProfile(validation);
   assertThat(profile.getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY).size())
       .isEqualTo(32);
   assertThat(validation.hasErrors()).isFalse();
 }
Exemplo n.º 2
0
  @Test
  public void should_create_sonar_way_profile() {
    ValidationMessages validation = ValidationMessages.create();

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

    assertThat(profile.getLanguage()).isEqualTo(Xml.KEY);
    assertThat(profile.getName()).isEqualTo(RulesProfile.SONAR_WAY_NAME);
    assertThat(profile.getActiveRulesByRepository(CheckRepository.REPOSITORY_KEY)).hasSize(4);
    assertThat(validation.hasErrors()).isFalse();
  }
  @Test
  public void importSimpleProfile() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent(
                "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    assertThat(profile.getActiveRules().size()).isEqualTo(2);
    assertNotNull(
        profile.getActiveRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode"));
    assertNotNull(profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage"));
    assertThat(messages.hasErrors()).isFalse();
  }
  @Test
  public void shouldCreateDefaultProfile() {
    ValidationMessages validation = ValidationMessages.create();

    RuleFinder ruleFinder = ruleFinder();
    CxxDefaultProfile definition =
        new CxxDefaultProfile(
            new XMLProfileParser(ruleFinder), new AnnotationProfileParser(ruleFinder));
    RulesProfile profile = definition.createProfile(validation);

    assertThat(profile.getLanguage()).isEqualTo(CxxLanguage.KEY);
    assertThat(profile.getActiveRulesByRepository("valgrind")).hasSize(15);
    assertThat(validation.hasErrors()).isFalse();
  }
  @Test
  public void testImportingSimpleProfile() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent("/org/sonar/plugins/php/pmd/simple-ruleset.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    assertThat(profile.getActiveRules().size(), is(3));
    assertNotNull(
        profile.getActiveRuleByConfigKey(
            REPOSITORY_KEY, "rulesets/codesize.xml/CyclomaticComplexity"));
    assertNotNull(
        profile.getActiveRuleByConfigKey(REPOSITORY_KEY, "rulesets/codesize.xml/NPathComplexity"));
    assertThat(messages.hasErrors(), is(false));
  }
Exemplo n.º 6
0
 public ValidationMessages restoreProfile(String xmlBackup) {
   ValidationMessages messages = ValidationMessages.create();
   RulesProfile profile = xmlProfileParser.parse(new StringReader(xmlBackup), messages);
   if (profile != null) {
     DatabaseSession session = sessionFactory.getSession();
     RulesProfile existingProfile =
         session.getSingleResult(
             RulesProfile.class, "name", profile.getName(), "language", profile.getLanguage());
     if (existingProfile != null) {
       messages.addErrorText(
           "The profile " + profile + " already exists. Please delete it before restoring.");
     } else if (!messages.hasErrors()) {
       session.saveWithoutFlush(profile);
       session.commit();
     }
   }
   return messages;
 }
Exemplo n.º 7
0
 /** Important : the ruby controller has already create the profile */
 public ValidationMessages importProfile(
     String profileName, String language, String importerKey, String profileDefinition) {
   ValidationMessages messages = ValidationMessages.create();
   ProfileImporter importer = getProfileImporter(importerKey);
   RulesProfile profile = importer.importProfile(new StringReader(profileDefinition), messages);
   if (!messages.hasErrors()) {
     DatabaseSession session = sessionFactory.getSession();
     RulesProfile persistedProfile =
         session.getSingleResult(RulesProfile.class, "name", profileName, "language", language);
     for (ActiveRule activeRule : profile.getActiveRules()) {
       ActiveRule persistedActiveRule =
           persistedProfile.activateRule(activeRule.getRule(), activeRule.getSeverity());
       for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
         persistedActiveRule.setParameter(activeRuleParam.getKey(), activeRuleParam.getValue());
       }
     }
     session.saveWithoutFlush(persistedProfile);
     session.commit();
   }
   return messages;
 }