added new JUnitStaticSuiteRule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@914 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-09-11 18:54:44 +00:00
parent f06d8e72d3
commit 5b29d00c22
6 changed files with 79 additions and 1 deletions

View File

@ -1,5 +1,5 @@
???? 2002 - 1.0rc2:
Added new rule: JUnitSpellingRule
Added new rules: JUnitSpellingRule, JUnitStaticSuiteRule
Added new ruleset - junit.
Fixed bug in PMD GUI - rules are now saved correctly.

View File

@ -0,0 +1,23 @@
/*
* User: tom
* Date: Sep 11, 2002
* Time: 2:45:29 PM
*/
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.rules.JUnitStaticSuiteRule;
public class JUnitStaticSuiteRuleTest extends RuleTst {
public JUnitStaticSuiteRuleTest(String name) {
super(name);
}
public void test1() throws Throwable {
runTest("JUnitStaticSuite1.java", 2, new JUnitStaticSuiteRule());
}
public void test2() throws Throwable {
runTest("JUnitStaticSuite2.java", 0, new JUnitStaticSuiteRule());
}
}

View File

@ -19,6 +19,25 @@ public class Foo extends TestCase {
public void setup() {} // oops, should be setUp
public void TearDown() {} // oops, should be tearDown
}
]]>
</example>
</rule>
<rule name="JUnitStaticSuite"
message="You have a suite() method that is not both public and static, so JUnit won't call it to get your TestSuite. Is that what you wanted to do?"
class="net.sourceforge.pmd.rules.JUnitStaticSuiteRule">
<description>
The suite() method needs to be both public and static.
</description>
<example>
<![CDATA[
import junit.framework.*;
public class Foo extends TestCase {
public void suite() {} // oops, should be static
private static void suite() {} // oops, should be public
}
]]>
</example>
</rule>

View File

@ -0,0 +1,29 @@
/*
* User: tom
* Date: Sep 11, 2002
* Time: 2:27:46 PM
*/
package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.ast.AccessNode;
public class JUnitStaticSuiteRule extends AbstractRule {
public Object visit(ASTMethodDeclarator node, Object data) {
if (!node.getImage().equals("suite")) {
return data;
}
AccessNode parent = (AccessNode)node.jjtGetParent();
if (!parent.isPublic() || !parent.isStatic()) {
RuleContext ctx = (RuleContext)data;
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginColumn()));
}
return data;
}
}

View File

@ -0,0 +1,4 @@
public class JUnitStaticSuite1 {
public TestSuite suite() {}
private static TestSuite suite() {}
}

View File

@ -0,0 +1,3 @@
public class JUnitStaticSuite2 {
public static TestSuite suite() {}
}