Skip to content

Commit

Permalink
#659: excluded JUnit public static methods from rule
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuchyn committed Feb 19, 2016
1 parent 0caa9bb commit b6059f3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
10 changes: 8 additions & 2 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[@Static='true' and @Public='true'
//ClassOrInterfaceBodyDeclaration[
MethodDeclaration[@Static='true' and @Public='true'
and not (
MethodDeclarator[
count(FormalParameters/FormalParameter)=1
Expand All @@ -217,7 +218,12 @@
and FormalParameters/FormalParameter[@Varargs='true']
] and not(ResultType/Type)
)
]
] and (
Annotation/MarkerAnnotation/Name[@Image!='BeforeClass' and @Image!='AfterClass'
and @Image!='Parameterized.Parameters']
or not (Annotation)
)
]
]]>
</value>
</property>
Expand Down
18 changes: 18 additions & 0 deletions qulice-pmd/src/test/java/com/qulice/pmd/PMDValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,22 @@ public void allowsPublicStaticMainMethod() throws Exception {
)
).validate();
}

/**
* PMDValidator can allow JUnit public static methods marked with:
* BeforeClass annotation.
* AfterClass annotation.
* Parameterized.Parameters annotation.
* @throws Exception If something wrong happens inside.
*/
@Test
public void allowsJunitFrameworkPublicStaticMethods() throws Exception {
new PMDAssert(
"JunitStaticPublicMethods.java",
Matchers.is(true),
Matchers.not(
Matchers.containsString(PMDValidatorTest.STATIC_METHODS)
)
).validate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package foo.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collection;
import java.util.Collections;

public class SomeTest {

@BeforeClass
public static void beforeClass(){
// setup before class
}

@Test
public void emptyTest(){
//test something
}

@AfterClass
public static void afterClass() throws Exception {
//tear down after class
}

@Parameterized.Parameters
public static Collection parameters() {
return Collections.EMPTY_LIST;
}
}

0 comments on commit b6059f3

Please sign in to comment.