Search in sources :

Example 86 with Violation

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

the class AuditEventDefaultFormatterTest method testCalculateBufferLength.

@Test
public void testCalculateBufferLength() throws Exception {
    final Violation violation = new Violation(1, 1, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent auditEvent = new AuditEvent(new Object(), "fileName", violation);
    final int result = TestUtil.invokeStaticMethod(AuditEventDefaultFormatter.class, "calculateBufferLength", auditEvent, SeverityLevel.ERROR.ordinal());
    assertWithMessage("Buffer length is not expected").that(result).isEqualTo(54);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 87 with Violation

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

the class AuditEventDefaultFormatterTest method testFormatFullyQualifiedModuleNameContainsCheckSuffix.

@Test
public void testFormatFullyQualifiedModuleNameContainsCheckSuffix() {
    final Violation violation = new Violation(1, 1, null, null, null, SeverityLevel.WARNING, null, TestModuleCheck.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. " + "[AuditEventDefaultFormatterTest$TestModule]";
    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)

Example 88 with Violation

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

the class CheckerTest method testDestroy.

@Test
public void testDestroy() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    final TestFileSetCheck fileSet = new TestFileSetCheck();
    checker.addFileSetCheck(fileSet);
    final DebugFilter filter = new DebugFilter();
    checker.addFilter(filter);
    final TestBeforeExecutionFileFilter fileFilter = new TestBeforeExecutionFileFilter();
    checker.addBeforeExecutionFileFilter(fileFilter);
    // should remove all listeners, file sets, and filters
    checker.destroy();
    final File tempFile = File.createTempFile("junit", null, temporaryFolder);
    checker.process(Collections.singletonList(tempFile));
    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.destroy() doesn't remove listeners.").that(auditAdapter.wasCalled()).isFalse();
    assertWithMessage("Checker.destroy() doesn't remove file sets.").that(fileSet.wasCalled()).isFalse();
    assertWithMessage("Checker.destroy() doesn't remove filters.").that(filter.wasCalled()).isFalse();
    assertWithMessage("Checker.destroy() doesn't remove file filters.").that(fileFilter.wasCalled()).isFalse();
}
Also used : TestFileSetCheck(com.puppycrawl.tools.checkstyle.internal.testmodules.TestFileSetCheck) Violation(com.puppycrawl.tools.checkstyle.api.Violation) TestBeforeExecutionFileFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.TestBeforeExecutionFileFilter) TreeSet(java.util.TreeSet) DebugAuditAdapter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter) File(java.io.File) DebugFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter) Test(org.junit.jupiter.api.Test)

Example 89 with Violation

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

the class CheckerTest method testRemoveFilter.

@Test
public void testRemoveFilter() {
    final Checker checker = new Checker();
    final DebugFilter filter = new DebugFilter();
    final DebugFilter f2 = new DebugFilter();
    checker.addFilter(filter);
    checker.addFilter(f2);
    checker.removeFilter(filter);
    f2.resetFilter();
    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 filter").that(f2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireErrors() does call removed filter").that(filter.wasCalled()).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeSet(java.util.TreeSet) DebugFilter(com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter) Test(org.junit.jupiter.api.Test)

Example 90 with Violation

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

the class CheckerTest method testRemoveListener.

@Test
public void testRemoveListener() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    final DebugAuditAdapter aa2 = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    checker.addListener(aa2);
    checker.removeListener(auditAdapter);
    // Let's try fire some events
    getFireAuditStartedMethod().invoke(checker);
    assertWithMessage("Checker.fireAuditStarted() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditStarted() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    getFireAuditFinished().invoke(checker);
    assertWithMessage("Checker.fireAuditFinished() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireAuditFinished() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    checker.fireFileStarted("Some File Name");
    assertWithMessage("Checker.fireFileStarted() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileStarted() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.resetListener();
    checker.fireFileFinished("Some File Name");
    assertWithMessage("Checker.fireFileFinished() doesn't call listener").that(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireFileFinished() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
    aa2.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(aa2.wasCalled()).isTrue();
    assertWithMessage("Checker.fireErrors() does call removed listener").that(auditAdapter.wasCalled()).isFalse();
}
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