Search in sources :

Example 76 with Violation

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

the class XpathFileGeneratorAstFilterTest method testAcceptNoToken.

@Test
public void testAcceptNoToken() {
    final Violation violation = new Violation(0, 0, 0, null, null, null, null, null, XpathFileGeneratorAstFilterTest.class, null);
    final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(null, null, violation, null);
    final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
    assertWithMessage("filter accepted").that(filter.accept(event)).isTrue();
    final AuditEvent auditEvent = new AuditEvent(this, "Test.java", violation);
    assertWithMessage("filter has no queries").that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent)).isNull();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 77 with Violation

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

the class XpathFileGeneratorAstFilterTest method testTabWidth.

@Test
public void testTabWidth() throws Exception {
    final Violation violation = new Violation(6, 7, TokenTypes.LITERAL_RETURN, "messages.properties", null, null, SeverityLevel.ERROR, null, XpathFileGeneratorAstFilterTest.class, null);
    final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent("InputXpathFileGeneratorAstFilter.java", violation);
    final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
    filter.setTabWidth(6);
    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/METHOD_DEF[./IDENT[@text='tabMethod']]/SLIST/LITERAL_RETURN");
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 78 with Violation

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

the class NewlineAtEndOfFileCheckTest method testWrongFile.

@Test
public void testWrongFile() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(NewlineAtEndOfFileCheck.class);
    final NewlineAtEndOfFileCheck check = new NewlineAtEndOfFileCheck();
    check.configure(checkConfig);
    final List<String> lines = new ArrayList<>(1);
    lines.add("txt");
    final File impossibleFile = new File("");
    final FileText fileText = new FileText(impossibleFile, lines);
    final Set<Violation> violations = check.process(impossibleFile, fileText);
    assertWithMessage("Amount of violations is unexpected").that(violations).hasSize(1);
    final Iterator<Violation> iterator = violations.iterator();
    assertWithMessage("Violation message differs from expected").that(iterator.next().getViolation()).isEqualTo(getCheckMessage(MSG_KEY_UNABLE_OPEN, ""));
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) ArrayList(java.util.ArrayList) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) FileText(com.puppycrawl.tools.checkstyle.api.FileText) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 79 with Violation

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

the class XMLLoggerTest method testAddErrorWithNullFileName.

@Test
public void testAddErrorWithNullFileName() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, null, violation);
    logger.addError(ev);
    logger.auditFinished(null);
    verifyXml(getPath("ExpectedXMLLoggerErrorNullFileName.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 80 with Violation

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

the class XMLLoggerTest method testAddError.

@Test
public void testAddError() throws Exception {
    final XMLLogger logger = new XMLLogger(outStream, OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", violation);
    logger.fileStarted(ev);
    logger.addError(ev);
    logger.fileFinished(ev);
    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)

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