Search in sources :

Example 81 with Violation

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

the class XMLLoggerTest method testAddIgnored.

@Test
public void testAddIgnored() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "key", null, SeverityLevel.IGNORE, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.addError(ev);
    logger.auditFinished(null);
    verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 82 with Violation

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

the class XMLLoggerTest method testAddExceptionBeforeFileFinished.

@Test
public void testAddExceptionBeforeFileFinished() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", null, null, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    final AuditEvent fileFinishedEvent = new AuditEvent(this, "Test.java");
    logger.fileFinished(fileFinishedEvent);
    logger.auditFinished(null);
    verifyXml(getPath("ExpectedXMLLoggerException3.xml"), outStream);
    assertWithMessage("Invalid close count").that(outStream.getCloseCount()).isEqualTo(1);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 83 with Violation

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

the class XpathQueryGeneratorTest method testConstructorWithTreeWalkerAuditEvent.

@Test
public void testConstructorWithTreeWalkerAuditEvent() {
    final Violation violation = new Violation(12, 1, "messages.properties", null, null, null, null, null, null);
    final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(new FileContents(fileText), "InputXpathQueryGenerator", violation, rootAst);
    final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(event, DEFAULT_TAB_WIDTH);
    final List<String> actual = queryGenerator.generate();
    final List<String> expected = Arrays.asList("/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGenerator']]", "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGenerator']]/MODIFIERS", "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGenerator']]/MODIFIERS" + "/LITERAL_PUBLIC");
    assertWithMessage("Generated queries do not match expected ones").that(actual).isEqualTo(expected);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 84 with Violation

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

the class Checker method processFiles.

/**
 * Processes a list of files with all FileSetChecks.
 *
 * @param files a list of files to process.
 * @throws CheckstyleException if error condition within Checkstyle occurs.
 * @throws Error wraps any java.lang.Error happened during execution
 * @noinspection ProhibitedExceptionThrown
 */
// -@cs[CyclomaticComplexity] no easy way to split this logic of processing the file
private void processFiles(List<File> files) throws CheckstyleException {
    for (final File file : files) {
        String fileName = null;
        try {
            fileName = file.getAbsolutePath();
            final long timestamp = file.lastModified();
            if (cacheFile != null && cacheFile.isInCache(fileName, timestamp) || !acceptFileStarted(fileName)) {
                continue;
            }
            if (cacheFile != null) {
                cacheFile.put(fileName, timestamp);
            }
            fireFileStarted(fileName);
            final SortedSet<Violation> fileMessages = processFile(file);
            fireErrors(fileName, fileMessages);
            fireFileFinished(fileName);
        }// processing. See https://github.com/checkstyle/checkstyle/issues/2285
         catch (Exception ex) {
            if (fileName != null && cacheFile != null) {
                cacheFile.remove(fileName);
            }
            // We need to catch all exceptions to put a reason failure (file name) in exception
            throw new CheckstyleException("Exception was thrown while processing " + file.getPath(), ex);
        } catch (Error error) {
            if (fileName != null && cacheFile != null) {
                cacheFile.remove(fileName);
            }
            // We need to catch all errors to put a reason failure (file name) in error
            throw new Error("Error was thrown while processing " + file.getPath(), error);
        }
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 85 with Violation

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

the class AuditEventDefaultFormatterTest method testFormatModuleWithModuleId.

@Test
public void testFormatModuleWithModuleId() {
    final Violation violation = new Violation(1, 1, null, null, null, SeverityLevel.WARNING, "ModuleId", TestModule.class, "Mocked violation.");
    final AuditEvent event = new AuditEvent("", "InputMockFile.java", violation);
    final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
    final String expected = "[WARN] InputMockFile.java:1:1: Mocked violation. [ModuleId]";
    assertWithMessage("Invalid format").that(formatter.format(event)).isEqualTo(expected);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Aggregations

Violation (com.puppycrawl.tools.checkstyle.api.Violation)109 Test (org.junit.jupiter.api.Test)98 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)51 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)17 File (java.io.File)14 TreeWalkerTest (com.puppycrawl.tools.checkstyle.TreeWalkerTest)13 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)10 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)8 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)7 FileText (com.puppycrawl.tools.checkstyle.api.FileText)7 TreeSet (java.util.TreeSet)6 DetailAstImpl (com.puppycrawl.tools.checkstyle.DetailAstImpl)5 ParseErrorMessage (com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage)3 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)3 DebugAuditAdapter (com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter)3 DebugFilter (com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 Method (java.lang.reflect.Method)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2