Added UseSingletonRuleTest and refactored out some common Rule Execution

code.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@127 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
David Dixon-Peugh
2002-06-28 17:31:23 +00:00
parent 2f663a2e24
commit 491b0848fb
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package test.net.sourceforge.pmd.rules;
/**
* RuleTst
*
* Extend your Rule TestCases from here to get some
* juicy code sharing.
*/
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import net.sourceforge.pmd.*;
import net.sourceforge.pmd.ast.*;
public class RuleTst
extends TestCase
{
public RuleTst( String name ) {
super( name );
}
public Report process( String fileName,
Rule rule )
throws Throwable
{
// Set up the Context
RuleContext ctx = new RuleContext();
ctx.setReport( new Report("xml", fileName) );
// Parse the file
InputStream javaFile =
getClass().getClassLoader().getResourceAsStream( fileName );
JavaParser parser = new JavaParser( javaFile );
ASTCompilationUnit astCU = parser.CompilationUnit();
// Collect the ACUs
List acus = new ArrayList();
acus.add( astCU );
// Apply the rules
rule.apply( acus, ctx );
// Return the report.
return ctx.getReport();
}
}

View File

@ -0,0 +1,46 @@
package test.net.sourceforge.pmd.rules.design;
import test.net.sourceforge.pmd.rules.*;
import java.util.List;
import java.util.ArrayList;
import java.io.InputStream;
import junit.framework.TestCase;
import net.sourceforge.pmd.*;
import net.sourceforge.pmd.ast.*;
import net.sourceforge.pmd.rules.design.*;
public class UseSingletonRuleTest
extends RuleTst
{
public UseSingletonRuleTest( String name ) {
super( name );
}
public void testUseSingleton1()
throws Throwable
{
Report report = process("design/UseSingleton1.java",
new UseSingletonRule());
assertEquals( 0, report.countViolationsInCurrentFile() );
}
public void testUseSingleton2()
throws Throwable
{
Report report = process("design/UseSingleton2.java",
new UseSingletonRule());
assertEquals( 0, report.countViolationsInCurrentFile() );
}
public void testUseSingleton3()
throws Throwable
{
Report report = process("design/UseSingleton3.java",
new UseSingletonRule());
assertEquals( 0, report.countViolationsInCurrentFile() );
}
}