diff --git a/shippable.yml b/shippable.yml index ac29db329860..372cac1458c2 100644 --- a/shippable.yml +++ b/shippable.yml @@ -24,7 +24,7 @@ env: - PROFILE="-Ppitest-checks-naming,no-validations"; POST_ACTION=check_survived - PROFILE="-Ppitest-checks-indentation,no-validations" - PROFILE="-Ppitest-checkstyle-tree-walker,no-validations"; POST_ACTION=check_survived - - PROFILE="-Ppitest-checkstyle-common,no-validations"; POST_ACTION=till_#5022 + - PROFILE="-Ppitest-checkstyle-common,no-validations"; POST_ACTION=check_survived - PROFILE="-Ppitest-checkstyle-main,no-validations"; POST_ACTION=check_survived - PROFILE="-Ppitest-checkstyle-api,no-validations"; POST_ACTION=off_till_resolved - PROFILE="-Ppitest-checkstyle-utils,no-validations"; POST_ACTION=off_till_resolved diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/XMLLogger.java b/src/main/java/com/puppycrawl/tools/checkstyle/XMLLogger.java index 5f58acff47ad..8590c8c054b3 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/XMLLogger.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/XMLLogger.java @@ -105,7 +105,7 @@ public XMLLogger(OutputStream outputStream, OutputStreamOptions outputStreamOpti * Sets the OutputStream. * @param outputStream the OutputStream to use **/ - private void setOutputStream(OutputStream outputStream) { + public final void setOutputStream(OutputStream outputStream) { final OutputStreamWriter osw = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); writer = new PrintWriter(osw); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java index 4d54fdbd6bd1..21158adfb325 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java @@ -25,6 +25,7 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; @@ -398,9 +399,42 @@ public void testAuditFinishedWithoutFileFinished() throws IOException { verifyLines(expectedLines); } - private String[] getOutStreamLines() + @Test + public void testClearsMessagesAfterAuditFinished() throws IOException { + final XMLLogger logger = new XMLLogger(outStream, false); + logger.auditStarted(null); + final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java"); + logger.fileStarted(fileStartedEvent); + + final LocalizedMessage message = + new LocalizedMessage(1, 1, + "messages.properties", "key", null, SeverityLevel.ERROR, null, + getClass(), null); + final AuditEvent errorEvent = new AuditEvent(this, "Test.java", message); + logger.addError(errorEvent); + logger.auditFinished(null); + + final CloseAndFlushTestByteArrayOutputStream secondOutStream = + new CloseAndFlushTestByteArrayOutputStream(); + logger.setOutputStream(secondOutStream); + logger.auditStarted(null); + logger.fileStarted(fileStartedEvent); + logger.addError(errorEvent); + logger.auditFinished(null); + + final String[] expectedLines = { + "", + "", + "", + }; + verifyLines(expectedLines); + verifyLines(secondOutStream, expectedLines); + } + + private static String[] getOutStreamLines(ByteArrayOutputStream stream) throws IOException { - final byte[] bytes = outStream.toByteArray(); + final byte[] bytes = stream.toByteArray(); final ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); final List lineList = new ArrayList<>(); @@ -422,13 +456,23 @@ private String[] getOutStreamLines() * Take into consideration checkstyle element (first and last lines). * @param expectedLines expected error report lines */ - private void verifyLines(String... expectedLines) + private void verifyLines(String... expectedLines) throws IOException { + verifyLines(outStream, expectedLines); + } + + /** + * Verify output lines from auditStart to auditEnd. + * Take into consideration checkstyle element (first and last lines). + * @param stream the output stream + * @param expectedLines expected error report lines + */ + private static void verifyLines(ByteArrayOutputStream stream, String... expectedLines) throws IOException { - final String[] lines = getOutStreamLines(); + final String[] lines = getOutStreamLines(stream); assertEquals("length.", expectedLines.length + 3, lines.length); assertEquals("first line.", - "", - lines[0]); + "", + lines[0]); final Pattern checkstyleOpenTag = Pattern.compile("^$"); assertTrue("second line.", checkstyleOpenTag.matcher(lines[1]).matches()); for (int i = 0; i < expectedLines.length; i++) {