Search in sources :

Example 1 with CloseAndFlushTestByteArrayOutputStream

use of com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream in project checkstyle by checkstyle.

the class CheckerTest method testDefaultLoggerClosesItStreams.

// -@cs[CheckstyleTestMakeup] must use raw class to directly initialize DefaultLogger
@Test
public void testDefaultLoggerClosesItStreams() throws Exception {
    final Checker checker = new Checker();
    try (CloseAndFlushTestByteArrayOutputStream testInfoOutputStream = new CloseAndFlushTestByteArrayOutputStream();
        CloseAndFlushTestByteArrayOutputStream testErrorOutputStream = new CloseAndFlushTestByteArrayOutputStream()) {
        checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
        checker.addListener(new DefaultLogger(testInfoOutputStream, OutputStreamOptions.CLOSE, testErrorOutputStream, OutputStreamOptions.CLOSE));
        final File tmpFile = File.createTempFile("file", ".java", temporaryFolder);
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
        verify(checker, tmpFile.getPath(), expected);
        assertWithMessage("Output stream close count").that(testInfoOutputStream.getCloseCount()).isEqualTo(1);
        assertWithMessage("Output stream flush count").that(testInfoOutputStream.getFlushCount()).isEqualTo(TestUtil.adjustFlushCountForOutputStreamClose(3));
        assertWithMessage("Error stream close count").that(testErrorOutputStream.getCloseCount()).isEqualTo(1);
        assertWithMessage("Error stream flush count").that(testErrorOutputStream.getFlushCount()).isEqualTo(TestUtil.adjustFlushCountForOutputStreamClose(1));
    }
}
Also used : File(java.io.File) CloseAndFlushTestByteArrayOutputStream(com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 2 with CloseAndFlushTestByteArrayOutputStream

use of com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream in project checkstyle by checkstyle.

the class CheckerTest method testXmlLoggerClosesItStreams.

// -@cs[CheckstyleTestMakeup] must use raw class to directly initialize DefaultLogger
@Test
public void testXmlLoggerClosesItStreams() throws Exception {
    final Checker checker = new Checker();
    try (CloseAndFlushTestByteArrayOutputStream testInfoOutputStream = new CloseAndFlushTestByteArrayOutputStream()) {
        checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
        checker.addListener(new XMLLogger(testInfoOutputStream, OutputStreamOptions.CLOSE));
        final File tmpFile = File.createTempFile("file", ".java", temporaryFolder);
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
        verify(checker, tmpFile.getPath(), tmpFile.getPath(), expected);
        assertWithMessage("Output stream close count").that(testInfoOutputStream.getCloseCount()).isEqualTo(1);
        assertWithMessage("Output stream flush count").that(testInfoOutputStream.getFlushCount()).isEqualTo(TestUtil.adjustFlushCountForOutputStreamClose(0));
    }
}
Also used : File(java.io.File) CloseAndFlushTestByteArrayOutputStream(com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

CloseAndFlushTestByteArrayOutputStream (com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream)2 File (java.io.File)2 Test (org.junit.jupiter.api.Test)2