Skip to content

Commit

Permalink
[Issue checkstyle#2987] Allow JUnit ClassRule to be public
Browse files Browse the repository at this point in the history
  • Loading branch information
avano committed Feb 29, 2016
1 parent 519c999 commit f12128e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
1 change: 1 addition & 0 deletions config/intellij-idea-inspections.xml
Expand Up @@ -1648,6 +1648,7 @@
<option name="ignorableAnnotations">
<value>
<item value="org.junit.Rule" />
<item value="org.junit.ClassRule" />
</value>
</option>
</inspection_tool>
Expand Down
Expand Up @@ -58,6 +58,7 @@
* </p>
* <ul>
* <li>org.junit.Rule</li>
* <li>org.junit.ClassRule</li>
* <li>com.google.common.annotations.VisibleForTesting</li>
* </ul>
* <p>
Expand Down Expand Up @@ -268,6 +269,7 @@ public class VisibilityModifierCheck
/** Default ignore annotations canonical names. */
private static final List<String> DEFAULT_IGNORE_ANNOTATIONS = ImmutableList.of(
"org.junit.Rule",
"org.junit.ClassRule",
"com.google.common.annotations.VisibleForTesting"
);

Expand Down
Expand Up @@ -198,12 +198,12 @@ public void testDefaultAnnotationPatterns() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(VisibilityModifierCheck.class);
final String[] expected = {
"39:19: " + getCheckMessage(MSG_KEY, "customAnnotatedPublic"),
"42:12: " + getCheckMessage(MSG_KEY, "customAnnotatedPackage"),
"45:22: " + getCheckMessage(MSG_KEY, "customAnnotatedProtected"),
"47:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
"48:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
"49:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
"40:19: " + getCheckMessage(MSG_KEY, "customAnnotatedPublic"),
"43:12: " + getCheckMessage(MSG_KEY, "customAnnotatedPackage"),
"46:22: " + getCheckMessage(MSG_KEY, "customAnnotatedProtected"),
"48:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
"49:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
"50:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
};
verify(checkConfig, getPath("InputAnnotatedVisibility.java"), expected);
}
Expand All @@ -216,17 +216,19 @@ public void testCustomAnnotationPatterns() throws Exception {
"com.puppycrawl.tools.checkstyle.checks.design."
+ "InputAnnotatedVisibility.CustomAnnotation");
final String[] expected = {
"15:28: " + getCheckMessage(MSG_KEY, "publicJUnitRule"),
"18:28: " + getCheckMessage(MSG_KEY, "fqPublicJUnitRule"),
"21:19: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPublic"),
"24:12: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPackage"),
"27:22: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedProtected"),
"30:19: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPublic"),
"33:12: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPackage"),
"36:22: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedProtected"),
"47:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
"48:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
"49:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
"16:28: " + getCheckMessage(MSG_KEY, "publicJUnitRule"),
"19:28: " + getCheckMessage(MSG_KEY, "fqPublicJUnitRule"),
"22:19: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPublic"),
"25:12: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPackage"),
"28:22: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedProtected"),
"31:19: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPublic"),
"34:12: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPackage"),
"37:22: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedProtected"),
"48:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
"49:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
"50:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
"59:35: " + getCheckMessage(MSG_KEY, "publicJUnitClassRule"),
"62:35: " + getCheckMessage(MSG_KEY, "fqPublicJUnitClassRule"),
};
verify(checkConfig, getPath("InputAnnotatedVisibility.java"), expected);
}
Expand All @@ -237,20 +239,22 @@ public void testIgnoreAnnotationNoPattern() throws Exception {
createCheckConfig(VisibilityModifierCheck.class);
checkConfig.addAttribute("ignoreAnnotationCanonicalNames", "");
final String[] expected = {
"15:28: " + getCheckMessage(MSG_KEY, "publicJUnitRule"),
"18:28: " + getCheckMessage(MSG_KEY, "fqPublicJUnitRule"),
"21:19: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPublic"),
"24:12: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPackage"),
"27:22: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedProtected"),
"30:19: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPublic"),
"33:12: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPackage"),
"36:22: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedProtected"),
"39:19: " + getCheckMessage(MSG_KEY, "customAnnotatedPublic"),
"42:12: " + getCheckMessage(MSG_KEY, "customAnnotatedPackage"),
"45:22: " + getCheckMessage(MSG_KEY, "customAnnotatedProtected"),
"47:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
"48:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
"49:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
"16:28: " + getCheckMessage(MSG_KEY, "publicJUnitRule"),
"19:28: " + getCheckMessage(MSG_KEY, "fqPublicJUnitRule"),
"22:19: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPublic"),
"25:12: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedPackage"),
"28:22: " + getCheckMessage(MSG_KEY, "googleCommonsAnnotatedProtected"),
"31:19: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPublic"),
"34:12: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedPackage"),
"37:22: " + getCheckMessage(MSG_KEY, "fqGoogleCommonsAnnotatedProtected"),
"40:19: " + getCheckMessage(MSG_KEY, "customAnnotatedPublic"),
"43:12: " + getCheckMessage(MSG_KEY, "customAnnotatedPackage"),
"46:22: " + getCheckMessage(MSG_KEY, "customAnnotatedProtected"),
"48:19: " + getCheckMessage(MSG_KEY, "unannotatedPublic"),
"49:12: " + getCheckMessage(MSG_KEY, "unannotatedPackage"),
"50:22: " + getCheckMessage(MSG_KEY, "unannotatedProtected"),
"59:35: " + getCheckMessage(MSG_KEY, "publicJUnitClassRule"),
"62:35: " + getCheckMessage(MSG_KEY, "fqPublicJUnitClassRule")
};
verify(checkConfig, getPath("InputAnnotatedVisibility.java"), expected);
}
Expand All @@ -260,7 +264,8 @@ public void testIgnoreAnnotationSameName() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(VisibilityModifierCheck.class);
final String[] expected = {
"10:28: " + getCheckMessage(MSG_KEY, "publicJUnitRule"),
"11:28: " + getCheckMessage(MSG_KEY, "publicJUnitRule"),
"14:28: " + getCheckMessage(MSG_KEY, "publicJUnitClassRule"),
};
verify(checkConfig, getPath("InputAnnotatedVisibilitySameTypeName.java"), expected);
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.google.common.annotations.VisibleForTesting;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

Expand Down Expand Up @@ -53,4 +54,10 @@ public class InputAnnotatedVisibility {
@Target(value={ElementType.FIELD})
public @interface CustomAnnotation {
}

@ClassRule
public static TemporaryFolder publicJUnitClassRule = new TemporaryFolder();

@org.junit.ClassRule
public static TemporaryFolder fqPublicJUnitClassRule = new TemporaryFolder();
}
Expand Up @@ -3,9 +3,13 @@
import org.junit.rules.TemporaryFolder;

import com.puppycrawl.tools.checkstyle.checks.design.InputLocalAnnotations.Rule;
import com.puppycrawl.tools.checkstyle.checks.design.InputLocalAnnotations.ClassRule;

public class InputAnnotatedVisibilitySameTypeName
{
@Rule
public TemporaryFolder publicJUnitRule = new TemporaryFolder();

@ClassRule
public TemporaryFolder publicJUnitClassRule = new TemporaryFolder();
}
Expand Up @@ -5,4 +5,8 @@ public class InputLocalAnnotations
public @interface Rule {

}

public @interface ClassRule {

}
}
6 changes: 4 additions & 2 deletions src/xdocs/config_design.xml
Expand Up @@ -857,7 +857,7 @@ public class Foo{
<td>ignoreAnnotationCanonicalNames</td>
<td>ignore annotations canonical names</td>
<td><a href="property_types.html#stringSet">String Set</a></td>
<td>org.junit.Rule, com.google.common.annotations.VisibleForTesting</td>
<td>org.junit.Rule, org.junit.ClassRule, com.google.common.annotations.VisibleForTesting</td>
</tr>
</table>
</subsection>
Expand Down Expand Up @@ -971,7 +971,7 @@ class SomeClass
</source>

<p>
To configure the Check passing fields annotated with @org.junit.Rule and
To configure the Check passing fields annotated with @org.junit.Rule, @org.junit.ClassRule and
@com.google.common.annotations.VisibleForTesting annotations:
</p>
<source>
Expand All @@ -985,6 +985,8 @@ class SomeClass
{
@org.junit.Rule
public TemporaryFolder publicJUnitRule = new TemporaryFolder(); // no warning
@org.junit.ClassRule
public static TemporaryFolder publicJUnitClassRule = new TemporaryFolder(); // no warning
@com.google.common.annotations.VisibleForTesting
public String testString = ""; // no warning
}
Expand Down

0 comments on commit f12128e

Please sign in to comment.