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.ASTFieldDeclaration;
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 FieldDeclTest extends ParserTst {
18  
19      public String makeAccessJavaCode(String access[]) {
20          String result = "public class Test { ";
21          for (int i = 0; i < access.length; i++) {
22              result += access[i] + " ";
23          }
24          return result + " int j;  }";
25      }
26  
27      public ASTFieldDeclaration getFieldDecl(String access[]) throws Throwable {
28          Set fields = getNodes(ASTFieldDeclaration.class, makeAccessJavaCode(access));
29  
30          assertEquals("Wrong number of fields", 1, fields.size());
31          Iterator i = fields.iterator();
32          return (ASTFieldDeclaration) i.next();
33      }
34  
35      @Test
36      public void testPublic() throws Throwable {
37          String access[] = {"public"};
38          ASTFieldDeclaration afd = getFieldDecl(access);
39          assertTrue("Expecting field to be public.", afd.isPublic());
40      }
41  
42      @Test
43      public void testProtected() throws Throwable {
44          String access[] = {"protected"};
45          ASTFieldDeclaration afd = getFieldDecl(access);
46          assertTrue("Expecting field to be protected.", afd.isProtected());
47      }
48  
49      @Test
50      public void testPrivate() throws Throwable {
51          String access[] = {"private"};
52          ASTFieldDeclaration afd = getFieldDecl(access);
53          assertTrue("Expecting field to be private.", afd.isPrivate());
54      }
55  
56      @Test
57      public void testStatic() throws Throwable {
58          String access[] = {"private", "static"};
59          ASTFieldDeclaration afd = getFieldDecl(access);
60          assertTrue("Expecting field to be static.", afd.isStatic());
61          assertTrue("Expecting field to be private.", afd.isPrivate());
62      }
63  
64      @Test
65      public void testFinal() throws Throwable {
66          String access[] = {"public", "final"};
67          ASTFieldDeclaration afd = getFieldDecl(access);
68          assertTrue("Expecting field to be final.", afd.isFinal());
69          assertTrue("Expecting field to be public.", afd.isPublic());
70      }
71  
72      @Test
73      public void testTransient() throws Throwable {
74          String access[] = {"private", "transient"};
75          ASTFieldDeclaration afd = getFieldDecl(access);
76          assertTrue("Expecting field to be private.", afd.isPrivate());
77          assertTrue("Expecting field to be transient.", afd.isTransient());
78      }
79  
80      @Test
81      public void testVolatile() throws Throwable {
82          String access[] = {"private", "volatile"};
83          ASTFieldDeclaration afd = getFieldDecl(access);
84          assertTrue("Expecting field to be volatile.", afd.isVolatile());
85          assertTrue("Expecting field to be private.", afd.isPrivate());
86      }
87  
88      public static junit.framework.Test suite() {
89          return new junit.framework.JUnit4TestAdapter(FieldDeclTest.class);
90      }
91  }