Search in sources :

Example 16 with AuditEvent

use of com.puppycrawl.tools.checkstyle.api.AuditEvent in project checkstyle by checkstyle.

the class XMLLoggerTest method testAddException.

@Test
public void testAddException() throws IOException {
    final XMLLogger logger = new XMLLogger(outStream, true);
    logger.auditStarted(null);
    final LocalizedMessage message = new LocalizedMessage(1, 1, "messages.properties", null, null, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", message);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    logger.auditFinished(null);
    final String[] expectedLines = { "<exception>", "<![CDATA[", "stackTrace]]>", "</exception>", "" };
    verifyLines(expectedLines);
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 17 with AuditEvent

use of com.puppycrawl.tools.checkstyle.api.AuditEvent in project checkstyle by checkstyle.

the class XMLLoggerTest method testAddErrorOnZeroColumns.

@Test
public void testAddErrorOnZeroColumns() throws IOException {
    final XMLLogger logger = new XMLLogger(outStream, true);
    logger.auditStarted(null);
    final LocalizedMessage message = new LocalizedMessage(1, 0, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", message);
    logger.addError(ev);
    logger.auditFinished(null);
    final String[] expectedLines = { "<error line=\"1\" severity=\"error\" message=\"key\"" + " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>" };
    verifyLines(expectedLines);
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 18 with AuditEvent

use of com.puppycrawl.tools.checkstyle.api.AuditEvent in project checkstyle by checkstyle.

the class XMLLoggerTest method testFileStarted.

@Test
public void testFileStarted() throws IOException {
    final XMLLogger logger = new XMLLogger(outStream, true);
    logger.auditStarted(null);
    final AuditEvent ev = new AuditEvent(this, "Test.java");
    logger.fileStarted(ev);
    logger.auditFinished(null);
    final String[] expectedLines = { "<file name=\"Test.java\">" };
    verifyLines(expectedLines);
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.Test)

Example 19 with AuditEvent

use of com.puppycrawl.tools.checkstyle.api.AuditEvent in project maven-plugins by apache.

the class CheckstyleReportGenerator method sortConfiguration.

private void sortConfiguration(List<ConfReference> result, Configuration config, ChainedItem<Configuration> parent, CheckstyleResults results) {
    for (Configuration childConfig : config.getChildren()) {
        String ruleName = childConfig.getName();
        if (treeWalkerNames.contains(ruleName)) {
            // special sub-case: TreeWalker is the parent of multiple rules, not an effective rule
            sortConfiguration(result, childConfig, new ChainedItem<>(config, parent), results);
        } else {
            String fixedmessage = getConfigAttribute(childConfig, null, "message", null);
            // Grab the severity from the rule configuration, use null as default value
            String configSeverity = getConfigAttribute(childConfig, null, "severity", null);
            // count rule violations
            long violations = 0;
            AuditEvent lastMatchedEvent = null;
            for (List<AuditEvent> errors : results.getFiles().values()) {
                for (AuditEvent event : errors) {
                    if (matchRule(event, ruleName, fixedmessage, configSeverity)) {
                        lastMatchedEvent = event;
                        violations++;
                    }
                }
            }
            if (// forget rules without violations
            violations > 0) {
                String category = RuleUtil.getCategory(lastMatchedEvent);
                result.add(new ConfReference(category, childConfig, parent, violations, result.size()));
            }
        }
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent)

Example 20 with AuditEvent

use of com.puppycrawl.tools.checkstyle.api.AuditEvent in project maven-plugins by apache.

the class CheckstyleReportGenerator method doFileEvents.

private void doFileEvents(List<AuditEvent> eventList, String filename) {
    for (AuditEvent event : eventList) {
        SeverityLevel level = event.getSeverityLevel();
        if ((getSeverityLevel() != null) && !(getSeverityLevel() != level)) {
            continue;
        }
        sink.tableRow();
        sink.tableCell();
        iconTool.iconSeverity(level.getName(), IconTool.TEXT_SIMPLE);
        sink.tableCell_();
        sink.tableCell();
        String category = RuleUtil.getCategory(event);
        if (category != null) {
            sink.text(category);
        }
        sink.tableCell_();
        sink.tableCell();
        String ruleName = RuleUtil.getName(event);
        if (ruleName != null) {
            sink.text(ruleName);
        }
        sink.tableCell_();
        sink.tableCell();
        sink.text(event.getMessage());
        sink.tableCell_();
        sink.tableCell();
        int line = event.getLine();
        if (getXrefLocation() != null && line != 0) {
            sink.link(getXrefLocation() + "/" + filename.replaceAll("\\.java$", ".html") + "#L" + line);
            sink.text(String.valueOf(line));
            sink.link_();
        } else if (line != 0) {
            sink.text(String.valueOf(line));
        }
        sink.tableCell_();
        sink.tableRow_();
    }
}
Also used : SeverityLevel(com.puppycrawl.tools.checkstyle.api.SeverityLevel) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent)

Aggregations

AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)47 Test (org.junit.Test)38 LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)4 ArrayList (java.util.ArrayList)4 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 Field (java.lang.reflect.Field)2 CheckstyleCheckerListener (org.apache.maven.plugins.checkstyle.exec.CheckstyleCheckerListener)2 CheckstyleResults (org.apache.maven.plugins.checkstyle.exec.CheckstyleResults)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SinkEventAttributeSet (org.apache.maven.doxia.sink.SinkEventAttributeSet)1