org.sonar.api.batch.rule.ActiveRule Java Examples

The following examples show how to use org.sonar.api.batch.rule.ActiveRule. 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: ClassReferenceElevatedClassLoaderFactoryTest.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testLoadClass() throws ClassNotFoundException, MalformedURLException {
    ClassloaderBuilder builder = new ClassloaderBuilder();
    builder.newClassloader("_api_", getClass().getClassLoader());
    builder.setMask("_api_", new Mask().addInclusion("java/").addInclusion("org/sonar/api/"));

    builder.newClassloader("_customPlugin");
    builder.setParent("_customPlugin", "_api_", new Mask());
    builder.setLoadingOrder("_customPlugin", ClassloaderBuilder.LoadingOrder.SELF_FIRST);

    for (URL pluginUrl : findSonarqubePluginJars()) {
        builder.addURL("_customPlugin", pluginUrl);
    }

    Map<String, ClassLoader> loaders = builder.build();
    ClassLoader classLoader = loaders.get("_customPlugin");

    Class<? extends Plugin> loadedClass = (Class<? extends Plugin>) classLoader.loadClass(SVN_PLUGIN_CLASS);

    ClassReferenceElevatedClassLoaderFactory testCase =
            new ClassReferenceElevatedClassLoaderFactory(ActiveRule.class.getName());
    ClassLoader elevatedLoader = testCase.createClassLoader(loadedClass);
    Class<?> elevatedClass = elevatedLoader.loadClass(loadedClass.getName());
    // Getting closer than this is going to be difficult since the URLClassLoader that actually loads is an inner class of evelvatedClassLoader
    assertNotSame(elevatedLoader, elevatedClass.getClassLoader());
}
 
Example #2
Source File: RubocopSensor.java    From sonar-ruby-plugin with MIT License 6 votes vote down vote up
@Override
public void execute(SensorContext sensorContext) {
    RubocopExecutorConfig config = RubocopExecutorConfig.fromSettings(sensorContext, resolver);

    if (!config.useExistingRubocopOutput() && config.getPathToRubocop() == null) {
        LOG.warn("Path to rubocop not defined or not found. Skipping rubocop analysis.");
        return;
    }

    String jsonResults = this.executor.execute(config, findFilesToLint(sensorContext, config));

    Map<String, List<RubocopIssue>> issues = parser.parse(jsonResults);

    if (issues.isEmpty()) {
        LOG.warn("Rubocop returned no result at all");
        return;
    }

    Collection<ActiveRule> allRules = sensorContext.activeRules().findByRepository("rubocop");
    Set<String> ruleNames = allRules.stream().map(rule -> rule.ruleKey().rule()).collect(Collectors.toSet());

    issues.entrySet().forEach(rubyFilesIssues -> generateSonarIssuesFromResults(rubyFilesIssues, sensorContext, ruleNames));
}
 
Example #3
Source File: CFLintConfigExporter.java    From sonar-coldfusion with Apache License 2.0 6 votes vote down vote up
public void save(Writer writer) throws IOException, XMLStreamException {
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw=null;
    try {
        xtw = xmlOutputFactory.createXMLStreamWriter(writer);

        xtw.writeStartDocument();
        xtw.writeStartElement("config");

        for (ActiveRule activeRule : ruleProfiles) {
            xtw.writeStartElement("includes");
            xtw.writeAttribute("code", activeRule.ruleKey().toString());
            xtw.writeEndElement();
        }

        xtw.writeEndElement();
        xtw.writeEndDocument();
    } finally {
        if(xtw!=null) {
            xtw.close();
        }
    }

}
 
Example #4
Source File: CheckstyleAuditListener.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void addError(AuditEvent event) {
    final String ruleKey = getRuleKey(event);

    if (Objects.nonNull(ruleKey)) {
        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 NewIssue issue = context.newIssue();
        final ActiveRule rule = ruleFinder.find(
                RuleKey.of(CheckstyleConstants.REPOSITORY_KEY, ruleKey));
        if (Objects.nonNull(issue) && Objects.nonNull(rule)) {
            final NewIssueLocation location = issue.newLocation()
                    .on(currentResource)
                    .at(currentResource.selectLine(getLineId(event)))
                    .message(message);
            issue.forRule(rule.ruleKey())
                    .at(location)
                    .save();
        }
    }
}
 
Example #5
Source File: CheckstyleAuditListenerTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addErrorTestForLine(final int pLineNo) {
    final ActiveRule rule = setupRule("repo", "key");

    final NewIssue newIssue = mock(NewIssue.class);
    final NewIssueLocation newLocation = mock(NewIssueLocation.class);
    when(context.newIssue()).thenReturn(newIssue);
    when(newIssue.newLocation()).thenReturn(newLocation);
    when(newIssue.forRule(rule.ruleKey())).thenReturn(newIssue);
    when(newIssue.at(newLocation)).thenReturn(newIssue);
    when(newLocation.on(any(InputComponent.class))).thenReturn(newLocation);
    when(newLocation.at(any(TextRange.class))).thenReturn(newLocation);
    when(newLocation.message(anyString())).thenReturn(newLocation);

    when(inputFile.selectLine(anyInt())).thenReturn(new DefaultTextRange(
        new DefaultTextPointer(1, 1), new DefaultTextPointer(1, 2)));

    final AuditEvent eventAdded = new AuditEvent(this, file.getAbsolutePath(),
        new LocalizedMessage(pLineNo, "", "", null, "", CheckstyleAuditListenerTest.class,
            "msg"));
    addErrorToListener(eventAdded);

    verify(newIssue, times(1)).save();
    verify(newIssue, times(1)).forRule(rule.ruleKey());
    verify(newIssue, times(1)).at(newLocation);
    verify(newIssue, times(1)).newLocation();
    verify(newLocation, times(1)).on(any());
    verify(newLocation, times(1)).at(any());
    verify(newLocation, times(1)).message(any());
}
 
Example #6
Source File: CheckstyleAuditListenerTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ActiveRule setupRule(String repo, String key) {
    final ActiveRule rule = mock(ActiveRule.class);
    when(rule.ruleKey()).thenReturn(RuleKey.of(repo, key));
    when(
            ruleFinder.find(RuleKey.of(CheckstyleConstants.REPOSITORY_KEY,
                    CheckstyleAuditListenerTest.class.getCanonicalName()))).thenReturn(rule);
    return rule;
}
 
Example #7
Source File: ActiveRuleWrapperScannerImpl.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ActiveRuleWrapperScannerImpl(ActiveRule activeRule) {
    this.activeRule = activeRule;
}
 
Example #8
Source File: CheckstyleAuditListenerTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void addErrorOnUnknownFile() {
    final ActiveRule rule = setupRule("repo", "key");
    addErrorToListener(event);
    verifyZeroInteractions(rule);
}