diff --git a/pmd/regress/test/net/sourceforge/pmd/ast/FieldDeclTest.java b/pmd/regress/test/net/sourceforge/pmd/ast/FieldDeclTest.java new file mode 100644 index 0000000000..e0f3e044fa --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/ast/FieldDeclTest.java @@ -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() ); + } +} diff --git a/pmd/regress/test/net/sourceforge/pmd/ast/MethodDeclTest.java b/pmd/regress/test/net/sourceforge/pmd/ast/MethodDeclTest.java new file mode 100644 index 0000000000..c67e0380b2 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/ast/MethodDeclTest.java @@ -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(); + } +} diff --git a/pmd/regress/test/net/sourceforge/pmd/ast/ParserTst.java b/pmd/regress/test/net/sourceforge/pmd/ast/ParserTst.java new file mode 100644 index 0000000000..c5e428f644 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/ast/ParserTst.java @@ -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(); + } +}