FieldDeclarations have the Access Flags on them now.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@118 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
David Dixon-Peugh
2002-06-28 16:33:01 +00:00
parent e6a027f77f
commit 248bfe81fd
3 changed files with 305 additions and 0 deletions

View File

@ -0,0 +1,108 @@
package test.net.sourceforge.pmd.ast;
import java.util.Set;
import java.util.Iterator;
import net.sourceforge.pmd.ast.*;
public class FieldDeclTest
extends ParserTst
{
public FieldDeclTest( String name ) {
super( name );
}
public String makeAccessJavaCode( String access[] ) {
String RC =
"public class Test { ";
for (int i = 0; i < access.length; i++) {
RC += access[i] + " ";
}
RC += " int j; }";
return RC;
}
public ASTFieldDeclaration getFieldDecl( String access[] )
throws Throwable
{
Set fields = getNodes( ASTFieldDeclaration.class,
makeAccessJavaCode( access ) );
assertEquals( "Wrong number of fields",
1, fields.size());
Iterator i = fields.iterator();
return (ASTFieldDeclaration) i.next();
}
public void testPublic()
throws Throwable
{
String access[] = { "public" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be public.",
afd.isPublic() );
}
public void testProtected()
throws Throwable
{
String access[] = { "protected" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be protected.",
afd.isProtected() );
}
public void testPrivate()
throws Throwable
{
String access[] = { "private" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be private.",
afd.isPrivate() );
}
public void testStatic()
throws Throwable
{
String access[] = { "private", "static" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be static.",
afd.isStatic() );
assertTrue( "Expecting field to be private.",
afd.isPrivate() );
}
public void testFinal()
throws Throwable
{
String access[] = { "public", "final" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be final.",
afd.isFinal() );
assertTrue( "Expecting field to be public.",
afd.isPublic() );
}
public void testTransient()
throws Throwable
{
String access[] = { "private", "transient" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be private.",
afd.isPrivate() );
assertTrue( "Expecting field to be transient.",
afd.isTransient() );
}
public void testVolatile()
throws Throwable
{
String access[] = { "private", "volatile" };
ASTFieldDeclaration afd = getFieldDecl( access );
assertTrue( "Expecting field to be volatile.",
afd.isVolatile() );
assertTrue( "Expecting field to be private.",
afd.isPrivate() );
}
}

View File

@ -0,0 +1,124 @@
package test.net.sourceforge.pmd.ast;
import java.io.StringReader;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import net.sourceforge.pmd.ast.*;
public class MethodDeclTest
extends ParserTst
{
public MethodDeclTest( String name ) {
super( name );
}
public void testPublic()
throws Throwable
{
String access[] = { "public" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be public.",
amd.isPublic() );
}
public void testPrivate()
throws Throwable
{
String access[] = { "private" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be private.",
amd.isPrivate() );
}
public void testProtected()
throws Throwable
{
String access[] = { "protected" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be protected.",
amd.isProtected() );
}
public void testFinal()
throws Throwable
{
String access[] = { "public", "final" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be final.",
amd.isFinal() );
assertTrue( "Expecting method to be public.",
amd.isPublic() );
}
public void testSynchronized()
throws Throwable
{
String access[] = { "public", "synchronized" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be synchronized.",
amd.isSynchronized() );
assertTrue( "Expecting method to be public.",
amd.isPublic() );
}
public void testAbstract()
throws Throwable
{
String access[] = { "public", "abstract" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be abstract.",
amd.isAbstract() );
assertTrue( "Expecting method to be public.",
amd.isPublic() );
}
public void testNative()
throws Throwable
{
String access[] = { "private", "native" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be native.",
amd.isNative() );
assertTrue( "Expecting method to be private.",
amd.isPrivate() );
}
public void testStrict()
throws Throwable
{
String access[] = { "public", "strictfp" };
ASTMethodDeclaration amd = getMethodDecl( access );
assertTrue( "Expecting method to be strict.",
amd.isStrict() );
assertTrue( "Expecting method to be public.",
amd.isPublic() );
}
public ASTMethodDeclaration getMethodDecl( String access[] )
throws Throwable
{
String javaCode =
"public class Test { ";
for (int i = 0; i < access.length; i++) {
javaCode += access[i] + " ";
}
javaCode +=
" void stuff() { } }";
Set methods = getNodes( ASTMethodDeclaration.class,
javaCode );
assertEquals( "Wrong number of methods",
1, methods.size());
Iterator i = methods.iterator();
return (ASTMethodDeclaration) i.next();
}
}

View File

@ -0,0 +1,73 @@
package test.net.sourceforge.pmd.ast;
import junit.framework.TestCase;
import java.util.Set;
import java.util.HashSet;
import java.io.StringReader;
import java.lang.reflect.*;
import net.sourceforge.pmd.ast.*;
public class ParserTst
extends TestCase
{
private class Collector
implements InvocationHandler
{
private Class clazz = null;
private Set collection = new HashSet();
public Collector( Class clazz ) {
this.clazz = clazz;
}
public Set getCollection() {
return collection;
}
public Object invoke( Object proxy,
Method method,
Object params[] )
throws Throwable
{
if (method.getName().equals("visit")) {
if (clazz.isInstance( params[0] )) {
collection.add( params[0] );
}
}
Method childrenAccept =
params[0].getClass().getMethod( "childrenAccept",
new Class[] { JavaParserVisitor.class,
Object.class } );
childrenAccept.invoke( params[0],
new Object[] { proxy, null } );
return null;
}
}
public ParserTst( String testName ) {
super( testName );
}
public Set getNodes( Class clazz,
String javaCode )
throws Throwable
{
Collector coll = new Collector( clazz );
JavaParser parser = new JavaParser( new StringReader( javaCode ));
ASTCompilationUnit cu = parser.CompilationUnit();
JavaParserVisitor jpv =
(JavaParserVisitor)
Proxy.newProxyInstance( JavaParserVisitor.class.getClassLoader(),
new Class[] { JavaParserVisitor.class },
coll );
jpv.visit( cu, null );
return coll.getCollection();
}
}