Skip to content

Commit

Permalink
Issue checkstyle#5022: Fixed pitest coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
soon committed Aug 30, 2017
1 parent 1862af7 commit 21fb664
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion shippable.yml
Expand Up @@ -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
Expand Down
Expand Up @@ -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);
}
Expand Down
56 changes: 50 additions & 6 deletions src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
"<file name=\"Test.java\">",
"<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\""
+ " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>",
"</file>",
};
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<String> lineList = new ArrayList<>();
Expand All @@ -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.",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
lines[0]);
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
lines[0]);
final Pattern checkstyleOpenTag = Pattern.compile("^<checkstyle version=\".*\">$");
assertTrue("second line.", checkstyleOpenTag.matcher(lines[1]).matches());
for (int i = 0; i < expectedLines.length; i++) {
Expand Down

0 comments on commit 21fb664

Please sign in to comment.