Search in sources :

Example 66 with Violation

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

the class TranslationCheckTest method testLogIoExceptionFileNotFound.

@Test
public void testLogIoExceptionFileNotFound() throws Exception {
    // I can't put wrong file here. Checkstyle fails before check started.
    // I saw some usage of file or handling of wrong file in Checker, or somewhere
    // in checks running part. So I had to do it with reflection to improve coverage.
    final TranslationCheck check = new TranslationCheck();
    final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
    final TestMessageDispatcher dispatcher = new TestMessageDispatcher();
    check.configure(checkConfig);
    check.setMessageDispatcher(dispatcher);
    final Set<String> keys = TestUtil.invokeMethod(check, "getTranslationKeys", new File(".no.such.file"));
    assertWithMessage("Translation keys should be empty when File is not found").that(keys).isEmpty();
    assertWithMessage("expected number of errors to fire").that(dispatcher.savedErrors).hasSize(1);
    final Violation violation = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, "general.fileNotFound", null, null, getClass(), null);
    assertWithMessage("Invalid violation").that(dispatcher.savedErrors.iterator().next().getViolation()).isEqualTo(violation.getViolation());
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 67 with Violation

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

the class TranslationCheckTest method testLogIoException.

@Test
public void testLogIoException() throws Exception {
    // I can't put wrong file here. Checkstyle fails before check started.
    // I saw some usage of file or handling of wrong file in Checker, or somewhere
    // in checks running part. So I had to do it with reflection to improve coverage.
    final TranslationCheck check = new TranslationCheck();
    final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
    final TestMessageDispatcher dispatcher = new TestMessageDispatcher();
    check.configure(checkConfig);
    check.setMessageDispatcher(dispatcher);
    final Exception exception = new IOException("test exception");
    TestUtil.invokeMethod(check, "logException", exception, new File(""));
    assertWithMessage("expected number of errors to fire").that(dispatcher.savedErrors.size()).isEqualTo(1);
    final Violation violation = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, "general.exception", new String[] { exception.getMessage() }, null, getClass(), null);
    assertWithMessage("Invalid violation").that(dispatcher.savedErrors.iterator().next().getViolation()).isEqualTo(violation.getViolation());
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 68 with Violation

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

the class OrderedPropertiesCheckTest method testKeepForLoopIntact.

/**
 * This test validates the PIT mutation of getIndex().
 * Here the for statement for
 * (int index = startLineNo; index < fileText.size(); index++)
 * will change to
 * for (int index = startLineNo; true; index++)
 * By creating a FileText having no lines it makes sure that
 * fileText.size() returning zero size.
 * This will keep the for loop intact.
 */
@Test
public void testKeepForLoopIntact() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(OrderedPropertiesCheck.class);
    final OrderedPropertiesCheck check = new OrderedPropertiesCheck();
    check.configure(checkConfig);
    final String fileName = getPath("InputOrderedProperties2EmptyValue.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);
}
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 69 with Violation

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

the class RequireThisCheckTest method testDefaultSwitch.

@Test
public void testDefaultSwitch() {
    final RequireThisCheck check = new RequireThisCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.ENUM, "ENUM"));
    check.visitToken(ast);
    final SortedSet<Violation> violations = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations).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 70 with Violation

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

the class UniquePropertiesCheckTest method testIoException.

/**
 * Tests IO exception, that can occur during reading of properties file.
 */
@Test
public void testIoException() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(UniquePropertiesCheck.class);
    final UniquePropertiesCheck check = new UniquePropertiesCheck();
    check.configure(checkConfig);
    final String fileName = getPath("InputUniquePropertiesCheckNotExisting.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 messages count: " + violations.size()).that(violations).hasSize(1);
    final Violation violation = violations.iterator().next();
    final String retrievedMessage = violations.iterator().next().getKey();
    assertWithMessage("violation key '" + retrievedMessage + "' is not valid").that(retrievedMessage).isEqualTo("unable.open.cause");
    assertWithMessage("violation '" + violation.getViolation() + "' 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)

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