Checkstyle different rules for different files

Building on barfuin's answer, I preferred not to have (yet) another XML file floating around. However, it is possible to configure suppressions directly in the CheckStyle XML config file:

  <module name="SuppressionSingleFilter">
    <metadata name="net.sf.eclipsecs.core.comment" value="Suppress MethodNameRegular check on unit tests"/>
    <property name="files" value=".*[\\/]src[\\/]test[\\/]"/>
    <property name="id" value="MethodNameRegular"/>
  </module>
  <module name="SuppressionSingleFilter">
    <metadata name="net.sf.eclipsecs.core.comment" value="Suppress MethodNameTest check except on unit tests"/>
    <property name="files" value=".*[\\/]src[\\/](?!test[\\/])"/>
    <property name="id" value="MethodNameTest"/>
  </module>

(This would be in addition to the two MethodName checks.)


You must define the MethodName check twice, with one instance checking the regular methods, and the other checking the test methods. Note the id property, which we will use to restrict the checks to their respective domains:

<module name="MethodName">
    <property name="id" value="MethodNameRegular"/>
    <property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
</module>
<module name="MethodName">
    <property name="id" value="MethodNameTest"/>
    <property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>

Next, the regular check must be suppressed for test methods and vice versa. This works only if you have a criterion by which to distinguish between the two kinds of classes. I use the Maven directory convention, which puts regular classes under src/main and test classes under src/test. Here is the suppression filter file:

<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
    <suppress files="[\\/]src[\\/]test[\\/].*" id="MethodNameRegular" />
    <suppress files="[\\/]src[\\/]main[\\/].*" id="MethodNameTest" />
</suppressions>