Search in sources :

Example 56 with Violation

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

the class XpathFilterElementTest method testNonMatchingModuleId.

@Test
public void testNonMatchingModuleId() throws Exception {
    final XpathFilterElement filter = new XpathFilterElement("InputXpathFilterElementSuppressByXpath", "Test", null, "id19", null);
    final Violation message = new Violation(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id20", getClass(), null);
    final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
    assertWithMessage("Event should be accepted").that(filter.accept(ev)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 57 with Violation

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

the class SeverityMatchFilterTest method testSeverity.

@Test
public void testSeverity() {
    filter.setSeverity(SeverityLevel.INFO);
    final AuditEvent ev = new AuditEvent(this, "Test.java");
    // event with no message has severity level INFO
    assertWithMessage("no message").that(filter.accept(ev)).isTrue();
    final SeverityLevel errorLevel = SeverityLevel.ERROR;
    final Violation errorMessage = new Violation(1, 0, "", "", null, errorLevel, null, getClass(), null);
    final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
    assertWithMessage("level:" + errorLevel).that(filter.accept(ev2)).isFalse();
    final SeverityLevel infoLevel = SeverityLevel.INFO;
    final Violation infoMessage = new Violation(1, 0, "", "", null, infoLevel, null, getClass(), null);
    final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoMessage);
    assertWithMessage("level:" + infoLevel).that(filter.accept(ev3)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) SeverityLevel(com.puppycrawl.tools.checkstyle.api.SeverityLevel) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 58 with Violation

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

the class SeverityMatchFilterTest method testDefault.

@Test
public void testDefault() {
    final AuditEvent ev = new AuditEvent(this, "Test.java");
    assertWithMessage("no message").that(filter.accept(ev)).isFalse();
    final SeverityLevel errorLevel = SeverityLevel.ERROR;
    final Violation errorMessage = new Violation(1, 0, "", "", null, errorLevel, null, getClass(), null);
    final AuditEvent ev2 = new AuditEvent(this, "ATest.java", errorMessage);
    assertWithMessage("level:" + errorLevel).that(filter.accept(ev2)).isTrue();
    final SeverityLevel infoLevel = SeverityLevel.INFO;
    final Violation infoViolation = new Violation(1, 0, "", "", null, infoLevel, null, getClass(), null);
    final AuditEvent ev3 = new AuditEvent(this, "ATest.java", infoViolation);
    assertWithMessage("level:" + infoLevel).that(filter.accept(ev3)).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) SeverityLevel(com.puppycrawl.tools.checkstyle.api.SeverityLevel) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 59 with Violation

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

the class SuppressFilterElementTest method testDecideByLine.

@Test
public void testDecideByLine() {
    final Violation violation = new Violation(10, 10, "", "", null, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "ATest.java", violation);
    final SuppressFilterElement filter1 = new SuppressFilterElement("Test", "Test", null, null, "1-10", null);
    final SuppressFilterElement filter2 = new SuppressFilterElement("Test", "Test", null, null, "1-9, 11", null);
    final SuppressFilterElement filter3 = new SuppressFilterElement("Test", "Test", null, null, null, null);
    // deny because there are matches on file name, check name, and line
    assertWithMessage("In range 1-10").that(filter1.accept(ev)).isFalse();
    assertWithMessage("Not in 1-9, 11").that(filter2.accept(ev)).isTrue();
    assertWithMessage("none").that(filter3.accept(ev)).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test) TreeWalkerTest(com.puppycrawl.tools.checkstyle.TreeWalkerTest)

Example 60 with Violation

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

the class SuppressFilterElementTest method testDecideByFileNameAndModuleMatchingCheckRegExpNotMatch.

@Test
public void testDecideByFileNameAndModuleMatchingCheckRegExpNotMatch() {
    final Violation message = new Violation(10, 10, "", "", null, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
    final SuppressFilterElement myFilter = new SuppressFilterElement("Test", "NON_EXISTENT_CHECK", null, "MyModule", null, null);
    assertWithMessage("Filter should accept valid event").that(myFilter.accept(ev)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test) TreeWalkerTest(com.puppycrawl.tools.checkstyle.TreeWalkerTest)

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