Search in sources :

Example 21 with AuditEvent

use of com.puppycrawl.tools.checkstyle.api.AuditEvent 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)

Example 22 with AuditEvent

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

the class CheckstyleResultsTest method testResults.

public void testResults() {
    Map<String, List<AuditEvent>> files = new HashMap<>();
    LocalizedMessage message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.INFO, null, getClass(), null);
    AuditEvent event = new AuditEvent(this, "file1", message);
    files.put("file1", Collections.singletonList(event));
    message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.WARNING, null, getClass(), null);
    List<AuditEvent> events = new ArrayList<>();
    events.add(new AuditEvent(this, "file2", message));
    events.add(new AuditEvent(this, "file2", message));
    files.put("file2", events);
    message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.ERROR, null, getClass(), null);
    events = new ArrayList<>();
    events.add(new AuditEvent(this, "file3", message));
    events.add(new AuditEvent(this, "file3", message));
    events.add(new AuditEvent(this, "file3", message));
    files.put("file3", events);
    message = new LocalizedMessage(0, 0, "", "", null, SeverityLevel.IGNORE, null, getClass(), null);
    events = new ArrayList<>();
    events.add(new AuditEvent(this, "file4", message));
    events.add(new AuditEvent(this, "file4", message));
    events.add(new AuditEvent(this, "file4", message));
    events.add(new AuditEvent(this, "file4", message));
    files.put("file4", events);
    results.setFiles(files);
    assertEquals("test total files", 4, results.getFiles().size());
    assertEquals("test file count", 4, results.getFileCount());
    assertEquals("test file severities", 0, results.getSeverityCount("file0", SeverityLevel.INFO));
    assertEquals("test file severities", 0, results.getSeverityCount("file0", SeverityLevel.WARNING));
    assertEquals("test file severities", 0, results.getSeverityCount("file0", SeverityLevel.ERROR));
    assertEquals("test file severities", 0, results.getSeverityCount("file0", SeverityLevel.IGNORE));
    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));
    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));
    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));
    assertEquals("test file violations", 4, 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", 4, results.getSeverityCount("file4", SeverityLevel.IGNORE));
    assertEquals("test INFO severity count", 1, results.getSeverityCount(SeverityLevel.INFO));
    assertEquals("test WARNING severity count", 2, results.getSeverityCount(SeverityLevel.WARNING));
    assertEquals("test ERROR severity count", 3, results.getSeverityCount(SeverityLevel.ERROR));
    assertEquals("test IGNORE severity count", 4, results.getSeverityCount(SeverityLevel.IGNORE));
    results.setFileViolations("file", Collections.<AuditEvent>emptyList());
    assertEquals("test file violations", 0, results.getFileViolations("file").size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage)

Example 23 with AuditEvent

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

the class AuditEventDefaultFormatterTest method testFormatModuleNameDoesNotContainCheckSuffix.

@Test
public void testFormatModuleNameDoesNotContainCheckSuffix() {
    final AuditEvent mock = PowerMockito.mock(AuditEvent.class);
    when(mock.getSourceName()).thenReturn("TestModule");
    when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING);
    when(mock.getLine()).thenReturn(1);
    when(mock.getColumn()).thenReturn(1);
    when(mock.getMessage()).thenReturn("Mocked message.");
    when(mock.getFileName()).thenReturn("InputMockFile.java");
    final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
    final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]";
    assertEquals(expected, formatter.format(mock));
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with AuditEvent

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

the class AuditEventDefaultFormatterTest method testFormatFullyQualifiedModuleNameContainsCheckSuffix.

@Test
public void testFormatFullyQualifiedModuleNameContainsCheckSuffix() {
    final AuditEvent mock = PowerMockito.mock(AuditEvent.class);
    when(mock.getSourceName()).thenReturn("com.test.package.TestModuleCheck");
    when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING);
    when(mock.getLine()).thenReturn(1);
    when(mock.getColumn()).thenReturn(1);
    when(mock.getMessage()).thenReturn("Mocked message.");
    when(mock.getFileName()).thenReturn("InputMockFile.java");
    final AuditEventFormatter formatter = new AuditEventDefaultFormatter();
    final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]";
    assertEquals(expected, formatter.format(mock));
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with AuditEvent

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

the class SuppressWarningsHolderTest method testIsSuppressed.

@Test
public void testIsSuppressed() throws Exception {
    final Class<?> entry = Class.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
    final Constructor<?> entryConstructor = entry.getDeclaredConstructor(String.class, int.class, int.class, int.class, int.class);
    entryConstructor.setAccessible(true);
    final Object entryInstance = entryConstructor.newInstance("MockEntry", 100, 100, 350, 350);
    final List<Object> entriesList = new ArrayList<>();
    entriesList.add(entryInstance);
    final ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
    PowerMockito.doReturn(entriesList).when(threadLocal, "get");
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final Field entries = holder.getClass().getDeclaredField("ENTRIES");
    entries.setAccessible(true);
    entries.set(holder, threadLocal);
    final Checker source = new Checker();
    final LocalizedMessage message = new LocalizedMessage(100, 10, null, null, null, "id", MemberNameCheck.class, "message");
    final AuditEvent event = new AuditEvent(source, "fileName", message);
    assertFalse(SuppressWarningsHolder.isSuppressed(event));
}
Also used : Field(java.lang.reflect.Field) Checker(com.puppycrawl.tools.checkstyle.Checker) ArrayList(java.util.ArrayList) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)47 Test (org.junit.Test)38 LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)4 ArrayList (java.util.ArrayList)4 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)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 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SinkEventAttributeSet (org.apache.maven.doxia.sink.SinkEventAttributeSet)1