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());
}
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();
}
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");
}
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);
}
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);
}
}
}
Aggregations