Skip to content

Commit

Permalink
Issue checkstyle#877: fix TreeWalkerTest JUnitTestsShouldIncludeAsser…
Browse files Browse the repository at this point in the history
…t violations
  • Loading branch information
vasylieva authored and romani committed Jul 4, 2017
1 parent 20c8a3d commit ab10e1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 0 additions & 2 deletions config/pmd-test.xml
Expand Up @@ -75,7 +75,6 @@
<!-- in ImportControlCheckTest pmd does not find asserts in private methods of the test class called from the test method -->
<!-- in AstRegressionTest pmd does not find asserts in inner classes methods called from the test method -->
<!-- in AllChecksTest pmd does not find asserts in lambdas -->
<!-- TreeWalkerTest should be rewrited-->
<property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration[@Image='SuppressionFilterTest']//MethodDeclarator[@Image='testEqualsAndHashCode']
| //ClassOrInterfaceDeclaration[@Image='SuppressionCommentFilterTest']//MethodDeclarator[@Image='testEqualsAndHashCodeOfTagClass']
| //ClassOrInterfaceDeclaration[@Image='SuppressWithNearbyCommentFilterTest']//MethodDeclarator[@Image='testEqualsAndHashCodeOfTagClass']
Expand All @@ -86,7 +85,6 @@
| /ClassOrInterfaceDeclaration[@Image='AstRegressionTest']//MethodDeclarator[@Image='testImpossibleExceptions']
| //ClassOrInterfaceDeclaration[@Image='AstRegressionTest']//MethodDeclarator[@Image='testImpossibleValid']
| //ClassOrInterfaceDeclaration[@Image='AllChecksTest']//MethodDeclarator[@Image='testAllModulesAreReferencedInConfigFile']
| //ClassOrInterfaceDeclaration[@Image='TreeWalkerTest']//MethodDeclarator[@Image='testProcessNonJavaFiles']
"/>
</properties>
</rule>
Expand Down
27 changes: 24 additions & 3 deletions src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java
Expand Up @@ -31,6 +31,7 @@
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand All @@ -50,6 +51,7 @@
import com.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck;
import com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck;
import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck;
import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
Expand Down Expand Up @@ -170,10 +172,29 @@ public void testForInvalidCheckImplementation() throws Exception {
@Test
public void testProcessNonJavaFiles() throws Exception {
final TreeWalker treeWalker = new TreeWalker();
treeWalker.setTabWidth(1);
final PackageObjectFactory factory = new PackageObjectFactory(
new HashSet<>(), Thread.currentThread().getContextClassLoader());
treeWalker.setModuleFactory(factory);
treeWalker.configure(new DefaultConfiguration("default config"));
final File file = new File("src/main/resources/checkstyle_packages.xml");
treeWalker.processFiltered(file, new ArrayList<>());
final DefaultConfiguration childConfig = createCheckConfig(JavadocParagraphCheck.class);
treeWalker.setupChild(childConfig);

final List<String> lines =
new ArrayList<>(Arrays.asList("package com.puppycrawl.tools.checkstyle;", "",
"error public class InputTreeWalkerFileWithViolation {}"));

try {
treeWalker.processFiltered(new File("input.java"), lines);
fail("Exception expected");
}
catch (CheckstyleException ex) {
assertEquals("Invalid exception message",
"MismatchedTokenException occurred during the analysis of file input.java.",
ex.getMessage());
}

// Should not throw exception as file with txt extension should not be processed at all
treeWalker.processFiltered(new File("input.txt"), lines);
}

@Test
Expand Down

0 comments on commit ab10e1a

Please sign in to comment.