Search in sources :

Example 61 with Violation

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

the class SuppressFilterElementTest method testDecideByMessage.

@Test
public void testDecideByMessage() {
    final Violation violation = new Violation(1, 0, "", "", null, null, getClass(), "Test");
    final AuditEvent ev = new AuditEvent(this, "ATest.java", violation);
    final SuppressFilterElement filter1 = new SuppressFilterElement(null, null, "Test", null, null, null);
    final SuppressFilterElement filter2 = new SuppressFilterElement(null, null, "Bad", null, null, null);
    assertWithMessage("Message match").that(filter1.accept(ev)).isFalse();
    assertWithMessage("Message not match").that(filter2.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)

Example 62 with Violation

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

the class SuppressFilterElementTest method testDecideByFileNameAndModuleMatchingRegExpNotMatch.

@Test
public void testDecideByFileNameAndModuleMatchingRegExpNotMatch() {
    final Violation message = new Violation(10, 10, "", "", null, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "T1est", message);
    assertWithMessage("Filter should accept valid event").that(filter.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)

Example 63 with Violation

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

the class SuppressFilterElementTest method testDecideByColumn.

@Test
public void testDecideByColumn() {
    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, null, "1-10");
    final SuppressFilterElement filter2 = new SuppressFilterElement("Test", "Test", null, null, null, "1-9, 11");
    // deny because there are matches on file name, check name, and column
    assertWithMessage("In range 1-10").that(filter1.accept(ev)).isFalse();
    assertWithMessage("Not in 1-9, 1)").that(filter2.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)

Example 64 with Violation

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

the class TreeWalker method processFiltered.

@Override
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
    // check if already checked and passed the file
    if (!ordinaryChecks.isEmpty() || !commentChecks.isEmpty()) {
        final FileContents contents = getFileContents();
        final DetailAST rootAST = JavaParser.parse(contents);
        if (!ordinaryChecks.isEmpty()) {
            walk(rootAST, contents, AstState.ORDINARY);
        }
        if (!commentChecks.isEmpty()) {
            final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST);
            walk(astWithComments, contents, AstState.WITH_COMMENTS);
        }
        if (filters.isEmpty()) {
            addViolations(violations);
        } else {
            final SortedSet<Violation> filteredViolations = getFilteredViolations(file.getAbsolutePath(), contents, rootAST);
            addViolations(filteredViolations);
        }
        violations.clear();
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 65 with Violation

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

the class TranslationCheckTest method testLogIllegalArgumentException.

@Test
public void testLogIllegalArgumentException() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
    checkConfig.addProperty("baseName", "^bad.*$");
    final String[] expected = { "0: " + new Violation(1, Definitions.CHECKSTYLE_BUNDLE, "general.exception", new String[] { "Malformed \\uxxxx encoding." }, null, getClass(), null).getViolation(), "1: " + getCheckMessage(MSG_KEY, "test") };
    final File[] propertyFiles = { new File(getPath("bad.properties")), new File(getPath("bad_es.properties")) };
    verify(createChecker(checkConfig), propertyFiles, getPath("bad.properties"), expected);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) File(java.io.File) 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