Search in sources :

Example 6 with Violation

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

the class OrderedPropertiesCheckTest method testIoException.

/**
 * Tests IO exception, that can occur during reading of properties file.
 */
@Test
public void testIoException() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
    final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
    check.configure(checkConfig);
    final String fileName = getPath("InputOrderedPropertiesCheckNotExisting.properties");
    final File file = new File(fileName);
    final FileText fileText = new FileText(file, Collections.emptyList());
    final SortedSet<Violation> violations = check.process(file, fileText);
    assertWithMessage("Wrong violations count").that(violations).hasSize(1);
    final Violation violation = violations.iterator().next();
    final String retrievedMessage = violations.iterator().next().getKey();
    assertWithMessage("violation key is not valid").that(retrievedMessage).isEqualTo("unable.open.cause");
    assertWithMessage("violation is not valid").that(getCheckMessage(MSG_IO_EXCEPTION_KEY, fileName, getFileNotFoundDetail(file))).isEqualTo(violation.getViolation());
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 7 with Violation

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

the class SuppressWarningsHolderTest method testIsSuppressedWithAllArgument.

@Test
public void testIsSuppressedWithAllArgument() throws Exception {
    populateHolder("all", 100, 100, 350, 350);
    final Checker source = new Checker();
    final Violation firstViolationForTest = new Violation(100, 10, null, null, null, "id", MemberNameCheck.class, "msg");
    final AuditEvent firstEventForTest = new AuditEvent(source, "fileName", firstViolationForTest);
    assertWithMessage("Event is suppressed").that(SuppressWarningsHolder.isSuppressed(firstEventForTest)).isFalse();
    final Violation secondViolationForTest = new Violation(100, 150, null, null, null, "id", MemberNameCheck.class, "msg");
    final AuditEvent secondEventForTest = new AuditEvent(source, "fileName", secondViolationForTest);
    assertWithMessage("Event is not suppressed").that(SuppressWarningsHolder.isSuppressed(secondEventForTest)).isTrue();
    final Violation thirdViolationForTest = new Violation(200, 1, null, null, null, "id", MemberNameCheck.class, "msg");
    final AuditEvent thirdEventForTest = new AuditEvent(source, "fileName", thirdViolationForTest);
    assertWithMessage("Event is not suppressed").that(SuppressWarningsHolder.isSuppressed(thirdEventForTest)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) Checker(com.puppycrawl.tools.checkstyle.Checker) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 8 with Violation

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

the class XpathFileGeneratorAstFilterTest method test.

@Test
public void test() throws Exception {
    final Violation violation = new Violation(3, 47, 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 xpath").that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent)).isEqualTo("/COMPILATION_UNIT/CLASS_DEF[./IDENT" + "[@text='InputXpathFileGeneratorAstFilter']]/OBJBLOCK/LCURLY");
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 9 with Violation

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

the class TranslationCheck method logException.

/**
 * Helper method to log an exception.
 *
 * @param exception the exception that occurred
 * @param file the file that could not be processed
 */
private void logException(Exception exception, File file) {
    final String[] args;
    final String key;
    if (exception instanceof NoSuchFileException) {
        args = null;
        key = "general.fileNotFound";
    } else {
        args = new String[] { exception.getMessage() };
        key = "general.exception";
    }
    final Violation message = new Violation(0, Definitions.CHECKSTYLE_BUNDLE, key, args, getId(), getClass(), null);
    final SortedSet<Violation> messages = new TreeSet<>();
    messages.add(message);
    getMessageDispatcher().fireErrors(file.getPath(), messages);
    log.debug("Exception occurred.", exception);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeSet(java.util.TreeSet) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 10 with Violation

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

the class TranslationCheck method validateUserSpecifiedLanguageCodes.

/**
 * Validates the correctness of user specified language codes for the check.
 *
 * @param languageCodes user specified language codes for the check.
 * @throws IllegalArgumentException when any item of languageCodes is not valid language code
 */
private void validateUserSpecifiedLanguageCodes(Set<String> languageCodes) {
    for (String code : languageCodes) {
        if (!isValidLanguageCode(code)) {
            final Violation msg = new Violation(1, TRANSLATION_BUNDLE, WRONG_LANGUAGE_CODE_KEY, new Object[] { code }, getId(), getClass(), null);
            final String exceptionMessage = String.format(Locale.ROOT, "%s [%s]", msg.getViolation(), TranslationCheck.class.getSimpleName());
            throw new IllegalArgumentException(exceptionMessage);
        }
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation)

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