Search in sources :

Example 91 with Violation

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

the class MainTest method testExistingTargetFileWithOneError.

/**
 * Similar test to {@link #testExistingTargetFileWithError}, but for PIT mutation tests:
 * this test fails if the boundary condition is changed from {@code if (exitStatus > 0)}
 * to {@code if (exitStatus > 1)}.
 *
 * @throws Exception should not throw anything
 */
@Test
public void testExistingTargetFileWithOneError(@SysErr Capturable systemErr, @SysOut Capturable systemOut) throws Exception {
    assertMainReturnCode(1, "-c", getPath("InputMainConfig-classname2-error.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 invalidPatternMessageMain = new Violation(1, "com.puppycrawl.tools.checkstyle.checks.naming.messages", "name.invalidPattern", new String[] { "InputMain1", "^[a-z0-9]*$" }, null, getClass(), null);
    final String expectedPath = getFilePath("InputMain1.java");
    assertWithMessage("Unexpected output log").that(systemOut.getCapturedData()).isEqualTo(addEndOfLine(auditStartMessage.getViolation(), "[ERROR] " + expectedPath + ":3:14: " + invalidPatternMessageMain.getViolation() + " [TypeName]", 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 92 with Violation

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

the class MainTest method testExistingDirectoryWithViolations.

@Test
public void testExistingDirectoryWithViolations(@SysErr Capturable systemErr, @SysOut Capturable systemOut) throws IOException {
    // we just reference there all violations
    final String[][] outputValues = { { "InputMainComplexityOverflow", "1", "172" } };
    final int allowedLength = 170;
    final String msgKey = "maxLen.file";
    final String bundle = "com.puppycrawl.tools.checkstyle.checks.sizes.messages";
    assertMainReturnCode(0, "-c", getPath("InputMainConfig-filelength.xml"), getPath(""));
    final String expectedPath = getFilePath("") + File.separator;
    final StringBuilder sb = new StringBuilder(28);
    sb.append(auditStartMessage.getViolation()).append(EOL);
    final String format = "[WARN] " + expectedPath + outputValues[0][0] + ".java:" + outputValues[0][1] + ": ";
    for (String[] outputValue : outputValues) {
        final String violation = new Violation(1, bundle, msgKey, new Integer[] { Integer.valueOf(outputValue[2]), allowedLength }, null, getClass(), null).getViolation();
        final String line = format + violation + " [FileLength]";
        sb.append(line).append(EOL);
    }
    sb.append(auditFinishMessage.getViolation()).append(EOL);
    assertWithMessage("Unexpected output log").that(systemOut.getCapturedData()).isEqualTo(sb.toString());
    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 93 with Violation

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

the class MainTest method testLoadPropertiesIoException.

@Test
public void testLoadPropertiesIoException() throws Exception {
    final Class<?>[] param = new Class<?>[1];
    param[0] = File.class;
    final Class<?> cliOptionsClass = Class.forName(Main.class.getName());
    final Method method = cliOptionsClass.getDeclaredMethod("loadProperties", param);
    method.setAccessible(true);
    try {
        method.invoke(null, new File("."));
        assertWithMessage("Exception was expected").fail();
    } catch (ReflectiveOperationException ex) {
        assertWithMessage("Invalid error cause").that(ex).hasCauseThat().isInstanceOf(CheckstyleException.class);
        // We do separate validation for message as in Windows
        // disk drive letter appear in message,
        // so we skip that drive letter for compatibility issues
        final Violation loadPropertiesMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, Main.LOAD_PROPERTIES_EXCEPTION, new String[] { "" }, null, getClass(), null);
        final String causeMessage = ex.getCause().getLocalizedMessage();
        final String violation = loadPropertiesMessage.getViolation();
        final boolean samePrefix = causeMessage.substring(0, causeMessage.indexOf(' ')).equals(violation.substring(0, violation.indexOf(' ')));
        final boolean sameSuffix = causeMessage.substring(causeMessage.lastIndexOf(' ')).equals(violation.substring(violation.lastIndexOf(' ')));
        assertWithMessage("Invalid violation").that(samePrefix || sameSuffix).isTrue();
        assertWithMessage("Invalid violation").that(causeMessage.contains(".'")).isTrue();
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Method(java.lang.reflect.Method) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 94 with Violation

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

the class MainTest method testExistingTargetFileWithError.

@Test
public void testExistingTargetFileWithError(@SysErr Capturable systemErr, @SysOut Capturable systemOut) throws Exception {
    assertMainReturnCode(2, "-c", getPath("InputMainConfig-classname2-error.xml"), getPath("InputMain.java"));
    final Violation errorCounterTwoMessage = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, Main.ERROR_COUNTER, new String[] { String.valueOf(2) }, null, getClass(), null);
    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(), "[ERROR] " + expectedPath + ":3:14: " + invalidPatternMessageMain.getViolation() + " [TypeName]", "[ERROR] " + expectedPath + ":5:7: " + invalidPatternMessageMainInner.getViolation() + " [TypeName]", 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 95 with Violation

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

the class SarifLoggerTest method testAddExceptions.

@Test
public void testAddExceptions() 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);
    final Violation violation2 = new Violation(1, 1, "messages.properties", "null", null, null, getClass(), "found an error");
    final AuditEvent ev2 = new AuditEvent(this, "Test.java", violation2);
    logger.fileStarted(ev);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    logger.fileFinished(ev);
    logger.fileStarted(ev2);
    logger.addException(ev2, new TestException("msg2", new RuntimeException("msg2")));
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerDoubleException.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