简体   繁体   English

PMD自定义junit方法命名规则不起作用

[英]PMD custom junit method naming rule not working

I have the following rule defined in my ruleSet.xml file: 我在ruleSet.xml文件中定义了以下规则:

<rule ref="category/java/codestyle.xml/MethodNamingConventions">
    <properties>
        <property name="junit4TestPattern" value="[a-z]*_[A-Z]{1}[a-z,A-Z]*_[A-Z]{1}[a-z,A-Z]*"/>
        <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration['.*FoobarRepository']"/>
    </properties>
</rule>

But when running PMD nothing fails yet I have alot of methods that are not conform the junit4 method naming pattern. 但是,当运行PMD时,仍然没有失败,但是我有很多不符合junit4方法命名模式的方法。 All method are annotated with org.junit.Test What is needed to trigger junit specific rules ? 所有方法都用org.junit.Test注释。触发junit特定规则需要什么?

I don't think it's a bug, your violationSuppressXPath just returns true all the time. 我不认为这是一个错误,您的violationSuppressXPath始终会返回true。 Remember, that it's evaluated with the violating node as the context node of the expression. 请记住,它是使用违反节点作为表达式的上下文节点进行评估的。

//ClassOrInterfaceDeclaration

// at the start of an XPath expression select all the descendants of the document root , so all the nodes in the file. //在XPath表达式的开始处,选择文档root的所有后代,因此选择文件中的所有节点。 So //ClassOrInterfaceDeclaration selects all the nodes of that type in the file, and not necessarily one that encloses the violation node. 因此, //ClassOrInterfaceDeclaration选择文件中该类型的所有节点,而不一定选择一个包含违反节点的节点。

['.*PrincipalRepository']

This predicate is always true, because any non-empty string is truthy. 该谓词始终为真,因为任何非空字符串都是真实的。 A predicate like ['foo'] is evaluated by converting the string to a boolean with the boolean function, which yields true() if the string is non-empty. 通过使用boolean函数将字符串转换为boolean可以评估谓词['foo'] ,如果字符串非空,则产生true() (Here you mean to test the name of the class, in the attribute @Image ) (这里的意思是在@Image属性中测试类的名称)

So basically the predicate doesn't test anything. 因此,该谓词基本上不会测试任何内容。 The effect, is that your violationSuppressXPath suppresses the violation any time the file where the violation is found contains some ClassOrInterfaceDeclaration anywhere, which is pretty frequent. 效果是,只要发现违规的文件在任何地方包含某些 ClassOrInterfaceDeclaration的地方(非常频繁),您的violationSuppressXPath抑制该违规。

To make this work, you could replace it with 要使其正常工作,您可以将其替换为

./ancestor::ClassOrInterfaceDeclaration[@Image = 'PrincipalRepository']

Note that unfortunately, XPath 1.0 does not support regular expressions so you can't do a regex test (though you can use contains or imitate an ends-with like in this answer ). 请注意,不幸的是,XPath 1.0不支持正则表达式,因此您无法进行正则表达式测试(尽管您可以使用contains或模仿ends-with该答案所示 )。 In this instance, I think a @SuppressWarnings("PMD.MethodNamingConventions") like you came up with is more appropriate. 在这种情况下,我认为像您想出的@SuppressWarnings("PMD.MethodNamingConventions")更为合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM