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 net.sourceforge.pmd.ast.ASTMethodDeclaration;
7 import test.net.sourceforge.pmd.testframework.ParserTst;
8
9 import java.util.Iterator;
10 import java.util.Set;
11
12 public class MethodDeclTest extends ParserTst {
13 public void testPublic() throws Throwable {
14 String access[] = {"public"};
15 ASTMethodDeclaration amd = getMethodDecl(access);
16 assertTrue("Expecting method to be public.", amd.isPublic());
17 }
18
19 public void testPrivate() throws Throwable {
20 String access[] = {"private"};
21 ASTMethodDeclaration amd = getMethodDecl(access);
22 assertTrue("Expecting method to be private.", amd.isPrivate());
23 }
24
25 public void testProtected() throws Throwable {
26 String access[] = {"protected"};
27 ASTMethodDeclaration amd = getMethodDecl(access);
28 assertTrue("Expecting method to be protected.", amd.isProtected());
29 }
30
31 public void testFinal() throws Throwable {
32 String access[] = {"public", "final"};
33 ASTMethodDeclaration amd = getMethodDecl(access);
34 assertTrue("Expecting method to be final.", amd.isFinal());
35 assertTrue("Expecting method to be public.", amd.isPublic());
36 }
37
38 public void testSynchronized() throws Throwable {
39 String access[] = {"public", "synchronized"};
40 ASTMethodDeclaration amd = getMethodDecl(access);
41 assertTrue("Expecting method to be synchronized.", amd.isSynchronized());
42 assertTrue("Expecting method to be public.", amd.isPublic());
43 }
44
45 public void testAbstract() throws Throwable {
46 String access[] = {"public", "abstract"};
47 ASTMethodDeclaration amd = getMethodDecl(access);
48 assertTrue("Expecting method to be abstract.", amd.isAbstract());
49 assertTrue("Expecting method to be public.", amd.isPublic());
50 }
51
52 public void testNative() throws Throwable {
53 String access[] = {"private", "native"};
54 ASTMethodDeclaration amd = getMethodDecl(access);
55 assertTrue("Expecting method to be native.", amd.isNative());
56 assertTrue("Expecting method to be private.", amd.isPrivate());
57 }
58
59 public void testStrict() throws Throwable {
60 String access[] = {"public", "strictfp"};
61 ASTMethodDeclaration amd = getMethodDecl(access);
62 assertTrue("Expecting method to be strict.", amd.isStrictfp());
63 assertTrue("Expecting method to be public.", amd.isPublic());
64 }
65
66 public ASTMethodDeclaration getMethodDecl(String access[]) throws Throwable {
67 String javaCode = "public class Test { ";
68 for (int i = 0; i < access.length; i++) {
69 javaCode += access[i] + " ";
70 }
71
72 javaCode += " void stuff() { } }";
73
74 Set methods = getNodes(ASTMethodDeclaration.class, javaCode);
75
76 assertEquals("Wrong number of methods", 1, methods.size());
77
78 Iterator i = methods.iterator();
79 return (ASTMethodDeclaration) i.next();
80 }
81 }