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.ASTFieldDeclaration;
7   import test.net.sourceforge.pmd.testframework.ParserTst;
8   
9   import java.util.Iterator;
10  import java.util.Set;
11  
12  public class FieldDeclTest extends ParserTst {
13      public String makeAccessJavaCode(String access[]) {
14          String result = "public class Test { ";
15          for (int i = 0; i < access.length; i++) {
16              result += access[i] + " ";
17          }
18          return result + " int j;  }";
19      }
20  
21      public ASTFieldDeclaration getFieldDecl(String access[]) throws Throwable {
22          Set fields = getNodes(ASTFieldDeclaration.class, makeAccessJavaCode(access));
23  
24          assertEquals("Wrong number of fields", 1, fields.size());
25          Iterator i = fields.iterator();
26          return (ASTFieldDeclaration) i.next();
27      }
28  
29      public void testPublic() throws Throwable {
30          String access[] = {"public"};
31          ASTFieldDeclaration afd = getFieldDecl(access);
32          assertTrue("Expecting field to be public.", afd.isPublic());
33      }
34  
35      public void testProtected() throws Throwable {
36          String access[] = {"protected"};
37          ASTFieldDeclaration afd = getFieldDecl(access);
38          assertTrue("Expecting field to be protected.", afd.isProtected());
39      }
40  
41      public void testPrivate() throws Throwable {
42          String access[] = {"private"};
43          ASTFieldDeclaration afd = getFieldDecl(access);
44          assertTrue("Expecting field to be private.", afd.isPrivate());
45      }
46  
47      public void testStatic() throws Throwable {
48          String access[] = {"private", "static"};
49          ASTFieldDeclaration afd = getFieldDecl(access);
50          assertTrue("Expecting field to be static.", afd.isStatic());
51          assertTrue("Expecting field to be private.", afd.isPrivate());
52      }
53  
54      public void testFinal() throws Throwable {
55          String access[] = {"public", "final"};
56          ASTFieldDeclaration afd = getFieldDecl(access);
57          assertTrue("Expecting field to be final.", afd.isFinal());
58          assertTrue("Expecting field to be public.", afd.isPublic());
59      }
60  
61      public void testTransient() throws Throwable {
62          String access[] = {"private", "transient"};
63          ASTFieldDeclaration afd = getFieldDecl(access);
64          assertTrue("Expecting field to be private.", afd.isPrivate());
65          assertTrue("Expecting field to be transient.", afd.isTransient());
66      }
67  
68      public void testVolatile() throws Throwable {
69          String access[] = {"private", "volatile"};
70          ASTFieldDeclaration afd = getFieldDecl(access);
71          assertTrue("Expecting field to be volatile.", afd.isVolatile());
72          assertTrue("Expecting field to be private.", afd.isPrivate());
73      }
74  }