Java Code Examples for org.sonar.plugins.java.Java

The following examples show how to use org.sonar.plugins.java.Java. These examples are extracted from open source projects. 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
@SuppressWarnings("rawtypes")
@Override
public void define(final Context context) {
    final NewRepository repository = context.createRepository(REPOSITORY_KEY, Java.KEY)
        .setName(REPOSITORY_NAME);
    final List<Class> checkClasses = ImmutableList.<Class> builder()
        .addAll(SmellChecksRegistrar.checkClasses())
        .build();
    new AnnotationBasedRulesDefinition(repository, Java.KEY).addRuleClasses(false, checkClasses);
    for (final NewRule rule : repository.rules()) {
        final String metadataKey = rule.key();
        // Setting internal key is essential for rule templates (see SONAR-6162), and it is not done by AnnotationBasedRulesDefinition from
        // sslr-squid-bridge version 2.5.1:
        rule.setInternalKey(metadataKey);
        rule.setHtmlDescription(readRuleDefinitionResource(metadataKey + ".html"));
        addMetadata(rule, metadataKey);
    }
    repository.done();
}
 
Example 2
Source Project: sonar-p3c-pmd   Source File: PmdP3CRulesDefinition.java    License: MIT License 5 votes vote down vote up
@Override
public void define(Context context) {
  NewRepository repository = context
    .createRepository(PmdConstants.P3C_REPOSITORY_KEY, Java.KEY)
    .setName(PmdConstants.P3C_REPOSITORY_NAME);

  PmdRulesDefinition.extractRulesData(repository, "/org/sonar/plugins/pmd/rules-p3c.xml", "/org/sonar/l10n/pmd/rules/pmd-p3c");

  repository.done();
}
 
Example 3
Source Project: sonar-p3c-pmd   Source File: PmdRulesDefinition.java    License: MIT License 5 votes vote down vote up
@Override
public void define(Context context) {
  NewRepository repository = context
    .createRepository(PmdConstants.REPOSITORY_KEY, Java.KEY)
    .setName(PmdConstants.REPOSITORY_NAME);

  extractRulesData(repository, "/org/sonar/plugins/pmd/rules.xml", "/org/sonar/l10n/pmd/rules/pmd");
  extractRulesData(repository, "/org/sonar/plugins/pmd/rules-p3c.xml", "/org/sonar/l10n/pmd/rules/pmd-p3c");

  repository.done();
}
 
Example 4
Source Project: sonar-p3c-pmd   Source File: PmdUnitTestsRulesDefinition.java    License: MIT License 5 votes vote down vote up
@Override
public void define(Context context) {
  NewRepository repository = context
    .createRepository(PmdConstants.TEST_REPOSITORY_KEY, Java.KEY)
    .setName(PmdConstants.TEST_REPOSITORY_NAME);

  PmdRulesDefinition.extractRulesData(repository, "/org/sonar/plugins/pmd/rules-unit-tests.xml", "/org/sonar/l10n/pmd/rules/pmd-unit-tests");

  repository.done();
}
 
Example 5
Source Project: sonar-p3c-pmd   Source File: PmdSensor.java    License: MIT License 5 votes vote down vote up
private boolean hasFilesToCheck(Type type, String repositoryKey) {
  FilePredicates predicates = fs.predicates();
  Iterable<File> files = fs.files(predicates.and(
    predicates.hasLanguage(Java.KEY),
    predicates.hasType(type)));
  return !Iterables.isEmpty(files) && !profile.getActiveRulesByRepository(repositoryKey).isEmpty();
}
 
Example 6
Source Project: sonar-p3c-pmd   Source File: PmdRulesDefinitionTest.java    License: MIT License 5 votes vote down vote up
@Test
public void test() {
  PmdRulesDefinition definition = new PmdRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);

  assertThat(repository.name()).isEqualTo(PmdConstants.REPOSITORY_NAME);
  assertThat(repository.language()).isEqualTo(Java.KEY);

  List<Rule> rules = repository.rules();
  //assertThat(rules).hasSize(268);

  for (Rule rule : rules) {
    assertThat(rule.key()).isNotNull();
    assertThat(rule.internalKey()).isNotNull();
    assertThat(rule.name()).isNotNull();
    assertThat(rule.htmlDescription()).isNotNull();
    assertThat(rule.severity()).isNotNull();

    for (Param param : rule.params()) {
      assertThat(param.name()).isNotNull();
      assertThat(param.description())
        .overridingErrorMessage("Description is not set for parameter '" + param.name() + "' of rule '" + rule.key())
        .isNotNull();
    }

    if (!"XPathRule".equals(rule.key())) {
      assertThat(rule.debtRemediationFunction())
        .overridingErrorMessage("Sqale remediation function is not set for rule '" + rule.key())
        .isNotNull();
      assertThat(rule.debtSubCharacteristic())
        .overridingErrorMessage("Sqale characteristic is not set for rule '" + rule.key())
        .isNotNull();
    }
  }
}
 
Example 7
@Test
public void shouldDescribePluginCorrectly() {
    final DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
    final CheckstyleSensor sensor = new CheckstyleSensor(null);

    sensor.describe(descriptor);
    assertThat(descriptor.languages()).containsOnly(Java.KEY);
    assertThat(descriptor.name()).isNotEmpty();
}
 
Example 8
@Before
public void setUp() {
    Mockito.when(this.fs.predicates())
        .thenReturn(this.predicates);
    Mockito.when(this.predicates.hasLanguage(Matchers.eq(Java.KEY)))
        .thenReturn(this.predicate);
}
 
Example 9
Source Project: sonar-p3c-pmd   Source File: PmdProfileExporter.java    License: MIT License 4 votes vote down vote up
public PmdProfileExporter() {
  super(PmdConstants.REPOSITORY_KEY, PmdConstants.PLUGIN_NAME);
  setSupportedLanguages(Java.KEY);
  setMimeType("application/xml");
}
 
Example 10
Source Project: sonar-p3c-pmd   Source File: PmdExecutor.java    License: MIT License 4 votes vote down vote up
public Iterable<File> javaFiles(Type fileType) {
  FilePredicates predicates = fs.predicates();
  return fs.files(predicates.and(
    predicates.hasLanguage(Java.KEY),
    predicates.hasType(fileType)));
}
 
Example 11
Source Project: sonar-p3c-pmd   Source File: PmdProfileImporter.java    License: MIT License 4 votes vote down vote up
public PmdProfileImporter(RuleFinder ruleFinder) {
  super(PmdConstants.REPOSITORY_KEY, PmdConstants.PLUGIN_NAME);
  setSupportedLanguages(Java.KEY);
  this.ruleFinder = ruleFinder;
}
 
Example 12
Source Project: sonar-p3c-pmd   Source File: PmdExecutorTest.java    License: MIT License 4 votes vote down vote up
static InputFile file(String path, Type type) {
  return new DefaultInputFile(path)
    .setAbsolutePath(new File(path).getAbsolutePath())
    .setType(type)
    .setLanguage(Java.KEY);
}
 
Example 13
Source Project: sonar-p3c-pmd   Source File: PmdUnitTestsRulesDefinitionTest.java    License: MIT License 4 votes vote down vote up
@Test
public void test() {

  PmdUnitTestsRulesDefinition definition = new PmdUnitTestsRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.TEST_REPOSITORY_KEY);

  assertThat(repository.name()).isEqualTo(PmdConstants.TEST_REPOSITORY_NAME);
  assertThat(repository.language()).isEqualTo(Java.KEY);

  List<Rule> rules = repository.rules();
  assertThat(rules).hasSize(17);

  for (Rule rule : rules) {
    assertThat(rule.key()).isNotNull();
    assertThat(rule.key()).isIn(
      "JUnitStaticSuite",
      "JUnitSpelling",
      "JUnitAssertionsShouldIncludeMessage",
      "JUnitTestsShouldIncludeAssert",
      "TestClassWithoutTestCases",
      "UnnecessaryBooleanAssertion",
      "UseAssertEqualsInsteadOfAssertTrue",
      "UseAssertSameInsteadOfAssertTrue",
      "UseAssertNullInsteadOfAssertTrue",
      "SimplifyBooleanAssertion",
      "UseAssertTrueInsteadOfAssertEquals",
      "JUnitTestContainsTooManyAsserts",
      "JUnit4SuitesShouldUseSuiteAnnotation",
      "JUnit4TestShouldUseAfterAnnotation",
      "JUnit4TestShouldUseBeforeAnnotation",
      "JUnit4TestShouldUseTestAnnotation",
      "JUnitUseExpected");
    assertThat(rule.internalKey()).isNotNull();
    assertThat(rule.name()).isNotNull();
    assertThat(rule.htmlDescription()).isNotNull();
    assertThat(rule.severity()).isNotNull();

    for (Param param : rule.params()) {
      assertThat(param.name()).isNotNull();
      assertThat(param.description())
        .overridingErrorMessage("Description is not set for parameter '" + param.name() + "' of rule '" + rule.key())
        .isNotNull();
    }

    if (!"XPathRule".equals(rule.key())) {
      assertThat(rule.debtRemediationFunction())
        .overridingErrorMessage("Sqale remediation function is not set for rule '" + rule.key())
        .isNotNull();
      assertThat(rule.debtSubCharacteristic())
        .overridingErrorMessage("Sqale characteristic is not set for rule '" + rule.key())
        .isNotNull();
    }
  }
}
 
Example 14
@Override
public void describe(SensorDescriptor descriptor) {
    descriptor.onlyOnLanguage(Java.KEY).name("CheckstyleSensor");
}
 
Example 15
/**
 * {@link SmellMeasuresSensor} IoC constructor.
 *
 * @param fileSystem SonarQube {@link FileSystem}
 */
public SmellMeasuresSensor(final FileSystem fileSystem) {
    this.fileSystem = fileSystem;
    this.javaFilesPredicate = this.fileSystem.predicates()
        .hasLanguage(Java.KEY);
}
 
Example 16
@Override
public void describe(final SensorDescriptor descriptor) {
    descriptor.name(this.getClass()
        .getSimpleName());
    descriptor.onlyOnLanguage(Java.KEY);
}
 
Example 17
@Test
public void define_should_createRepositoryAndRegisterAllSmellPluginChecks() {
    Mockito.when(this.repository.key())
        .thenReturn(EXPECTED_REPOSITORY_KEY);
    Mockito.when(this.rule.key())
        .thenReturn(LAST_RULE_KEY);
    // Some plumbering...
    Mockito.when(this.context.createRepository(Matchers.anyString(), Matchers.anyString()))
        .thenReturn(this.repository);
    Mockito.when(this.repository.setName(Matchers.anyString()))
        .thenReturn(this.repository);
    Mockito.when(this.repository.rules())
        .thenReturn(ImmutableList.<NewRule> of(this.rule));
    Mockito.when(this.repository.createRule(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.repository.rule(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setName(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setHtmlDescription(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setSeverity(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setTemplate(Matchers.anyBoolean()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setStatus(Matchers.any(RuleStatus.class)))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setTags(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setDebtSubCharacteristic(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setDebtRemediationFunction(Matchers.any(DebtRemediationFunction.class)))
        .thenReturn(this.rule);
    Mockito.when(this.rule.debtRemediationFunctions())
        .thenReturn(this.functions);
    Mockito.when(this.rule.setEffortToFixDescription(Matchers.anyString()))
        .thenReturn(this.rule);
    // Execute
    final SmellRulesDefinition sut = new SmellRulesDefinition();
    sut.define(this.context);
    // Verify behavior
    Mockito.verify(this.context, Mockito.times(1))
        .createRepository(Matchers.eq(EXPECTED_REPOSITORY_KEY), Matchers.eq(Java.KEY));
    Mockito.verify(this.repository, Mockito.times(1))
        .setName(Matchers.eq(EXPECTED_REPOSITORY_NAME));
    Mockito.verify(this.repository, Mockito.times(27))
        .createRule(this.captor.capture());
    Assertions.assertThat(this.captor.getValue())
        .isEqualTo(LAST_RULE_KEY);
    Mockito.verify(this.rule, Mockito.times(28))
        .key();
    Mockito.verify(this.rule, Mockito.times(1))
        .setInternalKey(Matchers.eq(LAST_RULE_KEY));
    Mockito.verify(this.repository, Mockito.times(1))
        .done();
}