Search in sources :

Example 31 with Violation

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

the class SarifLoggerTest method testLineOnly.

@Test
public void testLineOnly() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 0, "messages.properties", "ruleId", null, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.fileStarted(ev);
    logger.addError(ev);
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerLineOnly.sarif"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 32 with Violation

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

the class SarifLoggerTest method testAddError.

@Test
public void testAddError() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "ruleId", null, SeverityLevel.ERROR, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.fileStarted(ev);
    logger.addError(ev);
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerSingleError.sarif"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 33 with Violation

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

the class SarifLoggerTest method testAddErrors.

@Test
public void testAddErrors() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "ruleId", null, SeverityLevel.INFO, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    final Violation violation2 = new Violation(1, 1, "messages.properties", "ruleId2", null, SeverityLevel.IGNORE, null, getClass(), "found another error");
    final AuditEvent ev2 = new AuditEvent(this, "Test.java", violation2);
    logger.fileStarted(ev);
    logger.addError(ev);
    logger.fileFinished(ev);
    logger.fileStarted(ev2);
    logger.addError(ev2);
    logger.fileFinished(ev2);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerDoubleError.sarif"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 34 with Violation

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

the class SarifLoggerTest method testAddException.

@Test
public void testAddException() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation message = new Violation(1, 1, "messages.properties", "null", null, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, null, message);
    logger.fileStarted(ev);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerSingleException.sarif"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 35 with Violation

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

the class CheckerTest method testAddListener.

@Test
public void testAddListener() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    // Let's try fire some events
    getFireAuditStartedMethod().invoke(checker);
    assertWithMessage("Checker.fireAuditStarted() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditStarted() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    getFireAuditFinished().invoke(checker);
    assertWithMessage("Checker.fireAuditFinished() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditFinished() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    checker.fireFileStarted("Some File Name");
    assertWithMessage("Checker.fireFileStarted() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileStarted() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    checker.fireFileFinished("Some File Name");
    assertWithMessage("Checker.fireFileFinished() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileFinished() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
    auditAdapter.resetListener();
    final SortedSet<Violation> violations = new TreeSet<>();
    violations.add(new Violation(1, 0, "a Bundle", "message.key", new Object[] { "arg" }, null, getClass(), null));
    checker.fireErrors("Some File Name", violations);
    assertWithMessage("Checker.fireErrors() doesn't call listener").that(auditAdapter.wasCalled()).isTrue();
    assertWithMessage("Checker.fireErrors() doesn't pass event").that(auditAdapter.wasEventPassed()).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeSet(java.util.TreeSet) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) 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