1 /** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package test.net.sourceforge.pmd.ast; 5 6 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertTrue; 8 import net.sourceforge.pmd.ast.ASTMethodDeclaration; 9 10 import org.junit.Test; 11 12 import test.net.sourceforge.pmd.testframework.ParserTst; 13 14 import java.util.Iterator; 15 import java.util.Set; 16 17 public class MethodDeclTest extends ParserTst { 18 19 @Test 20 public void testPublic() throws Throwable { 21 String access[] = {"public"}; 22 ASTMethodDeclaration amd = getMethodDecl(access); 23 assertTrue("Expecting method to be public.", amd.isPublic()); 24 } 25 26 @Test 27 public void testPrivate() throws Throwable { 28 String access[] = {"private"}; 29 ASTMethodDeclaration amd = getMethodDecl(access); 30 assertTrue("Expecting method to be private.", amd.isPrivate()); 31 } 32 33 @Test 34 public void testProtected() throws Throwable { 35 String access[] = {"protected"}; 36 ASTMethodDeclaration amd = getMethodDecl(access); 37 assertTrue("Expecting method to be protected.", amd.isProtected()); 38 } 39 40 @Test 41 public void testFinal() throws Throwable { 42 String access[] = {"public", "final"}; 43 ASTMethodDeclaration amd = getMethodDecl(access); 44 assertTrue("Expecting method to be final.", amd.isFinal()); 45 assertTrue("Expecting method to be public.", amd.isPublic()); 46 } 47 48 @Test 49 public void testSynchronized() throws Throwable { 50 String access[] = {"public", "synchronized"}; 51 ASTMethodDeclaration amd = getMethodDecl(access); 52 assertTrue("Expecting method to be synchronized.", amd.isSynchronized()); 53 assertTrue("Expecting method to be public.", amd.isPublic()); 54 } 55 56 @Test 57 public void testAbstract() throws Throwable { 58 String access[] = {"public", "abstract"}; 59 ASTMethodDeclaration amd = getMethodDecl(access); 60 assertTrue("Expecting method to be abstract.", amd.isAbstract()); 61 assertTrue("Expecting method to be public.", amd.isPublic()); 62 } 63 64 @Test 65 public void testNative() throws Throwable { 66 String access[] = {"private", "native"}; 67 ASTMethodDeclaration amd = getMethodDecl(access); 68 assertTrue("Expecting method to be native.", amd.isNative()); 69 assertTrue("Expecting method to be private.", amd.isPrivate()); 70 } 71 72 @Test 73 public void testStrict() throws Throwable { 74 String access[] = {"public", "strictfp"}; 75 ASTMethodDeclaration amd = getMethodDecl(access); 76 assertTrue("Expecting method to be strict.", amd.isStrictfp()); 77 assertTrue("Expecting method to be public.", amd.isPublic()); 78 } 79 80 public ASTMethodDeclaration getMethodDecl(String access[]) throws Throwable { 81 String javaCode = "public class Test { "; 82 for (int i = 0; i < access.length; i++) { 83 javaCode += access[i] + " "; 84 } 85 86 javaCode += " void stuff() { } }"; 87 88 Set methods = getNodes(ASTMethodDeclaration.class, javaCode); 89 90 assertEquals("Wrong number of methods", 1, methods.size()); 91 92 Iterator i = methods.iterator(); 93 return (ASTMethodDeclaration) i.next(); 94 } 95 96 public static junit.framework.Test suite() { 97 return new junit.framework.JUnit4TestAdapter(MethodDeclTest.class); 98 } 99 }