Search in sources :

Example 11 with LocalizedMessage

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

the class TranslationCheck method validateUserSpecifiedLanguageCodes.

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

Example 12 with LocalizedMessage

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

the class Checker method fireErrors.

/**
     * Notify all listeners about the errors in a file.
     *
     * @param fileName the audited file
     * @param errors the audit errors from the file
     */
@Override
public void fireErrors(String fileName, SortedSet<LocalizedMessage> errors) {
    final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName);
    boolean hasNonFilteredViolations = false;
    for (final LocalizedMessage element : errors) {
        final AuditEvent event = new AuditEvent(this, stripped, element);
        if (filters.accept(event)) {
            hasNonFilteredViolations = true;
            for (final AuditListener listener : listeners) {
                listener.addError(event);
            }
        }
    }
    if (hasNonFilteredViolations && cache != null) {
        cache.remove(fileName);
    }
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage)

Example 13 with LocalizedMessage

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

the class CheckerTest method testAddFilter.

@Test
public void testAddFilter() {
    final Checker checker = new Checker();
    final DebugFilter filter = new DebugFilter();
    checker.addFilter(filter);
    filter.resetFilter();
    final SortedSet<LocalizedMessage> messages = new TreeSet<>();
    messages.add(new LocalizedMessage(0, 0, "a Bundle", "message.key", new Object[] { "arg" }, null, getClass(), null));
    checker.fireErrors("Some File Name", messages);
    assertTrue("Checker.fireErrors() doesn't call filter", filter.wasCalled());
}
Also used : TreeSet(java.util.TreeSet) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 14 with LocalizedMessage

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

the class CheckerTest method testAddListener.

@Test
public void testAddListener() throws Exception {
    final Checker checker = new Checker();
    final DebugAuditAdapter auditAdapter = new DebugAuditAdapter();
    checker.addListener(auditAdapter);
    // Let's try fire some events
    getFireAuditStartedMethod().invoke(checker);
    assertTrue("Checker.fireAuditStarted() doesn't call listener", auditAdapter.wasCalled());
    auditAdapter.resetListener();
    getFireAuditFinished().invoke(checker);
    assertTrue("Checker.fireAuditFinished() doesn't call listener", auditAdapter.wasCalled());
    auditAdapter.resetListener();
    checker.fireFileStarted("Some File Name");
    assertTrue("Checker.fireFileStarted() doesn't call listener", auditAdapter.wasCalled());
    auditAdapter.resetListener();
    checker.fireFileFinished("Some File Name");
    assertTrue("Checker.fireFileFinished() doesn't call listener", auditAdapter.wasCalled());
    auditAdapter.resetListener();
    final SortedSet<LocalizedMessage> messages = new TreeSet<>();
    messages.add(new LocalizedMessage(0, 0, "a Bundle", "message.key", new Object[] { "arg" }, null, getClass(), null));
    checker.fireErrors("Some File Name", messages);
    assertTrue("Checker.fireErrors() doesn't call listener", auditAdapter.wasCalled());
}
Also used : TreeSet(java.util.TreeSet) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 15 with LocalizedMessage

use of com.puppycrawl.tools.checkstyle.api.LocalizedMessage in project maven-plugins by apache.

the class CheckstyleReportListenerMultiSourceTest method testListeners.

public void testListeners() {
    fireAuditStarted(null);
    AuditEvent event = new AuditEvent(this, "/source/path/file1", null);
    fireFileStarted(event);
    LocalizedMessage message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.INFO, null, getClass(), null);
    fireAddError(new AuditEvent(this, "/source/path/file1", message));
    fireFileFinished(event);
    event = new AuditEvent(this, "/source/path2/file2", null);
    fireFileStarted(event);
    message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.WARNING, null, getClass(), null);
    fireAddError(new AuditEvent(this, "/source/path2/file2", message));
    fireAddError(new AuditEvent(this, "/source/path2/file2", message));
    fireFileFinished(event);
    event = new AuditEvent(this, "/source/path/file3", null);
    fireFileStarted(event);
    message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.ERROR, null, getClass(), null);
    fireAddError(new AuditEvent(this, "/source/path/file3", message));
    fireAddError(new AuditEvent(this, "/source/path/file3", message));
    fireAddError(new AuditEvent(this, "/source/path/file3", message));
    fireFileFinished(event);
    event = new AuditEvent(this, "/source/path2/file4", null);
    fireFileStarted(event);
    message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.IGNORE, null, getClass(), null);
    fireAddError(new AuditEvent(this, "/source/path2/file4", message));
    fireAddError(new AuditEvent(this, "/source/path2/file4", message));
    fireAddError(new AuditEvent(this, "/source/path2/file4", message));
    fireAddError(new AuditEvent(this, "/source/path2/file4", message));
    fireFileFinished(event);
    fireAuditFinished(null);
    CheckstyleCheckerListener listener = listenerMap.get(SeverityLevel.INFO);
    CheckstyleResults results = listener.getResults();
    assertEquals("Test total files", 4, results.getFiles().size());
    assertEquals("Test file count", 4, results.getFileCount());
    assertEquals("test file violations", 1, results.getFileViolations("file1").size());
    assertEquals("test file severities", 1, results.getSeverityCount("file1", SeverityLevel.INFO));
    assertEquals("test file severities", 0, results.getSeverityCount("file1", SeverityLevel.WARNING));
    assertEquals("test file severities", 0, results.getSeverityCount("file1", SeverityLevel.ERROR));
    assertEquals("test file severities", 0, results.getSeverityCount("file1", SeverityLevel.IGNORE));
    listener = listenerMap.get(SeverityLevel.WARNING);
    results = listener.getResults();
    assertEquals("Test total files", 4, results.getFiles().size());
    assertEquals("Test file count", 4, results.getFileCount());
    assertEquals("test file violations", 2, results.getFileViolations("file2").size());
    assertEquals("test file severities", 0, results.getSeverityCount("file2", SeverityLevel.INFO));
    assertEquals("test file severities", 2, results.getSeverityCount("file2", SeverityLevel.WARNING));
    assertEquals("test file severities", 0, results.getSeverityCount("file2", SeverityLevel.ERROR));
    assertEquals("test file severities", 0, results.getSeverityCount("file2", SeverityLevel.IGNORE));
    listener = listenerMap.get(SeverityLevel.ERROR);
    results = listener.getResults();
    assertEquals("Test total files", 4, results.getFiles().size());
    assertEquals("Test file count", 4, results.getFileCount());
    assertEquals("test file violations", 3, results.getFileViolations("file3").size());
    assertEquals("test file severities", 0, results.getSeverityCount("file3", SeverityLevel.INFO));
    assertEquals("test file severities", 0, results.getSeverityCount("file3", SeverityLevel.WARNING));
    assertEquals("test file severities", 3, results.getSeverityCount("file3", SeverityLevel.ERROR));
    assertEquals("test file severities", 0, results.getSeverityCount("file3", SeverityLevel.IGNORE));
    listener = listenerMap.get(SeverityLevel.IGNORE);
    results = listener.getResults();
    assertEquals("Test total files", 4, results.getFiles().size());
    assertEquals("Test file count", 4, results.getFileCount());
    assertEquals("test file violations", 0, results.getFileViolations("file4").size());
    assertEquals("test file severities", 0, results.getSeverityCount("file4", SeverityLevel.INFO));
    assertEquals("test file severities", 0, results.getSeverityCount("file4", SeverityLevel.WARNING));
    assertEquals("test file severities", 0, results.getSeverityCount("file4", SeverityLevel.ERROR));
    assertEquals("test file severities", 0, results.getSeverityCount("file4", SeverityLevel.IGNORE));
}
Also used : CheckstyleResults(org.apache.maven.plugins.checkstyle.exec.CheckstyleResults) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) CheckstyleCheckerListener(org.apache.maven.plugins.checkstyle.exec.CheckstyleCheckerListener) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage)

Aggregations

LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)37 Test (org.junit.Test)29 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)24 TreeSet (java.util.TreeSet)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 CheckstyleCheckerListener (org.apache.maven.plugins.checkstyle.exec.CheckstyleCheckerListener)2 CheckstyleResults (org.apache.maven.plugins.checkstyle.exec.CheckstyleResults)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)1 FileNotFoundException (java.io.FileNotFoundException)1 RandomAccessFile (java.io.RandomAccessFile)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1