Search in sources :

Example 21 with LocalizedMessage

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

Example 22 with LocalizedMessage

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

the class XMLLoggerTest method testAddError.

@Test
public void testAddError() throws IOException {
    final XMLLogger logger = new XMLLogger(outStream, true);
    logger.auditStarted(null);
    final LocalizedMessage message = new LocalizedMessage(1, 1, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", message);
    logger.addError(ev);
    logger.auditFinished(null);
    final String[] expectedLines = { "<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\"" + " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>" };
    verifyLines(expectedLines);
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 23 with LocalizedMessage

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

the class XMLLoggerTest method testAddIgnored.

@Test
public void testAddIgnored() throws IOException {
    final XMLLogger logger = new XMLLogger(outStream, true);
    logger.auditStarted(null);
    final LocalizedMessage message = new LocalizedMessage(1, 1, "messages.properties", "key", null, SeverityLevel.IGNORE, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "Test.java", message);
    logger.addError(ev);
    logger.auditFinished(null);
    final String[] expectedLines = CommonUtils.EMPTY_STRING_ARRAY;
    verifyLines(expectedLines);
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 24 with LocalizedMessage

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

the class NewlineAtEndOfFileCheckTest method testWrongFile.

@Test
public void testWrongFile() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(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 Set<LocalizedMessage> messages = check.process(impossibleFile, lines);
    assertEquals(1, messages.size());
    final Iterator<LocalizedMessage> iterator = messages.iterator();
    assertEquals(getCheckMessage(MSG_KEY_UNABLE_OPEN, ""), iterator.next().getMessage());
}
Also used : ArrayList(java.util.ArrayList) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

Example 25 with LocalizedMessage

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

the class SuppressElementTest method testDecideByFileNameAndModuleMatchingModuleNotEqual.

@Test
public void testDecideByFileNameAndModuleMatchingModuleNotEqual() {
    final LocalizedMessage message = new LocalizedMessage(10, 10, "", "", null, "TheirModule", getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
    filter.setModuleId("MyModule");
    assertTrue(filter.accept(ev));
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage) Test(org.junit.Test)

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