Search in sources :

Example 26 with Violation

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

the class MainTest method testExistingTargetFileWithViolations.

@Test
public void testExistingTargetFileWithViolations(@SysErr Capturable systemErr, @SysOut Capturable systemOut) throws IOException {
    assertMainReturnCode(0, "-c", getPath("InputMainConfig-classname2.xml"), getPath("InputMain.java"));
    final Violation invalidPatternMessageMain = new Violation(1, "com.puppycrawl.tools.checkstyle.checks.naming.messages", "name.invalidPattern", new String[] { "InputMain", "^[a-z0-9]*$" }, null, getClass(), null);
    final Violation invalidPatternMessageMainInner = new Violation(1, "com.puppycrawl.tools.checkstyle.checks.naming.messages", "name.invalidPattern", new String[] { "InputMainInner", "^[a-z0-9]*$" }, null, getClass(), null);
    final String expectedPath = getFilePath("InputMain.java");
    assertWithMessage("Unexpected output log").that(systemOut.getCapturedData()).isEqualTo(addEndOfLine(auditStartMessage.getViolation(), "[WARN] " + expectedPath + ":3:14: " + invalidPatternMessageMain.getViolation() + " [TypeName]", "[WARN] " + expectedPath + ":5:7: " + invalidPatternMessageMainInner.getViolation() + " [TypeName]", auditFinishMessage.getViolation()));
    assertWithMessage("Unexpected system error log").that(systemErr.getCapturedData()).isEqualTo("");
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) Test(org.junit.jupiter.api.Test)

Example 27 with Violation

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

the class MainTest method testCustomSimpleRootModule.

@Test
public void testCustomSimpleRootModule(@SysErr Capturable systemErr) {
    TestRootModuleChecker.reset();
    assertMainReturnCode(-2, "-c", getPath("InputMainConfig-custom-simple-root-module.xml"), getPath("InputMain.java"));
    final String checkstylePackage = "com.puppycrawl.tools.checkstyle.";
    final Violation unableToInstantiateExceptionMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, "PackageObjectFactory.unableToInstantiateExceptionMessage", new String[] { "TestRootModuleChecker", checkstylePackage + "TestRootModuleChecker, " + "TestRootModuleCheckerCheck, " + checkstylePackage + "TestRootModuleCheckerCheck" }, null, getClass(), null);
    assertWithMessage("Unexpected system error log").that(systemErr.getCapturedData().startsWith(checkstylePackage + "api.CheckstyleException: " + unableToInstantiateExceptionMessage.getViolation())).isTrue();
    assertWithMessage("Invalid checker state").that(TestRootModuleChecker.isProcessed()).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) Test(org.junit.jupiter.api.Test)

Example 28 with Violation

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

the class MainTest method testExistingTargetFileWithOneErrorAgainstSunCheck.

@Test
public void testExistingTargetFileWithOneErrorAgainstSunCheck(@SysErr Capturable systemErr, @SysOut Capturable systemOut) throws Exception {
    assertMainReturnCode(1, "-c", "/sun_checks.xml", getPath("InputMain1.java"));
    final Violation errorCounterTwoMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, Main.ERROR_COUNTER, new String[] { String.valueOf(1) }, null, getClass(), null);
    final Violation message = new Violation(1, "com.puppycrawl.tools.checkstyle.checks.javadoc.messages", "javadoc.packageInfo", new String[] {}, null, getClass(), null);
    final String expectedPath = getFilePath("InputMain1.java");
    assertWithMessage("Unexpected output log").that(systemOut.getCapturedData()).isEqualTo(addEndOfLine(auditStartMessage.getViolation(), "[ERROR] " + expectedPath + ":1: " + message.getViolation() + " [JavadocPackage]", auditFinishMessage.getViolation()));
    assertWithMessage("Unexpected system error log").that(systemErr.getCapturedData()).isEqualTo(addEndOfLine(errorCounterTwoMessage.getViolation()));
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) Test(org.junit.jupiter.api.Test)

Example 29 with Violation

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

the class SarifLoggerTest method testEmpty.

@Test
public void testEmpty() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "null", null, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, null, violation);
    logger.fileStarted(ev);
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerEmpty.sarif"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 30 with Violation

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

the class SarifLoggerTest method testAddErrorWithWarningLevel.

@Test
public void testAddErrorWithWarningLevel() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "ruleId", null, SeverityLevel.WARNING, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.fileStarted(ev);
    logger.addError(ev);
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerSingleWarning.sarif"), outStream);
}
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