Search in sources :

Example 11 with Violation

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

the class PackageObjectFactoryTest method testMakeCheckFromName.

@Test
public void testMakeCheckFromName() {
    final String name = "com.puppycrawl.tools.checkstyle.checks.naming.ConstantName";
    try {
        factory.createModule(name);
        assertWithMessage("Exception is expected").fail();
    } catch (CheckstyleException ex) {
        final Violation exceptionMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, new String[] { name, null }, 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 12 with Violation

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

the class PackageObjectFactoryTest method testCreateObjectFromFullModuleNamesWithExceptionByBruteForce.

@Test
public void testCreateObjectFromFullModuleNamesWithExceptionByBruteForce() {
    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, TRY_IN_ALL_REGISTERED_PACKAGES);
    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 13 with Violation

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

the class XMLLoggerTest method testAuditFinishedWithoutFileFinished.

@Test
public void testAuditFinishedWithoutFileFinished() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java");
    logger.fileStarted(fileStartedEvent);
    final Violation violation = new Violation(1, 1, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent errorEvent = new AuditEvent(this, "Test.java", violation);
    logger.addError(errorEvent);
    logger.fileFinished(errorEvent);
    logger.auditFinished(null);
    verifyXml(getPath("ExpectedXMLLoggerError.xml"), outStream, violation.getViolation());
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 14 with Violation

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

the class XMLLoggerTest method testAddExceptionBetweenFileStartedAndFinished.

@Test
public void testAddExceptionBetweenFileStartedAndFinished() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", null, null, null, getClass(), null);
    final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java");
    logger.fileStarted(fileStartedEvent);
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    final AuditEvent fileFinishedEvent = new AuditEvent(this, "Test.java");
    logger.fileFinished(fileFinishedEvent);
    logger.auditFinished(null);
    verifyXml(getPath("ExpectedXMLLoggerException2.xml"), outStream);
    assertWithMessage("Invalid close count").that(outStream.getCloseCount()).isEqualTo(1);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 15 with Violation

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

the class XMLLoggerTest method testAddException.

@Test
public void testAddException() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", null, null, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    logger.auditFinished(null);
    verifyXml(getPath("ExpectedXMLLoggerException.xml"), outStream);
    assertWithMessage("Invalid close count").that(outStream.getCloseCount()).isEqualTo(1);
}
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