当前位置: 首页>>代码示例>>Java>>正文


Java Rule类代码示例

本文整理汇总了Java中org.sonar.api.rules.Rule的典型用法代码示例。如果您正苦于以下问题:Java Rule类的具体用法?Java Rule怎么用?Java Rule使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Rule类属于org.sonar.api.rules包,在下文中一共展示了Rule类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: load_profile_keys

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Test
public void load_profile_keys() throws Exception {
  ruleFinder = mock(RuleFinder.class);
  when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
    @Override
    public Rule answer(InvocationOnMock iom) throws Throwable {
      String repositoryKey = (String) iom.getArguments()[0];
      String ruleKey = (String) iom.getArguments()[1];
      return Rule.create(repositoryKey, ruleKey, ruleKey);
    }
  });

  RulesProfile profile = RulesProfile.create("profile-name", "lang-key");
  ProfileDefinitionReader definitionReader = new ProfileDefinitionReader(ruleFinder);
  definitionReader.activateRules(profile, "repo-key", "org/sonarsource/analyzer/commons/Sonar_way_profile.json");
  assertThat(profile.getActiveRules()).hasSize(2);
  assertThat(profile.getActiveRule("repo-key", "S100")).isNotNull();
  assertThat(profile.getActiveRule("repo-key", "S110")).isNotNull();
  assertThat(profile.getActiveRule("repo-key", "S123")).isNull();
  assertThat(profile.getActiveRule("repo-key", "S666")).isNull();
}
 
开发者ID:SonarSource,项目名称:sonar-analyzer-commons,代码行数:22,代码来源:ProfileDefinitionReaderTest.java

示例2: fails_with_non_existing_rule_key

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Test
public void fails_with_non_existing_rule_key() throws Exception {
  ruleFinder = mock(RuleFinder.class);
  when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
    @Override
    public Rule answer(InvocationOnMock iom) throws Throwable {
      String repositoryKey = (String) iom.getArguments()[0];
      String ruleKey = (String) iom.getArguments()[1];
      if (ruleKey.equals("S666")) {
        return null;
      }
      return Rule.create(repositoryKey, ruleKey, ruleKey);
    }
  });

  RulesProfile profile = RulesProfile.create("profile-name", "lang-key");
  ProfileDefinitionReader definitionReader = new ProfileDefinitionReader(ruleFinder);

  thrown.expect(IllegalStateException.class);
  thrown.expectMessage("Failed to activate rule with key 'S666'. No corresponding rule found in repository with key 'repo-key'.");

  definitionReader.activateRules(profile, "repo-key", "org/sonarsource/analyzer/commons/Sonar_way_profile_invalid.json");
}
 
开发者ID:SonarSource,项目名称:sonar-analyzer-commons,代码行数:24,代码来源:ProfileDefinitionReaderTest.java

示例3: addError

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Override
public void addError(AuditEvent event) {
    final String ruleKey = getRuleKey(event);
    if (ruleKey != null) {
        final String message = getMessage(event);
        // In Checkstyle 5.5 exceptions are reported as an events from
        // TreeWalker
        if ("com.puppycrawl.tools.checkstyle.TreeWalker".equals(ruleKey)) {
            LOG.warn("{} : {}", event.getFileName(), message);
        }
        initResource(event);
        final Issuable issuable = perspectives.as(Issuable.class, currentResource);
        final Rule rule = ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
        if (rule != null && issuable != null) {
            final IssueBuilder issueBuilder = issuable.newIssueBuilder().ruleKey(rule.ruleKey())
                    .message(message).line(getLineId(event));
            issuable.addIssue(issueBuilder.build());
        }
    }
}
 
开发者ID:checkstyle,项目名称:sonar-checkstyle,代码行数:21,代码来源:CheckstyleAuditListener.java

示例4: addErrorTest

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Test
public void addErrorTest() {
    final Rule rule = setupRule("repo", "key");

    final Issuable issuable = setupIssuable();
    final IssueBuilder issueBuilder = mock(IssueBuilder.class);
    final Issue issue = mock(Issue.class);
    when(issuable.newIssueBuilder()).thenReturn(issueBuilder);
    when(issueBuilder.ruleKey(RuleKey.of("repo", "key"))).thenReturn(issueBuilder);
    when(issueBuilder.message(event.getMessage())).thenReturn(issueBuilder);
    when(issueBuilder.line(event.getLine())).thenReturn(issueBuilder);
    when(issueBuilder.build()).thenReturn(issue);

    addErrorToListener();

    verify(issuable).addIssue(issue);
    verify(issueBuilder).ruleKey(RuleKey.of("repo", "key"));
    verify(issueBuilder).message(event.getMessage());
    verify(issueBuilder).line(event.getLine());
    verify(rule).ruleKey();
}
 
开发者ID:checkstyle,项目名称:sonar-checkstyle,代码行数:22,代码来源:CheckstyleAuditListenerTest.java

示例5: addTheIdPropertyWhenManyInstancesWithTheSameConfigKey

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@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()));
}
 
开发者ID:checkstyle,项目名称:sonar-checkstyle,代码行数:23,代码来源:CheckstyleProfileExporterTest.java

示例6: exportParameters

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@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()));
}
 
开发者ID:checkstyle,项目名称:sonar-checkstyle,代码行数:22,代码来源:CheckstyleProfileExporterTest.java

示例7: getCheckstyleConfiguration

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void getCheckstyleConfiguration() throws Exception {
    fileSystem.setEncoding(StandardCharsets.UTF_8);
    final Settings settings = new Settings(new PropertyDefinitions(
            new CheckstylePlugin().getExtensions()));
    settings.setProperty(CheckstyleConstants.CHECKER_FILTERS_KEY,
            CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE);

    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), profile, 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);
}
 
开发者ID:checkstyle,项目名称:sonar-checkstyle,代码行数:26,代码来源:CheckstyleConfigurationTest.java

示例8: createRule

import org.sonar.api.rules.Rule; //导入依赖的package包/类
public static Rule createRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation, @Nullable TanaguruRuleTags ruleTagsAnnotation) {
  String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
  String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
  String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
  Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
  rule.setDescription(description);
  rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
  rule.setCardinality(ruleAnnotation.cardinality());
  setTags(rule, ruleTagsAnnotation);

  Field[] fields = clazz.getDeclaredFields();
  if (fields != null) {
    for (Field field : fields) {
      addRuleProperty(rule, field);
    }
  }

  return rule;
}
 
开发者ID:Asqatasun,项目名称:Asqatasun-Sonar-Plugin,代码行数:20,代码来源:RuleRepositoryHelper.java

示例9: addRuleProperty

import org.sonar.api.rules.Rule; //导入依赖的package包/类
private static void addRuleProperty(Rule rule, Field field) {
  org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
  if (propertyAnnotation != null) {
    String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
    RuleParam param = rule.createParameter(fieldKey);
    param.setDescription(propertyAnnotation.description());
    param.setDefaultValue(propertyAnnotation.defaultValue());
    if (!StringUtils.isBlank(propertyAnnotation.type())) {
      try {
        param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
      } catch (IllegalArgumentException e) {
        throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
      }
    } else {
      param.setType(guessType(field.getType()).name());
    }
  }
}
 
开发者ID:Asqatasun,项目名称:Asqatasun-Sonar-Plugin,代码行数:19,代码来源:RuleRepositoryHelper.java

示例10: parseEntry

import org.sonar.api.rules.Rule; //导入依赖的package包/类
private void parseEntry(SMInputCursor cursor) throws XMLStreamException {
  if ("s:String".equals(cursor.getLocalName())) {
    String keyValue = cursor.getAttrValue("x:Key");
    if (keyValue != null && keyValue.startsWith(DOTSETTINGS_RULE_PREFIX)) {
      String key = keyValue.substring(DOTSETTINGS_RULE_PREFIX.length());
      int endIndex = key.indexOf('/');
      if (endIndex <= 0) {
        return;
      }
      key = unescapeRuleKey(key.substring(0, endIndex));
      String severity = cursor.getElemStringValue();
      if (PRIORITY.containsKey(severity)) {
        profile.activateRule(Rule.create(configuration.repositoryKey(), key), PRIORITY.get(severity));
      } else if (!DO_NOT_SHOW.equals(severity)) {
        String message = "Skipping rule " + key + " because has an unexpected severity: " + severity;
        LOG.error(message);
        messages.addErrorText(message);
      }
    }
  }
}
 
开发者ID:GregBartlett,项目名称:sonar-resharper,代码行数:22,代码来源:ReSharperProfileImporter.java

示例11: testRulesDefinition

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Test
public void testRulesDefinition() {
  CSharpReSharperProvider.CSharpReSharperRulesDefinition rulesDefinition = new CSharpReSharperProvider.CSharpReSharperRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);

  assertThat(context.repositories()).hasSize(1);
  RulesDefinition.Repository repo = context.repositories().get(0);

  assertThat(repo.language()).isEqualTo("cs");
  assertThat(repo.key()).isEqualTo("resharper-cs");

  List<RulesDefinition.Rule> rules = repo.rules();
  assertThat(rules.size()).isEqualTo(675);
  boolean atLeastOneSqale = false;
  for (RulesDefinition.Rule rule : rules) {
    assertThat(rule.key()).isNotNull();
    assertThat(rule.name()).isNotNull();
    assertThat(rule.htmlDescription()).isNotNull();
    if (!Strings.isNullOrEmpty(rule.debtSubCharacteristic())) {
      atLeastOneSqale = true;
    }
  }
  assertTrue(atLeastOneSqale);
}
 
开发者ID:GregBartlett,项目名称:sonar-resharper,代码行数:26,代码来源:CSharpReSharperProviderTest.java

示例12: testProfileExporter

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Test
public void testProfileExporter() throws Exception {
  RulesProfile profile = RulesProfile.create();
  profile.activateRule(Rule.create(CSharpReSharperProvider.RESHARPER_CONF.repositoryKey(), "key1", "key1 name"), null);
  profile.activateRule(Rule.create(CSharpReSharperProvider.RESHARPER_CONF.repositoryKey(), "key2", "key2 name"), null);
  profile.activateRule(Rule.create(CSharpReSharperProvider.RESHARPER_CONF.repositoryKey(), "key3", "key3 name"), null);
  profile.activateRule(Rule.create(VBNetReSharperProvider.RESHARPER_CONF.repositoryKey(), "key4", "key4 name"), null);

  CSharpReSharperProvider.CSharpReSharperProfileExporter profileExporter = new CSharpReSharperProvider.CSharpReSharperProfileExporter();
  StringWriter writer = new StringWriter();
  profileExporter.exportProfile(profile, writer);
  assertThat(writer.toString().replace("\r", "").replace("\n", "")).isEqualTo("<wpf:ResourceDictionary xml:space=\"preserve\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:s=\"clr-namespace:System;assembly=mscorlib\" xmlns:ss=\"urn:shemas-jetbrains-com:settings-storage-xaml\" xmlns:wpf=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
    "  <s:String x:Key=\"/Default/CodeInspection/Highlighting/InspectionSeverities/=key1/@EntryIndexedValue\">WARNING</s:String>" +
    "  <s:String x:Key=\"/Default/CodeInspection/Highlighting/InspectionSeverities/=key2/@EntryIndexedValue\">WARNING</s:String>" +
    "  <s:String x:Key=\"/Default/CodeInspection/Highlighting/InspectionSeverities/=key3/@EntryIndexedValue\">WARNING</s:String>" +
    "</wpf:ResourceDictionary>");
}
 
开发者ID:GregBartlett,项目名称:sonar-resharper,代码行数:18,代码来源:CSharpReSharperProviderTest.java

示例13: testRulesDefinition

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Test
public void testRulesDefinition() {
  VBNetReSharperProvider.VBNetReSharperRulesDefinition rulesDefinition = new VBNetReSharperProvider.VBNetReSharperRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);

  assertThat(context.repositories()).hasSize(1);
  RulesDefinition.Repository repo = context.repositories().get(0);

  assertThat(repo.language()).isEqualTo("vbnet");
  assertThat(repo.key()).isEqualTo("resharper-vbnet");

  List<RulesDefinition.Rule> rules = repo.rules();
  assertThat(rules.size()).isEqualTo(675);
  boolean atLeastOneSqale = false;
  for (RulesDefinition.Rule rule : rules) {
    assertThat(rule.key()).isNotNull();
    assertThat(rule.name()).isNotNull();
    assertThat(rule.htmlDescription()).isNotNull();
    if (!Strings.isNullOrEmpty(rule.debtSubCharacteristic())) {
      atLeastOneSqale = true;
    }
  }
  assertTrue(atLeastOneSqale);
}
 
开发者ID:GregBartlett,项目名称:sonar-resharper,代码行数:26,代码来源:VBNetReSharperProviderTest.java

示例14: createRules

import org.sonar.api.rules.Rule; //导入依赖的package包/类
@Override
@SuppressFBWarnings("DE_MIGHT_IGNORE")
public List<Rule> createRules()
{
    InputStream input = getClass().getResourceAsStream(rulesRelativeFilePath);
    if (input == null) {
        throw new IllegalStateException("File not found: " + rulesRelativeFilePath);
    }
    try {
        return xmlRuleParser.parse(input);
    }
    finally {
        try {
            input.close();
        }
        catch (IOException e) {
            // ignore
        }
    }
}
 
开发者ID:checkstyle-addons,项目名称:checkstyle-addons,代码行数:21,代码来源:CheckstyleExtensionRepository.java

示例15: generateIssueSummary

import org.sonar.api.rules.Rule; //导入依赖的package包/类
protected String generateIssueSummary(Issue sonarIssue) {
sonarIssue.ruleKey().repository();
RuleI18nManager manager = null;
Rule rule = ruleFinder.findByKey(sonarIssue.ruleKey());
   String str3 = rule.getName();

   StringBuilder summary = new StringBuilder("Sonar Issue");
   summary.append(" - ");
   if (str3 != null)
   {
   	summary.append(str3);
   }
   else
   {
   	summary.append(sonarIssue.ruleKey().rule());
   }
   //summary.append(" - ");
//summary.append(sonarIssue.message());
   return summary.toString();
 }
 
开发者ID:aifraenkel,项目名称:caltec-tools,代码行数:21,代码来源:JiraIssueCreator.java


注:本文中的org.sonar.api.rules.Rule类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。