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 4ac6c0b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 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
69 changes: 64 additions & 5 deletions src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.junit.Test;

Expand Down Expand Up @@ -398,6 +399,33 @@ public void testAuditFinishedWithoutFileFinished() throws IOException {
verifyLines(expectedLines);
}

@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);

logger.auditStarted(null);
logger.auditFinished(null);

final String[] firstRunExpectedLines = {
"<file name=\"Test.java\">",
"<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\""
+ " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>",
"</file>",
};
verifyLines(firstRunExpectedLines, CommonUtils.EMPTY_STRING_ARRAY);
}

private String[] getOutStreamLines()
throws IOException {
final byte[] bytes = outStream.toByteArray();
Expand Down Expand Up @@ -426,15 +454,46 @@ private void verifyLines(String... expectedLines)
throws IOException {
final String[] lines = getOutStreamLines();
assertEquals("length.", expectedLines.length + 3, lines.length);
verifyLines(lines, 0, expectedLines);
}

/**
* Verify output lines from auditStart to auditEnd.
* Take into consideration checkstyle element (first and last lines).
* @param lines actual lines
* @param startIndex the actual lines start index
* @param expectedLines expected error report lines
*/
private void verifyLines(String[] lines, int startIndex, String... expectedLines) {
assertEquals("first line.",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
lines[0]);
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
lines[startIndex]);
final Pattern checkstyleOpenTag = Pattern.compile("^<checkstyle version=\".*\">$");
assertTrue("second line.", checkstyleOpenTag.matcher(lines[1]).matches());
assertTrue("second line.", checkstyleOpenTag.matcher(lines[startIndex + 1]).matches());
for (int i = 0; i < expectedLines.length; i++) {
assertEquals("line " + i + ".", expectedLines[i], lines[i + 2]);
final int lineIndex = startIndex + i + 2;
assertEquals("line " + lineIndex + ".", expectedLines[i], lines[lineIndex]);
}
assertEquals("last line.", "</checkstyle>", lines[startIndex + expectedLines.length + 2]);
}

/**
* Verify output lines bu blocks from auditStart to auditEnd.
* Take into consideration checkstyle element (first and last lines) for each block.
* @param expectedLinesBlocks expected error report lines
*/
private void verifyLines(String[]... expectedLinesBlocks)
throws IOException {
final String[] lines = getOutStreamLines();
final int totalLength = Stream.of(expectedLinesBlocks).mapToInt(x -> x.length).sum();
final int additionalHeaderAndFooterLines = 3 * expectedLinesBlocks.length;
assertEquals("length.", totalLength + additionalHeaderAndFooterLines, lines.length);

int startIndex = 0;
for (String[] expectedLines : expectedLinesBlocks) {
verifyLines(lines, startIndex, expectedLines);
startIndex += expectedLines.length + 3;
}
assertEquals("last line.", "</checkstyle>", lines[lines.length - 1]);
}

private static class TestException extends RuntimeException {
Expand Down

0 comments on commit 4ac6c0b

Please sign in to comment.