<?xml version="1.0"?>
<ruleset name="PMD ruleset for Checkstyle"
         xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0
                             http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>
        PMD ruleset for Checkstyle tests
    </description>
    <exclude-pattern>.*/src/main/.*</exclude-pattern>

    <rule ref="config/pmd.xml">
        <!--We do throws Exception, as any exception that in not caught in test should fail
            a test that is expected behavior and we like it as it follow fail-fast and helps
            to avoid extra catch blocks-->
        <exclude name="SignatureDeclareThrowsException"/>
        <!--it madness to move all string values to common variables,
            it will be hard to read test code-->
        <exclude name="AvoidDuplicateLiterals"/>
        <!--we do not need javadoc on test code-->
        <exclude name="CommentRequired"/>
        <!--we like static imports in UTs as them minimize the code and we know that most of
            such methods are from UT+moks frameworks-->
        <exclude name="TooManyStaticImports"/>
        <!--we try to keep all testing for class in one place/class/file to ease navigation
            between target class and it's test-->
        <exclude name="TooManyMethods"/>
        <!--we have too much UTs for each main class, it is better to keep all tests in one
            file to ease navigation and search for tes-->
        <exclude name="CouplingBetweenObjects"/>
        <!--we have too much UTs for each main class, it is better to keep all tests in one
            file to ease navigation and search for test-->
        <exclude name="ExcessiveImports"/>
        <!--test should be named as their main class-->
        <exclude name="ClassNamingConventions"/>
        <!--checkstyle do not use IP in configuration, so all values in tests are ok to use,
            they just another test string data-->
        <exclude name="AvoidUsingHardCodedIP"/>
        <!--UTests are required to be public by design-->
        <exclude name="ExcessivePublicCount"/>
        <!--we have too much UTs for each main class, it is better to keep all tests in one
            file to ease navigation and search for test-->
        <exclude name="ExcessiveClassLength"/>
        <!--we want to avoid wide throws signature, it is just test-->
        <exclude name="AvoidCatchingGenericException"/>
        <!--we do not want invest extra time to make logic ideal in test method, may be later...-->
        <exclude name="NPathComplexity"/>
        <!--conflicts with checkstyle rule-->
        <exclude name="JUnitUseExpected"/>
        <!--will be addressed in https://github.com/checkstyle/checkstyle/projects/5-->
        <exclude name="NonThreadSafeSingleton"/>
    </rule>

    <rule ref="category/java/design.xml/NcssCount">
        <properties>
            <!-- We want to keep it all in one place. -->
            <property name="classReportLevel" value="1000"/>
            <!-- A big enough value to satisfy the most cases. -->
            <property name="methodReportLevel" value="100"/>
            <!-- GeneratedJavaTokenTypesTest.testTokenNumbering' is simple but long as it recheck
                   each token one by one. Same goes for
                   GeneratedJavadocTokenTypesTest#testTokenNumbers.
                 JavadocTokenTypesTest.TokenValues contains several asserts as it checks each
                   token explicitly.
                 XdocsPagesTest.getModulePropertyExpectedTypeName is a complicated list of
                   different checks.
                 IndentationCheckTest has a lot of cases. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[@Image='JavadocTokenTypesTest']
                                 //MethodDeclaration[@Name='testTokenValues']
            | //ClassOrInterfaceDeclaration[@Image='GeneratedJavaTokenTypesTest']
                  //MethodDeclaration[@Name='testTokenNumbering']
            | //ClassOrInterfaceDeclaration[@Image='XdocsPagesTest']
                  //MethodDeclaration[@Name='getModulePropertyExpectedTypeName']
            | //ClassOrInterfaceDeclaration[@Image='GeneratedJavadocTokenTypesTest']
                  //MethodDeclaration[@Name='testTokenNumbers']
            | //ClassOrInterfaceDeclaration[@Image='IndentationCheckTest']"/>
        </properties>
    </rule>

    <rule ref="category/java/design.xml/ExcessiveMethodLength">
        <properties>
            <!-- GeneratedJavaTokenTypesTest.testTokenNumbering' is simple but long as it recheck
                   each token one by one. Same goes for
                   GeneratedJavadocTokenTypesTest#testTokenNumbers
                 ParenPadCheckTest.testNospaceWithComplexInput is intended to keep all in one
                   method.
                 JavadocTokenTypesTest.TokenValues contains several asserts as it checks each
                   token explicitly.
                 XdocsPagesTest.getModulePropertyExpectedTypeName is a complicated list of
                   different checks. -->
            <property name="violationSuppressXPath" value="
            //ClassOrInterfaceDeclaration[@Image='GeneratedJavaTokenTypesTest']
                //MethodDeclaration[@Name='testTokenNumbering']
            | //ClassOrInterfaceDeclaration[@Image='ParenPadCheckTest']
                //MethodDeclaration[@Name='testNospaceWithComplexInput']
            | //ClassOrInterfaceDeclaration[@Image='ParenPadTest']
                //MethodDeclaration[@Name='testMethodParen']
            | //ClassOrInterfaceDeclaration[@Image='JavadocTokenTypesTest']
                //MethodDeclaration[@Name='testTokenValues']
            | //ClassOrInterfaceDeclaration[@Image='XdocsPagesTest']
                //MethodDeclaration[@Name='getModulePropertyExpectedTypeName']
            | //ClassOrInterfaceDeclaration[@Image='GeneratedJavadocTokenTypesTest']
                //MethodDeclaration[@Name='testTokenNumbers']"/>
        </properties>
    </rule>

    <rule ref="category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert">
        <!-- PMD cannot find assert if it is located in private method of this class called from
               the test method or method of another class. -->
        <properties>
            <!-- In AllChecksTest, AstRegressionTest and ImportControlCheckTest
                   PMD does not find asserts in private methods of the test class called from the
                   test method.
                 In MainTest PMD does not find asserts in lambdas called in the method
                   exit.checkAssertionAfterwards.
                 In AstRegressionTest PMD does not find asserts in inner classes methods called
                   from the test method.
                 In XdocsPagesTest PMD does not find asserts in lambdas.
                 All test classes which starts with XpathRegression have asserts inside parent's
                 method. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[@Image='AllChecksTest'
              or @Image='AstRegressionTest'
              or @Image='ImportControlCheckTest']//PrimaryPrefix/Name[@Image='verify']
            | //ClassOrInterfaceDeclaration[@Image='MainTest']
                  //PrimaryPrefix//Name[starts-with(@Image, 'assert')]
            | //ClassOrInterfaceDeclaration[@Image='XdocsPagesTest']
                  //MethodDeclaration[@Name='testAllChecksPresentOnAvailableChecksPage']
            | //ClassOrInterfaceDeclaration[@Image='XdocsJavaDocsTest']
                  //MethodDeclaration[@Name='testAllCheckSectionJavaDocs']
            | //ClassOrInterfaceDeclaration[starts-with(@Image,'XpathRegression')]
                  //MethodDeclaration
            "/>
        </properties>
    </rule>

    <rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseBeforeAnnotation">
        <properties>
            <!-- A false positive. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[@Image='CommitValidationTest']"/>
        </properties>
    </rule>
    <rule ref="category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts">
        <properties>
            <property name="maximumAsserts" value="11"/>
            <!-- GeneratedJavadocTokenTypesTest.testTokenNumbers and
                   GeneratedJavadocTokenTypesTest.testRuleNumbers contains several asserts
                   as they check each token and each rule explicitly.
                 JavadocTokenTypes.testTokenValues contains several asserts as it checks each
                   token explicitly. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[@Image='JavadocTokenTypesTest']
                                 //MethodDeclaration[@Name='testTokenValues']
                        | //ClassOrInterfaceDeclaration[@Image='GeneratedJavaTokenTypesTest']
                              //MethodDeclaration[@Name='testTokenNumbering']
                        | //ClassOrInterfaceDeclaration[@Image='GeneratedJavadocTokenTypesTest']
                              //MethodDeclaration[@Name='testTokenNumbers']"/>
        </properties>
    </rule>

    <rule ref="category/java/codestyle.xml/ConfusingTernary">
        <properties>
            <!-- A false positive: commit validation is a sequence of checks, if we shuffle them
                   it would be broken. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[@Image='CommitValidationTest']
                                //MethodDeclaration[@Name='validateCommitMessage']"/>
        </properties>
    </rule>

    <rule ref="category/java/codestyle.xml/ShortMethodName">
        <properties>
            <!-- This inherited from GeneratedJavaLexer. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[@Image='AssertGeneratedJavaLexer']
                  //MethodDeclaration[@Name='LA']"/>
        </properties>
    </rule>

    <rule ref="category/java/design.xml/UseObjectForClearerAPI">
        <properties>
            <!-- This is checking for amount of String arguments more than 3 (hardcoded).
                 Extra abstraction(object) for single method does not make code more readable or
                 easy to maintain. The rule should also only check public methods but has a bug.
                 Suppress the false-positives. -->
            <property name="violationSuppressXPath"
                      value="//ClassOrInterfaceDeclaration[
                      @Image='SuppressionXpathSingleFilterTest']
                  //MethodDeclaration[@Name='createSuppressionXpathSingleFilter']
                  | //ClassOrInterfaceDeclaration[ @Image='XdocsPagesTest']
                        //MethodDeclaration[@Name='validateRuleNameOrder']"/>
        </properties>
    </rule>
    <rule ref="category/java/errorprone.xml/CloseResource">
        <properties>
            <!-- we do close streams in special methods -->
            <property name="violationSuppressXPath"
                      value="
                      //ClassOrInterfaceDeclaration[@Image='XdocsPagesTest']
                                 //MethodDeclaration[@Name='getModulePropertyExpectedValue']
                      |//ClassOrInterfaceDeclaration[@Image='DefaultLoggerPowerTest']
                                 //MethodDeclaration[@Name='testNewCtor']
                      |//ClassOrInterfaceDeclaration[@Image='ImportControlLoaderPowerTest']
                                 //MethodDeclaration[@Name='testInputStreamThatFailsOnClose']
                      |//ClassOrInterfaceDeclaration[@Image='ImportControlLoaderPowerTest']
                                 //MethodDeclaration[@Name='testInputStreamFailsOnRead']
                      |//ClassOrInterfaceDeclaration[@Image='XpathFileGeneratorAuditListenerTest']
                                 //MethodDeclaration[@Name='verifyOutput']
                      |//ClassOrInterfaceDeclaration[@Image='CommonUtilTest']
                                 //MethodDeclaration[@Name='testClose']
                      "/>
        </properties>
    </rule>
</ruleset>
