Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Issue checkstyle#4398: increase coverage of pitest-checkstyle-tree-wa…
Browse files Browse the repository at this point in the history
…lker profile to 89%
  • Loading branch information
vasylieva committed Jun 20, 2017
1 parent 993d7b9 commit 2e17a7e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
17 changes: 14 additions & 3 deletions pom.xml
Expand Up @@ -1996,15 +1996,26 @@
<param>com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinter</param>
<param>com.puppycrawl.tools.checkstyle.AstTreeStringPrinter</param>
<param>com.puppycrawl.tools.checkstyle.TreeWalker</param>
<param>com.puppycrawl.tools.checkstyle.Main</param>
</targetClasses>
<targetTests>
<param>com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinterTest</param>
<param>com.puppycrawl.tools.checkstyle.AstTreeStringPrinterTest</param>
<param>com.puppycrawl.tools.checkstyle.TreeWalkerTest</param>
<param>com.puppycrawl.tools.checkstyle.MainTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.blocks.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.design.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.header.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.imports.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.indentation.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.javadoc.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.metrics.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.modifiers.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.naming.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.regexp.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.sizes.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.whitespace.*</param>
</targetTests>
<mutationThreshold>83</mutationThreshold>
<mutationThreshold>89</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
Expand Up @@ -178,8 +178,6 @@ protected void processFiltered(File file, List<String> lines) throws CheckstyleE
final FileContents contents = new FileContents(text);
final DetailAST rootAST = parse(contents);

getMessageCollector().reset();

if (!ordinaryChecks.isEmpty()) {
walk(rootAST, contents, AstState.ORDINARY);
}
Expand Down
Expand Up @@ -31,6 +31,7 @@

import antlr.NoViableAltException;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.FileText;

public class AstTreeStringPrinterTest {

Expand Down Expand Up @@ -69,6 +70,17 @@ public void testParseFile() throws Exception {
Assert.assertEquals(expected, actual);
}

@Test
public void testPrintAst() throws Exception {
final FileText text = new FileText(
new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(),
System.getProperty("file.encoding", "UTF-8"));
final String actual = AstTreeStringPrinter.printAst(text, false);
final String expected = new String(Files.readAllBytes(Paths.get(
getPath("expectedInputAstTreeStringPrinter.txt"))), StandardCharsets.UTF_8);
Assert.assertEquals(expected, actual);
}

@Test
public void testParseFileWithComments() throws Exception {
final String actual = AstTreeStringPrinter.printFileAst(
Expand Down
Expand Up @@ -20,17 +20,22 @@
package com.puppycrawl.tools.checkstyle;

import static com.puppycrawl.tools.checkstyle.internal.TestUtils.assertUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;

import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class DetailNodeTreeStringPrinterTest {

Expand All @@ -51,7 +56,7 @@ public void testParseFile() throws Exception {
final String expected = new String(Files.readAllBytes(Paths.get(
getPath("expectedInputJavadocComment.txt"))), StandardCharsets.UTF_8)
.replaceAll("\\\\r\\\\n", "\\\\n");
Assert.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand All @@ -66,8 +71,31 @@ public void testParseFileWithError() throws Exception {
final String expected = "[ERROR:0] Javadoc comment at column 1 has parse error. "
+ "Missed HTML close tag 'qwe'. Sometimes it means that close tag missed "
+ "for one of previous tags.";
Assert.assertEquals(expected, ex.getMessage());
assertEquals(expected, ex.getMessage());
}
}

@Test
public void testCreationOfFakeCommentBlock() throws Exception {
final Method createFakeBlockComment =
Whitebox.getMethod(DetailNodeTreeStringPrinter.class,
"createFakeBlockComment", String.class);

final DetailAST testCommentBlock =
(DetailAST) createFakeBlockComment.invoke(null, "test_comment");
assertEquals(TokenTypes.BLOCK_COMMENT_BEGIN, testCommentBlock.getType());
assertEquals("/*", testCommentBlock.getText());
assertEquals(0, testCommentBlock.getLineNo());

final DetailAST contentCommentBlock = testCommentBlock.getFirstChild();
assertEquals(TokenTypes.COMMENT_CONTENT, contentCommentBlock.getType());
assertEquals("*test_comment", contentCommentBlock.getText());
assertEquals(0, contentCommentBlock.getLineNo());
assertEquals(-1, contentCommentBlock.getColumnNo());

final DetailAST endCommentBlock = contentCommentBlock.getNextSibling();
assertEquals(TokenTypes.BLOCK_COMMENT_END, endCommentBlock.getType());
assertEquals("*/", endCommentBlock.getText());
}

}
47 changes: 47 additions & 0 deletions src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java
Expand Up @@ -20,6 +20,7 @@
package com.puppycrawl.tools.checkstyle;

import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
Expand Down Expand Up @@ -48,11 +49,13 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Context;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
Expand Down Expand Up @@ -351,6 +354,31 @@ public void testBehaviourWithOrdinaryAndCommentChecks() throws Exception {
TreeWalker.parse(any(FileContents.class));
}

@Test
public void testFinishLocalSetupFullyInitialized() throws Exception {
final TreeWalker treeWalker = new TreeWalker();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
treeWalker.setClassLoader(contextClassLoader);
treeWalker.setSeverity("error");
treeWalker.setTabWidth(100);
treeWalker.finishLocalSetup();

final Context context = (Context) Whitebox.getInternalState(treeWalker, "childContext");
assertEquals(contextClassLoader, context.get("classLoader"));
assertEquals("error", context.get("severity"));
assertEquals(String.valueOf(100), context.get("tabWidth"));
}

@Test
public void testCheckInitIsCalledInTreeWalker() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(VerifyInitCheck.class);
final File file = temporaryFolder.newFile("file.pdf");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, file.getPath(), expected);
assertTrue(VerifyInitCheck.isInitWasCalled());
}

private static class BadJavaDocCheck extends AbstractCheck {
@Override
public int[] getDefaultTokens() {
Expand All @@ -368,6 +396,25 @@ public int[] getRequiredTokens() {
}
}

private static class VerifyInitCheck extends AbstractCheck {
private static boolean initWasCalled;

@Override
public int[] getDefaultTokens() {
return CommonUtils.EMPTY_INT_ARRAY;
}

@Override
public void init() {
super.init();
initWasCalled = true;
}

public static boolean isInitWasCalled() {
return initWasCalled;
}
}

private static class RequiredTokenIsNotInDefaultsCheck extends AbstractCheck {
@Override
public int[] getRequiredTokens() {
Expand Down

0 comments on commit 2e17a7e

Please sign in to comment.