Search in sources :

Example 71 with Violation

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

the class SuppressWarningsHolderTest method createAuditEvent.

private static AuditEvent createAuditEvent(String moduleId, int line, int column) {
    final Checker source = new Checker();
    final Violation violation = new Violation(line, column, null, null, null, moduleId, MemberNameCheck.class, "violation");
    return new AuditEvent(source, "filename", violation);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) Checker(com.puppycrawl.tools.checkstyle.Checker) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent)

Example 72 with Violation

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

the class NPathComplexityCheckTest method testDefaultHooks.

@Test
public void testDefaultHooks() {
    final NPathComplexityCheck npathComplexityCheckObj = new NPathComplexityCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
    npathComplexityCheckObj.visitToken(ast);
    final SortedSet<Violation> violations1 = npathComplexityCheckObj.getViolations();
    assertWithMessage("No exception violations expected").that(violations1).isEmpty();
    npathComplexityCheckObj.leaveToken(ast);
    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) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 73 with Violation

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

the class PackageObjectFactoryTest method testCreateModuleWithNonExistName.

@Test
public void testCreateModuleWithNonExistName() {
    final String[] names = { "NonExistClassOne", "NonExistClassTwo" };
    for (String name : names) {
        try {
            factory.createModule(name);
            assertWithMessage("Exception is expected").fail();
        } catch (CheckstyleException ex) {
            final String attemptedNames = BASE_PACKAGE + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + name + CHECK_SUFFIX + STRING_SEPARATOR + BASE_PACKAGE + PACKAGE_SEPARATOR + name + CHECK_SUFFIX;
            final Violation exceptionMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] { name, attemptedNames }, null, factory.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 74 with Violation

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

the class PackageObjectFactoryTest method testCreateObjectFromFullModuleNamesWithCantInstantiateException.

@Test
public void testCreateObjectFromFullModuleNamesWithCantInstantiateException() {
    final String package1 = BASE_PACKAGE + ".wrong1";
    final String package2 = BASE_PACKAGE + ".wrong2";
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final PackageObjectFactory objectFactory = new PackageObjectFactory(new LinkedHashSet<>(Arrays.asList(package1, package2)), classLoader);
    final String name = "FooCheck";
    final String checkName = name + CHECK_SUFFIX;
    try {
        objectFactory.createModule(name);
        assertWithMessage("Exception is expected").fail();
    } catch (CheckstyleException ex) {
        final String attemptedNames = package1 + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + checkName + STRING_SEPARATOR + package1 + PACKAGE_SEPARATOR + checkName + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + checkName;
        final Violation exceptionMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] { name, attemptedNames }, 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 75 with Violation

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

the class XpathFileGeneratorAstFilterTest method testNoXpathQuery.

@Test
public void testNoXpathQuery() throws Exception {
    final Violation violation = new Violation(10, 10, TokenTypes.LCURLY, "messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class, null);
    final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent("InputXpathFileGeneratorAstFilter.java", violation);
    final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
    assertWithMessage("filter accepted").that(filter.accept(event)).isTrue();
    final AuditEvent auditEvent = new AuditEvent(this, getPath("InputXpathFileGeneratorAstFilter.java"), violation);
    assertWithMessage("expected null").that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent)).isNull();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) 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