Search in sources :

Example 36 with Violation

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

the class CheckerTest method testAddFilter.

@Test
public void testAddFilter() {
    final Checker checker = new Checker();
    final DebugFilter filter = new DebugFilter();
    checker.addFilter(filter);
    filter.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(filter.wasCalled()).isTrue();
}
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 37 with Violation

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

the class PackageObjectFactoryTest method testCreateObjectFromFullModuleNamesWithAmbiguousException.

@Test
public void testCreateObjectFromFullModuleNamesWithAmbiguousException() {
    final String barPackage = BASE_PACKAGE + ".packageobjectfactory.bar";
    final String fooPackage = BASE_PACKAGE + ".packageobjectfactory.foo";
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final PackageObjectFactory objectFactory = new PackageObjectFactory(new LinkedHashSet<>(Arrays.asList(barPackage, fooPackage)), classLoader);
    final String name = "FooCheck";
    try {
        objectFactory.createModule(name);
        assertWithMessage("Exception is expected").fail();
    } catch (CheckstyleException ex) {
        final String optionalNames = barPackage + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + fooPackage + PACKAGE_SEPARATOR + name;
        final Violation exceptionMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, new String[] { name, optionalNames }, null, getClass(), null);
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo(exceptionMessage.getViolation());
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Test(org.junit.jupiter.api.Test)

Example 38 with Violation

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

the class DeclarationOrderCheckTest method testParents.

@Test
public void testParents() {
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.STATIC_INIT);
    final DetailAstImpl method = new DetailAstImpl();
    method.setType(TokenTypes.METHOD_DEF);
    parent.setFirstChild(method);
    final DetailAstImpl ctor = new DetailAstImpl();
    ctor.setType(TokenTypes.CTOR_DEF);
    method.setNextSibling(ctor);
    final DeclarationOrderCheck check = new DeclarationOrderCheck();
    check.visitToken(method);
    final SortedSet<Violation> violations1 = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations1).isEmpty();
    check.visitToken(ctor);
    final SortedSet<Violation> violations2 = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations2).isEmpty();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Violation(com.puppycrawl.tools.checkstyle.api.Violation) Test(org.junit.jupiter.api.Test)

Example 39 with Violation

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

the class DeclarationOrderCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.STATIC_INIT);
    final DetailAstImpl array = new DetailAstImpl();
    array.setType(TokenTypes.ARRAY_INIT);
    parent.setFirstChild(array);
    final DeclarationOrderCheck check = new DeclarationOrderCheck();
    check.visitToken(array);
    final SortedSet<Violation> violations = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations).isEmpty();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Violation(com.puppycrawl.tools.checkstyle.api.Violation) Test(org.junit.jupiter.api.Test)

Example 40 with Violation

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

the class NPathComplexityCheckTest method testVisitTokenBeforeExpressionRange.

@Test
public void testVisitTokenBeforeExpressionRange() {
    // Create first ast
    final DetailAstImpl astIf = mockAST(TokenTypes.LITERAL_IF, "if", 2, 2);
    final DetailAstImpl astIfLeftParen = mockAST(TokenTypes.LPAREN, "(", 3, 3);
    astIf.addChild(astIfLeftParen);
    final DetailAstImpl astIfTrue = mockAST(TokenTypes.LITERAL_TRUE, "true", 3, 3);
    astIf.addChild(astIfTrue);
    final DetailAstImpl astIfRightParen = mockAST(TokenTypes.RPAREN, ")", 4, 4);
    astIf.addChild(astIfRightParen);
    // Create ternary ast
    final DetailAstImpl astTernary = mockAST(TokenTypes.QUESTION, "?", 1, 1);
    final DetailAstImpl astTernaryTrue = mockAST(TokenTypes.LITERAL_TRUE, "true", 1, 2);
    astTernary.addChild(astTernaryTrue);
    final NPathComplexityCheck npathComplexityCheckObj = new NPathComplexityCheck();
    // visiting first ast, set expressionSpatialRange to [2,2 - 4,4]
    npathComplexityCheckObj.visitToken(astIf);
    final SortedSet<Violation> violations1 = npathComplexityCheckObj.getViolations();
    assertWithMessage("No exception violations expected").that(violations1).isEmpty();
    // visiting ternary, it lies before expressionSpatialRange
    npathComplexityCheckObj.visitToken(astTernary);
    final SortedSet<Violation> violations2 = npathComplexityCheckObj.getViolations();
    assertWithMessage("No exception violations expected").that(violations2).isEmpty();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Violation(com.puppycrawl.tools.checkstyle.api.Violation) 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