use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.
the class SuppressionCommentFilterTest method getTagsAfterExecution.
/**
* Calls the filter with a minimal set of inputs and returns a list of
* {@link SuppressionCommentFilter} internal type {@code Tag}.
* Our goal is 100% test coverage, for this we use white-box testing.
* So we need access to the implementation details. For this reason,
* it is necessary to use reflection to gain access to the inner field here.
*
* @return {@code Tag} list
*/
private static List<Comparable<Object>> getTagsAfterExecution(SuppressionCommentFilter filter, String filename, String... lines) {
final FileContents contents = new FileContents(new FileText(new File(filename), Arrays.asList(lines)));
for (int lineNo = 0; lineNo < lines.length; lineNo++) {
final int colNo = lines[lineNo].indexOf("//");
if (colNo >= 0) {
contents.reportSingleLineComment(lineNo + 1, colNo);
}
}
final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, filename, new Violation(1, null, null, null, null, Object.class, ""), null);
filter.accept(dummyEvent);
return TestUtil.getInternalState(filter, "tags");
}
use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.
the class SuppressionXpathSingleFilterTest method testThrowException.
@Test
public void testThrowException() {
final String xpath = "//CLASS_DEF[@text='InputSuppressionXpathSingleFilter']";
final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter("InputSuppressionXpathSingleFilter", "Test", null, null, xpath);
final Violation message = new Violation(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null);
final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, null);
try {
filter.accept(ev);
assertWithMessage("Exception is expected").fail();
} catch (IllegalStateException ex) {
assertWithMessage("Exception message does not match expected one").that(ex.getMessage()).contains("Cannot initialize context and evaluate query");
}
}
use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.
the class SuppressionXpathSingleFilterTest method testNonMatchingChecks.
@Test
public void testNonMatchingChecks() throws Exception {
final String xpath = "NON_MATCHING_QUERY";
final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter("InputSuppressionXpathSingleFilter", "NonMatchingRegexp", null, "id19", xpath);
final Violation message = new Violation(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null);
final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
assertWithMessage("Event should be accepted").that(filter.accept(ev)).isTrue();
}
use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.
the class SuppressionXpathSingleFilterTest method testDecideByMessage.
@Test
public void testDecideByMessage() throws Exception {
final Violation message = new Violation(0, 0, TokenTypes.CLASS_DEF, "", "", null, null, null, getClass(), "Test");
final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
final SuppressionXpathSingleFilter filter1 = createSuppressionXpathSingleFilter(null, null, "Test", null, null);
final SuppressionXpathSingleFilter filter2 = createSuppressionXpathSingleFilter(null, null, "Bad", null, null);
assertWithMessage("Message match").that(filter1.accept(ev)).isFalse();
assertWithMessage("Message not match").that(filter2.accept(ev)).isTrue();
}
use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.
the class SuppressWithPlainTextCommentFilterTest method testFilterWithDirectory.
@Test
public void testFilterWithDirectory() throws IOException {
final SuppressWithPlainTextCommentFilter filter = new SuppressWithPlainTextCommentFilter();
final AuditEvent event = new AuditEvent(this, getPath(""), new Violation(1, 1, "bundle", "key", null, SeverityLevel.ERROR, "moduleId", getClass(), "customMessage"));
assertWithMessage("filter should accept directory").that(filter.accept(event)).isTrue();
}
Aggregations